| /* SPDX-License-Identifier: GPL-2.0 */ | |
| #ifndef __TOOLS_LINUX_PRANDOM_H | |
| #define __TOOLS_LINUX_PRANDOM_H | |
| #include <linux/types.h> | |
| struct rnd_state { | |
| __u32 s1, s2, s3, s4; | |
| }; | |
| /* | |
| * Handle minimum values for seeds | |
| */ | |
| static inline u32 __seed(u32 x, u32 m) | |
| { | |
| return (x < m) ? x + m : x; | |
| } | |
| /** | |
| * prandom_seed_state - set seed for prandom_u32_state(). | |
| * @state: pointer to state structure to receive the seed. | |
| * @seed: arbitrary 64-bit value to use as a seed. | |
| */ | |
| static inline void prandom_seed_state(struct rnd_state *state, u64 seed) | |
| { | |
| u32 i = ((seed >> 32) ^ (seed << 10) ^ seed) & 0xffffffffUL; | |
| state->s1 = __seed(i, 2U); | |
| state->s2 = __seed(i, 8U); | |
| state->s3 = __seed(i, 16U); | |
| state->s4 = __seed(i, 128U); | |
| } | |
| /** | |
| * prandom_u32_state - seeded pseudo-random number generator. | |
| * @state: pointer to state structure holding seeded state. | |
| * | |
| * This is used for pseudo-randomness with no outside seeding. | |
| * For more random results, use get_random_u32(). | |
| */ | |
| static inline u32 prandom_u32_state(struct rnd_state *state) | |
| { | |
| #define TAUSWORTHE(s, a, b, c, d) (((s & c) << d) ^ (((s << a) ^ s) >> b)) | |
| state->s1 = TAUSWORTHE(state->s1, 6U, 13U, 4294967294U, 18U); | |
| state->s2 = TAUSWORTHE(state->s2, 2U, 27U, 4294967288U, 2U); | |
| state->s3 = TAUSWORTHE(state->s3, 13U, 21U, 4294967280U, 7U); | |
| state->s4 = TAUSWORTHE(state->s4, 3U, 12U, 4294967168U, 13U); | |
| return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4); | |
| } | |
| #endif // __TOOLS_LINUX_PRANDOM_H |