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

ruby-changes:29262

From: akr <ko1@a...>
Date: Sat, 15 Jun 2013 23:52:13 +0900 (JST)
Subject: [ruby-changes:29262] akr:r41314 (trunk): * bignum.c (bary_sub): New function.

akr	2013-06-15 23:52:02 +0900 (Sat, 15 Jun 2013)

  New Revision: 41314

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

  Log:
    * bignum.c (bary_sub): New function.
      (absint_numwords_generic): Use bary_sub.
      (bigsub_core): Skip unnecessary copy.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41313)
+++ ChangeLog	(revision 41314)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jun 15 23:50:24 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (bary_sub): New function.
+	  (absint_numwords_generic): Use bary_sub.
+	  (bigsub_core): Skip unnecessary copy.
+
 Sat Jun 15 22:05:30 2013  Tanaka Akira  <akr@f...>
 
 	* bignum.c (bary_mul): New function.
Index: bignum.c
===================================================================
--- bignum.c	(revision 41313)
+++ bignum.c	(revision 41314)
@@ -59,6 +59,7 @@ static BDIGIT bdigs_small_lshift(BDIGIT https://github.com/ruby/ruby/blob/trunk/bignum.c#L59
 static void bdigs_small_rshift(BDIGIT *zds, BDIGIT *xds, long n, int shift, int sign_bit);
 static void bary_unpack(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
 static void bary_mul(BDIGIT *zds, size_t zl, BDIGIT *xds, size_t xl, BDIGIT *yds, size_t yl);
+static void bary_sub(BDIGIT *zds, size_t zn, BDIGIT *xds, size_t xn, BDIGIT *yds, size_t yn);
 
 #define BIGNUM_DEBUG 0
 #if BIGNUM_DEBUG
@@ -599,6 +600,7 @@ absint_numwords_generic(size_t numbytes, https://github.com/ruby/ruby/blob/trunk/bignum.c#L600
     BDIGIT numbytes_bary[bdigit_roomof(sizeof(numbytes))];
     BDIGIT char_bit[1] = { CHAR_BIT };
     BDIGIT val_numbits_bary[bdigit_roomof(sizeof(numbytes) + 1)];
+    BDIGIT nlz_bits_in_msbyte_bary[1] = { nlz_bits_in_msbyte };
     VALUE v;
 
     /*
@@ -611,15 +613,17 @@ absint_numwords_generic(size_t numbytes, https://github.com/ruby/ruby/blob/trunk/bignum.c#L613
     bary_unpack(BARY_ARGS(numbytes_bary), &numbytes, 1, sizeof(numbytes), 0,
         INTEGER_PACK_NATIVE_BYTE_ORDER);
     bary_mul(BARY_ARGS(val_numbits_bary), BARY_ARGS(numbytes_bary), BARY_ARGS(char_bit));
+    if (nlz_bits_in_msbyte)
+        bary_sub(BARY_ARGS(val_numbits_bary), BARY_ARGS(val_numbits_bary), BARY_ARGS(nlz_bits_in_msbyte_bary));
 
     v = rb_integer_unpack(val_numbits_bary, numberof(val_numbits_bary), sizeof(BDIGIT), 0,
             INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
 
     val_numbits = SIZET2NUM(numbytes);
     val_numbits = rb_funcall(val_numbits, '*', 1, LONG2FIX(CHAR_BIT));
-    assert(rb_equal(val_numbits, v));
     if (nlz_bits_in_msbyte)
         val_numbits = rb_funcall(val_numbits, '-', 1, LONG2FIX(nlz_bits_in_msbyte));
+    assert(rb_equal(val_numbits, v));
     word_numbits_v = SIZET2NUM(word_numbits);
     div_mod = rb_funcall(val_numbits, rb_intern("divmod"), 1, word_numbits_v);
     div = RARRAY_AREF(div_mod, 0);
@@ -2850,6 +2854,8 @@ bigsub_core(BDIGIT *xds, long xn, BDIGIT https://github.com/ruby/ruby/blob/trunk/bignum.c#L2854
 	zds[i++] = BIGLO(num);
 	num = BIGDN(num);
     }
+    if (xds == zds && xn == zn)
+        return;
     while (i < xn) {
 	zds[i] = xds[i];
 	i++;
@@ -2860,6 +2866,15 @@ bigsub_core(BDIGIT *xds, long xn, BDIGIT https://github.com/ruby/ruby/blob/trunk/bignum.c#L2866
     }
 }
 
+static void
+bary_sub(BDIGIT *zds, size_t zn, BDIGIT *xds, size_t xn, BDIGIT *yds, size_t yn)
+{
+    assert(yn <= xn);
+    assert(xn <= zn);
+
+    bigsub_core(xds, xn, yds, yn, zds, zn);
+}
+
 static VALUE
 bigsub(VALUE x, VALUE y)
 {

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

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