1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* random.c (random_load): Use rb_integer_pack.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41159 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-07 23:12:55 +00:00
parent 97b5a9c102
commit 9ebd008f32
2 changed files with 7 additions and 44 deletions

View file

@ -1,3 +1,7 @@
Sat Jun 8 08:12:22 2013 Tanaka Akira <akr@fsij.org>
* random.c (random_load): Use rb_integer_pack.
Sat Jun 8 06:15:46 2013 Tanaka Akira <akr@fsij.org> Sat Jun 8 06:15:46 2013 Tanaka Akira <akr@fsij.org>
* random.c (numberof): Removed. * random.c (numberof): Removed.

View file

@ -690,50 +690,9 @@ random_load(VALUE obj, VALUE dump)
default: default:
rb_raise(rb_eArgError, "wrong dump data"); rb_raise(rb_eArgError, "wrong dump data");
} }
memset(mt->state, 0, sizeof(mt->state)); rb_integer_pack(state, NULL, NULL, mt->state, numberof(mt->state),
if (FIXNUM_P(state)) { sizeof(*mt->state), 0,
x = FIX2ULONG(state); INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
mt->state[0] = (unsigned int)x;
#if SIZEOF_LONG / SIZEOF_INT >= 2
mt->state[1] = (unsigned int)(x >> (sizeof(int) * CHAR_BIT));
#endif
#if SIZEOF_LONG / SIZEOF_INT >= 3
mt->state[2] = (unsigned int)(x >> 2 * (sizeof(int) * CHAR_BIT));
#endif
#if SIZEOF_LONG / SIZEOF_INT >= 4
mt->state[3] = (unsigned int)(x >> 3 * (sizeof(int) * CHAR_BIT));
#endif
}
else {
BDIGIT *dp, *de;
unsigned int *sp, *se;
BDIGIT_DBL dd;
int numbytes_in_dd;
Check_Type(state, T_BIGNUM);
dp = RBIGNUM_DIGITS(state);
de = dp + RBIGNUM_LEN(state);
sp = mt->state;
se = sp + sizeof(mt->state) / sizeof(*mt->state);;
dd = 0;
numbytes_in_dd = 0;
while (dp < de && sp < se) {
while (dp < de && SIZEOF_BDIGITS <= (int)sizeof(dd) - numbytes_in_dd) {
dd |= (BDIGIT_DBL)(*dp++) << (numbytes_in_dd * CHAR_BIT);
numbytes_in_dd += SIZEOF_BDIGITS;
}
while (sp < se && (int)sizeof(int) <= numbytes_in_dd) {
*sp++ = (unsigned int)dd;
if (sizeof(dd) == sizeof(int))
dd = 0;
else
dd >>= SIZEOF_INT * CHAR_BIT;
numbytes_in_dd -= SIZEOF_INT;
}
}
if (numbytes_in_dd && sp < se) {
*sp = (unsigned int)dd;
}
}
x = NUM2ULONG(left); x = NUM2ULONG(left);
if (x > numberof(mt->state)) { if (x > numberof(mt->state)) {
rb_raise(rb_eArgError, "wrong value"); rb_raise(rb_eArgError, "wrong value");