git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1023 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2000-10-31 08:37:47 +00:00
parent 5f4d324d3b
commit c90b1ecaf8
9 changed files with 224 additions and 101 deletions

View File

@ -1,3 +1,28 @@
Tue Oct 31 17:27:17 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
* bignum.c: change digit size to `long|int' if long long is
available.
* marshal.c (w_object): support `long|int' digits.
* marshal.c (r_object): ditto.
Sat Oct 28 23:54:22 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
* parse.y (yylex): allow =end at the end of file (without a
newline at the end).
Fri Oct 27 10:00:27 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
* bignum.c (rb_cstr2inum): should ignore trailing white spaces.
* bignum.c (rb_str2inum): string may not have sentinel NUL.
Fri Oct 27 02:37:22 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
* bignum.c (rb_cstr2inum): wrongly assigned base to c before
badcheck check.
Thu Oct 26 02:42:50 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp> Thu Oct 26 02:42:50 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
* lib/net/protocol.rb: Command#critical_ok * lib/net/protocol.rb: Command#critical_ok

View File

@ -1586,7 +1586,7 @@ rb_ary_flatten_bang(ary)
} }
id = rb_obj_id(ary2); id = rb_obj_id(ary2);
if (rb_ary_includes(flattening, id)) { if (rb_ary_includes(flattening, id)) {
rb_raise(rb_eArgError, "tryed to flatten recursive array"); rb_raise(rb_eArgError, "tried to flatten recursive array");
} }
rb_ary_push(flattening, id); rb_ary_push(flattening, id);
} }

189
bignum.c
View File

@ -20,15 +20,27 @@ VALUE rb_cBignum;
#define USHORT _USHORT #define USHORT _USHORT
#endif #endif
typedef unsigned short USHORT; #if SIZEOF_LONG*2 <= SIZEOF_LONG_LONG
typedef unsigned long BDIGIT;
typedef unsigned long long BDIGIT2;
typedef long long BDIGIT2_SIGNED;
#elif SIZEOF_INT*2 <= SIZEOF_LONG_LONG
typedef unsigned int BDIGIT;
typedef unsigned long long BDIGIT2;
typedef long long BDIGIT2_SIGNED;
#else
typedef unsigned short BDIGIT;
typedef unsigned long BDIGIT2;
typedef long BDIGIT2_SIGNED;
#endif
#define BDIGITS(x) ((USHORT*)RBIGNUM(x)->digits) #define BDIGITS(x) ((BDIGIT*)RBIGNUM(x)->digits)
#define BITSPERDIG (sizeof(short)*CHAR_BIT) #define BITSPERDIG (sizeof(BDIGIT)*CHAR_BIT)
#define BIGRAD (1L << BITSPERDIG) #define BIGRAD ((BDIGIT2)1 << BITSPERDIG)
#define DIGSPERLONG ((unsigned int)(sizeof(long)/sizeof(short))) #define DIGSPERLONG ((unsigned int)(sizeof(long)/sizeof(BDIGIT)))
#define BIGUP(x) ((unsigned long)(x) << BITSPERDIG) #define BIGUP(x) ((BDIGIT2)(x) << BITSPERDIG)
#define BIGDN(x) RSHIFT(x,BITSPERDIG) #define BIGDN(x) RSHIFT(x,BITSPERDIG)
#define BIGLO(x) ((USHORT)((x) & (BIGRAD-1))) #define BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1)))
static VALUE static VALUE
bignew_1(klass, len, sign) bignew_1(klass, len, sign)
@ -40,7 +52,7 @@ bignew_1(klass, len, sign)
OBJSETUP(big, klass, T_BIGNUM); OBJSETUP(big, klass, T_BIGNUM);
big->sign = sign; big->sign = sign;
big->len = len; big->len = len;
big->digits = ALLOC_N(USHORT, len); big->digits = ALLOC_N(BDIGIT, len);
return (VALUE)big; return (VALUE)big;
} }
@ -53,7 +65,7 @@ rb_big_clone(x)
{ {
VALUE z = bignew_1(CLASS_OF(x), RBIGNUM(x)->len, RBIGNUM(x)->sign); VALUE z = bignew_1(CLASS_OF(x), RBIGNUM(x)->len, RBIGNUM(x)->sign);
MEMCPY(BDIGITS(z), BDIGITS(x), USHORT, RBIGNUM(x)->len); MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, RBIGNUM(x)->len);
return z; return z;
} }
@ -62,8 +74,8 @@ rb_big_2comp(x) /* get 2's complement */
VALUE x; VALUE x;
{ {
long i = RBIGNUM(x)->len; long i = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(x); BDIGIT *ds = BDIGITS(x);
long num; BDIGIT2 num;
while (i--) ds[i] = ~ds[i]; while (i--) ds[i] = ~ds[i];
i = 0; num = 1; i = 0; num = 1;
@ -76,7 +88,7 @@ rb_big_2comp(x) /* get 2's complement */
for (i=1; i<RBIGNUM(x)->len; i++) { for (i=1; i<RBIGNUM(x)->len; i++) {
if (ds[i] != 0) return; if (ds[i] != 0) return;
} }
REALLOC_N(RBIGNUM(x)->digits, USHORT, RBIGNUM(x)->len++); REALLOC_N(RBIGNUM(x)->digits, BDIGIT, RBIGNUM(x)->len++);
ds = BDIGITS(x); ds = BDIGITS(x);
ds[RBIGNUM(x)->len-1] = 1; ds[RBIGNUM(x)->len-1] = 1;
} }
@ -88,12 +100,12 @@ bignorm(x)
{ {
if (!FIXNUM_P(x)) { if (!FIXNUM_P(x)) {
long len = RBIGNUM(x)->len; long len = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(x); BDIGIT *ds = BDIGITS(x);
while (len-- && !ds[len]) ; while (len-- && !ds[len]) ;
RBIGNUM(x)->len = ++len; RBIGNUM(x)->len = ++len;
if (len*sizeof(USHORT) <= sizeof(VALUE)) { if (len*sizeof(BDIGIT) <= sizeof(VALUE)) {
long num = 0; long num = 0;
while (len--) { while (len--) {
num = BIGUP(num) + ds[len]; num = BIGUP(num) + ds[len];
@ -120,16 +132,17 @@ VALUE
rb_uint2big(n) rb_uint2big(n)
unsigned long n; unsigned long n;
{ {
unsigned int i = 0; BDIGIT2 num = n;
USHORT *digits; long i = 0;
BDIGIT *digits;
VALUE big; VALUE big;
i = 0; i = 0;
big = bignew(DIGSPERLONG, 1); big = bignew(DIGSPERLONG, 1);
digits = BDIGITS(big); digits = BDIGITS(big);
while (i < DIGSPERLONG) { while (i < DIGSPERLONG) {
digits[i++] = BIGLO(n); digits[i++] = BIGLO(num);
n = BIGDN(n); num = BIGDN(num);
} }
i = DIGSPERLONG; i = DIGSPERLONG;
@ -181,11 +194,11 @@ rb_cstr2inum(str, base)
char *end; char *end;
int badcheck = (base==0)?1:0; int badcheck = (base==0)?1:0;
char sign = 1, c; char sign = 1, c;
unsigned long num; BDIGIT2 num;
long len, blen = 1; long len, blen = 1;
long i; long i;
VALUE z; VALUE z;
USHORT *zds; BDIGIT *zds;
while (*str && ISSPACE(*str)) str++; while (*str && ISSPACE(*str)) str++;
@ -237,10 +250,9 @@ rb_cstr2inum(str, base)
unsigned long val = strtoul((char*)str, &end, base); unsigned long val = strtoul((char*)str, &end, base);
if (badcheck) { if (badcheck) {
if (end == str || *end) if (end == str) goto bad; /* no number */
goto bad;
while (*end && ISSPACE(*end)) end++; while (*end && ISSPACE(*end)) end++;
if (*end) { if (*end) { /* trailing garbage */
bad: bad:
rb_raise(rb_eArgError, "invalid literal for Integer: %s", s); rb_raise(rb_eArgError, "invalid literal for Integer: %s", s);
} }
@ -279,16 +291,14 @@ rb_cstr2inum(str, base)
c = c - 'A' + 10; c = c - 'A' + 10;
break; break;
default: default:
c = base;
if (badcheck) { if (badcheck) {
if (ISSPACE(c)) { if (ISSPACE(c)) {
while (*str && ISSPACE(*str)) str++; while (*str && ISSPACE(*str)) str++;
if (*str) { if (!*str) break;
break;
}
} }
rb_raise(rb_eArgError, "invalid literal for Integer: %s", s); rb_raise(rb_eArgError, "invalid literal for Integer: %s", s);
} }
c = base;
break; break;
} }
if (c >= base) break; if (c >= base) break;
@ -296,7 +306,7 @@ rb_cstr2inum(str, base)
num = c; num = c;
for (;;) { for (;;) {
while (i<blen) { while (i<blen) {
num += zds[i]*base; num += (BDIGIT2)zds[i]*base;
zds[i++] = BIGLO(num); zds[i++] = BIGLO(num);
num = BIGDN(num); num = BIGDN(num);
} }
@ -320,6 +330,13 @@ rb_str2inum(str, base)
int len; int len;
s = rb_str2cstr(str, &len); s = rb_str2cstr(str, &len);
if (s[len]) { /* no sentinel somehow */
char *p = ALLOCA_N(char, len+1);
MEMCPY(p, s, char, len);
p[len] = '\0';
s = p;
}
if (len != strlen(s)) { if (len != strlen(s)) {
rb_raise(rb_eArgError, "string for Integer contains null byte"); rb_raise(rb_eArgError, "string for Integer contains null byte");
} }
@ -333,8 +350,8 @@ rb_big2str(x, base)
int base; int base;
{ {
VALUE t; VALUE t;
USHORT *ds; BDIGIT *ds;
unsigned long i, j, hbase; long i, j, hbase;
VALUE ss; VALUE ss;
char *s, c; char *s, c;
@ -344,19 +361,19 @@ rb_big2str(x, base)
i = RBIGNUM(x)->len; i = RBIGNUM(x)->len;
if (i == 0) return rb_str_new2("0"); if (i == 0) return rb_str_new2("0");
if (base == 10) { if (base == 10) {
j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i*241L)/800+2; j = (sizeof(BDIGIT)/sizeof(char)*CHAR_BIT*i*241L)/800+2;
hbase = 10000; hbase = 10000;
} }
else if (base == 16) { else if (base == 16) {
j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i)/4+2; j = (sizeof(BDIGIT)/sizeof(char)*CHAR_BIT*i)/4+2;
hbase = 0x10000; hbase = 0x10000;
} }
else if (base == 8) { else if (base == 8) {
j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i)+2; j = (sizeof(BDIGIT)/sizeof(char)*CHAR_BIT*i)+2;
hbase = 010000; hbase = 010000;
} }
else if (base == 2) { else if (base == 2) {
j = (sizeof(USHORT)*CHAR_BIT*i)+2; j = (sizeof(BDIGIT)*CHAR_BIT*i)+2;
hbase = 020; hbase = 020;
} }
else { else {
@ -373,10 +390,11 @@ rb_big2str(x, base)
s[0] = RBIGNUM(x)->sign ? '+' : '-'; s[0] = RBIGNUM(x)->sign ? '+' : '-';
while (i && j) { while (i && j) {
long k = i; long k = i;
unsigned long num = 0; BDIGIT2 num = 0;
while (k--) { while (k--) {
num = BIGUP(num) + ds[k]; num = BIGUP(num) + ds[k];
ds[k] = (USHORT)(num / hbase); ds[k] = (BDIGIT)(num / hbase);
num %= hbase; num %= hbase;
} }
if (ds[i-1] == 0) i--; if (ds[i-1] == 0) i--;
@ -408,11 +426,11 @@ big2ulong(x, type)
VALUE x; VALUE x;
char *type; char *type;
{ {
unsigned long num;
long len = RBIGNUM(x)->len; long len = RBIGNUM(x)->len;
USHORT *ds; BDIGIT2 num;
BDIGIT *ds;
if (len > sizeof(long)/sizeof(USHORT)) if (len > sizeof(long)/sizeof(BDIGIT))
rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type); rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
ds = BDIGITS(x); ds = BDIGITS(x);
num = 0; num = 0;
@ -450,9 +468,9 @@ static VALUE
dbl2big(d) dbl2big(d)
double d; double d;
{ {
unsigned long i = 0; long i = 0;
long c; long c;
USHORT *digits; BDIGIT *digits;
VALUE z; VALUE z;
double u = (d < 0)?-d:d; double u = (d < 0)?-d:d;
@ -473,7 +491,7 @@ dbl2big(d)
u *= BIGRAD; u *= BIGRAD;
c = (long)u; c = (long)u;
u -= c; u -= c;
digits[i] = (USHORT)c; digits[i] = (BDIGIT)c;
} }
return z; return z;
@ -492,7 +510,7 @@ rb_big2dbl(x)
{ {
double d = 0.0; double d = 0.0;
long i = RBIGNUM(x)->len; long i = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(x); BDIGIT *ds = BDIGITS(x);
while (i--) { while (i--) {
d = ds[i] + BIGRAD*d; d = ds[i] + BIGRAD*d;
@ -562,7 +580,7 @@ rb_big_eq(x, y)
} }
if (RBIGNUM(x)->sign != RBIGNUM(y)->sign) return Qfalse; if (RBIGNUM(x)->sign != RBIGNUM(y)->sign) return Qfalse;
if (RBIGNUM(x)->len != RBIGNUM(y)->len) return Qfalse; if (RBIGNUM(x)->len != RBIGNUM(y)->len) return Qfalse;
if (MEMCMP(BDIGITS(x),BDIGITS(y),USHORT,RBIGNUM(y)->len) != 0) return Qfalse; if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,RBIGNUM(y)->len) != 0) return Qfalse;
return Qtrue; return Qtrue;
} }
@ -583,7 +601,7 @@ rb_big_neg(x)
{ {
VALUE z = rb_big_clone(x); VALUE z = rb_big_clone(x);
long i = RBIGNUM(x)->len; long i = RBIGNUM(x)->len;
USHORT *ds = BDIGITS(z); BDIGIT *ds = BDIGITS(z);
if (!RBIGNUM(x)->sign) rb_big_2comp(z); if (!RBIGNUM(x)->sign) rb_big_2comp(z);
while (i--) ds[i] = ~ds[i]; while (i--) ds[i] = ~ds[i];
@ -598,8 +616,8 @@ bigsub(x, y)
VALUE x, y; VALUE x, y;
{ {
VALUE z = 0; VALUE z = 0;
USHORT *zds; BDIGIT *zds;
long num; BDIGIT2_SIGNED num;
long i; long i;
i = RBIGNUM(x)->len; i = RBIGNUM(x)->len;
@ -624,7 +642,7 @@ bigsub(x, y)
zds = BDIGITS(z); zds = BDIGITS(z);
for (i = 0, num = 0; i < RBIGNUM(y)->len; i++) { for (i = 0, num = 0; i < RBIGNUM(y)->len; i++) {
num += (long)BDIGITS(x)[i] - BDIGITS(y)[i]; num += (BDIGIT2_SIGNED)BDIGITS(x)[i] - BDIGITS(y)[i];
zds[i] = BIGLO(num); zds[i] = BIGLO(num);
num = BIGDN(num); num = BIGDN(num);
} }
@ -647,7 +665,7 @@ bigadd(x, y, sign)
char sign; char sign;
{ {
VALUE z; VALUE z;
long num; BDIGIT2 num;
long i, len; long i, len;
sign = (sign == RBIGNUM(y)->sign); sign = (sign == RBIGNUM(y)->sign);
@ -681,7 +699,7 @@ bigadd(x, y, sign)
BDIGITS(z)[i] = BDIGITS(y)[i]; BDIGITS(z)[i] = BDIGITS(y)[i];
i++; i++;
} }
BDIGITS(z)[i] = (USHORT)num; BDIGITS(z)[i] = (BDIGIT)num;
return z; return z;
} }
@ -729,9 +747,9 @@ rb_big_mul(x, y)
VALUE x, y; VALUE x, y;
{ {
long i, j; long i, j;
unsigned long n = 0; BDIGIT2 n = 0;
VALUE z; VALUE z;
USHORT *zds; BDIGIT *zds;
if (FIXNUM_P(x)) x = rb_int2big(FIX2LONG(x)); if (FIXNUM_P(x)) x = rb_int2big(FIX2LONG(x));
switch (TYPE(y)) { switch (TYPE(y)) {
@ -754,11 +772,11 @@ rb_big_mul(x, y)
zds = BDIGITS(z); zds = BDIGITS(z);
while (j--) zds[j] = 0; while (j--) zds[j] = 0;
for (i = 0; i < RBIGNUM(x)->len; i++) { for (i = 0; i < RBIGNUM(x)->len; i++) {
unsigned long dd = BDIGITS(x)[i]; BDIGIT2 dd = BDIGITS(x)[i];
if (dd == 0) continue; if (dd == 0) continue;
n = 0; n = 0;
for (j = 0; j < RBIGNUM(y)->len; j++) { for (j = 0; j < RBIGNUM(y)->len; j++) {
int ee = n + dd * BDIGITS(y)[j]; BDIGIT2 ee = n + (BDIGIT2)dd * BDIGITS(y)[j];
n = zds[i + j] + ee; n = zds[i + j] + ee;
if (ee) zds[i + j] = BIGLO(n); if (ee) zds[i + j] = BIGLO(n);
n = BIGDN(n); n = BIGDN(n);
@ -779,10 +797,10 @@ bigdivrem(x, y, divp, modp)
long nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len; long nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len;
long i, j; long i, j;
VALUE yy, z; VALUE yy, z;
USHORT *xds, *yds, *zds, *tds; BDIGIT *xds, *yds, *zds, *tds;
unsigned long t2; BDIGIT2 t2;
long num; BDIGIT2_SIGNED num;
USHORT dd, q; BDIGIT dd, q;
yds = BDIGITS(y); yds = BDIGITS(y);
if (ny == 0 && yds[0] == 0) rb_num_zerodiv(); if (ny == 0 && yds[0] == 0) rb_num_zerodiv();
@ -799,7 +817,7 @@ bigdivrem(x, y, divp, modp)
t2 = 0; i = nx; t2 = 0; i = nx;
while (i--) { while (i--) {
t2 = BIGUP(t2) + zds[i]; t2 = BIGUP(t2) + zds[i];
zds[i] = (USHORT)(t2 / dd); zds[i] = (BDIGIT)(t2 / dd);
t2 %= dd; t2 %= dd;
} }
RBIGNUM(z)->sign = RBIGNUM(x)->sign==RBIGNUM(y)->sign; RBIGNUM(z)->sign = RBIGNUM(x)->sign==RBIGNUM(y)->sign;
@ -812,13 +830,13 @@ bigdivrem(x, y, divp, modp)
zds = BDIGITS(z); zds = BDIGITS(z);
if (nx==ny) zds[nx+1] = 0; if (nx==ny) zds[nx+1] = 0;
while (!yds[ny-1]) ny--; while (!yds[ny-1]) ny--;
if ((dd = BIGRAD/(int)(yds[ny-1]+1)) != 1) { if ((dd = BIGRAD/(BDIGIT2_SIGNED)(yds[ny-1]+1)) != 1) {
yy = rb_big_clone(y); yy = rb_big_clone(y);
tds = BDIGITS(yy); tds = BDIGITS(yy);
j = 0; j = 0;
num = 0; num = 0;
while (j<ny) { while (j<ny) {
num += (long)yds[j]*dd; num += (BDIGIT2)yds[j]*dd;
tds[j++] = BIGLO(num); tds[j++] = BIGLO(num);
num = BIGDN(num); num = BIGDN(num);
} }
@ -826,11 +844,11 @@ bigdivrem(x, y, divp, modp)
j = 0; j = 0;
num = 0; num = 0;
while (j<nx) { while (j<nx) {
num += (long)xds[j]*dd; num += (BDIGIT2)xds[j]*dd;
zds[j++] = BIGLO(num); zds[j++] = BIGLO(num);
num = BIGDN(num); num = BIGDN(num);
} }
zds[j] = (USHORT)num; zds[j] = (BDIGIT)num;
} }
else { else {
zds[nx] = 0; zds[nx] = 0;
@ -840,12 +858,12 @@ bigdivrem(x, y, divp, modp)
j = nx==ny?nx+1:nx; j = nx==ny?nx+1:nx;
do { do {
if (zds[j] == yds[ny-1]) q = BIGRAD-1; if (zds[j] == yds[ny-1]) q = BIGRAD-1;
else q = (USHORT)((BIGUP(zds[j]) + zds[j-1])/yds[ny-1]); else q = (BDIGIT)((BIGUP(zds[j]) + zds[j-1])/yds[ny-1]);
if (q) { if (q) {
i = 0; num = 0; t2 = 0; i = 0; num = 0; t2 = 0;
do { /* multiply and subtract */ do { /* multiply and subtract */
int ee; BDIGIT2 ee;
t2 += (long)yds[i] * q; t2 += (BDIGIT2)yds[i] * q;
ee = num - BIGLO(t2); ee = num - BIGLO(t2);
num = zds[j - ny + i] + ee; num = zds[j - ny + i] + ee;
if (ee) zds[j - ny + i] = BIGLO(num); if (ee) zds[j - ny + i] = BIGLO(num);
@ -856,8 +874,8 @@ bigdivrem(x, y, divp, modp)
while (num) { /* "add back" required */ while (num) { /* "add back" required */
i = 0; num = 0; q--; i = 0; num = 0; q--;
do { do {
int ee = num + yds[i]; BDIGIT2 ee = num + yds[i];
num = (long) zds[j - ny + i] + ee; num = (BDIGIT2)zds[j - ny + i] + ee;
if (ee) zds[j - ny + i] = BIGLO(num); if (ee) zds[j - ny + i] = BIGLO(num);
num = BIGDN(num); num = BIGDN(num);
} while (++i < ny); } while (++i < ny);
@ -880,7 +898,7 @@ bigdivrem(x, y, divp, modp)
t2 = 0; i = ny; t2 = 0; i = ny;
while(i--) { while(i--) {
t2 = BIGUP(t2) + zds[i]; t2 = BIGUP(t2) + zds[i];
zds[i] = (USHORT)(t2 / dd); zds[i] = (BDIGIT)(t2 / dd);
t2 %= dd; t2 %= dd;
} }
} }
@ -1049,7 +1067,7 @@ rb_big_and(x, y)
VALUE x, y; VALUE x, y;
{ {
VALUE z; VALUE z;
USHORT *ds1, *ds2, *zds; BDIGIT *ds1, *ds2, *zds;
long i, l1, l2; long i, l1, l2;
char sign; char sign;
@ -1100,8 +1118,8 @@ rb_big_or(x, y)
VALUE x, y; VALUE x, y;
{ {
VALUE z; VALUE z;
USHORT *ds1, *ds2, *zds; BDIGIT *ds1, *ds2, *zds;
unsigned long i, l1, l2; long i, l1, l2;
char sign; char sign;
if (FIXNUM_P(y)) { if (FIXNUM_P(y)) {
@ -1152,8 +1170,8 @@ rb_big_xor(x, y)
VALUE x, y; VALUE x, y;
{ {
VALUE z; VALUE z;
USHORT *ds1, *ds2, *zds; BDIGIT *ds1, *ds2, *zds;
unsigned int i, l1, l2; long i, l1, l2;
char sign; char sign;
if (FIXNUM_P(y)) { if (FIXNUM_P(y)) {
@ -1207,12 +1225,12 @@ VALUE
rb_big_lshift(x, y) rb_big_lshift(x, y)
VALUE x, y; VALUE x, y;
{ {
USHORT *xds, *zds; BDIGIT *xds, *zds;
int shift = NUM2INT(y); int shift = NUM2INT(y);
int s1 = shift/BITSPERDIG; int s1 = shift/BITSPERDIG;
int s2 = shift%BITSPERDIG; int s2 = shift%BITSPERDIG;
VALUE z; VALUE z;
unsigned long num = 0; BDIGIT2 num = 0;
long len, i; long len, i;
if (shift < 0) return rb_big_rshift(x, INT2FIX(-shift)); if (shift < 0) return rb_big_rshift(x, INT2FIX(-shift));
@ -1236,12 +1254,12 @@ static VALUE
rb_big_rshift(x, y) rb_big_rshift(x, y)
VALUE x, y; VALUE x, y;
{ {
USHORT *xds, *zds; BDIGIT *xds, *zds;
int shift = NUM2INT(y); int shift = NUM2INT(y);
int s1 = shift/BITSPERDIG; int s1 = shift/BITSPERDIG;
int s2 = shift%BITSPERDIG; int s2 = shift%BITSPERDIG;
VALUE z; VALUE z;
unsigned long num = 0; BDIGIT2 num = 0;
long i = RBIGNUM(x)->len; long i = RBIGNUM(x)->len;
long j; long j;
@ -1268,7 +1286,7 @@ static VALUE
rb_big_aref(x, y) rb_big_aref(x, y)
VALUE x, y; VALUE x, y;
{ {
USHORT *xds; BDIGIT *xds;
int shift = NUM2INT(y); int shift = NUM2INT(y);
int s1, s2; int s1, s2;
@ -1294,12 +1312,11 @@ static VALUE
rb_big_hash(x) rb_big_hash(x)
VALUE x; VALUE x;
{ {
long i, len; long i, len, key;
int key; BDIGIT *digits;
USHORT *digits;
key = 0; digits = BDIGITS(x); key = 0; digits = BDIGITS(x); len = RBIGNUM(x)->len;
for (i=0,len=RBIGNUM(x)->len; i<RBIGNUM(x)->len; i++) { for (i=0; i<len; i++) {
key ^= *digits++; key ^= *digits++;
} }
return INT2FIX(key); return INT2FIX(key);
@ -1346,7 +1363,7 @@ rb_big_rand(max, rand)
len = RBIGNUM(max)->len; len = RBIGNUM(max)->len;
v = bignew(len,1); v = bignew(len,1);
while (len--) { while (len--) {
BDIGITS(v)[len] = ((USHORT)~0) * rand; BDIGITS(v)[len] = ((BDIGIT)~0) * rand;
} }
return rb_big_modulo((VALUE)v, max); return rb_big_modulo((VALUE)v, max);
@ -1356,7 +1373,7 @@ static VALUE
rb_big_size(big) rb_big_size(big)
VALUE big; VALUE big;
{ {
return INT2FIX(RBIGNUM(big)->len*sizeof(USHORT)); return INT2FIX(RBIGNUM(big)->len*sizeof(BDIGIT));
} }
static VALUE static VALUE

View File

@ -112,6 +112,7 @@ AC_OBJEXT
AC_CHECK_SIZEOF(int, 4) AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(short, 2) AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(long, 4) AC_CHECK_SIZEOF(long, 4)
AC_CHECK_SIZEOF(long long, 0)
AC_CHECK_SIZEOF(void*, 4) AC_CHECK_SIZEOF(void*, 4)
AC_CHECK_SIZEOF(float, 4) AC_CHECK_SIZEOF(float, 4)
AC_CHECK_SIZEOF(double, 8) AC_CHECK_SIZEOF(double, 8)

4
gc.c
View File

@ -80,7 +80,7 @@ ruby_xmalloc(size)
mem = malloc(size); mem = malloc(size);
if (!mem) { if (!mem) {
if (size >= 10 * 1024 * 1024) { if (size >= 10 * 1024 * 1024) {
rb_raise(rb_eNoMemError, "tryed to allocate too big memory"); rb_raise(rb_eNoMemError, "tried to allocate too big memory");
} }
mem_error("failed to allocate memory"); mem_error("failed to allocate memory");
} }
@ -120,7 +120,7 @@ ruby_xrealloc(ptr, size)
mem = realloc(ptr, size); mem = realloc(ptr, size);
if (!mem) if (!mem)
if (size >= 50 * 1024 * 1024) { if (size >= 50 * 1024 * 1024) {
rb_raise(rb_eNoMemError, "tryed to re-allocate too big memory"); rb_raise(rb_eNoMemError, "tried to re-allocate too big memory");
} }
mem_error("failed to allocate memory(realloc)"); mem_error("failed to allocate memory(realloc)");
} }

View File

@ -18,6 +18,42 @@
double strtod(); double strtod();
#endif #endif
#if SIZEOF_LONG*2 <= SIZEOF_LONG_LONG
typedef unsigned long BDIGIT;
#define SIZEOF_BDIGITS SIZEOF_LONG
#elif SIZEOF_INT*2 <= SIZEOF_LONG_LONG
typedef unsigned int BDIGIT;
#define SIZEOF_BDIGITS SIZEOF_INT
#else
typedef unsigned short BDIGIT;
#define SIZEOF_BDIGITS SIZEOF_SHORT
#endif
#define BITSPERSHORT (sizeof(short)*CHAR_BIT)
#define SHORTMASK ((1<<BITSPERSHORT)-1)
#define SHORTDN(x) RSHIFT(x,BITSPERSHORT)
#if SIZEOF_SHORT == SIZEOF_BDIGITS
#define SHORTLEN(x) (x)
#else
static int
shortlen(len, ds)
long len;
BDIGIT *ds;
{
BDIGIT num;
int offset = 0;
num = ds[len-1];
while (num) {
num = SHORTDN(num);
offset++;
}
return len*sizeof(BDIGIT)/sizeof(short) - offset;
}
#define SHORTLEN(x) shortlen((x),d)
#endif
#define MARSHAL_MAJOR 4 #define MARSHAL_MAJOR 4
#define MARSHAL_MINOR 4 #define MARSHAL_MINOR 4
@ -318,12 +354,23 @@ w_object(obj, arg, limit)
{ {
char sign = RBIGNUM(obj)->sign?'+':'-'; char sign = RBIGNUM(obj)->sign?'+':'-';
int len = RBIGNUM(obj)->len; int len = RBIGNUM(obj)->len;
unsigned short *d = RBIGNUM(obj)->digits; BDIGIT *d = RBIGNUM(obj)->digits;
w_byte(sign, arg); w_byte(sign, arg);
w_long(len, arg); w_long(SHORTLEN(len), arg);
while (len--) { while (len--) {
#if SIZEOF_BDIGITS > SIZEOF_SHORT
BDIGIT num = *d;
int i;
for (i=0; i<SIZEOF_BDIGITS; i+=sizeof(short)) {
w_short(num & SHORTMASK, arg);
num = SHORTDN(num);
if (num == 0) break;
}
#else
w_short(*d, arg); w_short(*d, arg);
#endif
d++; d++;
} }
} }
@ -752,15 +799,31 @@ r_object(arg)
case TYPE_BIGNUM: case TYPE_BIGNUM:
{ {
int len; int len;
unsigned short *digits; BDIGIT *digits;
NEWOBJ(big, struct RBignum); NEWOBJ(big, struct RBignum);
OBJSETUP(big, rb_cBignum, T_BIGNUM); OBJSETUP(big, rb_cBignum, T_BIGNUM);
big->sign = (r_byte(arg) == '+'); big->sign = (r_byte(arg) == '+');
big->len = len = r_long(arg); len = r_long(arg);
big->digits = digits = ALLOC_N(unsigned short, len); big->len = (len + 1) * sizeof(short) / sizeof(BDIGIT);
while (len--) { big->digits = digits = ALLOC_N(BDIGIT, big->len);
while (len > 0) {
#if SIZEOF_BDIGITS > SIZEOF_SHORT
BDIGIT num = 0;
int shift = 0;
int i;
for (i=0; i<SIZEOF_BDIGITS; i+=sizeof(short)) {
int j = r_short(arg);
num |= j << shift;
shift += BITSPERSHORT;
if (--len == 0) break;
}
*digits++ = num;
#else
*digits++ = r_short(arg); *digits++ = r_short(arg);
len--;
#endif
} }
big = RBIGNUM(rb_big_norm((VALUE)big)); big = RBIGNUM(rb_big_norm((VALUE)big));
if (TYPE(big) == T_BIGNUM) { if (TYPE(big) == T_BIGNUM) {

View File

@ -2863,7 +2863,8 @@ yylex()
return 0; return 0;
} }
if (c != '=') continue; if (c != '=') continue;
if (strncmp(lex_p, "end", 3) == 0 && ISSPACE(lex_p[3])) { if (strncmp(lex_p, "end", 3) == 0 &&
(lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) {
break; break;
} }
} }

View File

@ -385,6 +385,18 @@ $z = [1,2]
$y[$z] = 256 $y[$z] = 256
test_ok($y[$z] == 256) test_ok($y[$z] == 256)
$x = [1,2,3]
$x[1,0] = $x
test_ok($x == [1,1,2,3,2,3])
$x = [1,2,3]
$x[-1,0] = $x
test_ok($x == [1,2,1,2,3,3])
$x = [1,2,3]
$x.concat($x)
test_ok($x == [1,2,3,1,2,3])
test_check "iterator" test_check "iterator"
test_ok(!iterator?) test_ok(!iterator?)
@ -571,7 +583,6 @@ def fact(n)
end end
return f return f
end end
fact(3)
$x = fact(40) $x = fact(40)
test_ok($x == $x) test_ok($x == $x)
test_ok($x == fact(40)) test_ok($x == fact(40))
@ -613,6 +624,7 @@ $good = true;
for i in 4000..4096 for i in 4000..4096
n1 = 1 << i; n1 = 1 << i;
if (n1**2-1) / (n1+1) != (n1-1) if (n1**2-1) / (n1+1) != (n1-1)
p i
$good = false $good = false
end end
end end

View File

@ -1732,18 +1732,22 @@ mystrerror(int e)
extern int sys_nerr; extern int sys_nerr;
#endif #endif
DWORD source = 0; DWORD source = 0;
char *p;
if (e < 0 || e > sys_nerr) { if (e < 0 || e > sys_nerr) {
if (e < 0) if (e < 0)
e = GetLastError(); e = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0, if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
buffer, 512, NULL) == 0) { buffer, 512, NULL) == 0) {
strcpy (buffer, "Unknown Error"); strcpy(buffer, "Unknown Error");
}
for (p = buffer + strlen(buffer) - 1; buffer <= p; p--) {
if (*p != '\r' && *p != '\n') break;
*p = 0;
} }
return buffer; return buffer;
} }
return strerror(e); return strerror(e);
} }
// //