* random.c (int_pair_to_real_inclusive): Use rb_integer_unpack.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41160 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-08 00:50:40 +00:00
parent 9ebd008f32
commit 479b17ed12
2 changed files with 23 additions and 22 deletions

View File

@ -1,3 +1,7 @@
Sat Jun 8 09:49:42 2013 Tanaka Akira <akr@fsij.org>
* random.c (int_pair_to_real_inclusive): Use rb_integer_unpack.
Sat Jun 8 08:12:22 2013 Tanaka Akira <akr@fsij.org> Sat Jun 8 08:12:22 2013 Tanaka Akira <akr@fsij.org>
* random.c (random_load): Use rb_integer_pack. * random.c (random_load): Use rb_integer_pack.

View File

@ -205,12 +205,12 @@ genrand_real(struct MT *mt)
} }
/* generates a random number on [0,1] with 53-bit resolution*/ /* generates a random number on [0,1] with 53-bit resolution*/
static double int_pair_to_real_inclusive(unsigned int a, unsigned int b); static double int_pair_to_real_inclusive(uint32_t a, uint32_t b);
static double static double
genrand_real2(struct MT *mt) genrand_real2(struct MT *mt)
{ {
/* mt must be initialized */ /* mt must be initialized */
unsigned int a = genrand_int32(mt), b = genrand_int32(mt); uint32_t a = genrand_int32(mt), b = genrand_int32(mt);
return int_pair_to_real_inclusive(a, b); return int_pair_to_real_inclusive(a, b);
} }
@ -274,28 +274,25 @@ rb_genrand_real(void)
#define SIZEOF_INT32 (31/CHAR_BIT + 1) #define SIZEOF_INT32 (31/CHAR_BIT + 1)
static double static double
int_pair_to_real_inclusive(unsigned int a, unsigned int b) int_pair_to_real_inclusive(uint32_t a, uint32_t b)
{ {
VALUE x = rb_big_new(roomof(64, BITSPERDIG), 1); VALUE x;
VALUE m = rb_big_new(roomof(53, BITSPERDIG), 1); VALUE m;
BDIGIT *xd = BDIGITS(x); uint32_t xary[2], mary[2];
int i = 0;
double r; double r;
xd[i++] = (BDIGIT)b; /* (a << 32) | b */
#if BITSPERDIG < 32 xary[0] = a;
xd[i++] = (BDIGIT)(b >> BITSPERDIG); xary[1] = b;
#endif x = rb_integer_unpack(+1, xary, 2, sizeof(uint32_t), 0,
xd[i++] = (BDIGIT)a; INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
#if BITSPERDIG < 32
xd[i++] = (BDIGIT)(a >> BITSPERDIG); /* (1 << 53) | 1 */
#endif mary[0] = 0x00200000;
xd = BDIGITS(m); mary[1] = 0x00000001;
#if BITSPERDIG < 53 m = rb_integer_unpack(+1, mary, 2, sizeof(uint32_t), 0,
MEMZERO(xd, BDIGIT, roomof(53, BITSPERDIG) - 1); INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
#endif
xd[53 / BITSPERDIG] = (BDIGIT)1 << 53 % BITSPERDIG;
xd[0] |= 1;
x = rb_big_mul(x, m); x = rb_big_mul(x, m);
if (FIXNUM_P(x)) { if (FIXNUM_P(x)) {
#if CHAR_BIT * SIZEOF_LONG > 64 #if CHAR_BIT * SIZEOF_LONG > 64
@ -307,7 +304,7 @@ int_pair_to_real_inclusive(unsigned int a, unsigned int b)
else { else {
#if 64 % BITSPERDIG == 0 #if 64 % BITSPERDIG == 0
long len = RBIGNUM_LEN(x); long len = RBIGNUM_LEN(x);
xd = BDIGITS(x); BDIGIT *xd = BDIGITS(x);
MEMMOVE(xd, xd + 64 / BITSPERDIG, BDIGIT, len - 64 / BITSPERDIG); MEMMOVE(xd, xd + 64 / BITSPERDIG, BDIGIT, len - 64 / BITSPERDIG);
MEMZERO(xd + len - 64 / BITSPERDIG, BDIGIT, 64 / BITSPERDIG); MEMZERO(xd + len - 64 / BITSPERDIG, BDIGIT, 64 / BITSPERDIG);
r = rb_big2dbl(x); r = rb_big2dbl(x);