* bignum.c (bigdivrem): Refactored to use ALLOCV_N for temporally

buffers.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41278 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-13 12:26:33 +00:00
parent 138bc4c0c4
commit 63dd3e7ef9
2 changed files with 28 additions and 21 deletions

View File

@ -1,3 +1,8 @@
Thu Jun 13 21:24:09 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (bigdivrem): Refactored to use ALLOCV_N for temporally
buffers.
Thu Jun 13 18:54:11 2013 NAKAMURA Usaku <usa@ruby-lang.org> Thu Jun 13 18:54:11 2013 NAKAMURA Usaku <usa@ruby-lang.org>
* bignum.c (integer_unpack_num_bdigits_generic): reorder terms (but not * bignum.c (integer_unpack_num_bdigits_generic): reorder terms (but not

View File

@ -3754,16 +3754,19 @@ static VALUE
bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp) bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
{ {
struct big_div_struct bds; struct big_div_struct bds;
long nx = RBIGNUM_LEN(x), ny = RBIGNUM_LEN(y); long nx = RBIGNUM_LEN(x), ny = RBIGNUM_LEN(y), nz;
long i, j; long i, j;
VALUE z, yy, zz; VALUE z, zz;
BDIGIT *xds, *yds, *zds, *tds; VALUE tmpy = 0, tmpz = 0;
BDIGIT *xds, *yds, *zds, *tds, *qds;
BDIGIT_DBL t2; BDIGIT_DBL t2;
BDIGIT dd, q; BDIGIT dd, q;
if (BIGZEROP(y)) rb_num_zerodiv(); if (BIGZEROP(y)) rb_num_zerodiv();
xds = BDIGITS(x); xds = BDIGITS(x);
yds = BDIGITS(y); yds = BDIGITS(y);
while (0 < nx && !xds[nx-1]) nx--;
while (!yds[ny-1]) ny--;
if (nx < ny || (nx == ny && xds[nx - 1] < yds[ny - 1])) { if (nx < ny || (nx == ny && xds[nx - 1] < yds[ny - 1])) {
if (divp) *divp = rb_int2big(0); if (divp) *divp = rb_int2big(0);
if (modp) *modp = x; if (modp) *modp = x;
@ -3788,17 +3791,15 @@ bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
return Qnil; return Qnil;
} }
z = bignew(nx==ny?nx+2:nx+1, RBIGNUM_SIGN(x)==RBIGNUM_SIGN(y)); nz = nx==ny ? nx+2 : nx+1;
zds = BDIGITS(z); zds = ALLOCV_N(BDIGIT, tmpz, nz);
if (nx==ny) zds[nx+1] = 0; if (nx==ny) zds[nx+1] = 0;
while (!yds[ny-1]) ny--;
q = yds[ny-1]; q = yds[ny-1];
dd = nlz(q); dd = nlz(q);
q <<= dd; q <<= dd;
if (dd) { if (dd) {
yy = rb_big_clone(y); tds = ALLOCV_N(BDIGIT, tmpy, RBIGNUM_LEN(y));
tds = BDIGITS(yy);
j = 0; j = 0;
t2 = 0; t2 = 0;
while (j<ny) { while (j<ny) {
@ -3807,7 +3808,6 @@ bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
t2 = BIGDN(t2); t2 = BIGDN(t2);
} }
yds = tds; yds = tds;
RB_GC_GUARD(y) = yy;
j = 0; j = 0;
t2 = 0; t2 = 0;
while (j<nx) { while (j<nx) {
@ -3828,7 +3828,7 @@ bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
bds.zds = zds; bds.zds = zds;
bds.yds = yds; bds.yds = yds;
bds.stop = Qfalse; bds.stop = Qfalse;
bds.j = nx==ny?nx+1:nx; bds.j = nz - 1;
for (bds.nyzero = 0; !yds[bds.nyzero]; bds.nyzero++); for (bds.nyzero = 0; !yds[bds.nyzero]; bds.nyzero++);
if (nx > 10000 || ny > 10000) { if (nx > 10000 || ny > 10000) {
retry: retry:
@ -3845,16 +3845,14 @@ bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
} }
if (divp) { /* move quotient down in z */ if (divp) { /* move quotient down in z */
*divp = zz = rb_big_clone(z); j = nz - ny;
zds = BDIGITS(zz); *divp = zz = bignew(j, RBIGNUM_SIGN(x)==RBIGNUM_SIGN(y));
j = (nx==ny ? nx+2 : nx+1) - ny; qds = BDIGITS(zz);
for (i = 0;i < j;i++) zds[i] = zds[i+ny]; for (i = 0;i < j;i++) qds[i] = zds[i+ny];
if (!zds[i-1]) i--; if (!qds[i-1])
RBIGNUM_SET_LEN(zz, i); RBIGNUM_SET_LEN(zz, i-1);
} }
if (modp) { /* normalize remainder */ if (modp) { /* normalize remainder */
*modp = zz = rb_big_clone(z);
zds = BDIGITS(zz);
while (ny > 1 && !zds[ny-1]) --ny; while (ny > 1 && !zds[ny-1]) --ny;
if (dd) { if (dd) {
t2 = 0; i = ny; t2 = 0; i = ny;
@ -3866,10 +3864,14 @@ bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
} }
} }
if (!zds[ny-1]) ny--; if (!zds[ny-1]) ny--;
RBIGNUM_SET_LEN(zz, ny); *modp = zz = bignew(ny, RBIGNUM_SIGN(x));
RBIGNUM_SET_SIGN(zz, RBIGNUM_SIGN(x)); MEMCPY(BDIGITS(zz), zds, BDIGIT, ny);
} }
return z; if (tmpy)
ALLOCV_END(tmpy);
if (tmpz)
ALLOCV_END(tmpz);
return Qnil;
} }
static void static void