mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* random.c (limited_rand): Add a specialized path for the limit fits in 32 bit.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d4819da611
commit
169b867fbf
2 changed files with 24 additions and 9 deletions
|
@ -1,3 +1,8 @@
|
|||
Wed Feb 24 21:03:04 2016 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* random.c (limited_rand): Add a specialized path for the limit fits
|
||||
in 32 bit.
|
||||
|
||||
Tue Feb 23 21:52:24 2016 Martin Duerst <duerst@it.aoyama.ac.jp>
|
||||
|
||||
* enc/unicode/case-folding.rb, casefold.h: Outputting actual titlecase
|
||||
|
|
28
random.c
28
random.c
|
@ -826,21 +826,31 @@ static unsigned long
|
|||
limited_rand(struct MT *mt, unsigned long limit)
|
||||
{
|
||||
/* mt must be initialized */
|
||||
int i;
|
||||
unsigned long val, mask;
|
||||
|
||||
if (!limit) return 0;
|
||||
mask = make_mask(limit);
|
||||
retry:
|
||||
val = 0;
|
||||
for (i = SIZEOF_LONG/SIZEOF_INT32-1; 0 <= i; i--) {
|
||||
if ((mask >> (i * 32)) & 0xffffffff) {
|
||||
val |= (unsigned long)genrand_int32(mt) << (i * 32);
|
||||
val &= mask;
|
||||
if (limit < val)
|
||||
goto retry;
|
||||
|
||||
#if 4 < SIZEOF_LONG
|
||||
if (0xffffffff < limit) {
|
||||
int i;
|
||||
retry:
|
||||
val = 0;
|
||||
for (i = SIZEOF_LONG/SIZEOF_INT32-1; 0 <= i; i--) {
|
||||
if ((mask >> (i * 32)) & 0xffffffff) {
|
||||
val |= (unsigned long)genrand_int32(mt) << (i * 32);
|
||||
val &= mask;
|
||||
if (limit < val)
|
||||
goto retry;
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
do {
|
||||
val = genrand_int32(mt) & mask;
|
||||
} while (limit < val);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue