1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __TOOLS_LINUX_PRANDOM_H 3 #define __TOOLS_LINUX_PRANDOM_H 4 5 #include <linux/types.h> 6 7 struct rnd_state { 8 __u32 s1, s2, s3, s4; 9 }; 10 11 /* 12 * Handle minimum values for seeds 13 */ 14 static inline u32 __seed(u32 x, u32 m) 15 { 16 return (x < m) ? x + m : x; 17 } 18 19 /** 20 * prandom_seed_state - set seed for prandom_u32_state(). 21 * @state: pointer to state structure to receive the seed. 22 * @seed: arbitrary 64-bit value to use as a seed. 23 */ 24 static inline void prandom_seed_state(struct rnd_state *state, u64 seed) 25 { 26 u32 i = ((seed >> 32) ^ (seed << 10) ^ seed) & 0xffffffffUL; 27 28 state->s1 = __seed(i, 2U); 29 state->s2 = __seed(i, 8U); 30 state->s3 = __seed(i, 16U); 31 state->s4 = __seed(i, 128U); 32 } 33 34 /** 35 * prandom_u32_state - seeded pseudo-random number generator. 36 * @state: pointer to state structure holding seeded state. 37 * 38 * This is used for pseudo-randomness with no outside seeding. 39 * For more random results, use get_random_u32(). 40 */ 41 static inline u32 prandom_u32_state(struct rnd_state *state) 42 { 43 #define TAUSWORTHE(s, a, b, c, d) (((s & c) << d) ^ (((s << a) ^ s) >> b)) 44 state->s1 = TAUSWORTHE(state->s1, 6U, 13U, 4294967294U, 18U); 45 state->s2 = TAUSWORTHE(state->s2, 2U, 27U, 4294967288U, 2U); 46 state->s3 = TAUSWORTHE(state->s3, 13U, 21U, 4294967280U, 7U); 47 state->s4 = TAUSWORTHE(state->s4, 3U, 12U, 4294967168U, 13U); 48 49 return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4); 50 } 51 #endif // __TOOLS_LINUX_PRANDOM_H 52