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

ruby-changes:30289

From: akr <ko1@a...>
Date: Sat, 3 Aug 2013 00:55:18 +0900 (JST)
Subject: [ruby-changes:30289] akr:r42341 (trunk): * bignum.c (rb_big2str1): Simplify power_level calculation.

akr	2013-08-03 00:55:07 +0900 (Sat, 03 Aug 2013)

  New Revision: 42341

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

  Log:
    * bignum.c (rb_big2str1): Simplify power_level calculation.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42340)
+++ ChangeLog	(revision 42341)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Aug  3 00:52:43 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (rb_big2str1): Simplify power_level calculation.
+
 Sat Aug  3 00:34:20 2013  Masaki Matsushita  <glass.saga@g...>
 
 	* array.c (rb_ary_zip): use rb_ary_new2() to create buffer
Index: bignum.c
===================================================================
--- bignum.c	(revision 42340)
+++ bignum.c	(revision 42341)
@@ -4308,7 +4308,9 @@ big2str_karatsuba(struct big2str_struct https://github.com/ruby/ruby/blob/trunk/bignum.c#L4308
 
     /*
      * Precondition:
-     * abs(x) < maxpow_in_bdigit_dbl(base, &numdigits)**(2**power_level)
+     * abs(x) < maxpow**(2**power_level)
+     * where
+     *   maxpow = maxpow_in_bdigit_dbl(base, &numdigits)
      *
      * This function generates sequence of zeros, and then stringized abs(x) into b2s->ptr.
      *
@@ -4453,22 +4455,20 @@ rb_big2str1(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4455
         power = power_cache_get_power(base, power_level, NULL);
     }
     assert(power_level != MAX_BIG2STR_TABLE_ENTRIES);
-    if (FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power), RBIGNUM_LEN(power))) >= 0) {
-        power_level++;
-    }
 
-#ifndef NDEBUG
-    if (0 < power_level) {
-        VALUE power1 = power_cache_get_power(base, power_level-1, NULL);
-        assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power1), RBIGNUM_LEN(power1))) >= 0);
+    if (RBIGNUM_LEN(power) <= RBIGNUM_LEN(x)) {
         /*
-        {
-            VALUE power0 = power_cache_get_power(base, power_level, NULL);
-            assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power0), RBIGNUM_LEN(power0))) < 0);
-        }
-        */
+         * This increment guarantees x < power_cache_get_power(base, power_level)
+         * without invoking it actually.
+         * (power_cache_get_power(base, power_level) can be slow and not used
+         * in big2str_karatsuba.)
+         *
+         * Although it is possible that x < power_cache_get_power(base, power_level-1),
+         * it is no problem because big2str_karatsuba checks it and
+         * doesn't affect the result when b2s_data.ptr is NULL.
+         */
+        power_level++;
     }
-#endif
 
     b2s_data.negative = RBIGNUM_NEGATIVE_P(x);
     b2s_data.base = base;

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

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