diff options
author | Tom Lane | 2021-11-29 02:32:36 +0000 |
---|---|---|
committer | Tom Lane | 2021-11-29 02:33:07 +0000 |
commit | 3804539e48e794781c6145c7f988f5d507418fa8 (patch) | |
tree | 317904b43ca8c1d510b23cb8fdd7b05a75e971bc /src/tools/testint128.c | |
parent | f44ceb46ec2d8da48f6e145bf462d5620c25e079 (diff) |
Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.
Standardize on xoroshiro128** as our basic PRNG algorithm, eliminating
a bunch of platform dependencies as well as fundamentally-obsolete PRNG
code. In addition, this API replacement will ease replacing the
algorithm again in future, should that become necessary.
xoroshiro128** is a few percent slower than the drand48 family,
but it can produce full-width 64-bit random values not only 48-bit,
and it should be much more trustworthy. It's likely to be noticeably
faster than the platform's random(), depending on which platform you
are thinking about; and we can have non-global state vectors easily,
unlike with random(). It is not cryptographically strong, but neither
are the functions it replaces.
Fabien Coelho, reviewed by Dean Rasheed, Aleksander Alekseev, and myself
Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2105241211230.165418@pseudo
Diffstat (limited to 'src/tools/testint128.c')
-rw-r--r-- | src/tools/testint128.c | 29 |
1 files changed, 8 insertions, 21 deletions
diff --git a/src/tools/testint128.c b/src/tools/testint128.c index 71c345969a8..47cc685ab45 100644 --- a/src/tools/testint128.c +++ b/src/tools/testint128.c @@ -27,6 +27,7 @@ #endif #include "common/int128.h" +#include "common/pg_prng.h" /* * We assume the parts of this union are laid out compatibly. @@ -62,26 +63,10 @@ my_int128_compare(int128 x, int128 y) } /* - * Get a random uint64 value. - * We don't assume random() is good for more than 16 bits. - */ -static uint64 -get_random_uint64(void) -{ - uint64 x; - - x = (uint64) (random() & 0xFFFF) << 48; - x |= (uint64) (random() & 0xFFFF) << 32; - x |= (uint64) (random() & 0xFFFF) << 16; - x |= (uint64) (random() & 0xFFFF); - return x; -} - -/* * Main program. * * Generates a lot of random numbers and tests the implementation for each. - * The results should be reproducible, since we don't call srandom(). + * The results should be reproducible, since we use a fixed PRNG seed. * * You can give a loop count if you don't like the default 1B iterations. */ @@ -90,6 +75,8 @@ main(int argc, char **argv) { long count; + pg_prng_seed(&pg_global_prng_state, 0); + if (argc >= 2) count = strtol(argv[1], NULL, 0); else @@ -97,9 +84,9 @@ main(int argc, char **argv) while (count-- > 0) { - int64 x = get_random_uint64(); - int64 y = get_random_uint64(); - int64 z = get_random_uint64(); + int64 x = pg_prng_uint64(&pg_global_prng_state); + int64 y = pg_prng_uint64(&pg_global_prng_state); + int64 z = pg_prng_uint64(&pg_global_prng_state); test128 t1; test128 t2; @@ -151,7 +138,7 @@ main(int argc, char **argv) t1.hl.hi = x; t1.hl.lo = y; t2.hl.hi = z; - t2.hl.lo = get_random_uint64(); + t2.hl.lo = pg_prng_uint64(&pg_global_prng_state); if (my_int128_compare(t1.i128, t2.i128) != int128_compare(t1.I128, t2.I128)) |