* bignum.c: Don't hard code SIZEOF_BDIGITS for log_base(hbase).

(big2str_orig): hbase_numdigits argument added.
  (big2str_karatsuba): Ditto.
  (rb_big2str0): Calculate hbase_numdigits.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41004 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-05-31 14:24:31 +00:00
parent 1d80ad6a39
commit fc1f9d2cb2
2 changed files with 18 additions and 8 deletions

View File

@ -1,3 +1,10 @@
Fri May 31 23:22:24 2013 Tanaka Akira <akr@fsij.org>
* bignum.c: Don't hard code SIZEOF_BDIGITS for log_base(hbase).
(big2str_orig): hbase_numdigits argument added.
(big2str_karatsuba): Ditto.
(rb_big2str0): Calculate hbase_numdigits.
Fri May 31 17:57:21 2013 Zachary Scott <zachary@zacharyscott.net> Fri May 31 17:57:21 2013 Zachary Scott <zachary@zacharyscott.net>
* process.c: Improve Process::exec documentation * process.c: Improve Process::exec documentation

View File

@ -1055,7 +1055,7 @@ big2str_find_n1(VALUE x, int base)
} }
static long static long
big2str_orig(VALUE x, int base, char* ptr, long len, long hbase, int trim) big2str_orig(VALUE x, int base, char* ptr, long len, long hbase, int hbase_numdigits, int trim)
{ {
long i = RBIGNUM_LEN(x), j = len; long i = RBIGNUM_LEN(x), j = len;
BDIGIT* ds = BDIGITS(x); BDIGIT* ds = BDIGITS(x);
@ -1070,7 +1070,7 @@ big2str_orig(VALUE x, int base, char* ptr, long len, long hbase, int trim)
num %= hbase; num %= hbase;
} }
if (trim && ds[i-1] == 0) i--; if (trim && ds[i-1] == 0) i--;
k = SIZEOF_BDIGITS; k = hbase_numdigits;
while (k--) { while (k--) {
ptr[--j] = ruby_digitmap[num % base]; ptr[--j] = ruby_digitmap[num % base];
num /= base; num /= base;
@ -1088,7 +1088,7 @@ big2str_orig(VALUE x, int base, char* ptr, long len, long hbase, int trim)
static long static long
big2str_karatsuba(VALUE x, int base, char* ptr, big2str_karatsuba(VALUE x, int base, char* ptr,
long n1, long len, long hbase, int trim) long n1, long len, long hbase, int hbase_numdigits, int trim)
{ {
long lh, ll, m1; long lh, ll, m1;
VALUE b, q, r; VALUE b, q, r;
@ -1102,7 +1102,7 @@ big2str_karatsuba(VALUE x, int base, char* ptr,
} }
if (n1 <= KARATSUBA_DIGITS) { if (n1 <= KARATSUBA_DIGITS) {
return big2str_orig(x, base, ptr, len, hbase, trim); return big2str_orig(x, base, ptr, len, hbase, hbase_numdigits, trim);
} }
b = power_cache_get_power(base, n1, &m1); b = power_cache_get_power(base, n1, &m1);
@ -1110,10 +1110,10 @@ big2str_karatsuba(VALUE x, int base, char* ptr,
rb_obj_hide(q); rb_obj_hide(q);
rb_obj_hide(r); rb_obj_hide(r);
lh = big2str_karatsuba(q, base, ptr, (len - m1)/2, lh = big2str_karatsuba(q, base, ptr, (len - m1)/2,
len - m1, hbase, trim); len - m1, hbase, hbase_numdigits, trim);
rb_big_resize(q, 0); rb_big_resize(q, 0);
ll = big2str_karatsuba(r, base, ptr + lh, m1/2, ll = big2str_karatsuba(r, base, ptr + lh, m1/2,
m1, hbase, !lh && trim); m1, hbase, hbase_numdigits, !lh && trim);
rb_big_resize(r, 0); rb_big_resize(r, 0);
return lh + ll; return lh + ll;
@ -1125,6 +1125,7 @@ rb_big2str0(VALUE x, int base, int trim)
int off; int off;
VALUE ss, xx; VALUE ss, xx;
long n1, n2, len, hbase; long n1, n2, len, hbase;
int hbase_numdigits;
char* ptr; char* ptr;
if (FIXNUM_P(x)) { if (FIXNUM_P(x)) {
@ -1144,18 +1145,20 @@ rb_big2str0(VALUE x, int base, int trim)
ptr[0] = RBIGNUM_SIGN(x) ? '+' : '-'; ptr[0] = RBIGNUM_SIGN(x) ? '+' : '-';
hbase = base*base; hbase = base*base;
hbase_numdigits = 2;
#if SIZEOF_BDIGITS > 2 #if SIZEOF_BDIGITS > 2
hbase *= hbase; hbase *= hbase;
hbase_numdigits *= 2;
#endif #endif
off = !(trim && RBIGNUM_SIGN(x)); /* erase plus sign if trim */ off = !(trim && RBIGNUM_SIGN(x)); /* erase plus sign if trim */
xx = rb_big_clone(x); xx = rb_big_clone(x);
RBIGNUM_SET_SIGN(xx, 1); RBIGNUM_SET_SIGN(xx, 1);
if (n1 <= KARATSUBA_DIGITS) { if (n1 <= KARATSUBA_DIGITS) {
len = off + big2str_orig(xx, base, ptr + off, n2, hbase, trim); len = off + big2str_orig(xx, base, ptr + off, n2, hbase, hbase_numdigits, trim);
} }
else { else {
len = off + big2str_karatsuba(xx, base, ptr + off, n1, len = off + big2str_karatsuba(xx, base, ptr + off, n1,
n2, hbase, trim); n2, hbase, hbase_numdigits, trim);
} }
rb_big_resize(xx, 0); rb_big_resize(xx, 0);