1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* bignum.c (rb_big_pow): truncate all zero BDIGITs. [ruby-dev:30733]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12224 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2007-04-26 15:02:57 +00:00
parent 93bdc98f33
commit a379112975
2 changed files with 38 additions and 20 deletions

View file

@ -1,3 +1,7 @@
Fri Apr 27 00:03:48 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* bignum.c (rb_big_pow): truncate all zero BDIGITs. [ruby-dev:30733]
Thu Apr 26 17:31:00 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* bignum.c (rb_big_pow): reduce multiplying for even number.

View file

@ -90,34 +90,48 @@ rb_big_2comp(VALUE x) /* get 2's complement */
}
static VALUE
bignorm(VALUE x)
bigtrunc(VALUE x)
{
if (FIXNUM_P(x)) {
return x;
}
else if (TYPE(x) == T_BIGNUM) {
long len = RBIGNUM(x)->len;
BDIGIT *ds = BDIGITS(x);
long len = RBIGNUM(x)->len;
BDIGIT *ds = BDIGITS(x);
while (len-- && !ds[len]) ;
RBIGNUM(x)->len = ++len;
while (len-- && !ds[len]);
RBIGNUM(x)->len = ++len;
return x;
}
if (len*SIZEOF_BDIGITS <= sizeof(VALUE)) {
SIGNED_VALUE num = 0;
while (len--) {
num = BIGUP(num) + ds[len];
static VALUE
bigfixize(VALUE x)
{
long len = RBIGNUM(x)->len;
BDIGIT *ds = BDIGITS(x);
if (len*SIZEOF_BDIGITS <= sizeof(VALUE)) {
SIGNED_VALUE num = 0;
while (len--) {
num = BIGUP(num) + ds[len];
}
if (num >= 0) {
if (RBIGNUM(x)->sign) {
if (POSFIXABLE(num)) return LONG2FIX(num);
}
if (num >= 0) {
if (RBIGNUM(x)->sign) {
if (POSFIXABLE(num)) return LONG2FIX(num);
}
else if (NEGFIXABLE(-(long)num)) return LONG2FIX(-(long)num);
else {
if (NEGFIXABLE(-(long)num)) return LONG2FIX(-(long)num);
}
}
}
return x;
}
static VALUE
bignorm(VALUE x)
{
if (!FIXNUM_P(x) && TYPE(x) == T_BIGNUM) {
x = bigfixize(bigtrunc(x));
}
return x;
}
VALUE
rb_big_norm(VALUE x)
{
@ -1563,10 +1577,10 @@ rb_big_pow(VALUE x, VALUE y)
do {
yy /= 2;
x = rb_big_mul0(x, x);
if (!BDIGITS(x)[RBIGNUM(x)->len-1]) RBIGNUM(x)->len--;
bigtrunc(x);
} while (yy % 2 == 0);
z = z ? rb_big_mul0(z, x) : x;
if (!BDIGITS(z)[RBIGNUM(z)->len-1]) RBIGNUM(z)->len--;
bigtrunc(z);
}
return bignorm(z);
}