2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
bignum.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Jun 10 00:48:55 JST 1994
|
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 1993-2000 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include <math.h>
|
1999-01-19 23:59:39 -05:00
|
|
|
#include <ctype.h>
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_cBignum;
|
|
|
|
typedef unsigned short USHORT;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
#define BDIGITS(x) ((USHORT*)RBIGNUM(x)->digits)
|
1999-01-19 23:59:39 -05:00
|
|
|
#define BITSPERDIG (sizeof(short)*CHAR_BIT)
|
1998-01-16 07:13:05 -05:00
|
|
|
#define BIGRAD (1L << BITSPERDIG)
|
2000-01-04 23:41:21 -05:00
|
|
|
#define DIGSPERLONG ((unsigned int)(sizeof(long)/sizeof(short)))
|
1999-01-19 23:59:39 -05:00
|
|
|
#define BIGUP(x) ((unsigned long)(x) << BITSPERDIG)
|
1999-12-01 04:24:48 -05:00
|
|
|
#define BIGDN(x) RSHIFT(x,BITSPERDIG)
|
1999-01-19 23:59:39 -05:00
|
|
|
#define BIGLO(x) ((USHORT)((x) & (BIGRAD-1)))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
bignew_1(klass, len, sign)
|
|
|
|
VALUE klass;
|
1999-08-13 01:45:20 -04:00
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
char sign;
|
|
|
|
{
|
|
|
|
NEWOBJ(big, struct RBignum);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJSETUP(big, klass, T_BIGNUM);
|
1998-01-16 07:13:05 -05:00
|
|
|
big->sign = sign;
|
|
|
|
big->len = len;
|
2000-01-04 23:41:21 -05:00
|
|
|
big->digits = ALLOC_N(USHORT, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return (VALUE)big;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
#define bignew(len,sign) bignew_1(rb_cBignum,len,sign)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_clone(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE z = bignew_1(CLASS_OF(x), RBIGNUM(x)->len, RBIGNUM(x)->sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
MEMCPY(BDIGITS(z), BDIGITS(x), USHORT, RBIGNUM(x)->len);
|
|
|
|
return z;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_2comp(x) /* get 2's complement */
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *ds = BDIGITS(x);
|
|
|
|
long num;
|
|
|
|
|
|
|
|
while (i--) ds[i] = ~ds[i];
|
|
|
|
i = 0; num = 1;
|
|
|
|
do {
|
1999-01-19 23:59:39 -05:00
|
|
|
num += ds[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
ds[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
1998-01-16 07:19:22 -05:00
|
|
|
} while (i < RBIGNUM(x)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (ds[0] == 1 || ds[0] == 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
for (i=1; i<RBIGNUM(x)->len; i++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
if (ds[i] != 0) return;
|
|
|
|
}
|
2000-04-12 01:06:23 -04:00
|
|
|
REALLOC_N(RBIGNUM(x)->digits, USHORT, RBIGNUM(x)->len++);
|
1998-01-16 07:13:05 -05:00
|
|
|
ds = BDIGITS(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
ds[RBIGNUM(x)->len-1] = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
bignorm(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long len = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *ds = BDIGITS(x);
|
|
|
|
|
|
|
|
while (len-- && !ds[len]) ;
|
1998-01-16 07:19:22 -05:00
|
|
|
RBIGNUM(x)->len = ++len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (len*sizeof(USHORT) <= sizeof(VALUE)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
long num = 0;
|
|
|
|
while (len--) {
|
|
|
|
num = BIGUP(num) + ds[len];
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (num >= 0) {
|
|
|
|
if (RBIGNUM(x)->sign) {
|
|
|
|
if (POSFIXABLE(num)) return INT2FIX(num);
|
|
|
|
}
|
|
|
|
else if (NEGFIXABLE(-(long)num)) return INT2FIX(-(long)num);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return x;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_norm(x)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return bignorm(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_uint2big(n)
|
|
|
|
unsigned long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
unsigned int i = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *digits;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE big;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
i = 0;
|
2000-01-04 23:41:21 -05:00
|
|
|
big = bignew(DIGSPERLONG, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
digits = BDIGITS(big);
|
2000-01-04 23:41:21 -05:00
|
|
|
while (i < DIGSPERLONG) {
|
1998-01-16 07:13:05 -05:00
|
|
|
digits[i++] = BIGLO(n);
|
|
|
|
n = BIGDN(n);
|
|
|
|
}
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
i = DIGSPERLONG;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (i-- && !digits[i]) ;
|
1998-01-16 07:19:22 -05:00
|
|
|
RBIGNUM(big)->len = i+1;
|
|
|
|
return big;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_int2big(n)
|
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
long neg = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE big;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
n = -n;
|
|
|
|
neg = 1;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
big = rb_uint2big(n);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (neg) {
|
1998-01-16 07:19:22 -05:00
|
|
|
RBIGNUM(big)->sign = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
return big;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_uint2inum(n)
|
|
|
|
unsigned long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
if (POSFIXABLE(n)) return INT2FIX(n);
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_uint2big(n);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_int2inum(n)
|
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
if (FIXABLE(n)) return INT2FIX(n);
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_int2big(n);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_cstr2inum(str, base)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *str;
|
1998-01-16 07:13:05 -05:00
|
|
|
int base;
|
|
|
|
{
|
2000-01-04 23:41:21 -05:00
|
|
|
const char *s = str;
|
|
|
|
char *end;
|
|
|
|
int badcheck = (base==0)?1:0;
|
1998-01-16 07:13:05 -05:00
|
|
|
char sign = 1, c;
|
|
|
|
unsigned long num;
|
1999-08-13 01:45:20 -04:00
|
|
|
long len, blen = 1;
|
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE z;
|
|
|
|
USHORT *zds;
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
while (*str && ISSPACE(*str)) str++;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (*str == '+') {
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
else if (*str == '-') {
|
1998-01-16 07:13:05 -05:00
|
|
|
str++;
|
|
|
|
sign = 0;
|
|
|
|
}
|
|
|
|
if (base == 0) {
|
|
|
|
if (*str == '0') {
|
|
|
|
str++;
|
|
|
|
if (*str == 'x' || *str == 'X') {
|
|
|
|
str++;
|
|
|
|
base = 16;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
else if (*str == 'b' || *str == 'B') {
|
|
|
|
str++;
|
|
|
|
base = 2;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
else {
|
|
|
|
base = 8;
|
2000-01-04 23:41:21 -05:00
|
|
|
if (!*str) return INT2FIX(0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
base = 10;
|
2000-01-04 23:41:21 -05:00
|
|
|
if (!*str) return INT2FIX(0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (base == 8) {
|
1999-01-19 23:59:39 -05:00
|
|
|
while (str[0] == '0') str++;
|
|
|
|
len = 3*strlen(str)*sizeof(char);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
else { /* base == 10, 2 or 16 */
|
1999-01-19 23:59:39 -05:00
|
|
|
if (base == 16 && str[0] == '0' && (str[1] == 'x'||str[1] == 'X')) {
|
|
|
|
str += 2;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (base == 2 && str[0] == '0' && (str[1] == 'b'||str[1] == 'B')) {
|
|
|
|
str += 2;
|
|
|
|
}
|
2000-01-04 23:41:21 -05:00
|
|
|
while (*str && *str == '0') str++;
|
|
|
|
if (!*str) str--;
|
1999-01-19 23:59:39 -05:00
|
|
|
len = 4*strlen(str)*sizeof(char);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len <= (sizeof(VALUE)*CHAR_BIT)) {
|
2000-01-04 23:41:21 -05:00
|
|
|
unsigned long val = strtoul((char*)str, &end, base);
|
|
|
|
|
|
|
|
if (badcheck) {
|
|
|
|
if (end == str || *end)
|
|
|
|
goto bad;
|
|
|
|
while (*end && ISSPACE(*end)) end++;
|
|
|
|
if (*end) {
|
|
|
|
bad:
|
|
|
|
rb_raise(rb_eArgError, "invalid literal for Integer: %s", s);
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (POSFIXABLE(val)) {
|
|
|
|
if (sign) return INT2FIX(val);
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
long result = -(long)val;
|
1998-01-16 07:13:05 -05:00
|
|
|
return INT2FIX(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE big = rb_uint2big(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
RBIGNUM(big)->sign = sign;
|
|
|
|
return big;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
len = (len/BITSPERDIG)+1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
z = bignew(len, sign);
|
|
|
|
zds = BDIGITS(z);
|
|
|
|
for (i=len;i--;) zds[i]=0;
|
|
|
|
while (c = *str++) {
|
|
|
|
switch (c) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
c = c - '0';
|
|
|
|
break;
|
|
|
|
case 'a': case 'b': case 'c':
|
|
|
|
case 'd': case 'e': case 'f':
|
|
|
|
c = c - 'a' + 10;
|
|
|
|
break;
|
|
|
|
case 'A': case 'B': case 'C':
|
|
|
|
case 'D': case 'E': case 'F':
|
|
|
|
c = c - 'A' + 10;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c = base;
|
2000-01-04 23:41:21 -05:00
|
|
|
if (badcheck) {
|
|
|
|
if (ISSPACE(c)) {
|
|
|
|
while (*str && ISSPACE(*str)) str++;
|
|
|
|
if (*str) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb_raise(rb_eArgError, "invalid literal for Integer: %s", s);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c >= base) break;
|
|
|
|
i = 0;
|
|
|
|
num = c;
|
|
|
|
for (;;) {
|
|
|
|
while (i<blen) {
|
|
|
|
num += zds[i]*base;
|
|
|
|
zds[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
|
|
|
if (num) {
|
|
|
|
blen++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-01-04 23:41:21 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
VALUE
|
|
|
|
rb_str2inum(str, base)
|
|
|
|
VALUE str;
|
|
|
|
int base;
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
s = rb_str2cstr(str, &len);
|
|
|
|
if (len != strlen(s)) {
|
|
|
|
rb_raise(rb_eArgError, "string for Integer contains null byte");
|
|
|
|
}
|
|
|
|
return rb_cstr2inum(s, base);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static char hexmap[] = "0123456789abcdef";
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big2str(x, base)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
int base;
|
|
|
|
{
|
|
|
|
VALUE t;
|
|
|
|
USHORT *ds;
|
1999-08-13 01:45:20 -04:00
|
|
|
unsigned long i, j, hbase;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE ss;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *s, c;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (FIXNUM_P(x)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_fix2str(x, base);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
i = RBIGNUM(x)->len;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (i == 0) return rb_str_new2("0");
|
1998-01-16 07:13:05 -05:00
|
|
|
if (base == 10) {
|
|
|
|
j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i*241L)/800+2;
|
|
|
|
hbase = 10000;
|
|
|
|
}
|
|
|
|
else if (base == 16) {
|
|
|
|
j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i)/4+2;
|
|
|
|
hbase = 0x10000;
|
|
|
|
}
|
|
|
|
else if (base == 8) {
|
|
|
|
j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i)+2;
|
|
|
|
hbase = 010000;
|
|
|
|
}
|
|
|
|
else if (base == 2) {
|
|
|
|
j = (sizeof(USHORT)*CHAR_BIT*i)+2;
|
|
|
|
hbase = 020;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
j = 0;
|
|
|
|
hbase = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "bignum cannot treat base %d", base);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
t = rb_big_clone(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
ds = BDIGITS(t);
|
1999-01-19 23:59:39 -05:00
|
|
|
ss = rb_str_new(0, j);
|
1998-01-16 07:13:05 -05:00
|
|
|
s = RSTRING(ss)->ptr;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
s[0] = RBIGNUM(x)->sign ? '+' : '-';
|
1998-01-16 07:13:05 -05:00
|
|
|
while (i && j) {
|
1999-08-13 01:45:20 -04:00
|
|
|
long k = i;
|
1998-01-16 07:13:05 -05:00
|
|
|
unsigned long num = 0;
|
|
|
|
while (k--) {
|
|
|
|
num = BIGUP(num) + ds[k];
|
1999-01-19 23:59:39 -05:00
|
|
|
ds[k] = (USHORT)(num / hbase);
|
1998-01-16 07:13:05 -05:00
|
|
|
num %= hbase;
|
|
|
|
}
|
|
|
|
if (ds[i-1] == 0) i--;
|
|
|
|
k = 4;
|
|
|
|
while (k--) {
|
1999-01-19 23:59:39 -05:00
|
|
|
c = (char)(num % base);
|
1998-01-16 07:13:05 -05:00
|
|
|
s[--j] = hexmap[(int)c];
|
|
|
|
num /= base;
|
|
|
|
if (i == 0 && num == 0) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (s[j] == '0') j++;
|
1998-01-16 07:19:22 -05:00
|
|
|
RSTRING(ss)->len -= RBIGNUM(x)->sign?j:j-1;
|
|
|
|
memmove(RBIGNUM(x)->sign?s:s+1, s+j, RSTRING(ss)->len);
|
1998-01-16 07:13:05 -05:00
|
|
|
s[RSTRING(ss)->len] = '\0';
|
|
|
|
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_to_s(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_big2str(x, 10);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
static unsigned long
|
|
|
|
big2ulong(x, type)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
2000-01-04 23:41:21 -05:00
|
|
|
char *type;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
unsigned long num;
|
1999-08-13 01:45:20 -04:00
|
|
|
long len = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *ds;
|
|
|
|
|
|
|
|
if (len > sizeof(long)/sizeof(USHORT))
|
2000-03-07 03:37:59 -05:00
|
|
|
rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
|
1998-01-16 07:13:05 -05:00
|
|
|
ds = BDIGITS(x);
|
|
|
|
num = 0;
|
|
|
|
while (len--) {
|
|
|
|
num = BIGUP(num);
|
|
|
|
num += ds[len];
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
unsigned long
|
|
|
|
rb_big2ulong(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
|
|
|
unsigned long num = big2ulong(x, "unsigned long");
|
|
|
|
|
|
|
|
if (!RBIGNUM(x)->sign) return -num;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
long
|
|
|
|
rb_big2long(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
2000-01-04 23:41:21 -05:00
|
|
|
unsigned long num = big2ulong(x, "int");
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if ((long)num < 0) {
|
2000-03-07 03:37:59 -05:00
|
|
|
rb_raise(rb_eRangeError, "bignum too big to convert into `int'");
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
if (!RBIGNUM(x)->sign) return -(long)num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_big_to_i(x)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x;
|
|
|
|
{
|
|
|
|
return bignorm(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_dbl2big(d)
|
1998-01-16 07:13:05 -05:00
|
|
|
double d;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
unsigned long i = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
long c;
|
|
|
|
USHORT *digits;
|
|
|
|
VALUE z;
|
|
|
|
double u = (d < 0)?-d:d;
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (isinf(d)) {
|
|
|
|
rb_raise(rb_eFloatDomainError, d < 0 ? "-Infinity" : "Infinity");
|
|
|
|
}
|
|
|
|
if (isnan(d)) {
|
|
|
|
rb_raise(rb_eFloatDomainError, "NaN");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!POSFIXABLE(u) || 0 != (long)u) {
|
1998-01-16 07:13:05 -05:00
|
|
|
u /= (double)(BIGRAD);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
z = bignew(i, d>=0);
|
|
|
|
digits = BDIGITS(z);
|
|
|
|
while (i--) {
|
|
|
|
u *= BIGRAD;
|
|
|
|
c = (long)u;
|
|
|
|
u -= c;
|
1999-01-19 23:59:39 -05:00
|
|
|
digits[i] = (USHORT)c;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big2dbl(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
double d = 0.0;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *ds = BDIGITS(x);
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
d = ds[i] + BIGRAD*d;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(x)->sign) d = -d;
|
1998-01-16 07:13:05 -05:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_big_to_f(x)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(rb_big2dbl(x));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_cmp(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long xlen = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RBIGNUM(x)->sign > RBIGNUM(y)->sign) return INT2FIX(1);
|
|
|
|
if (RBIGNUM(x)->sign < RBIGNUM(y)->sign) return INT2FIX(-1);
|
|
|
|
if (xlen < RBIGNUM(y)->len)
|
|
|
|
return (RBIGNUM(x)->sign) ? INT2FIX(-1) : INT2FIX(1);
|
|
|
|
if (xlen > RBIGNUM(y)->len)
|
|
|
|
return (RBIGNUM(x)->sign) ? INT2FIX(1) : INT2FIX(-1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while(xlen-- && (BDIGITS(x)[xlen]==BDIGITS(y)[xlen]));
|
|
|
|
if (-1 == xlen) return INT2FIX(0);
|
|
|
|
return (BDIGITS(x)[xlen] > BDIGITS(y)[xlen]) ?
|
1998-01-16 07:19:22 -05:00
|
|
|
(RBIGNUM(x)->sign ? INT2FIX(1) : INT2FIX(-1)) :
|
|
|
|
(RBIGNUM(x)->sign ? INT2FIX(-1) : INT2FIX(1));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_eq(x, y)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (rb_big_cmp(x, y) == INT2FIX(0)) return Qtrue;
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_uminus(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE z = rb_big_clone(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
RBIGNUM(z)->sign = !RBIGNUM(x)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_neg(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE z = rb_big_clone(x);
|
1999-08-13 01:45:20 -04:00
|
|
|
long i = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *ds = BDIGITS(z);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!RBIGNUM(x)->sign) rb_big_2comp(z);
|
1998-01-16 07:13:05 -05:00
|
|
|
while (i--) ds[i] = ~ds[i];
|
1999-01-19 23:59:39 -05:00
|
|
|
if (RBIGNUM(x)->sign) rb_big_2comp(z);
|
1998-01-16 07:13:05 -05:00
|
|
|
RBIGNUM(z)->sign = !RBIGNUM(z)->sign;
|
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
bigsub(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE z = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *zds;
|
|
|
|
long num;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
i = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
/* if x is larger than y, swap */
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RBIGNUM(x)->len < RBIGNUM(y)->len) {
|
1998-01-16 07:13:05 -05:00
|
|
|
z = x; x = y; y = z; /* swap x y */
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
else if (RBIGNUM(x)->len == RBIGNUM(y)->len) {
|
1998-01-16 07:13:05 -05:00
|
|
|
while (i > 0) {
|
|
|
|
i--;
|
|
|
|
if (BDIGITS(x)[i] > BDIGITS(y)[i]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (BDIGITS(x)[i] < BDIGITS(y)[i]) {
|
|
|
|
z = x; x = y; y = z; /* swap x y */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
z = bignew(RBIGNUM(x)->len, (z == 0)?1:0);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i = 0, num = 0; i < RBIGNUM(y)->len; i++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
num += (long)BDIGITS(x)[i] - BDIGITS(y)[i];
|
|
|
|
zds[i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
while (num && i < RBIGNUM(x)->len) {
|
1998-01-16 07:13:05 -05:00
|
|
|
num += BDIGITS(x)[i];
|
|
|
|
zds[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
while (i < RBIGNUM(x)->len) {
|
1998-01-16 07:13:05 -05:00
|
|
|
zds[i] = BDIGITS(x)[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
bigadd(x, y, sign)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
char sign;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE z;
|
1998-01-16 07:13:05 -05:00
|
|
|
long num;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, len;
|
1998-01-16 07:19:22 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
sign = (sign == RBIGNUM(y)->sign);
|
|
|
|
if (RBIGNUM(x)->sign != sign) {
|
|
|
|
if (sign) return bigsub(y, x);
|
1998-01-16 07:13:05 -05:00
|
|
|
return bigsub(x, y);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
len = RBIGNUM(x)->len + 1;
|
|
|
|
z = x; x = y; y = z;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
len = RBIGNUM(y)->len + 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
z = bignew(len, sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
len = RBIGNUM(x)->len;
|
|
|
|
for (i = 0, num = 0; i < len; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
num += BDIGITS(x)[i] + BDIGITS(y)[i];
|
1998-01-16 07:13:05 -05:00
|
|
|
BDIGITS(z)[i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
len = RBIGNUM(y)->len;
|
|
|
|
while (num && i < len) {
|
1998-01-16 07:13:05 -05:00
|
|
|
num += BDIGITS(y)[i];
|
|
|
|
BDIGITS(z)[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
while (i < len) {
|
1998-01-16 07:13:05 -05:00
|
|
|
BDIGITS(z)[i] = BDIGITS(y)[i];
|
|
|
|
i++;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
BDIGITS(z)[i] = (USHORT)num;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_plus(x, y)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
/* fall through */
|
|
|
|
case T_BIGNUM:
|
|
|
|
return bigadd(x, y, 1);
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(rb_big2dbl(x) + RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_minus(x, y)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
/* fall through */
|
|
|
|
case T_BIGNUM:
|
|
|
|
return bigadd(x, y, 0);
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(rb_big2dbl(x) - RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_mul(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, j;
|
1998-01-16 07:13:05 -05:00
|
|
|
unsigned long n = 0;
|
|
|
|
VALUE z;
|
|
|
|
USHORT *zds;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (FIXNUM_P(x)) x = rb_int2big(FIX2LONG(x));
|
1998-01-16 07:13:05 -05:00
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(rb_big2dbl(x) * RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
j = RBIGNUM(x)->len + RBIGNUM(y)->len + 1;
|
|
|
|
z = bignew(j, RBIGNUM(x)->sign==RBIGNUM(y)->sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
while (j--) zds[j] = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i = 0; i < RBIGNUM(x)->len; i++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
unsigned long dd = BDIGITS(x)[i];
|
|
|
|
if (dd == 0) continue;
|
|
|
|
n = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
for (j = 0; j < RBIGNUM(y)->len; j++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
int ee = n + dd * BDIGITS(y)[j];
|
|
|
|
n = zds[i + j] + ee;
|
|
|
|
if (ee) zds[i + j] = BIGLO(n);
|
|
|
|
n = BIGDN(n);
|
|
|
|
}
|
|
|
|
if (n) {
|
|
|
|
zds[i + j] = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-01-19 23:59:39 -05:00
|
|
|
bigdivmod(x, y, div, mod, modulo)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE *div, *mod;
|
1999-01-19 23:59:39 -05:00
|
|
|
int modulo;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len;
|
|
|
|
long i, j;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE yy, z;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *xds, *yds, *zds, *tds;
|
1999-08-13 01:45:20 -04:00
|
|
|
unsigned long t2;
|
1998-01-16 07:13:05 -05:00
|
|
|
long num;
|
|
|
|
USHORT dd, q;
|
|
|
|
|
|
|
|
yds = BDIGITS(y);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ny == 0 && yds[0] == 0) rb_num_zerodiv();
|
1998-01-16 07:13:05 -05:00
|
|
|
if (nx < ny || nx == ny && BDIGITS(x)[nx - 1] < BDIGITS(y)[ny - 1]) {
|
|
|
|
if (div) *div = INT2FIX(0);
|
|
|
|
if (mod) *mod = bignorm(x);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
xds = BDIGITS(x);
|
|
|
|
if (ny == 1) {
|
|
|
|
dd = yds[0];
|
1999-01-19 23:59:39 -05:00
|
|
|
z = rb_big_clone(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
t2 = 0; i = nx;
|
|
|
|
while (i--) {
|
|
|
|
t2 = BIGUP(t2) + zds[i];
|
1999-01-19 23:59:39 -05:00
|
|
|
zds[i] = (USHORT)(t2 / dd);
|
1998-01-16 07:13:05 -05:00
|
|
|
t2 %= dd;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
RBIGNUM(z)->sign = RBIGNUM(x)->sign==RBIGNUM(y)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (div) *div = bignorm(z);
|
|
|
|
if (mod) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!RBIGNUM(y)->sign) t2 = -(long)t2;
|
1999-08-13 01:45:20 -04:00
|
|
|
*mod = INT2NUM(t2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
z = bignew(nx==ny?nx+2:nx+1, RBIGNUM(x)->sign==RBIGNUM(y)->sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
if (nx==ny) zds[nx+1] = 0;
|
|
|
|
while (!yds[ny-1]) ny--;
|
|
|
|
if ((dd = BIGRAD/(int)(yds[ny-1]+1)) != 1) {
|
1999-01-19 23:59:39 -05:00
|
|
|
yy = rb_big_clone(y);
|
|
|
|
tds = BDIGITS(yy);
|
1998-01-16 07:13:05 -05:00
|
|
|
j = 0;
|
|
|
|
num = 0;
|
|
|
|
while (j<ny) {
|
1999-01-19 23:59:39 -05:00
|
|
|
num += (long)yds[j]*dd;
|
1998-01-16 07:13:05 -05:00
|
|
|
tds[j++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
|
|
|
yds = tds;
|
|
|
|
j = 0;
|
|
|
|
num = 0;
|
|
|
|
while (j<nx) {
|
1999-01-19 23:59:39 -05:00
|
|
|
num += (long)xds[j]*dd;
|
1998-01-16 07:13:05 -05:00
|
|
|
zds[j++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
zds[j] = (USHORT)num;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
zds[nx] = 0;
|
|
|
|
j = nx;
|
|
|
|
while (j--) zds[j] = xds[j];
|
|
|
|
}
|
|
|
|
j = nx==ny?nx+1:nx;
|
|
|
|
do {
|
|
|
|
if (zds[j] == yds[ny-1]) q = BIGRAD-1;
|
1999-01-19 23:59:39 -05:00
|
|
|
else q = (USHORT)((BIGUP(zds[j]) + zds[j-1])/yds[ny-1]);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (q) {
|
|
|
|
i = 0; num = 0; t2 = 0;
|
|
|
|
do { /* multiply and subtract */
|
|
|
|
int ee;
|
1999-01-19 23:59:39 -05:00
|
|
|
t2 += (long)yds[i] * q;
|
1998-01-16 07:13:05 -05:00
|
|
|
ee = num - BIGLO(t2);
|
|
|
|
num = zds[j - ny + i] + ee;
|
|
|
|
if (ee) zds[j - ny + i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
t2 = BIGDN(t2);
|
|
|
|
} while (++i < ny);
|
|
|
|
num += zds[j - ny + i] - t2; /* borrow from high digit; don't update */
|
|
|
|
while (num) { /* "add back" required */
|
|
|
|
i = 0; num = 0; q--;
|
|
|
|
do {
|
|
|
|
int ee = num + yds[i];
|
|
|
|
num = (long) zds[j - ny + i] + ee;
|
|
|
|
if (ee) zds[j - ny + i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
} while (++i < ny);
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zds[j] = q;
|
|
|
|
} while (--j >= ny);
|
|
|
|
if (div) { /* move quotient down in z */
|
1999-01-19 23:59:39 -05:00
|
|
|
*div = rb_big_clone(z);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(*div);
|
|
|
|
j = (nx==ny ? nx+2 : nx+1) - ny;
|
|
|
|
for (i = 0;i < j;i++) zds[i] = zds[i+ny];
|
|
|
|
RBIGNUM(*div)->len = i;
|
|
|
|
*div = bignorm(*div);
|
|
|
|
}
|
|
|
|
if (mod) { /* just normalize remainder */
|
1999-01-19 23:59:39 -05:00
|
|
|
*mod = rb_big_clone(z);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (dd) {
|
|
|
|
zds = BDIGITS(*mod);
|
|
|
|
t2 = 0; i = ny;
|
|
|
|
while(i--) {
|
|
|
|
t2 = BIGUP(t2) + zds[i];
|
1999-01-19 23:59:39 -05:00
|
|
|
zds[i] = (USHORT)(t2 / dd);
|
1998-01-16 07:13:05 -05:00
|
|
|
t2 %= dd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RBIGNUM(*mod)->len = ny;
|
1999-01-19 23:59:39 -05:00
|
|
|
RBIGNUM(*mod)->sign = RBIGNUM(x)->sign;
|
|
|
|
if (modulo && RBIGNUM(x)->sign != RBIGNUM(y)->sign) {
|
1999-08-13 01:45:20 -04:00
|
|
|
long len = ny;
|
1999-01-19 23:59:39 -05:00
|
|
|
zds = BDIGITS(*mod);
|
|
|
|
while (len-- && !zds[len]);
|
|
|
|
if (len > 0) {
|
|
|
|
*mod = bigadd(*mod, y, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
*mod = bignorm(*mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_div(x, y)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(rb_big2dbl(x) / RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
bigdivmod(x, y, &z, 0, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_modulo(x, y, modulo)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
1999-01-19 23:59:39 -05:00
|
|
|
int modulo;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_dbl2big(RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
bigdivmod(x, y, 0, &z, modulo);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
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)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE div, mod;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_dbl2big(RFLOAT(y)->value);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
bigdivmod(x, y, &div, &mod, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_assoc_new(div, mod);;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_pow(x, y)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
double d;
|
1999-01-19 23:59:39 -05:00
|
|
|
long yy;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (y == INT2FIX(0)) return INT2FIX(1);
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FLOAT:
|
|
|
|
d = RFLOAT(y)->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_warn("in a**b, b may be too big");
|
|
|
|
d = rb_big2dbl(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FIXNUM:
|
1999-01-19 23:59:39 -05:00
|
|
|
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);
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (!FIXNUM_P(z)) z = bignorm(z);
|
1999-01-19 23:59:39 -05:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
d = (double)yy;
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_float_new(pow(rb_big2dbl(x), d));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_and(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
USHORT *ds1, *ds2, *zds;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, l1, l2;
|
1998-01-16 07:13:05 -05:00
|
|
|
char sign;
|
|
|
|
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(y, T_BIGNUM);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(y)->sign) {
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_big_clone(y);
|
|
|
|
rb_big_2comp(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-19 23:59:39 -05:00
|
|
|
x = rb_big_clone(x);
|
|
|
|
rb_big_2comp(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
l1 = RBIGNUM(y)->len;
|
|
|
|
l2 = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
ds1 = BDIGITS(y);
|
|
|
|
ds2 = BDIGITS(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
sign = RBIGNUM(y)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
l1 = RBIGNUM(x)->len;
|
|
|
|
l2 = RBIGNUM(y)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
ds1 = BDIGITS(x);
|
|
|
|
ds2 = BDIGITS(y);
|
1998-01-16 07:19:22 -05:00
|
|
|
sign = RBIGNUM(x)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
z = bignew(l2, RBIGNUM(x)->sign || RBIGNUM(y)->sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
|
|
|
|
for (i=0; i<l1; i++) {
|
|
|
|
zds[i] = ds1[i] & ds2[i];
|
|
|
|
}
|
|
|
|
for (; i<l2; i++) {
|
|
|
|
zds[i] = sign?0:ds2[i];
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!RBIGNUM(z)->sign) rb_big_2comp(z);
|
1998-01-16 07:13:05 -05:00
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_or(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
USHORT *ds1, *ds2, *zds;
|
1999-08-13 01:45:20 -04:00
|
|
|
unsigned long i, l1, l2;
|
1998-01-16 07:13:05 -05:00
|
|
|
char sign;
|
|
|
|
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(y, T_BIGNUM);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(y)->sign) {
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_big_clone(y);
|
|
|
|
rb_big_2comp(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-19 23:59:39 -05:00
|
|
|
x = rb_big_clone(x);
|
|
|
|
rb_big_2comp(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
l1 = RBIGNUM(y)->len;
|
|
|
|
l2 = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
ds1 = BDIGITS(y);
|
|
|
|
ds2 = BDIGITS(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
sign = RBIGNUM(y)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
l1 = RBIGNUM(x)->len;
|
|
|
|
l2 = RBIGNUM(y)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
ds1 = BDIGITS(x);
|
|
|
|
ds2 = BDIGITS(y);
|
1998-01-16 07:19:22 -05:00
|
|
|
sign = RBIGNUM(x)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
z = bignew(l2, RBIGNUM(x)->sign && RBIGNUM(y)->sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
|
|
|
|
for (i=0; i<l1; i++) {
|
|
|
|
zds[i] = ds1[i] | ds2[i];
|
|
|
|
}
|
|
|
|
for (; i<l2; i++) {
|
|
|
|
zds[i] = sign?ds2[i]:(BIGRAD-1);
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!RBIGNUM(z)->sign) rb_big_2comp(z);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_xor(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
USHORT *ds1, *ds2, *zds;
|
1999-01-19 23:59:39 -05:00
|
|
|
unsigned int i, l1, l2;
|
1998-01-16 07:13:05 -05:00
|
|
|
char sign;
|
|
|
|
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(y, T_BIGNUM);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(y)->sign) {
|
1999-01-19 23:59:39 -05:00
|
|
|
y = rb_big_clone(y);
|
|
|
|
rb_big_2comp(y);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-19 23:59:39 -05:00
|
|
|
x = rb_big_clone(x);
|
|
|
|
rb_big_2comp(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
l1 = RBIGNUM(y)->len;
|
|
|
|
l2 = RBIGNUM(x)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
ds1 = BDIGITS(y);
|
|
|
|
ds2 = BDIGITS(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
sign = RBIGNUM(y)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
l1 = RBIGNUM(x)->len;
|
|
|
|
l2 = RBIGNUM(y)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
ds1 = BDIGITS(x);
|
|
|
|
ds2 = BDIGITS(y);
|
1998-01-16 07:19:22 -05:00
|
|
|
sign = RBIGNUM(x)->sign;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
RBIGNUM(x)->sign = RBIGNUM(x)->sign?1:0;
|
|
|
|
RBIGNUM(y)->sign = RBIGNUM(y)->sign?1:0;
|
|
|
|
z = bignew(l2, !(RBIGNUM(x)->sign ^ RBIGNUM(y)->sign));
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
|
|
|
|
for (i=0; i<l1; i++) {
|
|
|
|
zds[i] = ds1[i] ^ ds2[i];
|
|
|
|
}
|
|
|
|
for (; i<l2; i++) {
|
|
|
|
zds[i] = sign?ds2[i]:~ds2[i];
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!RBIGNUM(z)->sign) rb_big_2comp(z);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE rb_big_rshift _((VALUE,VALUE));
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_lshift(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
USHORT *xds, *zds;
|
|
|
|
int shift = NUM2INT(y);
|
1999-08-13 01:45:20 -04:00
|
|
|
int s1 = shift/BITSPERDIG;
|
|
|
|
int s2 = shift%BITSPERDIG;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE z;
|
|
|
|
unsigned long num = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
long len, i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (shift < 0) return rb_big_rshift(x, INT2FIX(-shift));
|
1998-01-16 07:13:05 -05:00
|
|
|
xds = BDIGITS(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
len = RBIGNUM(x)->len;
|
|
|
|
z = bignew(len+s1+1, RBIGNUM(x)->sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
for (i=0; i<s1; i++) {
|
|
|
|
*zds++ = 0;
|
|
|
|
}
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
num = num | *xds++<<s2;
|
|
|
|
*zds++ = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
|
|
|
*zds = BIGLO(num);
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_rshift(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
USHORT *xds, *zds;
|
|
|
|
int shift = NUM2INT(y);
|
1999-08-13 01:45:20 -04:00
|
|
|
int s1 = shift/BITSPERDIG;
|
|
|
|
int s2 = shift%BITSPERDIG;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE z;
|
|
|
|
unsigned long num = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
long i = RBIGNUM(x)->len;
|
|
|
|
long j;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (shift < 0) return rb_big_lshift(x, INT2FIX(-shift));
|
1998-01-16 07:19:22 -05:00
|
|
|
if (s1 > RBIGNUM(x)->len) {
|
|
|
|
if (RBIGNUM(x)->sign)
|
1998-01-16 07:13:05 -05:00
|
|
|
return INT2FIX(0);
|
|
|
|
else
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
|
|
|
xds = BDIGITS(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
i = RBIGNUM(x)->len; j = i - s1;
|
|
|
|
z = bignew(j, RBIGNUM(x)->sign);
|
1998-01-16 07:13:05 -05:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
while (i--, j--) {
|
|
|
|
num = (num | xds[i]) >> s2;
|
|
|
|
zds[j] = BIGLO(num);
|
|
|
|
num = BIGUP(xds[i]);
|
|
|
|
}
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_aref(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
USHORT *xds;
|
|
|
|
int shift = NUM2INT(y);
|
1999-08-13 01:45:20 -04:00
|
|
|
int s1, s2;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (shift < 0) return INT2FIX(0);
|
1998-01-16 07:19:22 -05:00
|
|
|
s1 = shift/BITSPERDIG;
|
|
|
|
s2 = shift%BITSPERDIG;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
|
|
|
if (s1 >= RBIGNUM(x)->len) return INT2FIX(1);
|
1999-01-19 23:59:39 -05:00
|
|
|
x = rb_big_clone(x);
|
|
|
|
rb_big_2comp(x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
if (s1 >= RBIGNUM(x)->len) return INT2FIX(0);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
xds = BDIGITS(x);
|
|
|
|
if (xds[s1] & (1<<s2))
|
|
|
|
return INT2FIX(1);
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_hash(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
long i, len;
|
1999-01-19 23:59:39 -05:00
|
|
|
int key;
|
1998-01-16 07:13:05 -05:00
|
|
|
USHORT *digits;
|
|
|
|
|
|
|
|
key = 0; digits = BDIGITS(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
for (i=0,len=RBIGNUM(x)->len; i<RBIGNUM(x)->len; i++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
key ^= *digits++;
|
|
|
|
}
|
|
|
|
return INT2FIX(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_coerce(x, y)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x, y;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_assoc_new(rb_int2big(FIX2LONG(y)), x);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eTypeError, "Can't coerce %s to Bignum",
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_class2name(CLASS_OF(y)));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
/* not reached */
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_abs(x)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE x;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-19 23:59:39 -05:00
|
|
|
x = rb_big_clone(x);
|
1998-01-16 07:19:22 -05:00
|
|
|
RBIGNUM(x)->sign = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return x;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* !!!warnig!!!!
|
|
|
|
this is not really a random number!!
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
1999-12-06 04:04:03 -05:00
|
|
|
rb_big_rand(max, rand)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE max;
|
1999-12-06 04:04:03 -05:00
|
|
|
double rand;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-12-06 04:04:03 -05:00
|
|
|
VALUE v;
|
1999-08-13 01:45:20 -04:00
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
len = RBIGNUM(max)->len;
|
1999-12-06 04:04:03 -05:00
|
|
|
v = bignew(len,1);
|
1998-01-16 07:13:05 -05:00
|
|
|
while (len--) {
|
1999-12-06 04:04:03 -05:00
|
|
|
BDIGITS(v)[len] = ((USHORT)~0) * rand;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_big_mod((VALUE)v, max);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_big_size(big)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE big;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
return INT2FIX(RBIGNUM(big)->len*sizeof(USHORT));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_big_zero_p(big)
|
|
|
|
VALUE big;
|
|
|
|
{
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
|
|
|
Init_Bignum()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
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);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|