summaryrefslogtreecommitdiff
path: root/bignum.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-31 14:24:31 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-31 14:24:31 +0000
commitfc1f9d2cb2ad4592530f0130d75682a6c8b8c6a7 (patch)
tree5b0be5357a35e01370aa1854f62420727e061198 /bignum.c
parent1d80ad6a391610bb9225127d388ec96dd31c20ba (diff)
* 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
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/bignum.c b/bignum.c
index 9a28b5bac5..05f3abe41f 100644
--- a/bignum.c
+++ b/bignum.c
@@ -1055,7 +1055,7 @@ big2str_find_n1(VALUE x, int base)
}
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;
BDIGIT* ds = BDIGITS(x);
@@ -1070,7 +1070,7 @@ big2str_orig(VALUE x, int base, char* ptr, long len, long hbase, int trim)
num %= hbase;
}
if (trim && ds[i-1] == 0) i--;
- k = SIZEOF_BDIGITS;
+ k = hbase_numdigits;
while (k--) {
ptr[--j] = ruby_digitmap[num % base];
num /= base;
@@ -1088,7 +1088,7 @@ big2str_orig(VALUE x, int base, char* ptr, long len, long hbase, int trim)
static long
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;
VALUE b, q, r;
@@ -1102,7 +1102,7 @@ big2str_karatsuba(VALUE x, int base, char* ptr,
}
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);
@@ -1110,10 +1110,10 @@ big2str_karatsuba(VALUE x, int base, char* ptr,
rb_obj_hide(q);
rb_obj_hide(r);
lh = big2str_karatsuba(q, base, ptr, (len - m1)/2,
- len - m1, hbase, trim);
+ len - m1, hbase, hbase_numdigits, trim);
rb_big_resize(q, 0);
ll = big2str_karatsuba(r, base, ptr + lh, m1/2,
- m1, hbase, !lh && trim);
+ m1, hbase, hbase_numdigits, !lh && trim);
rb_big_resize(r, 0);
return lh + ll;
@@ -1125,6 +1125,7 @@ rb_big2str0(VALUE x, int base, int trim)
int off;
VALUE ss, xx;
long n1, n2, len, hbase;
+ int hbase_numdigits;
char* ptr;
if (FIXNUM_P(x)) {
@@ -1144,18 +1145,20 @@ rb_big2str0(VALUE x, int base, int trim)
ptr[0] = RBIGNUM_SIGN(x) ? '+' : '-';
hbase = base*base;
+ hbase_numdigits = 2;
#if SIZEOF_BDIGITS > 2
hbase *= hbase;
+ hbase_numdigits *= 2;
#endif
off = !(trim && RBIGNUM_SIGN(x)); /* erase plus sign if trim */
xx = rb_big_clone(x);
RBIGNUM_SET_SIGN(xx, 1);
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 {
len = off + big2str_karatsuba(xx, base, ptr + off, n1,
- n2, hbase, trim);
+ n2, hbase, hbase_numdigits, trim);
}
rb_big_resize(xx, 0);