In arc4random_uniform() detect simple "power of two" case and

return just (arc4random() % upper_bound)
This commit is contained in:
Andrey A. Chernov 2008-07-22 12:43:09 +00:00
parent c0046493c9
commit 61d35b6350

View File

@ -256,7 +256,11 @@ arc4random_uniform(u_int32_t upper_bound)
u_int32_t r, min;
if (upper_bound < 2)
return 0;
return (0);
/* Detect simple power of two case */
if ((upper_bound & -upper_bound) == upper_bound)
return (arc4random() % upper_bound);
#if (ULONG_MAX > 0xffffffffUL)
min = 0x100000000UL % upper_bound;