[前][次][番号順一覧][スレッド一覧]

ruby-changes:29226

From: akr <ko1@a...>
Date: Thu, 13 Jun 2013 21:27:30 +0900 (JST)
Subject: [ruby-changes:29226] akr:r41278 (trunk): * bignum.c (bigdivrem): Refactored to use ALLOCV_N for temporally

akr	2013-06-13 21:26:33 +0900 (Thu, 13 Jun 2013)

  New Revision: 41278

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41278

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

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41277)
+++ ChangeLog	(revision 41278)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jun 13 21:24:09 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (bigdivrem): Refactored to use ALLOCV_N for temporally
+	  buffers.
+
 Thu Jun 13 18:54:11 2013  NAKAMURA Usaku  <usa@r...>
 
 	* bignum.c (integer_unpack_num_bdigits_generic): reorder terms (but not
Index: bignum.c
===================================================================
--- bignum.c	(revision 41277)
+++ bignum.c	(revision 41278)
@@ -3754,16 +3754,19 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/bignum.c#L3754
 bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
 {
     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;
-    VALUE z, yy, zz;
-    BDIGIT *xds, *yds, *zds, *tds;
+    VALUE z, zz;
+    VALUE tmpy = 0, tmpz = 0;
+    BDIGIT *xds, *yds, *zds, *tds, *qds;
     BDIGIT_DBL t2;
     BDIGIT dd, q;
 
     if (BIGZEROP(y)) rb_num_zerodiv();
     xds = BDIGITS(x);
     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 (divp) *divp = rb_int2big(0);
 	if (modp) *modp = x;
@@ -3788,17 +3791,15 @@ bigdivrem(VALUE x, VALUE y, volatile VAL https://github.com/ruby/ruby/blob/trunk/bignum.c#L3791
 	return Qnil;
     }
 
-    z = bignew(nx==ny?nx+2:nx+1, RBIGNUM_SIGN(x)==RBIGNUM_SIGN(y));
-    zds = BDIGITS(z);
+    nz = nx==ny ? nx+2 : nx+1;
+    zds = ALLOCV_N(BDIGIT, tmpz, nz);
     if (nx==ny) zds[nx+1] = 0;
-    while (!yds[ny-1]) ny--;
 
     q = yds[ny-1];
     dd = nlz(q);
     q <<= dd;
     if (dd) {
-	yy = rb_big_clone(y);
-	tds = BDIGITS(yy);
+        tds = ALLOCV_N(BDIGIT, tmpy, RBIGNUM_LEN(y));
 	j = 0;
 	t2 = 0;
 	while (j<ny) {
@@ -3807,7 +3808,6 @@ bigdivrem(VALUE x, VALUE y, volatile VAL https://github.com/ruby/ruby/blob/trunk/bignum.c#L3808
 	    t2 = BIGDN(t2);
 	}
 	yds = tds;
-	RB_GC_GUARD(y) = yy;
 	j = 0;
 	t2 = 0;
 	while (j<nx) {
@@ -3828,7 +3828,7 @@ bigdivrem(VALUE x, VALUE y, volatile VAL https://github.com/ruby/ruby/blob/trunk/bignum.c#L3828
     bds.zds = zds;
     bds.yds = yds;
     bds.stop = Qfalse;
-    bds.j = nx==ny?nx+1:nx;
+    bds.j = nz - 1;
     for (bds.nyzero = 0; !yds[bds.nyzero]; bds.nyzero++);
     if (nx > 10000 || ny > 10000) {
       retry:
@@ -3845,16 +3845,14 @@ bigdivrem(VALUE x, VALUE y, volatile VAL https://github.com/ruby/ruby/blob/trunk/bignum.c#L3845
     }
 
     if (divp) {			/* move quotient down in z */
-	*divp = zz = rb_big_clone(z);
-	zds = BDIGITS(zz);
-	j = (nx==ny ? nx+2 : nx+1) - ny;
-	for (i = 0;i < j;i++) zds[i] = zds[i+ny];
-	if (!zds[i-1]) i--;
-	RBIGNUM_SET_LEN(zz, i);
+        j = nz - ny;
+	*divp = zz = bignew(j, RBIGNUM_SIGN(x)==RBIGNUM_SIGN(y));
+	qds = BDIGITS(zz);
+	for (i = 0;i < j;i++) qds[i] = zds[i+ny];
+	if (!qds[i-1])
+            RBIGNUM_SET_LEN(zz, i-1);
     }
     if (modp) {			/* normalize remainder */
-	*modp = zz = rb_big_clone(z);
-	zds = BDIGITS(zz);
 	while (ny > 1 && !zds[ny-1]) --ny;
 	if (dd) {
 	    t2 = 0; i = ny;
@@ -3866,10 +3864,14 @@ bigdivrem(VALUE x, VALUE y, volatile VAL https://github.com/ruby/ruby/blob/trunk/bignum.c#L3864
 	    }
 	}
 	if (!zds[ny-1]) ny--;
-	RBIGNUM_SET_LEN(zz, ny);
-	RBIGNUM_SET_SIGN(zz, RBIGNUM_SIGN(x));
+	*modp = zz = bignew(ny, 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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]