[Pixman] [PATCH 1/4] Fix lcg_rand_u32() to return 32 random bits.

Søren Sandmann sandmann at cs.au.dk
Fri Aug 5 19:26:03 PDT 2011


From: Søren Sandmann Pedersen <ssp at redhat.com>

The lcg_rand() function only returns 15 random bits, so lcg_rand_u32()
would always have 0 in bit 31 and bit 15. Fix that by calling
lcg_rand() three times, to generate 15, 15, and 2 random bits
respectively.

V2: Use the 10/11 most significant bits from the 3 lcg results and mix
them with the low ones from the adjacent one, as suggested by Andrea
Canciani.
---
 test/utils.h |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/test/utils.h b/test/utils.h
index 615ad78..000aaa6 100644
--- a/test/utils.h
+++ b/test/utils.h
@@ -44,10 +44,14 @@ lcg_rand_N (int max)
 static inline uint32_t
 lcg_rand_u32 (void)
 {
-    uint32_t lo = lcg_rand();
-    uint32_t hi = lcg_rand();
-
-    return (hi << 16) | lo;
+    /* This uses the 10/11 most significant bits from the 3 lcg results
+     * (and mixes them with the low from the adjacent one).
+     */
+    uint32_t lo = lcg_rand() >> -(32 - 15 - 11 * 2);
+    uint32_t mid = lcg_rand() << (32 - 15 - 11 * 1);
+    uint32_t hi = lcg_rand() << (32 - 15 - 11 * 0);
+
+    return (hi ^ mid ^ lo);
 }
 
 /* CRC 32 computation
-- 
1.7.4



More information about the Pixman mailing list