mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* bignum.c (BARY_TRUNC): New macro.
(bary_cmp): Use BARY_TRUNC. (bary_mul_toom3): Ditto. (bary_divmod): Ditto. (abs2twocomp): Ditto. (bigfixize): Ditto. (rb_cstr_to_inum): Ditto. (big2str_karatsuba): Ditto. (bigdivrem): Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42365 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
092511bac7
commit
5a13ad0386
2 changed files with 31 additions and 23 deletions
12
ChangeLog
12
ChangeLog
|
|
@ -1,3 +1,15 @@
|
||||||
|
Sun Aug 4 01:54:45 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* bignum.c (BARY_TRUNC): New macro.
|
||||||
|
(bary_cmp): Use BARY_TRUNC.
|
||||||
|
(bary_mul_toom3): Ditto.
|
||||||
|
(bary_divmod): Ditto.
|
||||||
|
(abs2twocomp): Ditto.
|
||||||
|
(bigfixize): Ditto.
|
||||||
|
(rb_cstr_to_inum): Ditto.
|
||||||
|
(big2str_karatsuba): Ditto.
|
||||||
|
(bigdivrem): Ditto.
|
||||||
|
|
||||||
Sun Aug 4 00:57:58 2013 Tanaka Akira <akr@fsij.org>
|
Sun Aug 4 00:57:58 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* bignum.c (big2str_karatsuba): Don't allocate new temporary buffer
|
* bignum.c (big2str_karatsuba): Don't allocate new temporary buffer
|
||||||
|
|
|
||||||
42
bignum.c
42
bignum.c
|
|
@ -121,6 +121,11 @@ STATIC_ASSERT(sizeof_long_and_sizeof_bdigit, SIZEOF_BDIGITS % SIZEOF_LONG == 0);
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#define BARY_TRUNC(ds, n) do { \
|
||||||
|
while (0 < (n) && (ds)[(n)-1] == 0) \
|
||||||
|
(n)--; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#define KARATSUBA_BALANCED(xn, yn) ((yn)/2 < (xn))
|
#define KARATSUBA_BALANCED(xn, yn) ((yn)/2 < (xn))
|
||||||
#define TOOM3_BALANCED(xn, yn) (((yn)+2)/3 * 2 < (xn))
|
#define TOOM3_BALANCED(xn, yn) (((yn)+2)/3 * 2 < (xn))
|
||||||
|
|
||||||
|
|
@ -506,10 +511,8 @@ bdigitdbl2bary(BDIGIT *ds, size_t n, BDIGIT_DBL num)
|
||||||
static int
|
static int
|
||||||
bary_cmp(const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
|
bary_cmp(const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
|
||||||
{
|
{
|
||||||
while (0 < xn && xds[xn-1] == 0)
|
BARY_TRUNC(xds, xn);
|
||||||
xn--;
|
BARY_TRUNC(yds, yn);
|
||||||
while (0 < yn && yds[yn-1] == 0)
|
|
||||||
yn--;
|
|
||||||
|
|
||||||
if (xn < yn)
|
if (xn < yn)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
@ -2364,8 +2367,7 @@ bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGI
|
||||||
else
|
else
|
||||||
bary_sub(zzds + 4*n, zzn - 4*n, zzds + 4*n, zzn - 4*n, z4ds, z4n);
|
bary_sub(zzds + 4*n, zzn - 4*n, zzds + 4*n, zzn - 4*n, z4ds, z4n);
|
||||||
|
|
||||||
while (0 < zzn && zzds[zzn-1] == 0)
|
BARY_TRUNC(zzds, zzn);
|
||||||
zzn--;
|
|
||||||
MEMCPY(zds, zzds, BDIGIT, zzn);
|
MEMCPY(zds, zzds, BDIGIT, zzn);
|
||||||
BDIGITS_ZERO(zds + zzn, zn - zzn);
|
BDIGITS_ZERO(zds + zzn, zn - zzn);
|
||||||
|
|
||||||
|
|
@ -2720,11 +2722,11 @@ bary_divmod(BDIGIT *qds, size_t nq, BDIGIT *rds, size_t nr, const BDIGIT *xds, s
|
||||||
assert(nx <= nq);
|
assert(nx <= nq);
|
||||||
assert(ny <= nr);
|
assert(ny <= nr);
|
||||||
|
|
||||||
while (0 < ny && !yds[ny-1]) ny--;
|
BARY_TRUNC(yds, ny);
|
||||||
if (ny == 0)
|
if (ny == 0)
|
||||||
rb_num_zerodiv();
|
rb_num_zerodiv();
|
||||||
|
|
||||||
while (0 < nx && !xds[nx-1]) nx--;
|
BARY_TRUNC(xds, nx);
|
||||||
if (nx == 0) {
|
if (nx == 0) {
|
||||||
BDIGITS_ZERO(qds, nq);
|
BDIGITS_ZERO(qds, nq);
|
||||||
BDIGITS_ZERO(rds, nr);
|
BDIGITS_ZERO(rds, nr);
|
||||||
|
|
@ -2973,8 +2975,7 @@ abs2twocomp(VALUE *xp, long *n_ret)
|
||||||
BDIGIT *ds = BDIGITS(x);
|
BDIGIT *ds = BDIGITS(x);
|
||||||
BDIGIT hibits = 0;
|
BDIGIT hibits = 0;
|
||||||
|
|
||||||
while (0 < n && ds[n-1] == 0)
|
BARY_TRUNC(ds, n);
|
||||||
n--;
|
|
||||||
|
|
||||||
if (n != 0 && RBIGNUM_NEGATIVE_P(x)) {
|
if (n != 0 && RBIGNUM_NEGATIVE_P(x)) {
|
||||||
VALUE z = bignew_1(CLASS_OF(x), n, 0);
|
VALUE z = bignew_1(CLASS_OF(x), n, 0);
|
||||||
|
|
@ -3021,8 +3022,7 @@ bigfixize(VALUE x)
|
||||||
BDIGIT u;
|
BDIGIT u;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (0 < len && ds[len-1] == 0)
|
BARY_TRUNC(ds, len);
|
||||||
len--;
|
|
||||||
|
|
||||||
if (len == 0) return INT2FIX(0);
|
if (len == 0) return INT2FIX(0);
|
||||||
|
|
||||||
|
|
@ -3911,8 +3911,7 @@ rb_cstr_to_inum(const char *str, int base, int badcheck)
|
||||||
vds = uds;
|
vds = uds;
|
||||||
uds = tds;
|
uds = tds;
|
||||||
}
|
}
|
||||||
while (0 < num_bdigits && uds[num_bdigits-1] == 0)
|
BARY_TRUNC(uds, num_bdigits);
|
||||||
num_bdigits--;
|
|
||||||
z = bignew(num_bdigits, sign);
|
z = bignew(num_bdigits, sign);
|
||||||
MEMCPY(BDIGITS(z), uds, BDIGIT, num_bdigits);
|
MEMCPY(BDIGITS(z), uds, BDIGIT, num_bdigits);
|
||||||
|
|
||||||
|
|
@ -4393,12 +4392,10 @@ big2str_karatsuba(struct big2str_struct *b2s, BDIGIT *xds, size_t xn,
|
||||||
rds = wds;
|
rds = wds;
|
||||||
qds = wds+rn;
|
qds = wds+rn;
|
||||||
bary_divmod(qds, qn, rds, rn, xds, xn, bds, bn);
|
bary_divmod(qds, qn, rds, rn, xds, xn, bds, bn);
|
||||||
while (0 < qn && qds[qn-1] == 0)
|
BARY_TRUNC(qds, qn);
|
||||||
qn--;
|
|
||||||
assert(qn <= bn);
|
assert(qn <= bn);
|
||||||
big2str_karatsuba(b2s, qds, qn, lower_power_level, lower_numdigits+taillen, qds+qn, wn-(rn+qn));
|
big2str_karatsuba(b2s, qds, qn, lower_power_level, lower_numdigits+taillen, qds+qn, wn-(rn+qn));
|
||||||
while (0 < rn && rds[rn-1] == 0)
|
BARY_TRUNC(rds, rn);
|
||||||
rn--;
|
|
||||||
big2str_karatsuba(b2s, rds, rn, lower_power_level, taillen, rds+rn, wn-rn);
|
big2str_karatsuba(b2s, rds, rn, lower_power_level, taillen, rds+rn, wn-rn);
|
||||||
if (tmpw)
|
if (tmpw)
|
||||||
ALLOCV_END(tmpw);
|
ALLOCV_END(tmpw);
|
||||||
|
|
@ -5564,12 +5561,12 @@ bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
|
||||||
BDIGIT dd;
|
BDIGIT dd;
|
||||||
|
|
||||||
yds = BDIGITS(y);
|
yds = BDIGITS(y);
|
||||||
while (0 < ny && !yds[ny-1]) ny--;
|
BARY_TRUNC(yds, ny);
|
||||||
if (ny == 0)
|
if (ny == 0)
|
||||||
rb_num_zerodiv();
|
rb_num_zerodiv();
|
||||||
|
|
||||||
xds = BDIGITS(x);
|
xds = BDIGITS(x);
|
||||||
while (0 < nx && !xds[nx-1]) nx--;
|
BARY_TRUNC(xds, nx);
|
||||||
|
|
||||||
if (nx < ny || (nx == ny && xds[nx - 1] < yds[ny - 1])) {
|
if (nx < ny || (nx == ny && xds[nx - 1] < yds[ny - 1])) {
|
||||||
if (divp) *divp = rb_int2big(0);
|
if (divp) *divp = rb_int2big(0);
|
||||||
|
|
@ -5623,13 +5620,12 @@ bigdivrem(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp)
|
||||||
|
|
||||||
if (divp) { /* move quotient down in z */
|
if (divp) { /* move quotient down in z */
|
||||||
j = nz - ny;
|
j = nz - ny;
|
||||||
while (0 < j && !zds[j-1+ny])
|
BARY_TRUNC(zds+ny, j);
|
||||||
j--;
|
|
||||||
*divp = zz = bignew(j, RBIGNUM_SIGN(x)==RBIGNUM_SIGN(y));
|
*divp = zz = bignew(j, RBIGNUM_SIGN(x)==RBIGNUM_SIGN(y));
|
||||||
MEMCPY(BDIGITS(zz), zds+ny, BDIGIT, j);
|
MEMCPY(BDIGITS(zz), zds+ny, BDIGIT, j);
|
||||||
}
|
}
|
||||||
if (modp) { /* normalize remainder */
|
if (modp) { /* normalize remainder */
|
||||||
while (ny > 0 && !zds[ny-1]) --ny;
|
BARY_TRUNC(zds, ny);
|
||||||
*modp = zz = bignew(ny, RBIGNUM_SIGN(x));
|
*modp = zz = bignew(ny, RBIGNUM_SIGN(x));
|
||||||
MEMCPY(BDIGITS(zz), zds, BDIGIT, ny);
|
MEMCPY(BDIGITS(zz), zds, BDIGIT, ny);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue