Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * $RANDOM support. |
| 4 | * |
Denys Vlasenko | e3c6e19 | 2009-10-09 23:35:30 +0200 | [diff] [blame] | 5 | * Copyright (C) 2009 Denys Vlasenko |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 8 | */ |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 9 | #ifndef SHELL_RANDOM_H |
| 10 | #define SHELL_RANDOM_H 1 |
| 11 | |
| 12 | PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 13 | |
| 14 | typedef struct random_t { |
Denys Vlasenko | fb18307 | 2014-03-13 12:52:43 +0100 | [diff] [blame] | 15 | /* State of random number generators: */ |
| 16 | |
| 17 | /* Galois LFSR (fast but weak) */ |
| 18 | int32_t galois_LFSR; /* must be signed! */ |
| 19 | |
| 20 | /* LCG (fast but weak) */ |
| 21 | uint32_t LCG; |
| 22 | |
| 23 | /* 64-bit xorshift (fast, moderate strength) */ |
| 24 | uint32_t xs64_x; |
| 25 | uint32_t xs64_y; |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 26 | } random_t; |
| 27 | |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 28 | #define UNINITED_RANDOM_T(rnd) \ |
| 29 | ((rnd)->galois_LFSR == 0) |
| 30 | |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 31 | #define INIT_RANDOM_T(rnd, nonzero, v) \ |
Denys Vlasenko | fb18307 | 2014-03-13 12:52:43 +0100 | [diff] [blame] | 32 | ((rnd)->galois_LFSR = (rnd)->xs64_x = (nonzero), (rnd)->LCG = (rnd)->xs64_y = (v)) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 33 | |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 34 | #define CLEAR_RANDOM_T(rnd) \ |
| 35 | ((rnd)->galois_LFSR = 0) |
| 36 | |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 37 | uint32_t next_random(random_t *rnd) FAST_FUNC; |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 38 | |
| 39 | POP_SAVED_FUNCTION_VISIBILITY |
| 40 | |
| 41 | #endif |