From 9ebd008f324aeb2f4b7017e30c75629fe7441d19 Mon Sep 17 00:00:00 2001 From: akr Date: Fri, 7 Jun 2013 23:12:55 +0000 Subject: [PATCH] * 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 --- ChangeLog | 4 ++++ random.c | 47 +++-------------------------------------------- 2 files changed, 7 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2abf0bff24..cd1b4c9640 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sat Jun 8 08:12:22 2013 Tanaka Akira + + * random.c (random_load): Use rb_integer_pack. + Sat Jun 8 06:15:46 2013 Tanaka Akira * random.c (numberof): Removed. diff --git a/random.c b/random.c index 98332c00b8..06abe1be77 100644 --- a/random.c +++ b/random.c @@ -690,50 +690,9 @@ random_load(VALUE obj, VALUE dump) default: rb_raise(rb_eArgError, "wrong dump data"); } - memset(mt->state, 0, sizeof(mt->state)); - if (FIXNUM_P(state)) { - x = FIX2ULONG(state); - 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; - } - } + rb_integer_pack(state, NULL, NULL, mt->state, numberof(mt->state), + sizeof(*mt->state), 0, + INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER); x = NUM2ULONG(left); if (x > numberof(mt->state)) { rb_raise(rb_eArgError, "wrong value");