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

ruby-changes:30222

From: akr <ko1@a...>
Date: Wed, 31 Jul 2013 19:59:11 +0900 (JST)
Subject: [ruby-changes:30222] akr:r42274 (trunk): * bignum.c (MAX_BIG2STR_TABLE_ENTRIES): Use SIZEOF_SIZE_T.

akr	2013-07-31 19:58:58 +0900 (Wed, 31 Jul 2013)

  New Revision: 42274

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

  Log:
    * bignum.c (MAX_BIG2STR_TABLE_ENTRIES): Use SIZEOF_SIZE_T.
      (power_cache_get_power0): Add rb_bug call for too bit i argument.
      (power_cache_get_power): Simplified.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42273)
+++ ChangeLog	(revision 42274)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jul 31 19:55:33 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (MAX_BIG2STR_TABLE_ENTRIES): Use SIZEOF_SIZE_T.
+	  (power_cache_get_power0): Add rb_bug call for too bit i argument.
+	  (power_cache_get_power): Simplified.
+
 Wed Jul 31 18:32:25 2013  Akinori MUSHA  <knu@i...>
 
 	* lib/uri/common.rb (URI.decode_www_form_component): Use String#b.
Index: bignum.c
===================================================================
--- bignum.c	(revision 42273)
+++ bignum.c	(revision 42274)
@@ -4082,7 +4082,7 @@ big_rshift(VALUE x, unsigned long shift) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4082
 
 #define LOG2_KARATSUBA_BIG2STR_DIGITS 7
 #define KARATSUBA_BIG2STR_DIGITS (1L<<LOG2_KARATSUBA_BIG2STR_DIGITS)
-#define MAX_BIG2STR_TABLE_ENTRIES 64
+#define MAX_BIG2STR_TABLE_ENTRIES (SIZEOF_SIZE_T * CHAR_BIT)
 
 static VALUE big2str_power_cache[35][MAX_BIG2STR_TABLE_ENTRIES];
 
@@ -4100,6 +4100,21 @@ power_cache_init(void) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4100
 static inline VALUE
 power_cache_get_power0(int base, int i)
 {
+    /* MAX_BIG2STR_TABLE_ENTRIES is big enough to that
+     * big2str_power_cache[base][MAX_BIG2STR_TABLE_ENTRIES-1] fills whole memory.
+     * So MAX_BIG2STR_TABLE_ENTRIES <= i is not possible to calculate.
+     *
+     * big2str_power_cache[base][MAX_BIG2STR_TABLE_ENTRIES-1] =
+     * (base**KARATSUBA_BIG2STR_DIGITS)**(MAX_BIG2STR_TABLE_ENTRIES-1) =
+     * (base**KARATSUBA_BIG2STR_DIGITS)**(SIZEOF_SIZE_T*CHAR_BIT-1) =
+     * base**(KARATSUBA_BIG2STR_DIGITS*(SIZEOF_SIZE_T*CHAR_BIT-1)) > 2**(SIZEOF_SIZE_T*CHAR_BIT-1)
+     * where
+     *   2 <= base
+     *   1 < KARATSUBA_BIG2STR_DIGITS
+     */
+    if (MAX_BIG2STR_TABLE_ENTRIES <= i)
+        rb_bug("too big power number requested: (%d**%ld)**(2**%d)", base, KARATSUBA_BIG2STR_DIGITS, i);
+
     if (NIL_P(big2str_power_cache[base - 2][i])) {
 	big2str_power_cache[base - 2][i] =
 	    i == 0 ? rb_big_pow(rb_int2big(base), INT2FIX(KARATSUBA_BIG2STR_DIGITS))
@@ -4113,24 +4128,15 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/bignum.c#L4128
 power_cache_get_power(int base, long n1, long* m1)
 {
     int i, m;
-    long j;
     VALUE t;
 
     if (n1 <= KARATSUBA_BIG2STR_DIGITS)
 	rb_bug("n1 > KARATSUBA_BIG2STR_DIGITS");
 
     m = bitsize(n1-1); /* ceil(log2(n1)) */
-    if (m1) *m1 = 1 << m;
+    if (m1) *m1 = 1 << m; /* smallest x which n1 <= x and x is a power of 2. */
     i = m - LOG2_KARATSUBA_BIG2STR_DIGITS;
-    if (i >= MAX_BIG2STR_TABLE_ENTRIES)
-	i = MAX_BIG2STR_TABLE_ENTRIES - 1;
     t = power_cache_get_power0(base, i);
-
-    j = KARATSUBA_BIG2STR_DIGITS*(1 << i);
-    while (n1 > j) {
-	t = bigsq(t);
-	j *= 2;
-    }
     return t;
 }
 

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

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