2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
bignum.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Jun 10 00:48:55 JST 1994
|
|
|
|
|
2002-01-11 12:18:54 +03:00
|
|
|
Copyright (C) 1993-2002 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
|
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-05-14 10:22:31 +04:00
|
|
|
#include "ruby.h"
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
#include <math.h>
|
1999-01-20 07:59:39 +03:00
|
|
|
#include <ctype.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cBignum;
|
2000-06-04 19:32:19 +04:00
|
|
|
|
|
|
|
#if defined __MINGW32__
|
|
|
|
#define USHORT _USHORT
|
|
|
|
#endif
|
|
|
|
|
2000-10-31 11:37:47 +03:00
|
|
|
#define BDIGITS(x) ((BDIGIT*)RBIGNUM(x)->digits)
|
2002-04-10 12:45:26 +04:00
|
|
|
#define BITSPERDIG (SIZEOF_BDIGITS*CHAR_BIT)
|
2000-11-01 11:49:40 +03:00
|
|
|
#define BIGRAD ((BDIGIT_DBL)1 << BITSPERDIG)
|
2002-04-10 12:45:26 +04:00
|
|
|
#define DIGSPERLONG ((unsigned int)(SIZEOF_LONG/SIZEOF_BDIGITS))
|
2002-03-14 09:23:46 +03:00
|
|
|
#if HAVE_LONG_LONG
|
2002-04-10 12:45:26 +04:00
|
|
|
# define DIGSPERLL ((unsigned int)(SIZEOF_LONG_LONG/SIZEOF_BDIGITS))
|
2002-03-14 09:23:46 +03:00
|
|
|
#endif
|
2000-11-01 11:49:40 +03:00
|
|
|
#define BIGUP(x) ((BDIGIT_DBL)(x) << BITSPERDIG)
|
1999-12-01 12:24:48 +03:00
|
|
|
#define BIGDN(x) RSHIFT(x,BITSPERDIG)
|
2000-10-31 11:37:47 +03:00
|
|
|
#define BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1)))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
bignew_1(klass, len, sign)
|
|
|
|
VALUE klass;
|
1999-08-13 09:45:20 +04:00
|
|
|
long len;
|
1998-01-16 15:13:05 +03:00
|
|
|
char sign;
|
|
|
|
{
|
|
|
|
NEWOBJ(big, struct RBignum);
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJSETUP(big, klass, T_BIGNUM);
|
1998-01-16 15:13:05 +03:00
|
|
|
big->sign = sign;
|
|
|
|
big->len = len;
|
2000-10-31 11:37:47 +03:00
|
|
|
big->digits = ALLOC_N(BDIGIT, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return (VALUE)big;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define bignew(len,sign) bignew_1(rb_cBignum,len,sign)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_clone(x)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE z = bignew_1(CLASS_OF(x), RBIGNUM(x)->len, RBIGNUM(x)->sign);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-31 11:37:47 +03:00
|
|
|
MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, RBIGNUM(x)->len);
|
1998-01-16 15:19:22 +03:00
|
|
|
return z;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-02-22 13:28:47 +03:00
|
|
|
static void
|
|
|
|
get2comp(x, carry) /* get 2's complement */
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
2002-02-22 13:28:47 +03:00
|
|
|
int carry;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i = RBIGNUM(x)->len;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds = BDIGITS(x);
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (i--) ds[i] = ~ds[i];
|
|
|
|
i = 0; num = 1;
|
|
|
|
do {
|
1999-01-20 07:59:39 +03:00
|
|
|
num += ds[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
ds[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
1998-01-16 15:19:22 +03:00
|
|
|
} while (i < RBIGNUM(x)->len);
|
2002-02-22 13:28:47 +03:00
|
|
|
if (!carry) return;
|
2002-04-10 12:45:26 +04:00
|
|
|
if ((ds[RBIGNUM(x)->len-1] & (1<<(BITSPERDIG-1))) == 0) {
|
2000-10-31 11:37:47 +03:00
|
|
|
REALLOC_N(RBIGNUM(x)->digits, BDIGIT, RBIGNUM(x)->len++);
|
1998-01-16 15:13:05 +03:00
|
|
|
ds = BDIGITS(x);
|
2002-04-10 12:45:26 +04:00
|
|
|
ds[RBIGNUM(x)->len-1] = ~0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-22 13:28:47 +03:00
|
|
|
void
|
|
|
|
rb_big_2comp(x) /* get 2's complement */
|
|
|
|
VALUE x;
|
|
|
|
{
|
2002-02-26 15:36:26 +03:00
|
|
|
get2comp(x, Qtrue);
|
2002-02-22 13:28:47 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
bignorm(x)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-07-12 10:06:50 +04:00
|
|
|
if (!FIXNUM_P(x)) {
|
|
|
|
long len = RBIGNUM(x)->len;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds = BDIGITS(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-12 10:06:50 +04:00
|
|
|
while (len-- && !ds[len]) ;
|
|
|
|
RBIGNUM(x)->len = ++len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-04-10 12:45:26 +04:00
|
|
|
if (len*SIZEOF_BDIGITS <= sizeof(VALUE)) {
|
2000-07-12 10:06:50 +04:00
|
|
|
long num = 0;
|
|
|
|
while (len--) {
|
|
|
|
num = BIGUP(num) + ds[len];
|
|
|
|
}
|
|
|
|
if (num >= 0) {
|
|
|
|
if (RBIGNUM(x)->sign) {
|
|
|
|
if (POSFIXABLE(num)) return INT2FIX(num);
|
|
|
|
}
|
|
|
|
else if (NEGFIXABLE(-(long)num)) return INT2FIX(-(long)num);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return x;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_norm(x)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return bignorm(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_uint2big(n)
|
|
|
|
unsigned long n;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num = n;
|
2000-10-31 11:37:47 +03:00
|
|
|
long i = 0;
|
|
|
|
BDIGIT *digits;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE big;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
i = 0;
|
2000-01-05 07:41:21 +03:00
|
|
|
big = bignew(DIGSPERLONG, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
digits = BDIGITS(big);
|
2000-01-05 07:41:21 +03:00
|
|
|
while (i < DIGSPERLONG) {
|
2000-10-31 11:37:47 +03:00
|
|
|
digits[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
i = DIGSPERLONG;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (i-- && !digits[i]) ;
|
1998-01-16 15:19:22 +03:00
|
|
|
RBIGNUM(big)->len = i+1;
|
|
|
|
return big;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_int2big(n)
|
|
|
|
long n;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
long neg = 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE big;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
n = -n;
|
|
|
|
neg = 1;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
big = rb_uint2big(n);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (neg) {
|
1998-01-16 15:19:22 +03:00
|
|
|
RBIGNUM(big)->sign = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return big;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_uint2inum(n)
|
|
|
|
unsigned long n;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (POSFIXABLE(n)) return INT2FIX(n);
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_uint2big(n);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_int2inum(n)
|
|
|
|
long n;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (FIXABLE(n)) return INT2FIX(n);
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_int2big(n);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
#ifdef HAVE_LONG_LONG
|
|
|
|
|
2002-04-10 12:45:26 +04:00
|
|
|
#define DIGSPERLONGLONG ((unsigned int)(SIZEOF_LONG_LONG/SIZEOF_BDIGITS))
|
2002-02-13 12:53:17 +03:00
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
void
|
|
|
|
rb_quad_pack(buf, val)
|
|
|
|
char *buf;
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
LONG_LONG q;
|
|
|
|
|
|
|
|
val = rb_to_int(val);
|
|
|
|
if (FIXNUM_P(val)) {
|
|
|
|
q = FIX2LONG(val);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
long len = RBIGNUM(val)->len;
|
|
|
|
BDIGIT *ds;
|
|
|
|
|
|
|
|
ds = BDIGITS(val);
|
|
|
|
q = 0;
|
|
|
|
while (len--) {
|
|
|
|
q = BIGUP(q);
|
|
|
|
q += ds[len];
|
|
|
|
}
|
|
|
|
}
|
2002-04-10 12:45:26 +04:00
|
|
|
memcpy(buf, (char*)&q, SIZEOF_LONG_LONG);
|
2002-02-13 12:01:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_quad_unpack(buf, sign)
|
|
|
|
const char *buf;
|
|
|
|
int sign;
|
|
|
|
{
|
|
|
|
unsigned LONG_LONG q;
|
|
|
|
long neg = 0;
|
|
|
|
long i = 0;
|
|
|
|
BDIGIT *digits;
|
|
|
|
VALUE big;
|
|
|
|
|
2002-04-10 12:45:26 +04:00
|
|
|
memcpy(&q, buf, SIZEOF_LONG_LONG);
|
2002-02-13 12:01:11 +03:00
|
|
|
if (sign) {
|
|
|
|
if (FIXABLE((LONG_LONG)q)) return INT2FIX((LONG_LONG)q);
|
|
|
|
if ((LONG_LONG)q < 0) {
|
|
|
|
q = -(LONG_LONG)q;
|
|
|
|
neg = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (POSFIXABLE(q)) return INT2FIX(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
big = bignew(DIGSPERLONGLONG, 1);
|
|
|
|
digits = BDIGITS(big);
|
|
|
|
while (i < DIGSPERLONGLONG) {
|
|
|
|
digits[i++] = BIGLO(q);
|
|
|
|
q = BIGDN(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = DIGSPERLONGLONG;
|
|
|
|
while (i-- && !digits[i]) ;
|
|
|
|
RBIGNUM(big)->len = i+1;
|
|
|
|
|
|
|
|
if (neg) {
|
|
|
|
RBIGNUM(big)->sign = 0;
|
|
|
|
}
|
|
|
|
return bignorm(big);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define QUAD_SIZE 8
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_quad_pack(buf, val)
|
|
|
|
char *buf;
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
long len;
|
|
|
|
|
|
|
|
memset(buf, 0, QUAD_SIZE);
|
|
|
|
val = rb_to_int(val);
|
|
|
|
if (FIXNUM_P(val)) {
|
2002-02-13 12:53:17 +03:00
|
|
|
val = rb_int2big(FIX2LONG(val));
|
2002-02-13 12:01:11 +03:00
|
|
|
}
|
2002-04-10 12:45:26 +04:00
|
|
|
len = RBIGNUM(val)->len * SIZEOF_BDIGITS;
|
2002-02-13 12:01:11 +03:00
|
|
|
if (len > QUAD_SIZE) len = QUAD_SIZE;
|
|
|
|
memcpy(buf, (char*)BDIGITS(val), len);
|
2002-02-13 12:53:17 +03:00
|
|
|
if (!RBIGNUM(val)->sign) {
|
|
|
|
len = QUAD_SIZE;
|
|
|
|
while (len--) {
|
2002-03-26 06:01:30 +03:00
|
|
|
*buf = ~*buf;
|
2002-03-22 10:26:42 +03:00
|
|
|
buf++;
|
2002-02-13 12:53:17 +03:00
|
|
|
}
|
|
|
|
}
|
2002-02-13 12:01:11 +03:00
|
|
|
}
|
|
|
|
|
2002-04-10 12:45:26 +04:00
|
|
|
#define BNEG(b) (RSHIFT(((BDIGIT*)b)[QUAD_SIZE/SIZEOF_BDIGITS-1],BITSPERDIG-1) != 0)
|
2002-02-13 12:53:17 +03:00
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
VALUE
|
|
|
|
rb_quad_unpack(buf, sign)
|
|
|
|
const char *buf;
|
|
|
|
int sign;
|
|
|
|
{
|
2002-04-10 12:45:26 +04:00
|
|
|
VALUE big = bignew(QUAD_SIZE/SIZEOF_BDIGITS, 1);
|
2002-02-13 12:01:11 +03:00
|
|
|
|
|
|
|
memcpy((char*)BDIGITS(big), buf, QUAD_SIZE);
|
2002-02-13 12:53:17 +03:00
|
|
|
if (sign && BNEG(buf)) {
|
|
|
|
long len = QUAD_SIZE;
|
|
|
|
char *tmp = (char*)BDIGITS(big);
|
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
RBIGNUM(big)->sign = 0;
|
2002-02-13 12:53:17 +03:00
|
|
|
while (len--) {
|
2002-03-22 10:26:42 +03:00
|
|
|
*tmp = ~*tmp;
|
|
|
|
tmp++;
|
2002-02-13 12:53:17 +03:00
|
|
|
}
|
2002-02-13 12:01:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return bignorm(big);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
2002-02-01 13:23:22 +03:00
|
|
|
rb_cstr_to_inum(str, base, badcheck)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *str;
|
1998-01-16 15:13:05 +03:00
|
|
|
int base;
|
2002-02-01 13:23:22 +03:00
|
|
|
int badcheck;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-01-05 07:41:21 +03:00
|
|
|
const char *s = str;
|
|
|
|
char *end;
|
2002-02-01 13:23:22 +03:00
|
|
|
char sign = 1, c, nondigit = 0;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num;
|
1999-08-13 09:45:20 +04:00
|
|
|
long len, blen = 1;
|
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE z;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *zds;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-02-01 13:23:22 +03:00
|
|
|
if (badcheck) {
|
|
|
|
while (ISSPACE(*str)) str++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (ISSPACE(*str) || *str == '_') str++;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2000-12-22 12:00:23 +03:00
|
|
|
if (str[0] == '+') {
|
1999-01-20 07:59:39 +03:00
|
|
|
str++;
|
|
|
|
}
|
2000-12-22 12:00:23 +03:00
|
|
|
else if (str[0] == '-') {
|
1998-01-16 15:13:05 +03:00
|
|
|
str++;
|
|
|
|
sign = 0;
|
|
|
|
}
|
2001-03-14 07:45:46 +03:00
|
|
|
if (str[0] == '+' || str[0] == '-') {
|
|
|
|
if (badcheck) goto bad;
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
2002-02-01 13:23:22 +03:00
|
|
|
if (base <= 0) {
|
2000-12-22 12:00:23 +03:00
|
|
|
if (str[0] == '0') {
|
|
|
|
if (str[1] == 'x' || str[1] == 'X') {
|
1998-01-16 15:13:05 +03:00
|
|
|
base = 16;
|
|
|
|
}
|
2000-12-22 12:00:23 +03:00
|
|
|
else if (str[1] == 'b' || str[1] == 'B') {
|
1999-08-13 09:45:20 +04:00
|
|
|
base = 2;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
|
|
|
base = 8;
|
|
|
|
}
|
|
|
|
}
|
2002-02-01 13:23:22 +03:00
|
|
|
else if (base < -1) {
|
|
|
|
base = -base;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (base == 8) {
|
2001-12-26 20:01:20 +03:00
|
|
|
len = 3;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
else { /* base == 10, 2 or 16 */
|
1999-01-20 07:59:39 +03:00
|
|
|
if (base == 16 && str[0] == '0' && (str[1] == 'x'||str[1] == 'X')) {
|
|
|
|
str += 2;
|
|
|
|
}
|
2001-07-31 10:24:45 +04:00
|
|
|
else if (base == 2 && str[0] == '0' && (str[1] == 'b'||str[1] == 'B')) {
|
1999-08-13 09:45:20 +04:00
|
|
|
str += 2;
|
|
|
|
}
|
2001-12-26 20:01:20 +03:00
|
|
|
len = 4;
|
|
|
|
}
|
2001-12-27 12:53:54 +03:00
|
|
|
if (*str == '0') { /* squeeze preceeding 0s */
|
|
|
|
while (*++str == '0');
|
|
|
|
--str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-12-26 20:01:20 +03:00
|
|
|
len *= strlen(str)*sizeof(char);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (len <= (sizeof(VALUE)*CHAR_BIT)) {
|
2000-01-05 07:41:21 +03:00
|
|
|
unsigned long val = strtoul((char*)str, &end, base);
|
|
|
|
|
2000-12-22 06:22:25 +03:00
|
|
|
if (*end == '_') goto bigparse;
|
2000-01-05 07:41:21 +03:00
|
|
|
if (badcheck) {
|
2002-01-04 17:15:33 +03:00
|
|
|
if (end == str) goto bad; /* no number */
|
2002-01-16 05:20:25 +03:00
|
|
|
while (*end && ISSPACE(*end)) end++;
|
2000-10-31 11:37:47 +03:00
|
|
|
if (*end) { /* trailing garbage */
|
2000-01-05 07:41:21 +03:00
|
|
|
bad:
|
2002-01-04 17:15:33 +03:00
|
|
|
rb_invalid_str(s, "Integer");
|
2000-01-05 07:41:21 +03:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (POSFIXABLE(val)) {
|
|
|
|
if (sign) return INT2FIX(val);
|
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
long result = -(long)val;
|
1998-01-16 15:13:05 +03:00
|
|
|
return INT2FIX(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE big = rb_uint2big(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
RBIGNUM(big)->sign = sign;
|
|
|
|
return big;
|
|
|
|
}
|
|
|
|
}
|
2000-12-22 06:22:25 +03:00
|
|
|
bigparse:
|
1998-01-16 15:19:22 +03:00
|
|
|
len = (len/BITSPERDIG)+1;
|
2000-12-22 06:22:25 +03:00
|
|
|
if (badcheck && *str == '_') goto bad;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
z = bignew(len, sign);
|
|
|
|
zds = BDIGITS(z);
|
|
|
|
for (i=len;i--;) zds[i]=0;
|
|
|
|
while (c = *str++) {
|
|
|
|
switch (c) {
|
2000-12-25 09:29:27 +03:00
|
|
|
case '8': case '9':
|
|
|
|
if (base == 8) {
|
|
|
|
c = base;
|
|
|
|
break;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
2000-12-25 09:29:27 +03:00
|
|
|
case '5': case '6': case '7':
|
1998-01-16 15:13:05 +03:00
|
|
|
c = c - '0';
|
2002-02-01 13:23:22 +03:00
|
|
|
nondigit = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case 'a': case 'b': case 'c':
|
|
|
|
case 'd': case 'e': case 'f':
|
2002-02-01 13:23:22 +03:00
|
|
|
c -= 'a' - 'A';
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'A': case 'B': case 'C':
|
|
|
|
case 'D': case 'E': case 'F':
|
2002-02-01 13:23:22 +03:00
|
|
|
if (base != 16) {
|
|
|
|
nondigit = c;
|
|
|
|
c = base;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = c - 'A' + 10;
|
|
|
|
nondigit = 0;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
2000-12-22 06:22:25 +03:00
|
|
|
case '_':
|
2002-02-01 13:23:22 +03:00
|
|
|
if (badcheck) {
|
|
|
|
if (nondigit) goto bad;
|
|
|
|
nondigit = c;
|
|
|
|
}
|
2000-12-22 06:22:25 +03:00
|
|
|
continue;
|
1998-01-16 15:13:05 +03:00
|
|
|
default:
|
2000-10-31 11:37:47 +03:00
|
|
|
c = base;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c >= base) break;
|
|
|
|
i = 0;
|
|
|
|
num = c;
|
|
|
|
for (;;) {
|
|
|
|
while (i<blen) {
|
2000-11-01 11:49:40 +03:00
|
|
|
num += (BDIGIT_DBL)zds[i]*base;
|
1998-01-16 15:13:05 +03:00
|
|
|
zds[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
|
|
|
if (num) {
|
|
|
|
blen++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-12-25 09:29:27 +03:00
|
|
|
if (badcheck) {
|
|
|
|
str--;
|
|
|
|
if (s+1 < str && str[-1] == '_') goto bad;
|
2001-12-26 20:01:20 +03:00
|
|
|
while (*str && ISSPACE(*str)) str++;
|
2000-12-25 09:29:27 +03:00
|
|
|
if (*str) goto bad;
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
VALUE
|
2002-02-01 13:23:22 +03:00
|
|
|
rb_str_to_inum(str, base, badcheck)
|
2000-01-05 07:41:21 +03:00
|
|
|
VALUE str;
|
|
|
|
int base;
|
2002-02-01 13:23:22 +03:00
|
|
|
int badcheck;
|
2000-01-05 07:41:21 +03:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int len;
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(str);
|
|
|
|
s = RSTRING(str)->ptr;
|
|
|
|
len = RSTRING(str)->len;
|
2000-10-31 11:37:47 +03:00
|
|
|
if (s[len]) { /* no sentinel somehow */
|
|
|
|
char *p = ALLOCA_N(char, len+1);
|
|
|
|
|
|
|
|
MEMCPY(p, s, char, len);
|
|
|
|
p[len] = '\0';
|
|
|
|
s = p;
|
|
|
|
}
|
2002-02-01 13:23:22 +03:00
|
|
|
if (badcheck && len != strlen(s)) {
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_raise(rb_eArgError, "string for Integer contains null byte");
|
|
|
|
}
|
2002-02-01 13:23:22 +03:00
|
|
|
return rb_cstr_to_inum(s, base, badcheck);
|
|
|
|
}
|
|
|
|
|
2002-03-14 09:23:46 +03:00
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ull2big(n)
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
unsigned LONG_LONG n;
|
2002-03-14 09:23:46 +03:00
|
|
|
{
|
|
|
|
BDIGIT_DBL num = n;
|
|
|
|
long i = 0;
|
|
|
|
BDIGIT *digits;
|
|
|
|
VALUE big;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
big = bignew(DIGSPERLL, 1);
|
|
|
|
digits = BDIGITS(big);
|
|
|
|
while (i < DIGSPERLL) {
|
|
|
|
digits[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = DIGSPERLL;
|
|
|
|
while (i-- && !digits[i]) ;
|
|
|
|
RBIGNUM(big)->len = i+1;
|
|
|
|
return big;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ll2big(n)
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
LONG_LONG n;
|
2002-03-14 09:23:46 +03:00
|
|
|
{
|
|
|
|
long neg = 0;
|
|
|
|
VALUE big;
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
n = -n;
|
|
|
|
neg = 1;
|
|
|
|
}
|
|
|
|
big = rb_ull2big(n);
|
|
|
|
if (neg) {
|
|
|
|
RBIGNUM(big)->sign = 0;
|
|
|
|
}
|
|
|
|
return big;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ull2inum(n)
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
unsigned LONG_LONG n;
|
2002-03-14 09:23:46 +03:00
|
|
|
{
|
|
|
|
if (POSFIXABLE(n)) return INT2FIX(n);
|
|
|
|
return rb_ull2big(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_ll2inum(n)
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
LONG_LONG n;
|
2002-03-14 09:23:46 +03:00
|
|
|
{
|
|
|
|
if (FIXABLE(n)) return INT2FIX(n);
|
|
|
|
return rb_ll2big(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LONG_LONG */
|
|
|
|
|
2002-02-01 13:23:22 +03:00
|
|
|
VALUE
|
|
|
|
rb_cstr2inum(str, base)
|
|
|
|
const char *str;
|
|
|
|
int base;
|
|
|
|
{
|
|
|
|
return rb_cstr_to_inum(str, base, base==0);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_str2inum(str, base)
|
|
|
|
VALUE str;
|
|
|
|
int base;
|
|
|
|
{
|
|
|
|
return rb_str_to_inum(str, base, base==0);
|
2000-01-05 07:41:21 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static char hexmap[] = "0123456789abcdef";
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big2str(x, base)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
int base;
|
|
|
|
{
|
2001-05-21 08:22:54 +04:00
|
|
|
volatile VALUE t;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds;
|
|
|
|
long i, j, hbase;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ss;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, c;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (FIXNUM_P(x)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_fix2str(x, base);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
i = RBIGNUM(x)->len;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (i == 0) return rb_str_new2("0");
|
1998-01-16 15:13:05 +03:00
|
|
|
if (base == 10) {
|
2002-04-10 12:45:26 +04:00
|
|
|
j = (SIZEOF_BDIGITS/sizeof(char)*CHAR_BIT*i*241L)/800+2;
|
1998-01-16 15:13:05 +03:00
|
|
|
hbase = 10000;
|
|
|
|
}
|
|
|
|
else if (base == 16) {
|
2002-04-10 12:45:26 +04:00
|
|
|
j = (SIZEOF_BDIGITS/sizeof(char)*CHAR_BIT*i)/4+2;
|
1998-01-16 15:13:05 +03:00
|
|
|
hbase = 0x10000;
|
|
|
|
}
|
|
|
|
else if (base == 8) {
|
2002-04-10 12:45:26 +04:00
|
|
|
j = (SIZEOF_BDIGITS/sizeof(char)*CHAR_BIT*i)+2;
|
1998-01-16 15:13:05 +03:00
|
|
|
hbase = 010000;
|
|
|
|
}
|
|
|
|
else if (base == 2) {
|
2002-04-10 12:45:26 +04:00
|
|
|
j = (SIZEOF_BDIGITS*CHAR_BIT*i)+2;
|
1998-01-16 15:13:05 +03:00
|
|
|
hbase = 020;
|
|
|
|
}
|
|
|
|
else {
|
2001-11-19 08:03:03 +03:00
|
|
|
rb_raise(rb_eArgError, "illegal radix %d", base);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
t = rb_big_clone(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
ds = BDIGITS(t);
|
1999-01-20 07:59:39 +03:00
|
|
|
ss = rb_str_new(0, j);
|
1998-01-16 15:13:05 +03:00
|
|
|
s = RSTRING(ss)->ptr;
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
s[0] = RBIGNUM(x)->sign ? '+' : '-';
|
1998-01-16 15:13:05 +03:00
|
|
|
while (i && j) {
|
1999-08-13 09:45:20 +04:00
|
|
|
long k = i;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num = 0;
|
2000-10-31 11:37:47 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
while (k--) {
|
|
|
|
num = BIGUP(num) + ds[k];
|
2000-10-31 11:37:47 +03:00
|
|
|
ds[k] = (BDIGIT)(num / hbase);
|
1998-01-16 15:13:05 +03:00
|
|
|
num %= hbase;
|
|
|
|
}
|
|
|
|
if (ds[i-1] == 0) i--;
|
|
|
|
k = 4;
|
|
|
|
while (k--) {
|
1999-01-20 07:59:39 +03:00
|
|
|
c = (char)(num % base);
|
1998-01-16 15:13:05 +03:00
|
|
|
s[--j] = hexmap[(int)c];
|
|
|
|
num /= base;
|
|
|
|
if (i == 0 && num == 0) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (s[j] == '0') j++;
|
1998-01-16 15:19:22 +03: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 15:13:05 +03:00
|
|
|
s[RSTRING(ss)->len] = '\0';
|
|
|
|
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2001-11-19 08:03:03 +03:00
|
|
|
rb_big_to_s(argc, argv, x)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-11-19 08:03:03 +03:00
|
|
|
VALUE b;
|
|
|
|
int base;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &b);
|
|
|
|
if (argc == 0) base = 10;
|
|
|
|
else base = NUM2INT(b);
|
|
|
|
return rb_big2str(x, base);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
static unsigned long
|
|
|
|
big2ulong(x, type)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
2000-01-05 07:41:21 +03:00
|
|
|
char *type;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long len = RBIGNUM(x)->len;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-04-10 12:45:26 +04:00
|
|
|
if (len > SIZEOF_LONG/SIZEOF_BDIGITS)
|
2000-03-07 11:37:59 +03:00
|
|
|
rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
|
1998-01-16 15:13:05 +03:00
|
|
|
ds = BDIGITS(x);
|
|
|
|
num = 0;
|
|
|
|
while (len--) {
|
|
|
|
num = BIGUP(num);
|
|
|
|
num += ds[len];
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
unsigned long
|
|
|
|
rb_big2ulong(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
|
|
|
unsigned long num = big2ulong(x, "unsigned long");
|
|
|
|
|
|
|
|
if (!RBIGNUM(x)->sign) return -num;
|
2000-05-12 13:07:57 +04:00
|
|
|
return num;
|
2000-01-05 07:41:21 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
long
|
|
|
|
rb_big2long(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
2000-01-05 07:41:21 +03:00
|
|
|
unsigned long num = big2ulong(x, "int");
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
if ((long)num < 0 && (RBIGNUM(x)->sign || (long)num != LONG_MIN)) {
|
2000-03-07 11:37:59 +03:00
|
|
|
rb_raise(rb_eRangeError, "bignum too big to convert into `int'");
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
if (!RBIGNUM(x)->sign) return -(long)num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2002-03-14 09:23:46 +03:00
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
static unsigned LONG_LONG
|
2002-03-14 09:23:46 +03:00
|
|
|
big2ull(x, type)
|
|
|
|
VALUE x;
|
|
|
|
char *type;
|
|
|
|
{
|
|
|
|
long len = RBIGNUM(x)->len;
|
|
|
|
BDIGIT_DBL num;
|
|
|
|
BDIGIT *ds;
|
|
|
|
|
2002-04-10 12:45:26 +04:00
|
|
|
if (len > SIZEOF_LONG_LONG/SIZEOF_BDIGITS)
|
2002-03-14 09:23:46 +03:00
|
|
|
rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
|
|
|
|
ds = BDIGITS(x);
|
|
|
|
num = 0;
|
|
|
|
while (len--) {
|
|
|
|
num = BIGUP(num);
|
|
|
|
num += ds[len];
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
unsigned LONG_LONG
|
2002-03-14 09:23:46 +03:00
|
|
|
rb_big2ull(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
unsigned LONG_LONG num = big2ull(x, "unsigned long long");
|
2002-03-14 09:23:46 +03:00
|
|
|
|
|
|
|
if (!RBIGNUM(x)->sign) return -num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
LONG_LONG
|
2002-03-14 09:23:46 +03:00
|
|
|
rb_big2ll(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
unsigned LONG_LONG num = big2ull(x, "long long");
|
2002-03-14 09:23:46 +03:00
|
|
|
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
if ((LONG_LONG)num < 0 && (RBIGNUM(x)->sign
|
|
|
|
|| (LONG_LONG)num != LLONG_MIN)) {
|
2002-03-14 09:23:46 +03:00
|
|
|
rb_raise(rb_eRangeError, "bignum too big to convert into `long long'");
|
|
|
|
}
|
* bignum.c, intern.h (rb_ull2big, rb_ll2big, rb_ull2inum, rb_ll2inum,
big2ull, rb_big2ull, rb_big2ll): use LONG_LONG macro instead of
long long.
* numeric.c, intern.h, ruby.h (rb_num2ll, rb_num2ull): ditto.
* ruby.h: use _I64_MAX and _I64_MIN if they are defined (for VC++).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2207 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-03-15 11:51:31 +03:00
|
|
|
if (!RBIGNUM(x)->sign) return -(LONG_LONG)num;
|
2002-03-14 09:23:46 +03:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LONG_LONG */
|
|
|
|
|
2000-06-22 12:29:58 +04:00
|
|
|
static VALUE
|
|
|
|
dbl2big(d)
|
1998-01-16 15:13:05 +03:00
|
|
|
double d;
|
|
|
|
{
|
2000-10-31 11:37:47 +03:00
|
|
|
long i = 0;
|
2000-11-02 12:04:54 +03:00
|
|
|
BDIGIT c;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *digits;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE z;
|
|
|
|
double u = (d < 0)?-d:d;
|
|
|
|
|
1999-08-13 09: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 15:13:05 +03:00
|
|
|
u /= (double)(BIGRAD);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
z = bignew(i, d>=0);
|
|
|
|
digits = BDIGITS(z);
|
|
|
|
while (i--) {
|
|
|
|
u *= BIGRAD;
|
2000-11-02 12:04:54 +03:00
|
|
|
c = (BDIGIT)u;
|
1998-01-16 15:13:05 +03:00
|
|
|
u -= c;
|
2000-11-02 12:04:54 +03:00
|
|
|
digits[i] = c;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-06-22 12:29:58 +04:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_dbl2big(d)
|
|
|
|
double d;
|
|
|
|
{
|
|
|
|
return bignorm(dbl2big(d));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big2dbl(x)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
double d = 0.0;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i = RBIGNUM(x)->len;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds = BDIGITS(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
d = ds[i] + BIGRAD*d;
|
|
|
|
}
|
2002-06-18 10:29:07 +04:00
|
|
|
if (isinf(d)) d = HUGE_VAL;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(x)->sign) d = -d;
|
1998-01-16 15:13:05 +03:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_big_to_f(x)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_float_new(rb_big2dbl(x));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_cmp(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long xlen = RBIGNUM(x)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
2000-06-05 12:46:59 +04:00
|
|
|
case T_FLOAT:
|
2001-10-30 11:43:28 +03:00
|
|
|
{
|
|
|
|
double d = rb_big2dbl(x);
|
|
|
|
|
|
|
|
if (d == RFLOAT(y)->value) return INT2FIX(0);
|
|
|
|
if (d > RFLOAT(y)->value) return INT2FIX(1);
|
|
|
|
if (d < RFLOAT(y)->value) return INT2FIX(-1);
|
|
|
|
}
|
2000-06-05 12:46:59 +04:00
|
|
|
break;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03: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 15:13:05 +03: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 15:19:22 +03:00
|
|
|
(RBIGNUM(x)->sign ? INT2FIX(1) : INT2FIX(-1)) :
|
|
|
|
(RBIGNUM(x)->sign ? INT2FIX(-1) : INT2FIX(1));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_eq(x, y)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
2000-06-05 12:46:59 +04:00
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
y = rb_int2big(FIX2LONG(y));
|
|
|
|
break;
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
case T_FLOAT:
|
2002-04-18 12:46:18 +04:00
|
|
|
if (rb_big2dbl(x) == RFLOAT(y)->value)
|
|
|
|
return Qtrue;
|
|
|
|
else
|
|
|
|
return Qfalse;
|
2000-06-05 12:46:59 +04:00
|
|
|
default:
|
2002-04-18 12:46:18 +04:00
|
|
|
return rb_equal(y, x);
|
2000-06-05 12:46:59 +04:00
|
|
|
}
|
|
|
|
if (RBIGNUM(x)->sign != RBIGNUM(y)->sign) return Qfalse;
|
|
|
|
if (RBIGNUM(x)->len != RBIGNUM(y)->len) return Qfalse;
|
2000-10-31 11:37:47 +03:00
|
|
|
if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,RBIGNUM(y)->len) != 0) return Qfalse;
|
2000-06-05 12:46:59 +04:00
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-11-08 09:43:14 +03:00
|
|
|
static VALUE
|
|
|
|
rb_big_eql(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
if (TYPE(y) != T_BIGNUM) return Qfalse;
|
|
|
|
if (RBIGNUM(x)->sign != RBIGNUM(y)->sign) return Qfalse;
|
|
|
|
if (RBIGNUM(x)->len != RBIGNUM(y)->len) return Qfalse;
|
|
|
|
if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,RBIGNUM(y)->len) != 0) return Qfalse;
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_uminus(x)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE z = rb_big_clone(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
RBIGNUM(z)->sign = !RBIGNUM(x)->sign;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_neg(x)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE z = rb_big_clone(x);
|
1999-08-13 09:45:20 +04:00
|
|
|
long i = RBIGNUM(x)->len;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds = BDIGITS(z);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-02-22 13:28:47 +03:00
|
|
|
if (!RBIGNUM(x)->sign) get2comp(z, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (i--) ds[i] = ~ds[i];
|
2002-02-22 13:28:47 +03:00
|
|
|
if (RBIGNUM(x)->sign) get2comp(z, Qfalse);
|
1998-01-16 15:13:05 +03:00
|
|
|
RBIGNUM(z)->sign = !RBIGNUM(z)->sign;
|
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
bigsub(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE z = 0;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *zds;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL_SIGNED num;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
i = RBIGNUM(x)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
/* if x is larger than y, swap */
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RBIGNUM(x)->len < RBIGNUM(y)->len) {
|
1998-01-16 15:13:05 +03:00
|
|
|
z = x; x = y; y = z; /* swap x y */
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
else if (RBIGNUM(x)->len == RBIGNUM(y)->len) {
|
1998-01-16 15:13:05 +03: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 15:19:22 +03:00
|
|
|
z = bignew(RBIGNUM(x)->len, (z == 0)?1:0);
|
1998-01-16 15:13:05 +03:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i = 0, num = 0; i < RBIGNUM(y)->len; i++) {
|
2000-11-01 11:49:40 +03:00
|
|
|
num += (BDIGIT_DBL_SIGNED)BDIGITS(x)[i] - BDIGITS(y)[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
zds[i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
while (num && i < RBIGNUM(x)->len) {
|
1998-01-16 15:13:05 +03:00
|
|
|
num += BDIGITS(x)[i];
|
|
|
|
zds[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
while (i < RBIGNUM(x)->len) {
|
1998-01-16 15:13:05 +03:00
|
|
|
zds[i] = BDIGITS(x)[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2000-07-12 10:06:50 +04:00
|
|
|
return z;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
bigadd(x, y, sign)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
char sign;
|
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE z;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i, len;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
sign = (sign == RBIGNUM(y)->sign);
|
|
|
|
if (RBIGNUM(x)->sign != sign) {
|
|
|
|
if (sign) return bigsub(y, x);
|
1998-01-16 15:13:05 +03:00
|
|
|
return bigsub(x, y);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
len = RBIGNUM(x)->len + 1;
|
|
|
|
z = x; x = y; y = z;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:19:22 +03:00
|
|
|
len = RBIGNUM(y)->len + 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
z = bignew(len, sign);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
len = RBIGNUM(x)->len;
|
|
|
|
for (i = 0, num = 0; i < len; i++) {
|
2000-11-01 11:49:40 +03:00
|
|
|
num += (BDIGIT_DBL)BDIGITS(x)[i] + BDIGITS(y)[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
BDIGITS(z)[i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
len = RBIGNUM(y)->len;
|
|
|
|
while (num && i < len) {
|
1998-01-16 15:13:05 +03:00
|
|
|
num += BDIGITS(y)[i];
|
|
|
|
BDIGITS(z)[i++] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
while (i < len) {
|
1998-01-16 15:13:05 +03:00
|
|
|
BDIGITS(z)[i] = BDIGITS(y)[i];
|
|
|
|
i++;
|
|
|
|
}
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGITS(z)[i] = (BDIGIT)num;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-12 10:06:50 +04:00
|
|
|
return z;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_plus(x, y)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
/* fall through */
|
|
|
|
case T_BIGNUM:
|
2000-07-12 10:06:50 +04:00
|
|
|
return bignorm(bigadd(x, y, 1));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_float_new(rb_big2dbl(x) + RFLOAT(y)->value);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_minus(x, y)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
/* fall through */
|
|
|
|
case T_BIGNUM:
|
2000-07-12 10:06:50 +04:00
|
|
|
return bignorm(bigadd(x, y, 0));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_float_new(rb_big2dbl(x) - RFLOAT(y)->value);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_mul(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long i, j;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL n = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE z;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *zds;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FIXNUM_P(x)) x = rb_int2big(FIX2LONG(x));
|
1998-01-16 15:13:05 +03:00
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_float_new(rb_big2dbl(x) * RFLOAT(y)->value);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
j = RBIGNUM(x)->len + RBIGNUM(y)->len + 1;
|
|
|
|
z = bignew(j, RBIGNUM(x)->sign==RBIGNUM(y)->sign);
|
1998-01-16 15:13:05 +03:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
while (j--) zds[j] = 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i = 0; i < RBIGNUM(x)->len; i++) {
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL dd = BDIGITS(x)[i];
|
1998-01-16 15:13:05 +03:00
|
|
|
if (dd == 0) continue;
|
|
|
|
n = 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
for (j = 0; j < RBIGNUM(y)->len; j++) {
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL ee = n + (BDIGIT_DBL)dd * BDIGITS(y)[j];
|
1998-01-16 15:13:05 +03:00
|
|
|
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
|
2000-07-12 10:06:50 +04:00
|
|
|
bigdivrem(x, y, divp, modp)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
2000-07-12 10:06:50 +04:00
|
|
|
VALUE *divp, *modp;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
long nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len;
|
|
|
|
long i, j;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE yy, z;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *xds, *yds, *zds, *tds;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL t2;
|
|
|
|
BDIGIT_DBL_SIGNED num;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT dd, q;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
yds = BDIGITS(y);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ny == 0 && yds[0] == 0) rb_num_zerodiv();
|
1998-01-16 15:13:05 +03:00
|
|
|
if (nx < ny || nx == ny && BDIGITS(x)[nx - 1] < BDIGITS(y)[ny - 1]) {
|
2000-07-12 11:33:34 +04:00
|
|
|
if (divp) *divp = rb_int2big(0);
|
2000-07-12 10:06:50 +04:00
|
|
|
if (modp) *modp = x;
|
1998-01-16 15:13:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
xds = BDIGITS(x);
|
|
|
|
if (ny == 1) {
|
|
|
|
dd = yds[0];
|
1999-01-20 07:59:39 +03:00
|
|
|
z = rb_big_clone(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
t2 = 0; i = nx;
|
|
|
|
while (i--) {
|
|
|
|
t2 = BIGUP(t2) + zds[i];
|
2000-10-31 11:37:47 +03:00
|
|
|
zds[i] = (BDIGIT)(t2 / dd);
|
1998-01-16 15:13:05 +03:00
|
|
|
t2 %= dd;
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
RBIGNUM(z)->sign = RBIGNUM(x)->sign==RBIGNUM(y)->sign;
|
2001-01-09 10:26:21 +03:00
|
|
|
if (modp) {
|
|
|
|
*modp = rb_uint2big((unsigned long)t2);
|
|
|
|
RBIGNUM(*modp)->sign = RBIGNUM(x)->sign;
|
|
|
|
}
|
2000-07-12 10:06:50 +04:00
|
|
|
if (divp) *divp = z;
|
1998-01-16 15:13:05 +03:00
|
|
|
return;
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
z = bignew(nx==ny?nx+2:nx+1, RBIGNUM(x)->sign==RBIGNUM(y)->sign);
|
1998-01-16 15:13:05 +03:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
if (nx==ny) zds[nx+1] = 0;
|
|
|
|
while (!yds[ny-1]) ny--;
|
2000-11-08 08:29:37 +03:00
|
|
|
|
|
|
|
dd = 0;
|
|
|
|
q = yds[ny-1];
|
|
|
|
while ((q & (1<<(BITSPERDIG-1))) == 0) {
|
|
|
|
q <<= 1;
|
|
|
|
dd++;
|
|
|
|
}
|
|
|
|
if (dd) {
|
1999-01-20 07:59:39 +03:00
|
|
|
yy = rb_big_clone(y);
|
|
|
|
tds = BDIGITS(yy);
|
1998-01-16 15:13:05 +03:00
|
|
|
j = 0;
|
2000-11-08 08:29:37 +03:00
|
|
|
t2 = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (j<ny) {
|
2000-11-08 08:29:37 +03:00
|
|
|
t2 += (BDIGIT_DBL)yds[j]<<dd;
|
|
|
|
tds[j++] = BIGLO(t2);
|
|
|
|
t2 = BIGDN(t2);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
yds = tds;
|
|
|
|
j = 0;
|
2000-11-08 08:29:37 +03:00
|
|
|
t2 = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (j<nx) {
|
2000-11-08 08:29:37 +03:00
|
|
|
t2 += (BDIGIT_DBL)xds[j]<<dd;
|
|
|
|
zds[j++] = BIGLO(t2);
|
|
|
|
t2 = BIGDN(t2);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-11-08 08:29:37 +03:00
|
|
|
zds[j] = (BDIGIT)t2;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
zds[nx] = 0;
|
|
|
|
j = nx;
|
|
|
|
while (j--) zds[j] = xds[j];
|
|
|
|
}
|
2000-11-08 08:29:37 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
j = nx==ny?nx+1:nx;
|
|
|
|
do {
|
|
|
|
if (zds[j] == yds[ny-1]) q = BIGRAD-1;
|
2000-10-31 11:37:47 +03:00
|
|
|
else q = (BDIGIT)((BIGUP(zds[j]) + zds[j-1])/yds[ny-1]);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (q) {
|
|
|
|
i = 0; num = 0; t2 = 0;
|
|
|
|
do { /* multiply and subtract */
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL ee;
|
|
|
|
t2 += (BDIGIT_DBL)yds[i] * q;
|
1998-01-16 15:13:05 +03:00
|
|
|
ee = num - BIGLO(t2);
|
2000-11-01 11:49:40 +03:00
|
|
|
num = (BDIGIT_DBL)zds[j - ny + i] + ee;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (ee) zds[j - ny + i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
t2 = BIGDN(t2);
|
|
|
|
} while (++i < ny);
|
2000-11-01 11:49:40 +03:00
|
|
|
num += zds[j - ny + i] - t2;/* borrow from high digit; don't update */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (num) { /* "add back" required */
|
|
|
|
i = 0; num = 0; q--;
|
|
|
|
do {
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL ee = num + yds[i];
|
|
|
|
num = (BDIGIT_DBL)zds[j - ny + i] + ee;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (ee) zds[j - ny + i] = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
} while (++i < ny);
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zds[j] = q;
|
|
|
|
} while (--j >= ny);
|
2000-07-12 10:06:50 +04:00
|
|
|
if (divp) { /* move quotient down in z */
|
|
|
|
*divp = rb_big_clone(z);
|
2000-07-12 10:33:23 +04:00
|
|
|
zds = BDIGITS(*divp);
|
1998-01-16 15:13:05 +03:00
|
|
|
j = (nx==ny ? nx+2 : nx+1) - ny;
|
|
|
|
for (i = 0;i < j;i++) zds[i] = zds[i+ny];
|
2000-07-12 10:33:23 +04:00
|
|
|
RBIGNUM(*divp)->len = i;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-07-12 10:06:50 +04:00
|
|
|
if (modp) { /* just normalize remainder */
|
|
|
|
*modp = rb_big_clone(z);
|
2001-05-02 08:22:21 +04:00
|
|
|
zds = BDIGITS(*modp);
|
2001-05-11 09:24:59 +04:00
|
|
|
while (ny-- && !zds[ny]); ++ny;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (dd) {
|
2001-05-02 08:22:21 +04:00
|
|
|
t2 = 0; i = ny;
|
1998-01-16 15:13:05 +03:00
|
|
|
while(i--) {
|
2000-11-08 08:29:37 +03:00
|
|
|
t2 = (t2 | zds[i]) >> dd;
|
|
|
|
q = zds[i];
|
|
|
|
zds[i] = BIGLO(t2);
|
|
|
|
t2 = BIGUP(q);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2000-07-12 10:06:50 +04:00
|
|
|
RBIGNUM(*modp)->len = ny;
|
|
|
|
RBIGNUM(*modp)->sign = RBIGNUM(x)->sign;
|
2000-07-06 11:21:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bigdivmod(x, y, divp, modp)
|
|
|
|
VALUE x, y;
|
|
|
|
VALUE *divp, *modp;
|
|
|
|
{
|
|
|
|
VALUE mod;
|
|
|
|
|
|
|
|
bigdivrem(x, y, divp, &mod);
|
|
|
|
if (RBIGNUM(x)->sign != RBIGNUM(y)->sign && RBIGNUM(mod)->len > 0) {
|
|
|
|
if (divp) *divp = bigadd(*divp, rb_int2big(1), 0);
|
|
|
|
if (modp) *modp = bigadd(mod, y, 1);
|
|
|
|
}
|
|
|
|
else {
|
2000-07-12 10:06:50 +04:00
|
|
|
if (divp) *divp = *divp;
|
|
|
|
if (modp) *modp = mod;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_div(x, y)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_float_new(rb_big2dbl(x) / RFLOAT(y)->value);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
bigdivmod(x, y, &z, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-12 10:06:50 +04:00
|
|
|
return bignorm(z);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2000-07-06 11:21:26 +04:00
|
|
|
rb_big_modulo(x, y)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-07-03 09:46:36 +04:00
|
|
|
bigdivmod(x, y, 0, &z);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-12 10:06:50 +04:00
|
|
|
return bignorm(z);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
static VALUE
|
|
|
|
rb_big_remainder(x, y)
|
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE z;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
y = rb_int2big(FIX2LONG(y));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return rb_num_coerce_bin(x, y);
|
|
|
|
}
|
|
|
|
bigdivrem(x, y, 0, &z);
|
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_divmod(x, y)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
VALUE div, mod;
|
|
|
|
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-07-03 09:46:36 +04:00
|
|
|
bigdivmod(x, y, &div, &mod);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-12 10:06:50 +04:00
|
|
|
return rb_assoc_new(bignorm(div), bignorm(mod));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_pow(x, y)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE x, y;
|
|
|
|
{
|
|
|
|
double d;
|
1999-01-20 07:59:39 +03:00
|
|
|
long yy;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (y == INT2FIX(0)) return INT2FIX(1);
|
|
|
|
switch (TYPE(y)) {
|
|
|
|
case T_FLOAT:
|
|
|
|
d = RFLOAT(y)->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BIGNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_warn("in a**b, b may be too big");
|
|
|
|
d = rb_big2dbl(y);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FIXNUM:
|
1999-01-20 07:59:39 +03:00
|
|
|
yy = NUM2LONG(y);
|
|
|
|
if (yy > 0) {
|
2002-06-11 11:02:23 +04:00
|
|
|
VALUE z = x;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
for (;;) {
|
2002-06-11 11:02:23 +04:00
|
|
|
yy -= 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (yy == 0) break;
|
|
|
|
while (yy % 2 == 0) {
|
2002-06-11 11:02:23 +04:00
|
|
|
yy /= 2;
|
1999-01-20 07:59:39 +03:00
|
|
|
x = rb_big_mul(x, x);
|
|
|
|
}
|
|
|
|
z = rb_big_mul(z, x);
|
|
|
|
}
|
2002-06-11 11:02:23 +04:00
|
|
|
return bignorm(z);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
d = (double)yy;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_num_coerce_bin(x, y);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_float_new(pow(rb_big2dbl(x), d));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_and(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE z;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds1, *ds2, *zds;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i, l1, l2;
|
1998-01-16 15:13:05 +03:00
|
|
|
char sign;
|
|
|
|
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(y, T_BIGNUM);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(y)->sign) {
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_big_clone(y);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(y, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-20 07:59:39 +03:00
|
|
|
x = rb_big_clone(x);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(x, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
l1 = RBIGNUM(y)->len;
|
|
|
|
l2 = RBIGNUM(x)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
ds1 = BDIGITS(y);
|
|
|
|
ds2 = BDIGITS(x);
|
1998-01-16 15:19:22 +03:00
|
|
|
sign = RBIGNUM(y)->sign;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:19:22 +03:00
|
|
|
l1 = RBIGNUM(x)->len;
|
|
|
|
l2 = RBIGNUM(y)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
ds1 = BDIGITS(x);
|
|
|
|
ds2 = BDIGITS(y);
|
1998-01-16 15:19:22 +03:00
|
|
|
sign = RBIGNUM(x)->sign;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
z = bignew(l2, RBIGNUM(x)->sign || RBIGNUM(y)->sign);
|
1998-01-16 15:13:05 +03: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];
|
|
|
|
}
|
2002-02-22 13:28:47 +03:00
|
|
|
if (!RBIGNUM(z)->sign) get2comp(z, Qfalse);
|
1998-01-16 15:13:05 +03:00
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_or(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE z;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds1, *ds2, *zds;
|
|
|
|
long i, l1, l2;
|
1998-01-16 15:13:05 +03:00
|
|
|
char sign;
|
|
|
|
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(y, T_BIGNUM);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(y)->sign) {
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_big_clone(y);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(y, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-20 07:59:39 +03:00
|
|
|
x = rb_big_clone(x);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(x, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
l1 = RBIGNUM(y)->len;
|
|
|
|
l2 = RBIGNUM(x)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
ds1 = BDIGITS(y);
|
|
|
|
ds2 = BDIGITS(x);
|
1998-01-16 15:19:22 +03:00
|
|
|
sign = RBIGNUM(y)->sign;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:19:22 +03:00
|
|
|
l1 = RBIGNUM(x)->len;
|
|
|
|
l2 = RBIGNUM(y)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
ds1 = BDIGITS(x);
|
|
|
|
ds2 = BDIGITS(y);
|
1998-01-16 15:19:22 +03:00
|
|
|
sign = RBIGNUM(x)->sign;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
z = bignew(l2, RBIGNUM(x)->sign && RBIGNUM(y)->sign);
|
1998-01-16 15:13:05 +03: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);
|
|
|
|
}
|
2002-02-22 13:28:47 +03:00
|
|
|
if (!RBIGNUM(z)->sign) get2comp(z, Qfalse);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_xor(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE z;
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *ds1, *ds2, *zds;
|
|
|
|
long i, l1, l2;
|
1998-01-16 15:13:05 +03:00
|
|
|
char sign;
|
|
|
|
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_int2big(FIX2LONG(y));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(y, T_BIGNUM);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(y)->sign) {
|
1999-01-20 07:59:39 +03:00
|
|
|
y = rb_big_clone(y);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(y, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-20 07:59:39 +03:00
|
|
|
x = rb_big_clone(x);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(x, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RBIGNUM(x)->len > RBIGNUM(y)->len) {
|
|
|
|
l1 = RBIGNUM(y)->len;
|
|
|
|
l2 = RBIGNUM(x)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
ds1 = BDIGITS(y);
|
|
|
|
ds2 = BDIGITS(x);
|
1998-01-16 15:19:22 +03:00
|
|
|
sign = RBIGNUM(y)->sign;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:19:22 +03:00
|
|
|
l1 = RBIGNUM(x)->len;
|
|
|
|
l2 = RBIGNUM(y)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
ds1 = BDIGITS(x);
|
|
|
|
ds2 = BDIGITS(y);
|
1998-01-16 15:19:22 +03:00
|
|
|
sign = RBIGNUM(x)->sign;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03: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 15:13:05 +03: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];
|
|
|
|
}
|
2002-02-22 13:28:47 +03:00
|
|
|
if (!RBIGNUM(z)->sign) get2comp(z, Qfalse);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE rb_big_rshift _((VALUE,VALUE));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_lshift(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *xds, *zds;
|
1998-01-16 15:13:05 +03:00
|
|
|
int shift = NUM2INT(y);
|
1999-08-13 09:45:20 +04:00
|
|
|
int s1 = shift/BITSPERDIG;
|
|
|
|
int s2 = shift%BITSPERDIG;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE z;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
long len, i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (shift < 0) return rb_big_rshift(x, INT2FIX(-shift));
|
1998-01-16 15:19:22 +03:00
|
|
|
len = RBIGNUM(x)->len;
|
|
|
|
z = bignew(len+s1+1, RBIGNUM(x)->sign);
|
1998-01-16 15:13:05 +03:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
for (i=0; i<s1; i++) {
|
|
|
|
*zds++ = 0;
|
|
|
|
}
|
2000-06-28 12:31:35 +04:00
|
|
|
xds = BDIGITS(x);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i<len; i++) {
|
2000-12-22 06:22:25 +03:00
|
|
|
num = num | (BDIGIT_DBL)*xds++<<s2;
|
1998-01-16 15:13:05 +03:00
|
|
|
*zds++ = BIGLO(num);
|
|
|
|
num = BIGDN(num);
|
|
|
|
}
|
|
|
|
*zds = BIGLO(num);
|
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_rshift(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *xds, *zds;
|
1998-01-16 15:13:05 +03:00
|
|
|
int shift = NUM2INT(y);
|
1999-08-13 09:45:20 +04:00
|
|
|
int s1 = shift/BITSPERDIG;
|
|
|
|
int s2 = shift%BITSPERDIG;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE z;
|
2000-11-01 11:49:40 +03:00
|
|
|
BDIGIT_DBL num = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
long i = RBIGNUM(x)->len;
|
|
|
|
long j;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (shift < 0) return rb_big_lshift(x, INT2FIX(-shift));
|
2002-02-15 07:43:58 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (s1 > RBIGNUM(x)->len) {
|
|
|
|
if (RBIGNUM(x)->sign)
|
1998-01-16 15:13:05 +03:00
|
|
|
return INT2FIX(0);
|
|
|
|
else
|
|
|
|
return INT2FIX(-1);
|
|
|
|
}
|
2002-02-15 07:43:58 +03:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
|
|
|
x = rb_big_clone(x);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(x, Qtrue);
|
2002-02-15 07:43:58 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
xds = BDIGITS(x);
|
1998-01-16 15:19:22 +03:00
|
|
|
i = RBIGNUM(x)->len; j = i - s1;
|
|
|
|
z = bignew(j, RBIGNUM(x)->sign);
|
1998-01-16 15:13:05 +03:00
|
|
|
zds = BDIGITS(z);
|
|
|
|
while (i--, j--) {
|
|
|
|
num = (num | xds[i]) >> s2;
|
|
|
|
zds[j] = BIGLO(num);
|
|
|
|
num = BIGUP(xds[i]);
|
|
|
|
}
|
2002-02-15 07:43:58 +03:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(z, Qfalse);
|
2002-02-15 07:43:58 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return bignorm(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_aref(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT *xds;
|
2001-11-01 08:11:24 +03:00
|
|
|
int shift;
|
1999-08-13 09:45:20 +04:00
|
|
|
int s1, s2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-11-01 08:11:24 +03:00
|
|
|
if (TYPE(y) == T_BIGNUM) {
|
|
|
|
if (!RBIGNUM(y)->sign || RBIGNUM(x)->sign)
|
|
|
|
return INT2FIX(0);
|
|
|
|
return INT2FIX(1);
|
|
|
|
}
|
|
|
|
shift = NUM2INT(y);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (shift < 0) return INT2FIX(0);
|
1998-01-16 15:19:22 +03:00
|
|
|
s1 = shift/BITSPERDIG;
|
|
|
|
s2 = shift%BITSPERDIG;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
|
|
|
if (s1 >= RBIGNUM(x)->len) return INT2FIX(1);
|
1999-01-20 07:59:39 +03:00
|
|
|
x = rb_big_clone(x);
|
2002-02-22 13:28:47 +03:00
|
|
|
get2comp(x, Qtrue);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:19:22 +03:00
|
|
|
if (s1 >= RBIGNUM(x)->len) return INT2FIX(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
xds = BDIGITS(x);
|
|
|
|
if (xds[s1] & (1<<s2))
|
|
|
|
return INT2FIX(1);
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_hash(x)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-10-31 11:37:47 +03:00
|
|
|
long i, len, key;
|
|
|
|
BDIGIT *digits;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-31 11:37:47 +03:00
|
|
|
key = 0; digits = BDIGITS(x); len = RBIGNUM(x)->len;
|
|
|
|
for (i=0; i<len; i++) {
|
1998-01-16 15:13:05 +03:00
|
|
|
key ^= *digits++;
|
|
|
|
}
|
|
|
|
return INT2FIX(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_coerce(x, y)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x, y;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (FIXNUM_P(y)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_assoc_new(rb_int2big(FIX2LONG(y)), x);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eTypeError, "Can't coerce %s to Bignum",
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_class2name(CLASS_OF(y)));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
/* not reached */
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_abs(x)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE x;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!RBIGNUM(x)->sign) {
|
1999-01-20 07:59:39 +03:00
|
|
|
x = rb_big_clone(x);
|
1998-01-16 15:19:22 +03:00
|
|
|
RBIGNUM(x)->sign = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return x;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* !!!warnig!!!!
|
|
|
|
this is not really a random number!!
|
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
1999-12-06 12:04:03 +03:00
|
|
|
rb_big_rand(max, rand)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE max;
|
1999-12-06 12:04:03 +03:00
|
|
|
double rand;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-12-06 12:04:03 +03:00
|
|
|
VALUE v;
|
1999-08-13 09:45:20 +04:00
|
|
|
long len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
len = RBIGNUM(max)->len;
|
1999-12-06 12:04:03 +03:00
|
|
|
v = bignew(len,1);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len--) {
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGITS(v)[len] = ((BDIGIT)~0) * rand;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
return rb_big_modulo((VALUE)v, max);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_big_size(big)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE big;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-04-10 12:45:26 +04:00
|
|
|
return INT2FIX(RBIGNUM(big)->len*SIZEOF_BDIGITS);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_big_zero_p(big)
|
|
|
|
VALUE big;
|
|
|
|
{
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
Init_Bignum()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_cBignum = rb_define_class("Bignum", rb_cInteger);
|
|
|
|
|
2001-11-19 08:03:03 +03:00
|
|
|
rb_define_method(rb_cBignum, "to_s", rb_big_to_s, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2000-07-06 11:21:26 +04:00
|
|
|
rb_define_method(rb_cBignum, "%", rb_big_modulo, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cBignum, "divmod", rb_big_divmod, 1);
|
2000-07-06 11:21:26 +04:00
|
|
|
rb_define_method(rb_cBignum, "modulo", rb_big_modulo, 1);
|
|
|
|
rb_define_method(rb_cBignum, "remainder", rb_big_remainder, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2001-11-08 09:43:14 +03:00
|
|
|
rb_define_method(rb_cBignum, "eql?", rb_big_eql, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cBignum, "hash", rb_big_hash, 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 15:13:05 +03:00
|
|
|
}
|