]> xenbits.xensource.com Git - libvirt.git/commitdiff
random: link with -lm when needed
authorEric Blake <eblake@redhat.com>
Tue, 14 Aug 2012 17:36:38 +0000 (11:36 -0600)
committerEric Blake <eblake@redhat.com>
Tue, 14 Aug 2012 21:33:10 +0000 (15:33 -0600)
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
bootstrap.conf
src/Makefile.am
src/util/virrandom.c

diff --git a/.gnulib b/.gnulib
index dbd914496c99c52220e5f5ba4121d6cb55fb3beb..271dd74fdf54ec2a03e73a5173b0b5697f6088f1 160000 (submodule)
--- a/.gnulib
+++ b/.gnulib
@@ -1 +1 @@
-Subproject commit dbd914496c99c52220e5f5ba4121d6cb55fb3beb
+Subproject commit 271dd74fdf54ec2a03e73a5173b0b5697f6088f1
index a4e1c2fa1b6349c770ad8e889a9540855901939a..a6cfe24c18002df9ab901cda62f5719fb9205645 100644 (file)
@@ -61,6 +61,7 @@ intprops
 ioctl
 isatty
 largefile
+ldexp
 listen
 localeconv
 maintainer-makefile
index e94f9771862f8512dacc2b2a77a7bf87b0aa7d5c..cec478948a805f73db01ca767d47621b59e65ff0 100644 (file)
@@ -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)
index 363fcab42e9098eecd12a80507744f58722c77df..73c6adf080b74bb9aed24df803e9122693b02267 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <inttypes.h>
 #include <math.h>
+#include <strings.h>
 
 #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;
 }