diff options
author | ache <ache@FreeBSD.org> | 2008-07-22 12:43:09 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 2008-07-22 12:43:09 +0000 |
commit | 1cdd160d5093e9601fbfb3d2d7f65fd3b87df1a7 (patch) | |
tree | 5ffd58b79b3f9d9258ee560622881668e81198dc /lib/libc/gen | |
parent | e1bf8eba273f6a33d0c6eed3154db0a8c0cad3b2 (diff) | |
download | FreeBSD-src-1cdd160d5093e9601fbfb3d2d7f65fd3b87df1a7.zip FreeBSD-src-1cdd160d5093e9601fbfb3d2d7f65fd3b87df1a7.tar.gz |
In arc4random_uniform() detect simple "power of two" case and
return just (arc4random() % upper_bound)
Diffstat (limited to 'lib/libc/gen')
-rw-r--r-- | lib/libc/gen/arc4random.c | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c index 1c7dead..56e457b 100644 --- a/lib/libc/gen/arc4random.c +++ b/lib/libc/gen/arc4random.c @@ -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; |