mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* random.c (limited_big_rand): The argument, limit, is changed to
VALUE. Use rb_integer_pack and rb_integer_unpack. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41171 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
483cbff8dc
commit
a76abae51d
2 changed files with 29 additions and 27 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Sat Jun 8 19:04:15 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* random.c (limited_big_rand): The argument, limit, is changed to
|
||||||
|
VALUE. Use rb_integer_pack and rb_integer_unpack.
|
||||||
|
|
||||||
Sat Jun 8 17:15:18 2013 Tanaka Akira <akr@fsij.org>
|
Sat Jun 8 17:15:18 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* random.c (make_seed_value): Fix the length given for
|
* random.c (make_seed_value): Fix the length given for
|
||||||
|
|
51
random.c
51
random.c
|
@ -746,38 +746,31 @@ limited_rand(struct MT *mt, unsigned long limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
limited_big_rand(struct MT *mt, struct RBignum *limit)
|
limited_big_rand(struct MT *mt, VALUE limit)
|
||||||
{
|
{
|
||||||
/* mt must be initialized */
|
/* mt must be initialized */
|
||||||
|
|
||||||
unsigned long mask, lim, rnd;
|
unsigned long mask, lim, rnd;
|
||||||
struct RBignum *val;
|
long i;
|
||||||
long i, len;
|
|
||||||
int boundary;
|
int boundary;
|
||||||
|
|
||||||
len = (RBIGNUM_LEN(limit) * SIZEOF_BDIGITS + 3) / 4;
|
size_t len;
|
||||||
val = (struct RBignum *)rb_big_clone((VALUE)limit);
|
uint32_t *tmp, *lim_array, *rnd_array;
|
||||||
RBIGNUM_SET_SIGN(val, 1);
|
VALUE vtmp;
|
||||||
#if SIZEOF_BDIGITS == 2
|
VALUE val;
|
||||||
# define BIG_GET32(big,i) \
|
|
||||||
(RBIGNUM_DIGITS(big)[(i)*2] | \
|
len = rb_absint_size_in_word(limit, 32, NULL);
|
||||||
((i)*2+1 < RBIGNUM_LEN(big) ? \
|
tmp = ALLOCV_N(uint32_t, vtmp, len*2);
|
||||||
(RBIGNUM_DIGITS(big)[(i)*2+1] << 16) : \
|
lim_array = tmp;
|
||||||
0))
|
rnd_array = tmp + len;
|
||||||
# define BIG_SET32(big,i,d) \
|
rb_integer_pack(limit, NULL, NULL, lim_array, len, sizeof(uint32_t), 0,
|
||||||
((RBIGNUM_DIGITS(big)[(i)*2] = (d) & 0xffff), \
|
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
|
||||||
((i)*2+1 < RBIGNUM_LEN(big) ? \
|
|
||||||
(RBIGNUM_DIGITS(big)[(i)*2+1] = (d) >> 16) : \
|
|
||||||
0))
|
|
||||||
#else
|
|
||||||
/* SIZEOF_BDIGITS == 4 */
|
|
||||||
# define BIG_GET32(big,i) (RBIGNUM_DIGITS(big)[(i)])
|
|
||||||
# define BIG_SET32(big,i,d) (RBIGNUM_DIGITS(big)[(i)] = (d))
|
|
||||||
#endif
|
|
||||||
retry:
|
retry:
|
||||||
mask = 0;
|
mask = 0;
|
||||||
boundary = 1;
|
boundary = 1;
|
||||||
for (i = len-1; 0 <= i; i--) {
|
for (i = len-1; 0 <= i; i--) {
|
||||||
lim = BIG_GET32(limit, i);
|
lim = lim_array[i];
|
||||||
mask = mask ? 0xffffffff : make_mask(lim);
|
mask = mask ? 0xffffffff : make_mask(lim);
|
||||||
if (mask) {
|
if (mask) {
|
||||||
rnd = genrand_int32(mt) & mask;
|
rnd = genrand_int32(mt) & mask;
|
||||||
|
@ -791,9 +784,13 @@ limited_big_rand(struct MT *mt, struct RBignum *limit)
|
||||||
else {
|
else {
|
||||||
rnd = 0;
|
rnd = 0;
|
||||||
}
|
}
|
||||||
BIG_SET32(val, i, (BDIGIT)rnd);
|
rnd_array[i] = rnd;
|
||||||
}
|
}
|
||||||
return rb_big_norm((VALUE)val);
|
val = rb_integer_unpack(+1, rnd_array, len, sizeof(uint32_t), 0,
|
||||||
|
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
|
||||||
|
ALLOCV_END(vtmp);
|
||||||
|
|
||||||
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -967,7 +964,7 @@ rand_int(struct MT *mt, VALUE vmax, int restrictive)
|
||||||
r = limited_rand(mt, max);
|
r = limited_rand(mt, max);
|
||||||
return LONG2NUM(r);
|
return LONG2NUM(r);
|
||||||
}
|
}
|
||||||
ret = limited_big_rand(mt, RBIGNUM(vmax));
|
ret = limited_big_rand(mt, vmax);
|
||||||
RB_GC_GUARD(vmax);
|
RB_GC_GUARD(vmax);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -1009,7 +1006,7 @@ rand_range(struct MT* mt, VALUE range)
|
||||||
excl = 0;
|
excl = 0;
|
||||||
goto fixnum;
|
goto fixnum;
|
||||||
}
|
}
|
||||||
v = limited_big_rand(mt, RBIGNUM(vmax));
|
v = limited_big_rand(mt, vmax);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (v = rb_check_to_float(vmax), !NIL_P(v)) {
|
else if (v = rb_check_to_float(vmax), !NIL_P(v)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue