From c606671aaad10a9bc87f226bc473a091e00a9629 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 14 Aug 2012 11:36:38 -0600 Subject: [PATCH] random: link with -lm when needed Use of ldexp() requires -lm on some platforms; use gnulib to determine this for our makefile. Also, optimize virRandomInt() for the case of a power-of-two limit (actually rather common, given that Daniel has a pending patch to replace virRandomBits(10) with code that will default to virRandomInt(1024) on default SELinux settings). * .gnulib: Update to latest, for ldexp. * bootstrap.conf (gnulib_modules): Import ldexp. * src/Makefile.am (libvirt_util_la_CFLAGS): Link with -lm when needed. * src/util/virrandom.c (virRandomInt): Optimize powers of 2. --- .gnulib | 2 +- bootstrap.conf | 1 + src/Makefile.am | 2 +- src/util/virrandom.c | 4 ++++ 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gnulib b/.gnulib index dbd914496c..271dd74fdf 160000 --- a/.gnulib +++ b/.gnulib @@ -1 +1 @@ -Subproject commit dbd914496c99c52220e5f5ba4121d6cb55fb3beb +Subproject commit 271dd74fdf54ec2a03e73a5173b0b5697f6088f1 diff --git a/bootstrap.conf b/bootstrap.conf index a4e1c2fa1b..a6cfe24c18 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -61,6 +61,7 @@ intprops ioctl isatty largefile +ldexp listen localeconv maintainer-makefile diff --git a/src/Makefile.am b/src/Makefile.am index e94f977186..cec478948a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -654,7 +654,7 @@ libvirt_util_la_SOURCES = \ $(UTIL_SOURCES) libvirt_util_la_CFLAGS = $(CAPNG_CFLAGS) $(YAJL_CFLAGS) $(LIBNL_CFLAGS) \ $(AM_CFLAGS) $(AUDIT_CFLAGS) $(DEVMAPPER_CFLAGS) \ - $(DBUS_CFLAGS) + $(DBUS_CFLAGS) $(LDEXP_LIBM) libvirt_util_la_LIBADD = $(CAPNG_LIBS) $(YAJL_LIBS) $(LIBNL_LIBS) \ $(THREAD_LIBS) $(AUDIT_LIBS) $(DEVMAPPER_LIBS) \ $(RT_LIBS) $(DBUS_LIBS) $(MSCOM_LIBS) $(LIBXML_LIBS) diff --git a/src/util/virrandom.c b/src/util/virrandom.c index 363fcab42e..73c6adf080 100644 --- a/src/util/virrandom.c +++ b/src/util/virrandom.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "virrandom.h" #include "threads.h" @@ -135,6 +136,9 @@ double virRandom(void) */ uint32_t virRandomInt(uint32_t max) { + if ((max & (max - 1)) == 0) + return virRandomBits(ffs(max) - 1); + double val = virRandom(); return val * max; }