* bignum.c (big2str_alloc): New function to allocate the result string.
It is called after actual length is calculated. (big2str_struct): Add fields: negative, result and ptr. (big2str_orig): Write out the result via b2s->ptr. (big2str_orig): Ditto. (rb_big2str1): Don't allocate the result string at beginning. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42303 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
478bd3128c
commit
93055423bd
@ -1,3 +1,12 @@
|
|||||||
|
Thu Aug 1 12:37:58 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* bignum.c (big2str_alloc): New function to allocate the result string.
|
||||||
|
It is called after actual length is calculated.
|
||||||
|
(big2str_struct): Add fields: negative, result and ptr.
|
||||||
|
(big2str_orig): Write out the result via b2s->ptr.
|
||||||
|
(big2str_orig): Ditto.
|
||||||
|
(rb_big2str1): Don't allocate the result string at beginning.
|
||||||
|
|
||||||
Thu Aug 1 07:36:27 2013 Tanaka Akira <akr@fsij.org>
|
Thu Aug 1 07:36:27 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* bignum.c (big2str_orig): Use temporary buffer when trim mode.
|
* bignum.c (big2str_orig): Use temporary buffer when trim mode.
|
||||||
|
117
bignum.c
117
bignum.c
@ -4213,15 +4213,29 @@ big2str_find_n1(VALUE x, int base)
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct big2str_struct {
|
struct big2str_struct {
|
||||||
|
int negative;
|
||||||
int base;
|
int base;
|
||||||
BDIGIT hbase;
|
BDIGIT hbase;
|
||||||
int hbase_numdigits;
|
int hbase_numdigits;
|
||||||
BDIGIT_DBL hbase2;
|
BDIGIT_DBL hbase2;
|
||||||
int hbase2_numdigits;
|
int hbase2_numdigits;
|
||||||
|
VALUE result;
|
||||||
|
char *ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
static size_t
|
static void
|
||||||
big2str_orig(struct big2str_struct *b2s, VALUE x, char* ptr, size_t len, int trim)
|
big2str_alloc(struct big2str_struct *b2s, size_t len)
|
||||||
|
{
|
||||||
|
if (LONG_MAX-1 < len)
|
||||||
|
rb_raise(rb_eArgError, "too big number");
|
||||||
|
b2s->result = rb_usascii_str_new(0, (long)(len + 1)); /* plus one for sign */
|
||||||
|
b2s->ptr = RSTRING_PTR(b2s->result);
|
||||||
|
if (b2s->negative)
|
||||||
|
*b2s->ptr++ = '-';
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
big2str_orig(struct big2str_struct *b2s, VALUE x, size_t len, size_t taillen)
|
||||||
{
|
{
|
||||||
long i = RBIGNUM_LEN(x);
|
long i = RBIGNUM_LEN(x);
|
||||||
size_t j;
|
size_t j;
|
||||||
@ -4229,24 +4243,28 @@ big2str_orig(struct big2str_struct *b2s, VALUE x, char* ptr, size_t len, int tri
|
|||||||
BDIGIT* ds = BDIGITS(x);
|
BDIGIT* ds = BDIGITS(x);
|
||||||
BDIGIT_DBL num;
|
BDIGIT_DBL num;
|
||||||
char buf[SIZEOF_BDIGIT_DBL*CHAR_BIT], *p;
|
char buf[SIZEOF_BDIGIT_DBL*CHAR_BIT], *p;
|
||||||
|
int trim = !b2s->ptr;
|
||||||
|
|
||||||
assert(i <= 2);
|
assert(i <= 2);
|
||||||
|
|
||||||
if (trim) {
|
|
||||||
p = buf;
|
|
||||||
j = sizeof(buf);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
p = ptr;
|
|
||||||
j = len;
|
|
||||||
}
|
|
||||||
|
|
||||||
num = 0;
|
num = 0;
|
||||||
if (0 < i)
|
if (0 < i)
|
||||||
num = ds[0];
|
num = ds[0];
|
||||||
if (1 < i)
|
if (1 < i)
|
||||||
num |= BIGUP(ds[1]);
|
num |= BIGUP(ds[1]);
|
||||||
|
|
||||||
|
if (num == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (trim) {
|
||||||
|
p = buf;
|
||||||
|
j = sizeof(buf);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
p = b2s->ptr;
|
||||||
|
j = len;
|
||||||
|
}
|
||||||
|
|
||||||
k = b2s->hbase2_numdigits;
|
k = b2s->hbase2_numdigits;
|
||||||
while (k--) {
|
while (k--) {
|
||||||
p[--j] = ruby_digitmap[num % b2s->base];
|
p[--j] = ruby_digitmap[num % b2s->base];
|
||||||
@ -4255,45 +4273,43 @@ big2str_orig(struct big2str_struct *b2s, VALUE x, char* ptr, size_t len, int tri
|
|||||||
if (trim && num == 0) break;
|
if (trim && num == 0) break;
|
||||||
}
|
}
|
||||||
if (trim) {
|
if (trim) {
|
||||||
while (j < sizeof(buf) && p[j] == '0') j++;
|
while (j < sizeof(buf) && buf[j] == '0')
|
||||||
assert(sizeof(buf)-j <= len);
|
j++;
|
||||||
MEMCPY(ptr, p + j, char, sizeof(buf) - j);
|
|
||||||
len = sizeof(buf) - j;
|
len = sizeof(buf) - j;
|
||||||
|
big2str_alloc(b2s, len + taillen);
|
||||||
|
MEMCPY(b2s->ptr, buf + j, char, len);
|
||||||
}
|
}
|
||||||
return len;
|
b2s->ptr += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t
|
static void
|
||||||
big2str_karatsuba(struct big2str_struct *b2s, VALUE x, char* ptr,
|
big2str_karatsuba(struct big2str_struct *b2s, VALUE x,
|
||||||
int power_level, size_t len, int trim)
|
int power_level, size_t len, size_t taillen)
|
||||||
{
|
{
|
||||||
size_t lh, ll, m1;
|
size_t m1;
|
||||||
VALUE b, q, r;
|
VALUE b, q, r;
|
||||||
|
|
||||||
if (BIGZEROP(x)) {
|
if (BIGZEROP(x)) {
|
||||||
if (trim) return 0;
|
if (b2s->ptr) {
|
||||||
else {
|
memset(b2s->ptr, '0', len);
|
||||||
memset(ptr, '0', len);
|
b2s->ptr += len;
|
||||||
return len;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (power_level < 0) {
|
if (power_level < 0) {
|
||||||
return big2str_orig(b2s, x, ptr, len, trim);
|
big2str_orig(b2s, x, len, taillen);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
b = power_cache_get_power(b2s->base, power_level, &m1);
|
b = power_cache_get_power(b2s->base, power_level, &m1);
|
||||||
bigdivmod(x, b, &q, &r);
|
bigdivmod(x, b, &q, &r);
|
||||||
rb_obj_hide(q);
|
rb_obj_hide(q);
|
||||||
rb_obj_hide(r);
|
rb_obj_hide(r);
|
||||||
lh = big2str_karatsuba(b2s, q, ptr, power_level-1,
|
big2str_karatsuba(b2s, q, power_level-1, len ? len-m1 : 0, m1+taillen);
|
||||||
len - m1, trim);
|
|
||||||
rb_big_resize(q, 0);
|
rb_big_resize(q, 0);
|
||||||
ll = big2str_karatsuba(b2s, r, ptr + lh, power_level-1,
|
big2str_karatsuba(b2s, r, power_level-1, m1, taillen);
|
||||||
m1, !lh && trim);
|
|
||||||
rb_big_resize(r, 0);
|
rb_big_resize(r, 0);
|
||||||
|
|
||||||
return lh + ll;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
@ -4330,10 +4346,8 @@ big2str_base_powerof2(VALUE x, size_t len, int base, int trim)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_big2str1(VALUE x, int base, int trim)
|
rb_big2str1(VALUE x, int base, int trim)
|
||||||
{
|
{
|
||||||
int off;
|
VALUE xx;
|
||||||
VALUE ss, xx;
|
|
||||||
size_t n2, len;
|
size_t n2, len;
|
||||||
char* ptr;
|
|
||||||
struct big2str_struct b2s_data;
|
struct big2str_struct b2s_data;
|
||||||
int power_level;
|
int power_level;
|
||||||
VALUE power;
|
VALUE power;
|
||||||
@ -4355,13 +4369,6 @@ rb_big2str1(VALUE x, int base, int trim)
|
|||||||
return big2str_base_powerof2(x, n2, base, trim);
|
return big2str_base_powerof2(x, n2, base, trim);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (LONG_MAX-1 < n2)
|
|
||||||
rb_raise(rb_eArgError, "too big number");
|
|
||||||
|
|
||||||
ss = rb_usascii_str_new(0, (long)(n2 + 1)); /* plus one for sign */
|
|
||||||
ptr = RSTRING_PTR(ss);
|
|
||||||
ptr[0] = RBIGNUM_SIGN(x) ? '+' : '-';
|
|
||||||
|
|
||||||
power_level = 0;
|
power_level = 0;
|
||||||
power = power_cache_get_power(base, power_level, NULL);
|
power = power_cache_get_power(base, power_level, NULL);
|
||||||
while (power_level < MAX_BIG2STR_TABLE_ENTRIES &&
|
while (power_level < MAX_BIG2STR_TABLE_ENTRIES &&
|
||||||
@ -4380,27 +4387,39 @@ rb_big2str1(VALUE x, int base, int trim)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b2s_data.negative = RBIGNUM_NEGATIVE_P(x);
|
||||||
b2s_data.base = base;
|
b2s_data.base = base;
|
||||||
b2s_data.hbase = maxpow_in_bdigit(base, &b2s_data.hbase_numdigits);
|
b2s_data.hbase = maxpow_in_bdigit(base, &b2s_data.hbase_numdigits);
|
||||||
b2s_data.hbase2 = maxpow_in_bdigit_dbl(base, &b2s_data.hbase2_numdigits);
|
b2s_data.hbase2 = maxpow_in_bdigit_dbl(base, &b2s_data.hbase2_numdigits);
|
||||||
off = !(trim && RBIGNUM_SIGN(x)); /* erase plus sign if trim */
|
|
||||||
|
if (trim) {
|
||||||
|
b2s_data.result = Qnil;
|
||||||
|
b2s_data.ptr = NULL;
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
len = n2;
|
||||||
|
if (LONG_MAX-1 < n2)
|
||||||
|
rb_raise(rb_eArgError, "too big number");
|
||||||
|
b2s_data.result = rb_usascii_str_new(0, (long)(n2 + 1)); /* plus one for sign */
|
||||||
|
b2s_data.ptr = RSTRING_PTR(b2s_data.result);
|
||||||
|
*b2s_data.ptr++ = b2s_data.negative ? '-' : '+';
|
||||||
|
}
|
||||||
|
|
||||||
xx = rb_big_clone(x);
|
xx = rb_big_clone(x);
|
||||||
RBIGNUM_SET_SIGN(xx, 1);
|
RBIGNUM_SET_SIGN(xx, 1);
|
||||||
if (power_level < 0) {
|
if (power_level < 0) {
|
||||||
len = off + big2str_orig(&b2s_data, xx, ptr + off, n2, trim);
|
big2str_orig(&b2s_data, xx, len, 0);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
len = off + big2str_karatsuba(&b2s_data, xx, ptr + off, power_level,
|
big2str_karatsuba(&b2s_data, xx, power_level, len, 0);
|
||||||
n2, trim);
|
|
||||||
}
|
}
|
||||||
rb_big_resize(xx, 0);
|
rb_big_resize(xx, 0);
|
||||||
|
|
||||||
assert(len <= LONG_MAX);
|
*b2s_data.ptr = '\0';
|
||||||
|
rb_str_resize(b2s_data.result, (long)(b2s_data.ptr - RSTRING_PTR(b2s_data.result)));
|
||||||
|
|
||||||
ptr[len] = '\0';
|
return b2s_data.result;
|
||||||
rb_str_resize(ss, (long)len);
|
|
||||||
|
|
||||||
return ss;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* deprecated */
|
/* deprecated */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user