summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-08 00:50:40 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-08 00:50:40 +0000
commit479b17ed12c749eb2e6716672b0c0765ad87267a (patch)
tree3c1402a835bb7471150e44faed21a1a052533e62 /random.c
parent9ebd008f324aeb2f4b7017e30c75629fe7441d19 (diff)
* 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
Diffstat (limited to 'random.c')
-rw-r--r--random.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/random.c b/random.c
index 06abe1be77..b5c0c4c502 100644
--- a/random.c
+++ b/random.c
@@ -205,12 +205,12 @@ genrand_real(struct MT *mt)
}
/* 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
genrand_real2(struct MT *mt)
{
/* 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);
}
@@ -274,28 +274,25 @@ rb_genrand_real(void)
#define SIZEOF_INT32 (31/CHAR_BIT + 1)
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 m = rb_big_new(roomof(53, BITSPERDIG), 1);
- BDIGIT *xd = BDIGITS(x);
- int i = 0;
+ VALUE x;
+ VALUE m;
+ uint32_t xary[2], mary[2];
double r;
- xd[i++] = (BDIGIT)b;
-#if BITSPERDIG < 32
- xd[i++] = (BDIGIT)(b >> BITSPERDIG);
-#endif
- xd[i++] = (BDIGIT)a;
-#if BITSPERDIG < 32
- xd[i++] = (BDIGIT)(a >> BITSPERDIG);
-#endif
- xd = BDIGITS(m);
-#if BITSPERDIG < 53
- MEMZERO(xd, BDIGIT, roomof(53, BITSPERDIG) - 1);
-#endif
- xd[53 / BITSPERDIG] = (BDIGIT)1 << 53 % BITSPERDIG;
- xd[0] |= 1;
+ /* (a << 32) | b */
+ xary[0] = a;
+ xary[1] = b;
+ x = rb_integer_unpack(+1, xary, 2, sizeof(uint32_t), 0,
+ INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
+
+ /* (1 << 53) | 1 */
+ mary[0] = 0x00200000;
+ mary[1] = 0x00000001;
+ m = rb_integer_unpack(+1, mary, 2, sizeof(uint32_t), 0,
+ INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
+
x = rb_big_mul(x, m);
if (FIXNUM_P(x)) {
#if CHAR_BIT * SIZEOF_LONG > 64
@@ -307,7 +304,7 @@ int_pair_to_real_inclusive(unsigned int a, unsigned int b)
else {
#if 64 % BITSPERDIG == 0
long len = RBIGNUM_LEN(x);
- xd = BDIGITS(x);
+ BDIGIT *xd = BDIGITS(x);
MEMMOVE(xd, xd + 64 / BITSPERDIG, BDIGIT, len - 64 / BITSPERDIG);
MEMZERO(xd + len - 64 / BITSPERDIG, BDIGIT, 64 / BITSPERDIG);
r = rb_big2dbl(x);