MCPcopy Create free account
hub / github.com/F-Stack/f-stack / rte_rand_max

Function rte_rand_max

dpdk/lib/eal/common/rte_random.c:144–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

142}
143
144uint64_t
145rte_rand_max(uint64_t upper_bound)
146{
147 struct rte_rand_state *state;
148 uint8_t ones;
149 uint8_t leading_zeros;
150 uint64_t mask = ~((uint64_t)0);
151 uint64_t res;
152
153 if (unlikely(upper_bound < 2))
154 return 0;
155
156 state = __rte_rand_get_state();
157
158 ones = rte_popcount64(upper_bound);
159
160 /* Handle power-of-2 upper_bound as a special case, since it
161 * has no bias issues.
162 */
163 if (unlikely(ones == 1))
164 return __rte_rand_lfsr258(state) & (upper_bound - 1);
165
166 /* The approach to avoiding bias is to create a mask that
167 * stretches beyond the request value range, and up to the
168 * next power-of-2. In case the masked generated random value
169 * is equal to or greater than the upper bound, just discard
170 * the value and generate a new one.
171 */
172
173 leading_zeros = rte_clz64(upper_bound);
174 mask >>= leading_zeros;
175
176 do {
177 res = __rte_rand_lfsr258(state) & mask;
178 } while (unlikely(res >= upper_bound));
179
180 return res;
181}
182
183double
184rte_drand(void)

Callers 12

fill_attributesFunction · 0.85
fill_pcapng_fileFunction · 0.85
test_alignFunction · 0.85
writer_runFunction · 0.85
reader_runFunction · 0.85
get_rand_fragsFunction · 0.85
test_rand_perf_typeFunction · 0.85
test_ip_fragFunction · 0.85
test_basicFunction · 0.85
test_dropFunction · 0.85

Calls 4

__rte_rand_get_stateFunction · 0.85
rte_popcount64Function · 0.85
__rte_rand_lfsr258Function · 0.85
rte_clz64Function · 0.85

Tested by 10

fill_pcapng_fileFunction · 0.68
test_alignFunction · 0.68
writer_runFunction · 0.68
reader_runFunction · 0.68
get_rand_fragsFunction · 0.68
test_rand_perf_typeFunction · 0.68
test_ip_fragFunction · 0.68
test_basicFunction · 0.68
test_dropFunction · 0.68