mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* bignum.c (bary_mul_balance): Reduce work memory.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41830 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ed92ae818f
commit
ffe55cdc1e
3 changed files with 44 additions and 10 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Mon Jul 8 20:55:22 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* bignum.c (bary_mul_balance): Reduce work memory.
|
||||||
|
|
||||||
Mon Jul 8 08:26:15 2013 Martin Bosslet <Martin.Bosslet@gmail.com>
|
Mon Jul 8 08:26:15 2013 Martin Bosslet <Martin.Bosslet@gmail.com>
|
||||||
|
|
||||||
* test/openssl/test_pkey_ec.rb: Skip tests for "Oakley" curves as
|
* test/openssl/test_pkey_ec.rb: Skip tests for "Oakley" curves as
|
||||||
|
|
38
bignum.c
38
bignum.c
|
@ -1542,28 +1542,46 @@ static void
|
||||||
bary_mul_balance(BDIGIT *zds, size_t zl, BDIGIT *xds, size_t xl, BDIGIT *yds, size_t yl)
|
bary_mul_balance(BDIGIT *zds, size_t zl, BDIGIT *xds, size_t xl, BDIGIT *yds, size_t yl)
|
||||||
{
|
{
|
||||||
VALUE work = 0;
|
VALUE work = 0;
|
||||||
size_t r, n;
|
|
||||||
BDIGIT *wds;
|
BDIGIT *wds;
|
||||||
size_t wl;
|
size_t yl0 = yl;
|
||||||
|
size_t r, n;
|
||||||
|
size_t wl = 0;
|
||||||
|
|
||||||
assert(xl + yl <= zl);
|
assert(xl + yl <= zl);
|
||||||
assert(2 * xl <= yl || 3 * xl <= 2*(yl+2));
|
assert(2 * xl <= yl || 3 * xl <= 2*(yl+2));
|
||||||
|
|
||||||
wl = xl * 2;
|
MEMZERO(zds, BDIGIT, xl);
|
||||||
wds = ALLOCV_N(BDIGIT, work, wl);
|
|
||||||
|
|
||||||
MEMZERO(zds, BDIGIT, zl);
|
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
while (yl > 0) {
|
while (yl > 0) {
|
||||||
|
BDIGIT *tds;
|
||||||
|
size_t tl;
|
||||||
r = xl > yl ? yl : xl;
|
r = xl > yl ? yl : xl;
|
||||||
bary_mul(wds, xl + r, xds, xl, yds + n, r);
|
tl = xl + r;
|
||||||
bary_add(zds + n, zl - n,
|
if (2 * (xl + r) <= zl - n) {
|
||||||
zds + n, zl - n,
|
tds = zds + n + xl + r;
|
||||||
wds, xl + r);
|
bary_mul(tds, tl, xds, xl, yds + n, r);
|
||||||
|
MEMZERO(zds + n + xl, BDIGIT, r);
|
||||||
|
bary_add(zds + n, tl,
|
||||||
|
zds + n, tl,
|
||||||
|
tds, tl);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (wl < xl) {
|
||||||
|
wl = xl;
|
||||||
|
wds = ALLOCV_N(BDIGIT, work, wl);
|
||||||
|
}
|
||||||
|
tds = zds + n;
|
||||||
|
MEMCPY(wds, zds + n, BDIGIT, xl);
|
||||||
|
bary_mul(tds, tl, xds, xl, yds + n, r);
|
||||||
|
bary_add(zds + n, tl,
|
||||||
|
zds + n, tl,
|
||||||
|
wds, xl);
|
||||||
|
}
|
||||||
yl -= r;
|
yl -= r;
|
||||||
n += r;
|
n += r;
|
||||||
}
|
}
|
||||||
|
MEMZERO(zds+xl+yl0, BDIGIT, zl - (xl+yl0));
|
||||||
|
|
||||||
if (work)
|
if (work)
|
||||||
ALLOCV_END(work);
|
ALLOCV_END(work);
|
||||||
|
|
|
@ -43,6 +43,18 @@ class TestBignum < Test::Unit::TestCase
|
||||||
assert_equal(z, x.big_mul_balance(y))
|
assert_equal(z, x.big_mul_balance(y))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_mul_balance_2x16
|
||||||
|
x = (1 << Bignum::BITSPERDIG) | 1
|
||||||
|
y = (1 << Bignum::BITSPERDIG*16) | 1
|
||||||
|
assert_equal(x.big_mul_normal(y), x.big_mul_balance(y))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mul_balance_2x17
|
||||||
|
x = (1 << Bignum::BITSPERDIG) | 1
|
||||||
|
y = (1 << Bignum::BITSPERDIG*17) | 1
|
||||||
|
assert_equal(x.big_mul_normal(y), x.big_mul_balance(y))
|
||||||
|
end
|
||||||
|
|
||||||
def test_mul_karatsuba
|
def test_mul_karatsuba
|
||||||
x = (1 << BITSPERDIG) | 1
|
x = (1 << BITSPERDIG) | 1
|
||||||
y = (1 << BITSPERDIG) | 1
|
y = (1 << BITSPERDIG) | 1
|
||||||
|
|
Loading…
Add table
Reference in a new issue