* random.c (rand_init): Use rb_integer_pack.

(roomof): Removed.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41167 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-08 06:46:45 +00:00
parent 90b1a8f431
commit ccce83c454
2 changed files with 26 additions and 46 deletions

View File

@ -1,3 +1,8 @@
Sat Jun 8 15:30:03 2013 Tanaka Akira <akr@fsij.org>
* random.c (rand_init): Use rb_integer_pack.
(roomof): Removed.
Sat Jun 8 14:58:32 2013 Tanaka Akira <akr@fsij.org> Sat Jun 8 14:58:32 2013 Tanaka Akira <akr@fsij.org>
* internal.h (INTEGER_PACK_FORCE_BIGNUM): New flag constant. * internal.h (INTEGER_PACK_FORCE_BIGNUM): New flag constant.

View File

@ -270,7 +270,6 @@ rb_genrand_real(void)
#define BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1))) #define BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1)))
#define BDIGMAX ((BDIGIT)-1) #define BDIGMAX ((BDIGIT)-1)
#define roomof(n, m) (int)(((n)+(m)-1) / (m))
#define SIZEOF_INT32 (31/CHAR_BIT + 1) #define SIZEOF_INT32 (31/CHAR_BIT + 1)
static double static double
@ -379,58 +378,34 @@ static VALUE
rand_init(struct MT *mt, VALUE vseed) rand_init(struct MT *mt, VALUE vseed)
{ {
volatile VALUE seed; volatile VALUE seed;
long blen = 0; uint32_t buf0[SIZEOF_LONG / SIZEOF_INT32 * 4], *buf = buf0;
long fixnum_seed; size_t len;
int i, j, len; int sign;
unsigned int buf0[SIZEOF_LONG / SIZEOF_INT32 * 4], *buf = buf0;
seed = rb_to_int(vseed); seed = rb_to_int(vseed);
switch (TYPE(seed)) {
case T_FIXNUM: len = rb_absint_size_in_word(seed, 32, NULL);
len = 1; if (MT_MAX_STATE < len)
fixnum_seed = FIX2LONG(seed); len = MT_MAX_STATE;
if (fixnum_seed < 0) if (len > numberof(buf0))
fixnum_seed = -fixnum_seed; buf = ALLOC_N(unsigned int, len);
buf[0] = (unsigned int)(fixnum_seed & 0xffffffff); rb_integer_pack(seed, &sign, NULL, buf, len, sizeof(uint32_t), 0,
#if SIZEOF_LONG > SIZEOF_INT32 INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
if ((long)(int32_t)fixnum_seed != fixnum_seed) { if (sign < 0)
if ((buf[1] = (unsigned int)(fixnum_seed >> 32)) != 0) ++len; sign = -sign;
} if (sign != 2) { /* not overflow */
#endif while (0 < len && buf[len-1] == 0)
break; len--;
case T_BIGNUM: }
blen = RBIGNUM_LEN(seed); if (len == 0) {
if (blen == 0) { buf[0] = 0;
len = 1; len = 1;
}
else {
if (blen > MT_MAX_STATE * SIZEOF_INT32 / SIZEOF_BDIGITS)
blen = MT_MAX_STATE * SIZEOF_INT32 / SIZEOF_BDIGITS;
len = roomof((int)blen * SIZEOF_BDIGITS, SIZEOF_INT32);
}
/* allocate ints for init_by_array */
if (len > numberof(buf0)) buf = ALLOC_N(unsigned int, len);
memset(buf, 0, len * sizeof(*buf));
len = 0;
for (i = (int)(blen-1); 0 <= i; i--) {
j = i * SIZEOF_BDIGITS / SIZEOF_INT32;
#if SIZEOF_BDIGITS < SIZEOF_INT32
buf[j] <<= BITSPERDIG;
#endif
buf[j] |= RBIGNUM_DIGITS(seed)[i];
if (!len && buf[j]) len = j;
}
++len;
break;
default:
rb_raise(rb_eTypeError, "failed to convert %s into Integer",
rb_obj_classname(vseed));
} }
if (len <= 1) { if (len <= 1) {
init_genrand(mt, buf[0]); init_genrand(mt, buf[0]);
} }
else { else {
if (buf[len-1] == 1) /* remove leading-zero-guard */ if (sign != 2 && buf[len-1] == 1) /* remove leading-zero-guard */
len--; len--;
init_by_array(mt, buf, len); init_by_array(mt, buf, len);
} }