|
| 1 | +/* |
| 2 | + * 32-bit xorwow-based PRNG with a seed input. Based on George Marsaglia's |
| 3 | + * Xorshift RNGs paper. |
| 4 | + * |
| 5 | + * Copyright (c) 2025 Nicholas Clark <[email protected]> |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License version 2 |
| 9 | + * as published by the Free Software Foundation. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <stdint.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <stdbool.h> |
| 23 | +#include <sys/time.h> |
| 24 | + |
| 25 | +#include "genimage.h" |
| 26 | + |
| 27 | +struct prng_state { |
| 28 | + uint32_t lane[5]; |
| 29 | + uint32_t counter; |
| 30 | +}; |
| 31 | + |
| 32 | +static uint32_t prng_getrandom(struct prng_state *state) |
| 33 | +{ |
| 34 | + uint32_t buffer = state->lane[4]; |
| 35 | + |
| 36 | + state->lane[4] = state->lane[3]; |
| 37 | + state->lane[3] = state->lane[2]; |
| 38 | + state->lane[2] = state->lane[1]; |
| 39 | + state->lane[1] = state->lane[0]; |
| 40 | + |
| 41 | + buffer ^= buffer >> 2; |
| 42 | + buffer ^= buffer << 1; |
| 43 | + buffer ^= state->lane[0] ^ (state->lane[0] << 4); |
| 44 | + state->lane[0] = buffer; |
| 45 | + state->counter += 0x587C5; |
| 46 | + |
| 47 | + return buffer + state->counter; |
| 48 | +} |
| 49 | + |
| 50 | +static void prng_seed(struct prng_state *state, uint32_t seed) |
| 51 | +{ |
| 52 | + state->counter = seed; |
| 53 | + state->lane[0] = 1 + (seed & 0x0FFFFFFFu); |
| 54 | + state->lane[1] = 2 + (seed & 0xFF0FFFFFu); |
| 55 | + state->lane[2] = 3 + (seed & 0xFFF00FFFu); |
| 56 | + state->lane[3] = 4 + (seed & 0xFFFFF0FFu); |
| 57 | + state->lane[4] = 5 + (seed & 0xFFFFFFF0u); |
| 58 | + |
| 59 | + for (int x = 0; x < 8; x++) { |
| 60 | + prng_getrandom(state); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +static struct prng_state prng = { { 1, 2, 3, 4, 5 }, 0 }; |
| 65 | +static bool prng_active = false; |
| 66 | + |
| 67 | +void random32_init(void) |
| 68 | +{ |
| 69 | + struct timeval tv; |
| 70 | + gettimeofday(&tv, NULL); |
| 71 | + srandom(tv.tv_usec); |
| 72 | + prng_active = false; |
| 73 | +} |
| 74 | + |
| 75 | +void random32_enable_prng(const char *seed, size_t length) |
| 76 | +{ |
| 77 | + prng_seed(&prng, crc32(seed, length)); |
| 78 | + prng_active = true; |
| 79 | +} |
| 80 | + |
| 81 | +uint32_t random32(void) |
| 82 | +{ |
| 83 | + if (prng_active) { |
| 84 | + return prng_getrandom(&prng); |
| 85 | + } |
| 86 | + return random(); |
| 87 | +} |
0 commit comments