summaryrefslogtreecommitdiff
path: root/bignum.c
diff options
context:
space:
mode:
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c27
1 files changed, 12 insertions, 15 deletions
diff --git a/bignum.c b/bignum.c
index 36a46af3fd..e411e93f1b 100644
--- a/bignum.c
+++ b/bignum.c
@@ -16,11 +16,11 @@ VALUE rb_cBignum;
typedef unsigned short USHORT;
#define BDIGITS(x) RBIGNUM(x)->digits
-#define BITSPERDIG (sizeof(short)*CHAR_BIT)
+#define BITSPERDIG (sizeof(USHORT)*CHAR_BIT)
#define BIGRAD (1L << BITSPERDIG)
-#define DIGSPERINT ((unsigned int)(sizeof(long)/sizeof(short)))
+#define DIGSPERLONG ((unsigned int)(sizeof(long)/sizeof(USHORT)))
#define BIGUP(x) ((unsigned long)(x) << BITSPERDIG)
-#define BIGDN(x) (((x)<0) ? ~((~(x))>>BITSPERDIG) : (x)>>BITSPERDIG)
+#define BIGDN(x) RSHIFT(x,BITSPERDIG)
#define BIGLO(x) ((USHORT)((x) & (BIGRAD-1)))
static VALUE
@@ -33,7 +33,7 @@ bignew_1(klass, len, sign)
OBJSETUP(big, klass, T_BIGNUM);
big->sign = sign;
big->len = len;
- BDIGITS(big) = ALLOC_N(USHORT, len);
+ big->digits = ALLOC_N(USHORT, len);
return (VALUE)big;
}
@@ -116,14 +116,14 @@ rb_uint2big(n)
VALUE big;
i = 0;
- big = bignew(DIGSPERINT, 1);
+ big = bignew(DIGSPERLONG, 1);
digits = BDIGITS(big);
- while (i < DIGSPERINT) {
+ while (i < DIGSPERLONG) {
digits[i++] = BIGLO(n);
n = BIGDN(n);
}
- i = DIGSPERINT;
+ i = DIGSPERLONG;
while (i-- && !digits[i]) ;
RBIGNUM(big)->len = i+1;
return big;
@@ -1255,20 +1255,17 @@ rb_big_abs(x)
*/
VALUE
-rb_big_rand(max)
+rb_big_rand(max, rand)
VALUE max;
+ double rand;
{
- struct RBignum *v;
+ VALUE v;
long len;
len = RBIGNUM(max)->len;
- v = RBIGNUM(bignew(len,1));
+ v = bignew(len,1);
while (len--) {
-#ifdef HAVE_RANDOM
- BDIGITS(v)[len] = random();
-#else
- BDIGITS(v)[len] = rand();
-#endif
+ BDIGITS(v)[len] = ((USHORT)~0) * rand;
}
return rb_big_mod((VALUE)v, max);