1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Ericsson AB
10 #include <rte_branch_prediction.h>
11 #include <rte_cycles.h>
12 #include <rte_lcore.h>
13 #include <rte_random.h>
15 struct rte_rand_state {
21 } __rte_cache_aligned;
23 static struct rte_rand_state rand_states[RTE_MAX_LCORE];
26 __rte_rand_lcg32(uint32_t *seed)
28 *seed = 1103515245U * *seed + 12345U;
34 __rte_rand_lcg64(uint32_t *seed)
39 /* A 64-bit LCG would have been much cleaner, but good
40 * multiplier/increments for such seem hard to come by.
43 low = __rte_rand_lcg32(seed);
44 high = __rte_rand_lcg32(seed);
46 return low | (high << 32);
50 __rte_rand_lfsr258_gen_seed(uint32_t *seed, uint64_t min_value)
54 res = __rte_rand_lcg64(seed);
63 __rte_srand_lfsr258(uint64_t seed, struct rte_rand_state *state)
67 lcg_seed = (uint32_t)(seed ^ (seed >> 32));
69 state->z1 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 2UL);
70 state->z2 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 512UL);
71 state->z3 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 4096UL);
72 state->z4 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 131072UL);
73 state->z5 = __rte_rand_lfsr258_gen_seed(&lcg_seed, 8388608UL);
77 rte_srand(uint64_t seed)
79 unsigned int lcore_id;
81 /* add lcore_id to seed to avoid having the same sequence */
82 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
83 __rte_srand_lfsr258(seed + lcore_id, &rand_states[lcore_id]);
86 static __rte_always_inline uint64_t
87 __rte_rand_lfsr258_comp(uint64_t z, uint64_t a, uint64_t b, uint64_t c,
90 return ((z & c) << d) ^ (((z << a) ^ z) >> b);
93 /* Based on L’Ecuyer, P.: Tables of maximally equidistributed combined
97 static __rte_always_inline uint64_t
98 __rte_rand_lfsr258(struct rte_rand_state *state)
100 state->z1 = __rte_rand_lfsr258_comp(state->z1, 1UL, 53UL,
101 18446744073709551614UL, 10UL);
102 state->z2 = __rte_rand_lfsr258_comp(state->z2, 24UL, 50UL,
103 18446744073709551104UL, 5UL);
104 state->z3 = __rte_rand_lfsr258_comp(state->z3, 3UL, 23UL,
105 18446744073709547520UL, 29UL);
106 state->z4 = __rte_rand_lfsr258_comp(state->z4, 5UL, 24UL,
107 18446744073709420544UL, 23UL);
108 state->z5 = __rte_rand_lfsr258_comp(state->z5, 3UL, 33UL,
109 18446744073701163008UL, 8UL);
111 return state->z1 ^ state->z2 ^ state->z3 ^ state->z4 ^ state->z5;
114 static __rte_always_inline
115 struct rte_rand_state *__rte_rand_get_state(void)
117 unsigned int lcore_id;
119 lcore_id = rte_lcore_id();
121 if (unlikely(lcore_id == LCORE_ID_ANY))
122 lcore_id = rte_get_main_lcore();
124 return &rand_states[lcore_id];
130 struct rte_rand_state *state;
132 state = __rte_rand_get_state();
134 return __rte_rand_lfsr258(state);
138 rte_rand_max(uint64_t upper_bound)
140 struct rte_rand_state *state;
142 uint8_t leading_zeros;
143 uint64_t mask = ~((uint64_t)0);
146 if (unlikely(upper_bound < 2))
149 state = __rte_rand_get_state();
151 ones = __builtin_popcountll(upper_bound);
153 /* Handle power-of-2 upper_bound as a special case, since it
154 * has no bias issues.
156 if (unlikely(ones == 1))
157 return __rte_rand_lfsr258(state) & (upper_bound - 1);
159 /* The approach to avoiding bias is to create a mask that
160 * stretches beyond the request value range, and up to the
161 * next power-of-2. In case the masked generated random value
162 * is equal to or greater than the upper bound, just discard
163 * the value and generate a new one.
166 leading_zeros = __builtin_clzll(upper_bound);
167 mask >>= leading_zeros;
170 res = __rte_rand_lfsr258(state) & mask;
171 } while (unlikely(res >= upper_bound));
177 __rte_random_initial_seed(void)
179 #ifdef RTE_LIBEAL_USE_GETENTROPY
183 ge_rc = getentropy(&ge_seed, sizeof(ge_seed));
189 unsigned int rdseed_low;
190 unsigned int rdseed_high;
192 /* first fallback: rdseed instruction, if available */
193 if (_rdseed32_step(&rdseed_low) == 1 &&
194 _rdseed32_step(&rdseed_high) == 1)
195 return (uint64_t)rdseed_low | ((uint64_t)rdseed_high << 32);
197 /* second fallback: seed using rdtsc */
198 return rte_get_tsc_cycles();
201 RTE_INIT(rte_rand_init)
205 seed = __rte_random_initial_seed();