From 1cdd160d5093e9601fbfb3d2d7f65fd3b87df1a7 Mon Sep 17 00:00:00 2001 From: ache Date: Tue, 22 Jul 2008 12:43:09 +0000 Subject: In arc4random_uniform() detect simple "power of two" case and return just (arc4random() % upper_bound) --- lib/libc/gen/arc4random.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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; -- cgit v1.1