summaryrefslogtreecommitdiff
path: root/bignum.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
commit210367ec889f5910e270d6ea2c7ddb8a8d939e61 (patch)
treefeb35473da45947378fbc02defe39bcd79ef600e /bignum.c
parent9c5b1986a36c7a700b4c76817e35aa874ba7907c (diff)
This commit was generated by cvs2svn to compensate for changes in r372,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c568
1 files changed, 316 insertions, 252 deletions
diff --git a/bignum.c b/bignum.c
index 17d9c839c5..770e86ad80 100644
--- a/bignum.c
+++ b/bignum.c
@@ -9,28 +9,28 @@
************************************************/
#include "ruby.h"
-#include <ctype.h>
#include <math.h>
+#include <ctype.h>
-extern VALUE cInteger;
-VALUE cBignum;
+VALUE rb_cBignum;
+typedef unsigned short USHORT;
#define BDIGITS(x) RBIGNUM(x)->digits
-#define BITSPERDIG (sizeof(USHORT)*CHAR_BIT)
+#define BITSPERDIG (sizeof(short)*CHAR_BIT)
#define BIGRAD (1L << BITSPERDIG)
-#define DIGSPERINT ((UINT)(sizeof(INT)/sizeof(USHORT)))
-#define BIGUP(x) ((UINT)(x) << BITSPERDIG)
-#define BIGDN(x) ((x) >> BITSPERDIG)
-#define BIGLO(x) ((x) & (BIGRAD-1))
+#define DIGSPERINT ((unsigned int)(sizeof(long)/sizeof(short)))
+#define BIGUP(x) ((unsigned long)(x) << BITSPERDIG)
+#define BIGDN(x) (((x)<0) ? ~((~(x))>>BITSPERDIG) : (x)>>BITSPERDIG)
+#define BIGLO(x) ((USHORT)((x) & (BIGRAD-1)))
static VALUE
-bignew_1(class, len, sign)
- VALUE class;
- UINT len;
+bignew_1(klass, len, sign)
+ VALUE klass;
+ size_t len;
char sign;
{
NEWOBJ(big, struct RBignum);
- OBJSETUP(big, cBignum, T_BIGNUM);
+ OBJSETUP(big, klass, T_BIGNUM);
big->sign = sign;
big->len = len;
BDIGITS(big) = ALLOC_N(USHORT, len);
@@ -38,10 +38,10 @@ bignew_1(class, len, sign)
return (VALUE)big;
}
-#define bignew(len,sign) bignew_1(cBignum,len,sign)
+#define bignew(len,sign) bignew_1(rb_cBignum,len,sign)
VALUE
-big_clone(x)
+rb_big_clone(x)
VALUE x;
{
VALUE z = bignew_1(CLASS_OF(x), RBIGNUM(x)->len, RBIGNUM(x)->sign);
@@ -51,22 +51,22 @@ big_clone(x)
}
void
-big_2comp(x) /* get 2's complement */
+rb_big_2comp(x) /* get 2's complement */
VALUE x;
{
- UINT i = RBIGNUM(x)->len;
+ int i = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(x);
long num;
while (i--) ds[i] = ~ds[i];
i = 0; num = 1;
do {
- num += (long)ds[i];
+ num += ds[i];
ds[i++] = BIGLO(num);
num = BIGDN(num);
} while (i < RBIGNUM(x)->len);
if (ds[0] == 1 || ds[0] == 0) {
- for (i=1;i<RBIGNUM(x)->len;i++) {
+ for (i=1; i<RBIGNUM(x)->len; i++) {
if (ds[i] != 0) return;
}
REALLOC_N(BDIGITS(x), USHORT, RBIGNUM(x)->len++);
@@ -79,39 +79,39 @@ static VALUE
bignorm(x)
VALUE x;
{
- UINT len = RBIGNUM(x)->len;
+ size_t len = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(x);
while (len-- && !ds[len]) ;
RBIGNUM(x)->len = ++len;
- if (len*sizeof(USHORT) < sizeof(VALUE) ||
- (len*sizeof(USHORT) == sizeof(VALUE) &&
- ds[sizeof(VALUE)/sizeof(USHORT)-1] <= 0x3fff)) {
+ if (len*sizeof(USHORT) <= sizeof(VALUE)) {
long num = 0;
while (len--) {
num = BIGUP(num) + ds[len];
}
- if (RBIGNUM(x)->sign) {
- if (POSFIXABLE(num)) return INT2FIX(num);
+ if (num >= 0) {
+ if (RBIGNUM(x)->sign) {
+ if (POSFIXABLE(num)) return INT2FIX(num);
+ }
+ else if (NEGFIXABLE(-(long)num)) return INT2FIX(-(long)num);
}
- else if (NEGFIXABLE(-num)) return INT2FIX(-num);
}
return x;
}
VALUE
-big_norm(x)
+rb_big_norm(x)
VALUE x;
{
- return bignorm(RBIGNUM(x));
+ return bignorm(x);
}
VALUE
-uint2big(n)
- UINT n;
+rb_uint2big(n)
+ unsigned long n;
{
- UINT i = 0;
+ unsigned int i = 0;
USHORT *digits;
VALUE big;
@@ -130,17 +130,17 @@ uint2big(n)
}
VALUE
-int2big(n)
- INT n;
+rb_int2big(n)
+ long n;
{
- INT neg = 0;
+ long neg = 0;
VALUE big;
if (n < 0) {
n = -n;
neg = 1;
}
- big = uint2big(n);
+ big = rb_uint2big(n);
if (neg) {
RBIGNUM(big)->sign = 0;
}
@@ -148,34 +148,39 @@ int2big(n)
}
VALUE
-uint2inum(n)
- UINT n;
+rb_uint2inum(n)
+ unsigned long n;
{
if (POSFIXABLE(n)) return INT2FIX(n);
- return uint2big(n);
+ return rb_uint2big(n);
}
VALUE
-int2inum(n)
- INT n;
+rb_int2inum(n)
+ long n;
{
if (FIXABLE(n)) return INT2FIX(n);
- return int2big(n);
+ return rb_int2big(n);
}
VALUE
-str2inum(str, base)
- UCHAR *str;
+rb_str2inum(str, base)
+ char *str;
int base;
{
char sign = 1, c;
unsigned long num;
- UINT len, blen = 1, i;
+ size_t len, blen = 1;
+ int i;
VALUE z;
USHORT *zds;
- while (isspace(*str)) str++;
- if (*str == '-') {
+ while (ISSPACE(*str)) str++;
+
+ if (*str == '+') {
+ str++;
+ }
+ else if (*str == '-') {
str++;
sign = 0;
}
@@ -195,26 +200,30 @@ str2inum(str, base)
base = 10;
}
}
- len = strlen(str);
if (base == 8) {
- len = 3*len*sizeof(char);
+ while (str[0] == '0') str++;
+ len = 3*strlen(str)*sizeof(char);
}
else { /* base == 10 or 16 */
- len = 4*len*sizeof(char);
+ if (base == 16 && str[0] == '0' && (str[1] == 'x'||str[1] == 'X')) {
+ str += 2;
+ }
+ while (str[0] == '0') str++;
+ len = 4*strlen(str)*sizeof(char);
}
if (len <= (sizeof(VALUE)*CHAR_BIT)) {
- UINT val = strtoul((char*)str, 0, base);
+ unsigned int val = strtoul((char*)str, 0, base);
if (POSFIXABLE(val)) {
if (sign) return INT2FIX(val);
else {
- INT result = -(INT)val;
+ long result = -(long)val;
return INT2FIX(result);
}
}
else {
- VALUE big = uint2big(val);
+ VALUE big = rb_uint2big(val);
RBIGNUM(big)->sign = sign;
return big;
}
@@ -263,21 +272,21 @@ str2inum(str, base)
static char hexmap[] = "0123456789abcdef";
VALUE
-big2str(x, base)
+rb_big2str(x, base)
VALUE x;
int base;
{
VALUE t;
USHORT *ds;
- UINT i, j, hbase;
+ unsigned int i, j, hbase;
VALUE ss;
- UCHAR *s, c;
+ char *s, c;
if (FIXNUM_P(x)) {
- return fix2str(x, base);
+ return rb_fix2str(x, base);
}
i = RBIGNUM(x)->len;
- if (i == 0) return str_new2("0");
+ if (i == 0) return rb_str_new2("0");
if (base == 10) {
j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i*241L)/800+2;
hbase = 10000;
@@ -297,12 +306,12 @@ big2str(x, base)
else {
j = 0;
hbase = 0;
- Fail("bignum cannot treat base %d", base);
+ rb_raise(rb_eArgError, "bignum cannot treat base %d", base);
}
- t = big_clone(x);
+ t = rb_big_clone(x);
ds = BDIGITS(t);
- ss = str_new(0, j);
+ ss = rb_str_new(0, j);
s = RSTRING(ss)->ptr;
s[0] = RBIGNUM(x)->sign ? '+' : '-';
@@ -311,13 +320,13 @@ big2str(x, base)
unsigned long num = 0;
while (k--) {
num = BIGUP(num) + ds[k];
- ds[k] = num / hbase;
+ ds[k] = (USHORT)(num / hbase);
num %= hbase;
}
if (ds[i-1] == 0) i--;
k = 4;
while (k--) {
- c = num % base;
+ c = (char)(num % base);
s[--j] = hexmap[(int)c];
num /= base;
if (i == 0 && num == 0) break;
@@ -332,44 +341,56 @@ big2str(x, base)
}
static VALUE
-big_to_s(x)
+rb_big_to_s(x)
VALUE x;
{
- return big2str(x, 10);
+ return rb_big2str(x, 10);
}
-INT
-big2int(x)
+unsigned long
+rb_big2ulong(x)
VALUE x;
{
- UINT num;
- UINT len = RBIGNUM(x)->len;
+ unsigned long num;
+ size_t len = RBIGNUM(x)->len;
USHORT *ds;
if (len > sizeof(long)/sizeof(USHORT))
- ArgError("Bignum too big to convert into fixnum");
+ rb_raise(rb_eArgError, "bignum too big to convert into `uint'");
ds = BDIGITS(x);
num = 0;
while (len--) {
num = BIGUP(num);
num += ds[len];
}
- if (!RBIGNUM(x)->sign) return -num;
return num;
}
-VALUE
-big_to_i(x)
+long
+rb_big2long(x)
+ VALUE x;
+{
+ unsigned long num = rb_big2ulong(x);
+
+ if ((long)num < 0) {
+ rb_raise(rb_eArgError, "bignum too big to convert into `int'");
+ }
+ if (!RBIGNUM(x)->sign) return -(long)num;
+ return num;
+}
+
+static VALUE
+rb_big_to_i(x)
VALUE x;
{
return bignorm(x);
}
VALUE
-dbl2big(d)
+rb_dbl2big(d)
double d;
{
- UINT i = 0;
+ unsigned int i = 0;
long c;
USHORT *digits;
VALUE z;
@@ -385,18 +406,18 @@ dbl2big(d)
u *= BIGRAD;
c = (long)u;
u -= c;
- digits[i] = c;
+ digits[i] = (USHORT)c;
}
return bignorm(z);
}
double
-big2dbl(x)
+rb_big2dbl(x)
VALUE x;
{
double d = 0.0;
- UINT i = RBIGNUM(x)->len;
+ size_t i = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(x);
while (i--) {
@@ -406,29 +427,29 @@ big2dbl(x)
return d;
}
-VALUE
-big_to_f(x)
+static VALUE
+rb_big_to_f(x)
VALUE x;
{
- return float_new(big2dbl(x));
+ return rb_float_new(rb_big2dbl(x));
}
static VALUE
-big_cmp(x, y)
+rb_big_cmp(x, y)
VALUE x, y;
{
- int xlen = RBIGNUM(x)->len;
+ size_t xlen = RBIGNUM(x)->len;
switch (TYPE(y)) {
case T_FIXNUM:
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
break;
case T_BIGNUM:
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
if (RBIGNUM(x)->sign > RBIGNUM(y)->sign) return INT2FIX(1);
@@ -446,18 +467,18 @@ big_cmp(x, y)
}
static VALUE
-big_eq(x, y)
+rb_big_eq(x, y)
VALUE x, y;
{
- if (big_cmp(x, y) == INT2FIX(0)) return TRUE;
- return FALSE;
+ if (rb_big_cmp(x, y) == INT2FIX(0)) return Qtrue;
+ return Qfalse;
}
static VALUE
-big_uminus(x)
+rb_big_uminus(x)
VALUE x;
{
- VALUE z = big_clone(x);
+ VALUE z = rb_big_clone(x);
RBIGNUM(z)->sign = !RBIGNUM(x)->sign;
@@ -465,16 +486,16 @@ big_uminus(x)
}
static VALUE
-big_neg(x)
+rb_big_neg(x)
VALUE x;
{
- VALUE z = big_clone(x);
- UINT i = RBIGNUM(x)->len;
+ VALUE z = rb_big_clone(x);
+ size_t i = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(z);
- if (!RBIGNUM(x)->sign) big_2comp(z);
+ if (!RBIGNUM(x)->sign) rb_big_2comp(z);
while (i--) ds[i] = ~ds[i];
- if (RBIGNUM(x)->sign) big_2comp(z);
+ if (RBIGNUM(x)->sign) rb_big_2comp(z);
RBIGNUM(z)->sign = !RBIGNUM(z)->sign;
return bignorm(z);
@@ -487,7 +508,7 @@ bigsub(x, y)
VALUE z = 0;
USHORT *zds;
long num;
- UINT i;
+ size_t i;
i = RBIGNUM(x)->len;
/* if x is larger than y, swap */
@@ -535,10 +556,11 @@ bigadd(x, y, sign)
{
VALUE z;
long num;
- UINT i, len;
+ size_t i, len;
- if (RBIGNUM(x)->sign == (RBIGNUM(y)->sign ^ sign)) {
- if (RBIGNUM(y)->sign == sign) return bigsub(y, x);
+ sign = (sign == RBIGNUM(y)->sign);
+ if (RBIGNUM(x)->sign != sign) {
+ if (sign) return bigsub(y, x);
return bigsub(x, y);
}
@@ -549,11 +571,11 @@ bigadd(x, y, sign)
else {
len = RBIGNUM(y)->len + 1;
}
- z = bignew(len, sign==RBIGNUM(y)->sign);
+ z = bignew(len, sign);
len = RBIGNUM(x)->len;
for (i = 0, num = 0; i < len; i++) {
- num += (long)(BDIGITS(x)[i] + BDIGITS(y)[i]);
+ num += BDIGITS(x)[i] + BDIGITS(y)[i];
BDIGITS(z)[i] = BIGLO(num);
num = BIGDN(num);
}
@@ -567,72 +589,72 @@ bigadd(x, y, sign)
BDIGITS(z)[i] = BDIGITS(y)[i];
i++;
}
- BDIGITS(z)[i] = num;
+ BDIGITS(z)[i] = (USHORT)num;
return bignorm(z);
}
VALUE
-big_plus(x, y)
+rb_big_plus(x, y)
VALUE x, y;
{
switch (TYPE(y)) {
case T_FIXNUM:
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
/* fall through */
case T_BIGNUM:
return bigadd(x, y, 1);
case T_FLOAT:
- return float_new(big2dbl(x) + RFLOAT(y)->value);
+ return rb_float_new(rb_big2dbl(x) + RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
VALUE
-big_minus(x, y)
+rb_big_minus(x, y)
VALUE x, y;
{
switch (TYPE(y)) {
case T_FIXNUM:
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
/* fall through */
case T_BIGNUM:
return bigadd(x, y, 0);
case T_FLOAT:
- return float_new(big2dbl(x) - RFLOAT(y)->value);
+ return rb_float_new(rb_big2dbl(x) - RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
}
VALUE
-big_mul(x, y)
+rb_big_mul(x, y)
VALUE x, y;
{
- UINT i = 0, j;
+ size_t i, j;
unsigned long n = 0;
VALUE z;
USHORT *zds;
- if (FIXNUM_P(x)) x = int2big(FIX2INT(x));
+ if (FIXNUM_P(x)) x = rb_int2big(FIX2LONG(x));
switch (TYPE(y)) {
case T_FIXNUM:
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
break;
case T_BIGNUM:
break;
case T_FLOAT:
- return float_new(big2dbl(x) * RFLOAT(y)->value);
+ return rb_float_new(rb_big2dbl(x) * RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
j = RBIGNUM(x)->len + RBIGNUM(y)->len + 1;
@@ -658,19 +680,21 @@ big_mul(x, y)
}
static void
-bigdivmod(x, y, div, mod)
+bigdivmod(x, y, div, mod, modulo)
VALUE x, y;
VALUE *div, *mod;
+ int modulo;
{
- UINT nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len, i, j;
- VALUE z;
+ size_t nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len;
+ int i, j;
+ VALUE yy, z;
USHORT *xds, *yds, *zds, *tds;
- unsigned long t2;
+ size_t t2;
long num;
USHORT dd, q;
yds = BDIGITS(y);
- if (ny == 0 && yds[0] == 0) num_zerodiv();
+ if (ny == 0 && yds[0] == 0) rb_num_zerodiv();
if (nx < ny || nx == ny && BDIGITS(x)[nx - 1] < BDIGITS(y)[ny - 1]) {
if (div) *div = INT2FIX(0);
if (mod) *mod = bignorm(x);
@@ -679,17 +703,17 @@ bigdivmod(x, y, div, mod)
xds = BDIGITS(x);
if (ny == 1) {
dd = yds[0];
- z = big_clone(x);
+ z = rb_big_clone(x);
zds = BDIGITS(z);
t2 = 0; i = nx;
while (i--) {
t2 = BIGUP(t2) + zds[i];
- zds[i] = t2 / dd;
+ zds[i] = (USHORT)(t2 / dd);
t2 %= dd;
}
if (div) *div = bignorm(z);
if (mod) {
- if (!RBIGNUM(y)->sign) t2 = -t2;
+ if (!RBIGNUM(y)->sign) t2 = -(long)t2;
*mod = INT2FIX(t2);
}
return;
@@ -699,12 +723,12 @@ bigdivmod(x, y, div, mod)
if (nx==ny) zds[nx+1] = 0;
while (!yds[ny-1]) ny--;
if ((dd = BIGRAD/(int)(yds[ny-1]+1)) != 1) {
- y = big_clone(y);
- tds = BDIGITS(y);
+ yy = rb_big_clone(y);
+ tds = BDIGITS(yy);
j = 0;
num = 0;
while (j<ny) {
- num += (unsigned long)yds[j]*dd;
+ num += (long)yds[j]*dd;
tds[j++] = BIGLO(num);
num = BIGDN(num);
}
@@ -712,11 +736,11 @@ bigdivmod(x, y, div, mod)
j = 0;
num = 0;
while (j<nx) {
- num += (unsigned long)xds[j]*dd;
+ num += (long)xds[j]*dd;
zds[j++] = BIGLO(num);
num = BIGDN(num);
}
- zds[j] = num;
+ zds[j] = (USHORT)num;
}
else {
zds[nx] = 0;
@@ -726,12 +750,12 @@ bigdivmod(x, y, div, mod)
j = nx==ny?nx+1:nx;
do {
if (zds[j] == yds[ny-1]) q = BIGRAD-1;
- else q = (BIGUP(zds[j]) + zds[j-1])/yds[ny-1];
+ else q = (USHORT)((BIGUP(zds[j]) + zds[j-1])/yds[ny-1]);
if (q) {
i = 0; num = 0; t2 = 0;
do { /* multiply and subtract */
int ee;
- t2 += (unsigned long)yds[i] * q;
+ t2 += (long)yds[i] * q;
ee = num - BIGLO(t2);
num = zds[j - ny + i] + ee;
if (ee) zds[j - ny + i] = BIGLO(num);
@@ -753,7 +777,7 @@ bigdivmod(x, y, div, mod)
zds[j] = q;
} while (--j >= ny);
if (div) { /* move quotient down in z */
- *div = big_clone(z);
+ *div = rb_big_clone(z);
zds = BDIGITS(*div);
j = (nx==ny ? nx+2 : nx+1) - ny;
for (i = 0;i < j;i++) zds[i] = zds[i+ny];
@@ -761,105 +785,130 @@ bigdivmod(x, y, div, mod)
*div = bignorm(*div);
}
if (mod) { /* just normalize remainder */
- *mod = big_clone(z);
+ *mod = rb_big_clone(z);
if (dd) {
zds = BDIGITS(*mod);
t2 = 0; i = ny;
while(i--) {
t2 = BIGUP(t2) + zds[i];
- zds[i] = t2 / dd;
+ zds[i] = (USHORT)(t2 / dd);
t2 %= dd;
}
}
RBIGNUM(*mod)->len = ny;
- RBIGNUM(*mod)->sign = RBIGNUM(y)->sign;
+ RBIGNUM(*mod)->sign = RBIGNUM(x)->sign;
+ if (modulo && RBIGNUM(x)->sign != RBIGNUM(y)->sign) {
+ size_t len = ny;
+ zds = BDIGITS(*mod);
+ while (len-- && !zds[len]);
+ if (len > 0) {
+ *mod = bigadd(*mod, y, 1);
+ return;
+ }
+ }
*mod = bignorm(*mod);
}
}
static VALUE
-big_div(x, y)
+rb_big_div(x, y)
VALUE x, y;
{
VALUE z;
switch (TYPE(y)) {
case T_FIXNUM:
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
break;
case T_BIGNUM:
break;
case T_FLOAT:
- return float_new(big2dbl(x) / RFLOAT(y)->value);
+ return rb_float_new(rb_big2dbl(x) / RFLOAT(y)->value);
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
- bigdivmod(x, y, &z, 0);
+ bigdivmod(x, y, &z, 0, 0);
return z;
}
+
static VALUE
-big_mod(x, y)
+rb_big_modulo(x, y, modulo)
VALUE x, y;
+ int modulo;
{
VALUE z;
switch (TYPE(y)) {
case T_FIXNUM:
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
break;
case T_BIGNUM:
break;
case T_FLOAT:
- y = dbl2big(RFLOAT(y)->value);
+ y = rb_dbl2big(RFLOAT(y)->value);
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
- bigdivmod(x, y, 0, &z);
+ bigdivmod(x, y, 0, &z, modulo);
return z;
}
static VALUE
-big_divmod(x, y)
+rb_big_mod(x, y)
+ VALUE x, y;
+{
+ return rb_big_modulo(x, y, 1);
+}
+
+static VALUE
+rb_big_remainder(x, y)
+ VALUE x, y;
+{
+ return rb_big_modulo(x, y, 0);
+}
+
+static VALUE
+rb_big_divmod(x, y)
VALUE x, y;
{
VALUE div, mod;
switch (TYPE(y)) {
case T_FIXNUM:
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
break;
case T_FLOAT:
- y = dbl2big(RFLOAT(y)->value);
+ y = rb_dbl2big(RFLOAT(y)->value);
break;
case T_BIGNUM:
break;
default:
- return num_coerce_bin(x, y);
+ return rb_num_coerce_bin(x, y);
}
- bigdivmod(x, y, &div, &mod);
+ bigdivmod(x, y, &div, &mod, 1);
- return assoc_new(div, mod);;
+ return rb_assoc_new(div, mod);;
}
VALUE
-big_pow(x, y)
+rb_big_pow(x, y)
VALUE x, y;
{
double d;
- VALUE z;
+ long yy;
if (y == INT2FIX(0)) return INT2FIX(1);
switch (TYPE(y)) {
@@ -868,57 +917,59 @@ big_pow(x, y)
break;
case T_BIGNUM:
- if (RBIGNUM(y)->sign) goto pos_big;
- d = big2dbl(y);
+ rb_warn("in a**b, b may be too big");
+ d = rb_big2dbl(y);
break;
case T_FIXNUM:
- if (FIX2INT(y) > 0) goto pos_big;
- d = (double)FIX2INT(y);
+ yy = NUM2LONG(y);
+ if (yy > 0) {
+ VALUE z;
+
+ z = x;
+ for (;;) {
+ yy = yy - 1;
+ if (yy == 0) break;
+ while (yy % 2 == 0) {
+ yy = yy / 2;
+ x = rb_big_mul(x, x);
+ }
+ z = rb_big_mul(z, x);
+ }
+ return z;
+ }
+ d = (double)yy;
break;
default:
- return num_coerce_bin(x, y);
- }
- return float_new(pow(big2dbl(x), d));
-
- pos_big:
- z = x;
- for (;;) {
- y = rb_funcall(y, '-', 1, INT2FIX(1));
- if (y == INT2FIX(0)) break;
- while (rb_funcall(y, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
- y = rb_funcall(y, '/', 1, INT2FIX(2));
- x = big_mul(x, x);
- }
- z = big_mul(z, x);
+ return rb_num_coerce_bin(x, y);
}
- return z;
+ return rb_float_new(pow(rb_big2dbl(x), d));
}
VALUE
-big_and(x, y)
+rb_big_and(x, y)
VALUE x, y;
{
VALUE z;
USHORT *ds1, *ds2, *zds;
- UINT i, l1, l2;
+ size_t i, l1, l2;
char sign;
if (FIXNUM_P(y)) {
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
}
else {
Check_Type(y, T_BIGNUM);
}
if (!RBIGNUM(y)->sign) {
- y = big_clone(y);
- big_2comp(y);
+ y = rb_big_clone(y);
+ rb_big_2comp(y);
}
if (!RBIGNUM(x)->sign) {
- x = big_clone(x);
- big_2comp(x);
+ x = rb_big_clone(x);
+ rb_big_2comp(x);
}
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
l1 = RBIGNUM(y)->len;
@@ -943,33 +994,33 @@ big_and(x, y)
for (; i<l2; i++) {
zds[i] = sign?0:ds2[i];
}
- if (!RBIGNUM(z)->sign) big_2comp(z);
+ if (!RBIGNUM(z)->sign) rb_big_2comp(z);
return bignorm(z);
}
VALUE
-big_or(x, y)
+rb_big_or(x, y)
VALUE x, y;
{
VALUE z;
USHORT *ds1, *ds2, *zds;
- UINT i, l1, l2;
+ unsigned int i, l1, l2;
char sign;
if (FIXNUM_P(y)) {
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
}
else {
Check_Type(y, T_BIGNUM);
}
if (!RBIGNUM(y)->sign) {
- y = big_clone(y);
- big_2comp(y);
+ y = rb_big_clone(y);
+ rb_big_2comp(y);
}
if (!RBIGNUM(x)->sign) {
- x = big_clone(x);
- big_2comp(x);
+ x = rb_big_clone(x);
+ rb_big_2comp(x);
}
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
l1 = RBIGNUM(y)->len;
@@ -994,34 +1045,34 @@ big_or(x, y)
for (; i<l2; i++) {
zds[i] = sign?ds2[i]:(BIGRAD-1);
}
- if (!RBIGNUM(z)->sign) big_2comp(z);
+ if (!RBIGNUM(z)->sign) rb_big_2comp(z);
return bignorm(z);
}
VALUE
-big_xor(x, y)
+rb_big_xor(x, y)
VALUE x, y;
{
VALUE z;
USHORT *ds1, *ds2, *zds;
- UINT i, l1, l2;
+ unsigned int i, l1, l2;
char sign;
if (FIXNUM_P(y)) {
- y = int2big(FIX2INT(y));
+ y = rb_int2big(FIX2LONG(y));
}
else {
Check_Type(y, T_BIGNUM);
}
if (!RBIGNUM(y)->sign) {
- y = big_clone(y);
- big_2comp(y);
+ y = rb_big_clone(y);
+ rb_big_2comp(y);
}
if (!RBIGNUM(x)->sign) {
- x = big_clone(x);
- big_2comp(x);
+ x = rb_big_clone(x);
+ rb_big_2comp(x);
}
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
l1 = RBIGNUM(y)->len;
@@ -1048,26 +1099,26 @@ big_xor(x, y)
for (; i<l2; i++) {
zds[i] = sign?ds2[i]:~ds2[i];
}
- if (!RBIGNUM(z)->sign) big_2comp(z);
+ if (!RBIGNUM(z)->sign) rb_big_2comp(z);
return bignorm(z);
}
-static VALUE big_rshift();
+static VALUE rb_big_rshift _((VALUE,VALUE));
VALUE
-big_lshift(x, y)
+rb_big_lshift(x, y)
VALUE x, y;
{
USHORT *xds, *zds;
int shift = NUM2INT(y);
- UINT s1 = shift/BITSPERDIG;
- UINT s2 = shift%BITSPERDIG;
+ size_t s1 = shift/BITSPERDIG;
+ size_t s2 = shift%BITSPERDIG;
VALUE z;
unsigned long num = 0;
- UINT len, i;
+ size_t len, i;
- if (shift < 0) return big_rshift(x, INT2FIX(-shift));
+ if (shift < 0) return rb_big_rshift(x, INT2FIX(-shift));
xds = BDIGITS(x);
len = RBIGNUM(x)->len;
z = bignew(len+s1+1, RBIGNUM(x)->sign);
@@ -1085,19 +1136,19 @@ big_lshift(x, y)
}
static VALUE
-big_rshift(x, y)
+rb_big_rshift(x, y)
VALUE x, y;
{
USHORT *xds, *zds;
int shift = NUM2INT(y);
- UINT s1 = shift/BITSPERDIG;
- UINT s2 = shift%BITSPERDIG;
+ size_t s1 = shift/BITSPERDIG;
+ size_t s2 = shift%BITSPERDIG;
VALUE z;
unsigned long num = 0;
- UINT i = RBIGNUM(x)->len;
- UINT j;
+ size_t i = RBIGNUM(x)->len;
+ size_t j;
- if (shift < 0) return big_lshift(x, INT2FIX(-shift));
+ if (shift < 0) return rb_big_lshift(x, INT2FIX(-shift));
if (s1 > RBIGNUM(x)->len) {
if (RBIGNUM(x)->sign)
return INT2FIX(0);
@@ -1117,12 +1168,12 @@ big_rshift(x, y)
}
static VALUE
-big_aref(x, y)
+rb_big_aref(x, y)
VALUE x, y;
{
USHORT *xds;
int shift = NUM2INT(y);
- UINT s1, s2;
+ size_t s1, s2;
if (shift < 0) return INT2FIX(0);
s1 = shift/BITSPERDIG;
@@ -1130,8 +1181,8 @@ big_aref(x, y)
if (!RBIGNUM(x)->sign) {
if (s1 >= RBIGNUM(x)->len) return INT2FIX(1);
- x = big_clone(x);
- big_2comp(x);
+ x = rb_big_clone(x);
+ rb_big_2comp(x);
}
else {
if (s1 >= RBIGNUM(x)->len) return INT2FIX(0);
@@ -1143,10 +1194,11 @@ big_aref(x, y)
}
static VALUE
-big_hash(x)
+rb_big_hash(x)
VALUE x;
{
- int i, len, key;
+ size_t i, len;
+ int key;
USHORT *digits;
key = 0; digits = BDIGITS(x);
@@ -1157,27 +1209,29 @@ big_hash(x)
}
static VALUE
-big_coerce(x, y)
+rb_big_coerce(x, y)
VALUE x, y;
{
if (FIXNUM_P(y)) {
- return assoc_new(int2big(FIX2INT(y)), x);
+ return rb_assoc_new(rb_int2big(FIX2LONG(y)), x);
}
else {
- TypeError("can't coerce %s to Bignum", rb_class2name(CLASS_OF(y)));
+ rb_raise(rb_eTypeError, "can't coerce %s to Bignum",
+ rb_class2name(CLASS_OF(y)));
}
/* not reached */
+ return Qnil;
}
static VALUE
-big_abs(x)
+rb_big_abs(x)
VALUE x;
{
if (!RBIGNUM(x)->sign) {
- x = big_clone(x);
+ x = rb_big_clone(x);
RBIGNUM(x)->sign = 1;
}
- return (VALUE)x;
+ return x;
}
/* !!!warnig!!!!
@@ -1185,11 +1239,11 @@ big_abs(x)
*/
VALUE
-big_rand(max)
+rb_big_rand(max)
VALUE max;
{
struct RBignum *v;
- int len;
+ size_t len;
len = RBIGNUM(max)->len;
v = RBIGNUM(bignew(len,1));
@@ -1201,47 +1255,57 @@ big_rand(max)
#endif
}
- return big_mod(v, max);
+ return rb_big_mod((VALUE)v, max);
}
static VALUE
-big_size(big)
+rb_big_size(big)
VALUE big;
{
return INT2FIX(RBIGNUM(big)->len*sizeof(USHORT));
}
+static VALUE
+rb_big_zero_p(big)
+ VALUE big;
+{
+ return Qfalse;
+}
+
void
Init_Bignum()
{
- cBignum = rb_define_class("Bignum", cInteger);
-
- rb_undef_method(CLASS_OF(cBignum), "new");
-
- rb_define_method(cBignum, "to_s", big_to_s, 0);
- rb_define_method(cBignum, "coerce", big_coerce, 1);
- rb_define_method(cBignum, "-@", big_uminus, 0);
- rb_define_method(cBignum, "+", big_plus, 1);
- rb_define_method(cBignum, "-", big_minus, 1);
- rb_define_method(cBignum, "*", big_mul, 1);
- rb_define_method(cBignum, "/", big_div, 1);
- rb_define_method(cBignum, "%", big_mod, 1);
- rb_define_method(cBignum, "divmod", big_divmod, 1);
- rb_define_method(cBignum, "**", big_pow, 1);
- rb_define_method(cBignum, "&", big_and, 1);
- rb_define_method(cBignum, "|", big_or, 1);
- rb_define_method(cBignum, "^", big_xor, 1);
- rb_define_method(cBignum, "~", big_neg, 0);
- rb_define_method(cBignum, "<<", big_lshift, 1);
- rb_define_method(cBignum, ">>", big_rshift, 1);
- rb_define_method(cBignum, "[]", big_aref, 1);
-
- rb_define_method(cBignum, "<=>", big_cmp, 1);
- rb_define_method(cBignum, "==", big_eq, 1);
- rb_define_method(cBignum, "eql?", big_eq, 1);
- rb_define_method(cBignum, "hash", big_hash, 0);
- rb_define_method(cBignum, "to_i", big_to_i, 0);
- rb_define_method(cBignum, "to_f", big_to_f, 0);
- rb_define_method(cBignum, "abs", big_abs, 0);
- rb_define_method(cBignum, "size", big_size, 0);
+ rb_cBignum = rb_define_class("Bignum", rb_cInteger);
+
+ rb_undef_method(CLASS_OF(rb_cBignum), "new");
+
+ rb_define_method(rb_cBignum, "to_s", rb_big_to_s, 0);
+ rb_define_method(rb_cBignum, "coerce", rb_big_coerce, 1);
+ rb_define_method(rb_cBignum, "-@", rb_big_uminus, 0);
+ rb_define_method(rb_cBignum, "+", rb_big_plus, 1);
+ rb_define_method(rb_cBignum, "-", rb_big_minus, 1);
+ rb_define_method(rb_cBignum, "*", rb_big_mul, 1);
+ rb_define_method(rb_cBignum, "/", rb_big_div, 1);
+ rb_define_method(rb_cBignum, "%", rb_big_mod, 1);
+ rb_define_method(rb_cBignum, "divmod", rb_big_divmod, 1);
+ rb_define_method(rb_cBignum, "remainder", rb_big_remainder, 1);
+ rb_define_method(rb_cBignum, "**", rb_big_pow, 1);
+ rb_define_method(rb_cBignum, "&", rb_big_and, 1);
+ rb_define_method(rb_cBignum, "|", rb_big_or, 1);
+ rb_define_method(rb_cBignum, "^", rb_big_xor, 1);
+ rb_define_method(rb_cBignum, "~", rb_big_neg, 0);
+ rb_define_method(rb_cBignum, "<<", rb_big_lshift, 1);
+ rb_define_method(rb_cBignum, ">>", rb_big_rshift, 1);
+ rb_define_method(rb_cBignum, "[]", rb_big_aref, 1);
+
+ rb_define_method(rb_cBignum, "<=>", rb_big_cmp, 1);
+ rb_define_method(rb_cBignum, "==", rb_big_eq, 1);
+ rb_define_method(rb_cBignum, "===", rb_big_eq, 1);
+ rb_define_method(rb_cBignum, "eql?", rb_big_eq, 1);
+ rb_define_method(rb_cBignum, "hash", rb_big_hash, 0);
+ rb_define_method(rb_cBignum, "to_i", rb_big_to_i, 0);
+ rb_define_method(rb_cBignum, "to_f", rb_big_to_f, 0);
+ rb_define_method(rb_cBignum, "abs", rb_big_abs, 0);
+ rb_define_method(rb_cBignum, "size", rb_big_size, 0);
+ rb_define_method(rb_cBignum, "zero?", rb_big_zero_p, 0);
}