123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #if !defined RANDTEST
- # include "libbb.h"
- # include "random.h"
- # define RAND_BASH_MASK 0x7fff
- #else
- # include <stdint.h>
- # include <unistd.h>
- # include <stdio.h>
- # include <time.h>
- # define FAST_FUNC
- # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
- # define POP_SAVED_FUNCTION_VISIBILITY
- # define monotonic_us() time(NULL)
- # include "random.h"
- # define RAND_BASH_MASK 0xffffffff
- #endif
- uint32_t FAST_FUNC
- next_random(random_t *rnd)
- {
-
- enum { MASK = 0x8000000b };
-
-
-
- enum {
- a = 2,
- b = 7,
- c = 3,
- };
- uint32_t t;
- if (UNINITED_RANDOM_T(rnd)) {
-
- INIT_RANDOM_T(rnd, getpid(), monotonic_us());
- }
-
- rnd->LCG = 1664525 * rnd->LCG + 1013904223;
-
- t = (rnd->galois_LFSR << 1);
- if (rnd->galois_LFSR < 0)
- t ^= MASK;
- rnd->galois_LFSR = t;
-
- again:
- t = rnd->xs64_x ^ (rnd->xs64_x << a);
- rnd->xs64_x = rnd->xs64_y;
- rnd->xs64_y = rnd->xs64_y ^ (rnd->xs64_y >> c) ^ t ^ (t >> b);
-
- if (rnd->xs64_y == 0 && rnd->xs64_x <= 2)
- goto again;
-
- t = rnd->galois_LFSR - rnd->LCG + rnd->xs64_y;
-
- return t & RAND_BASH_MASK;
- }
- #ifdef RANDTEST
- static random_t rnd;
- int main(int argc, char **argv)
- {
- int i;
- uint32_t buf[4096];
- for (;;) {
- for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) {
- buf[i] = next_random(&rnd);
- }
- write(1, buf, sizeof(buf));
- }
- return 0;
- }
- #endif
|