Revert "balancing multiplication like that in Ruby 1.9" This reverts commit e68439aae10d003250afa6c1f57025bfee5f82ed. Conflicts: bn_mp_balance_mul.c makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
diff --git a/bn_mp_balance_mul.c b/bn_mp_balance_mul.c
deleted file mode 100644
index 17ddedf..0000000
--- a/bn_mp_balance_mul.c
+++ /dev/null
@@ -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$ */
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index 4fdf5ac..64e32cc 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -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
diff --git a/tommath.h b/tommath.h
index 13cc64a..800d4b6 100644
--- a/tommath.h
+++ b/tommath.h
@@ -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);
diff --git a/tommath_class.h b/tommath_class.h
index afd08a4..a06d186 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -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