From: Pavel Charvat Date: Tue, 9 Jun 2020 09:27:08 +0000 (+0200) Subject: Random: Old pseudo-random implementation moved to random-legacy.c X-Git-Tag: v6.5.14~11 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=794fd1868850e47faca2137a52a7f17b4f9fdf36;p=libucw.git Random: Old pseudo-random implementation moved to random-legacy.c --- diff --git a/ucw/Makefile b/ucw/Makefile index dfdc9fbe..b9bff392 100644 --- a/ucw/Makefile +++ b/ucw/Makefile @@ -21,7 +21,8 @@ LIBUCW_MODS= \ fw-hex \ char-cat char-upper char-lower unicode varint stkstring \ wildmatch regex \ - prime primetable random \ + prime primetable \ + random-legacy \ time-stamp time-timer time-conf \ bit-ffs bit-fls bit-array \ url \ diff --git a/ucw/lib.h b/ucw/lib.h index 0465ca1e..6f93bb7e 100644 --- a/ucw/lib.h +++ b/ucw/lib.h @@ -232,7 +232,7 @@ void *big_alloc(u64 len) LIKE_MALLOC; /** Allocate a large memory block in the void *big_alloc_zero(u64 len) LIKE_MALLOC; /** Allocate and clear a large memory block. **/ void big_free(void *start, u64 len); /** Free block allocated by @big_alloc() or @big_alloc_zero(). **/ -/*** === Random numbers (random.c) ***/ +/*** === Random numbers (random-legacy.c) ***/ uint random_u32(void); /** Return a pseudorandom 32-bit number. **/ uint random_max(uint max); /** Return a pseudorandom 32-bit number in range [0,@max). **/ diff --git a/ucw/random-legacy.c b/ucw/random-legacy.c new file mode 100644 index 00000000..016a17a2 --- /dev/null +++ b/ucw/random-legacy.c @@ -0,0 +1,58 @@ +/* + * UCW Library -- Unbiased Random Numbers + * + * (c) 1998--2006 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include + +#include + +/* We expect the random generator in libc to give at least 30 bits of randomness */ +COMPILE_ASSERT(RAND_MAX_RANGE_TEST, RAND_MAX >= (1 << 30)-1); + +uint +random_u32(void) +{ + return (random() & 0xffff) | ((random() & 0xffff) << 16); +} + +uint +random_max(uint max) +{ + uint r, l; + + ASSERT(max <= (1 << 30)); + l = (RAND_MAX + 1U) - ((RAND_MAX + 1U) % max); + do + r = random(); + while (r >= l); + return r % max; +} + +u64 +random_u64(void) +{ + return + ((u64)(random() & 0xffff) << 48) | + ((u64)(random() & 0xffffff) << 24) | + (random() & 0xffffff); +} + +u64 +random_max_u64(u64 max) +{ + if (max < (1 << 30)) + return random_max(max); + + u64 r, l, m; + m = 0xffffffffffffffff; + l = m - (m % max); + do + r = random_u64(); + while (r >= l); + return r % max; +} diff --git a/ucw/random.c b/ucw/random.c deleted file mode 100644 index 016a17a2..00000000 --- a/ucw/random.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * UCW Library -- Unbiased Random Numbers - * - * (c) 1998--2006 Martin Mares - * - * This software may be freely distributed and used according to the terms - * of the GNU Lesser General Public License. - */ - -#include - -#include - -/* We expect the random generator in libc to give at least 30 bits of randomness */ -COMPILE_ASSERT(RAND_MAX_RANGE_TEST, RAND_MAX >= (1 << 30)-1); - -uint -random_u32(void) -{ - return (random() & 0xffff) | ((random() & 0xffff) << 16); -} - -uint -random_max(uint max) -{ - uint r, l; - - ASSERT(max <= (1 << 30)); - l = (RAND_MAX + 1U) - ((RAND_MAX + 1U) % max); - do - r = random(); - while (r >= l); - return r % max; -} - -u64 -random_u64(void) -{ - return - ((u64)(random() & 0xffff) << 48) | - ((u64)(random() & 0xffffff) << 24) | - (random() & 0xffffff); -} - -u64 -random_max_u64(u64 max) -{ - if (max < (1 << 30)) - return random_max(max); - - u64 r, l, m; - m = 0xffffffffffffffff; - l = m - (m % max); - do - r = random_u64(); - while (r >= l); - return r % max; -}