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

ruby-changes:29149

From: akr <ko1@a...>
Date: Mon, 10 Jun 2013 05:39:19 +0900 (JST)
Subject: [ruby-changes:29149] akr:r41201 (trunk): * bignum.c (absint_numwords_small): New function.

akr	2013-06-10 05:39:05 +0900 (Mon, 10 Jun 2013)

  New Revision: 41201

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

  Log:
    * bignum.c (absint_numwords_small): New function.
      (absint_numwords_generic): Use absint_numwords_small if possible.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41200)
+++ ChangeLog	(revision 41201)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jun 10 05:38:23 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (absint_numwords_small): New function.
+	  (absint_numwords_generic): Use absint_numwords_small if possible.
+
 Mon Jun 10 01:07:57 2013  Tanaka Akira  <akr@f...>
 
 	* bignum.c (absint_numwords_bytes): New function.
Index: bignum.c
===================================================================
--- bignum.c	(revision 41200)
+++ bignum.c	(revision 41201)
@@ -555,7 +555,7 @@ absint_numwords_bytes(size_t numbytes, i https://github.com/ruby/ruby/blob/trunk/bignum.c#L555
     }
     else {
         s = q - 1;
-        t = word_numbits + r * CHAR_BIT - nlz_bits_in_msbyte;
+        t = word_numbits - nlz_bits_in_msbyte + r * CHAR_BIT;
     }
     div = s + t / word_numbits;
     mod = t % word_numbits;
@@ -566,6 +566,20 @@ absint_numwords_bytes(size_t numbytes, i https://github.com/ruby/ruby/blob/trunk/bignum.c#L566
 }
 
 size_t
+absint_numwords_small(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
+{
+    size_t val_numbits = numbytes * CHAR_BIT - nlz_bits_in_msbyte;
+    size_t div = val_numbits / word_numbits;
+    size_t mod = val_numbits % word_numbits;
+    size_t numwords;
+    size_t nlz_bits;
+    numwords = mod == 0 ? div : div + 1;
+    nlz_bits = mod == 0 ? 0 : word_numbits - mod;
+    *nlz_bits_ret = nlz_bits;
+    return numwords;
+}
+
+size_t
 absint_numwords_generic(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
 {
     VALUE val_numbits, word_numbits_v;
@@ -634,7 +648,16 @@ rb_absint_numwords(VALUE val, size_t wor https://github.com/ruby/ruby/blob/trunk/bignum.c#L648
 
     numbytes = rb_absint_size(val, &nlz_bits_in_msbyte);
 
-    if (word_numbits % CHAR_BIT == 0) {
+    if (numbytes <= SIZE_MAX / CHAR_BIT) {
+        numwords = absint_numwords_small(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits);
+#if 0
+        size_t numwords0, nlz_bits0;
+        numwords0 = absint_numwords_generic(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits0);
+        assert(numwords0 == numwords);
+        assert(nlz_bits0 == nlz_bits);
+#endif
+    }
+    else if (word_numbits % CHAR_BIT == 0) {
         numwords = absint_numwords_bytes(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits);
 #if 0
         size_t numwords0, nlz_bits0;

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

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