Revert "balancing multiplication like that in Ruby 1.9"

This reverts commit e68439aae10d003250afa6c1f57025bfee5f82ed.

Conflicts:
	bn_mp_balance_mul.c
	makefile
This commit is contained in:
Steffen Jaeckel 2014-10-18 18:54:06 +02:00
parent 9ca37ca01c
commit b425b0ea1f
4 changed files with 4 additions and 127 deletions

View File

@ -1,101 +0,0 @@
#include <tommath.h>
#ifdef BN_MP_BALANCE_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/* c = |a| * |b| using balancing multiplication.
* If |a| is much less than |b|,
* we firstly split b into chunks such that length of each one is
* roughly equal to that of |a|.
*/
int mp_balance_mul (mp_int * a, mp_int * b, mp_int * c)
{
/* the following algorithm is taken from
* Ruby core; namely, function 'bigmul1_balance'
* from 'bignum.c'
*/
mp_int t1, t2;
long i, an, bn, r, n;
int res, min, max;
int err = MP_MEM;
mp_digit *bds, *t1ds;
an = a->used;
bn = b->used;
if ((res = mp_grow(c, an + bn)) != MP_OKAY) {
return res;
}
if (mp_init_size(&t1, an) != MP_OKAY) {
goto ERR;
}
bds = b->dp;
t1ds = t1.dp;
n = 0;
mp_int x;
c->used = an + bn;
while (bn > 0) {
r = MIN(an, bn);
for (i = 0; i < r; ++i)
t1ds[i] = bds[n + i];
t1.used = r;
mp_init_size(&t2, an + r);
mp_mul(a, &t1, &t2);
if (t2.used > c->used - n) {
min = c->used - n; max = t2.used;
x.used = t2.used; x.dp = t2.dp;
} else {
min = t2.used; max = c->used - n;
x.used = c->used - n; x.dp = c->dp + n;
}
register mp_digit u, *tmpx, *tmpt2, *tmpcn;
register int j;
tmpx = tmpcn = x.dp; tmpt2 = t2.dp;
u = 0;
for (j = 0; j < min; j++) {
*tmpcn = *tmpx++ + *tmpt2++ + u;
u = *tmpcn >> ((mp_digit)DIGIT_BIT);
*tmpcn++ &= MP_MASK;
}
if (min != max) {
for (; j < max; j++) {
*tmpcn = x.dp[j] + u;
u = *tmpcn >> ((mp_digit)DIGIT_BIT);
*tmpcn++ &= MP_MASK;
}
}
*tmpcn++ = u;
bn -= r;
n += r;
}
mp_clamp(c);
return MP_OKAY;
ERR:
return err;
}
#endif
/* $Source$ */
/* $Revision$ */
/* $Date$ */

View File

@ -21,27 +21,15 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
int res, neg;
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
int an, bn, tn;
mp_int * t;
an = a -> used;
bn = b -> used;
if (an > bn) {
tn = an; an = bn; bn = tn;
t = a; a = b; b = t;
}
/* now a->used <= b->used */
/* use Toom-Cook? */
#ifdef BN_MP_TOOM_MUL_C
if (a->used >= TOOM_MUL_CUTOFF) {
if (2 * an <= bn) goto balance;
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
res = mp_toom_mul(a, b, c);
} else
#endif
#ifdef BN_MP_KARATSUBA_MUL_C
/* use Karatsuba? */
if (a->used >= KARATSUBA_MUL_CUTOFF) {
if (2 * an <= bn) goto balance;
if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
res = mp_karatsuba_mul (a, b, c);
} else
#endif
@ -56,7 +44,8 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
#ifdef BN_FAST_S_MP_MUL_DIGS_C
if ((digs < MP_WARRAY) &&
a->used <= (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
MIN(a->used, b->used) <=
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
res = fast_s_mp_mul_digs (a, b, c, digs);
} else
#endif
@ -67,17 +56,8 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
#endif
}
ret:
c->sign = (c->used > 0) ? neg : MP_ZPOS;
return res;
balance:
/* if a is much smaller than b
* use balance multiplication
* (the idea is taken from Ruby core)
*/
res = mp_balance_mul(a, b, c);
goto ret;
}
#endif

View File

@ -577,7 +577,6 @@ int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
int fast_s_mp_sqr(mp_int *a, mp_int *b);
int s_mp_sqr(mp_int *a, mp_int *b);
int mp_balance_mul(mp_int *a, mp_int *b, mp_int *c);
int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
int mp_karatsuba_sqr(mp_int *a, mp_int *b);

View File

@ -20,7 +20,6 @@
#define BN_MP_ADD_D_C
#define BN_MP_ADDMOD_C
#define BN_MP_AND_C
#define BN_MP_BALANCE_MUL_C
#define BN_MP_CLAMP_C
#define BN_MP_CLEAR_C
#define BN_MP_CLEAR_MULTI_C