3 * $Id: mpx-kmul.c,v 1.1 1999/12/10 23:23:51 mdw Exp $
5 * Karatsuba's multiplication algorithm
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: mpx-kmul.c,v $
33 * Revision 1.1 1999/12/10 23:23:51 mdw
34 * Karatsuba-Ofman multiplication algorithm.
38 /*----- Header files ------------------------------------------------------*/
44 /*----- Tweakables --------------------------------------------------------*/
46 /* --- @KARATSUBA_CUTOFF@ --- *
48 * If either of the arguments to @mpx_kmul@ contains this number of words or
49 * fewer, the job is dumped out to @mpx_umul@ instead. Reduce the size when
50 * testing, to ensure better coverage.
54 # undef KARATSUBA_CUTOFF
55 # define KARATSUBA_CUTOFF 2
58 /*----- Addition macros ---------------------------------------------------*/
60 #define UADD(dv, av, avl) do { \
62 const mpw *_av = (av), *_avl = (avl); \
65 while (_av < _avl) { \
70 _x = (mpd)_a + (mpd)_b + _c; \
72 _c = _x >> MPW_BITS; \
75 mpd _x = (mpd)*_dv + (mpd)_c; \
77 _c = _x >> MPW_BITS; \
81 #define UADD2(dv, dvl, av, avl, bv, bvl) do { \
82 mpw *_dv = (dv), *_dvl = (dvl); \
83 const mpw *_av = (av), *_avl = (avl); \
84 const mpw *_bv = (bv), *_bvl = (bvl); \
87 while (_av < _avl || _bv < _bvl) { \
90 _a = (_av < _avl) ? *_av++ : 0; \
91 _b = (_bv < _bvl) ? *_bv++ : 0; \
92 _x = (mpd)_a + (mpd)_b + _c; \
94 _c = _x >> MPW_BITS; \
101 #define USUB(dv, av, avl) do { \
103 const mpw *_av = (av), *_avl = (avl); \
106 while (_av < _avl) { \
111 _x = (mpd)_b - (mpd)_a - _c; \
113 if (_x >> MPW_BITS) \
119 mpd _x = (mpd)*_dv - (mpd)_c; \
121 if (_x >> MPW_BITS) \
128 /*----- Main code ---------------------------------------------------------*/
130 /* --- @mpx_kmul@ --- *
132 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
133 * @const mpw *av, *avl@ = pointer to first argument
134 * @const mpw *bv, *bvl@ = pointer to second argument
135 * @mpw *sv, *svl@ = pointer to scratch workspace
139 * Use: Multiplies two multiprecision integers using Karatsuba's
140 * algorithm. This is rather faster than traditional long
141 * multiplication (e.g., @mpx_umul@) on large numbers, although
142 * more expensive on small ones.
144 * The destination must be twice as large as the larger
145 * argument. The scratch space must be twice as large as the
146 * larger argument, plus the magic number @KARATSUBA_SLOP@.
147 * (Actually, a number of words proportional to the depth of
148 * recursion, but since recusion is strongly bounded by memory,
149 * I can replace it with a constant as long as it's `big
153 void mpx_kmul(mpw
*dv
, mpw
*dvl
,
154 const mpw
*av
, const mpw
*avl
,
155 const mpw
*bv
, const mpw
*bvl
,
158 const mpw
*avm
, *bvm
;
161 /* --- Dispose of easy cases to @mpx_umul@ --- *
163 * Karatsuba is only a win on large numbers, because of all the
164 * recursiveness and bookkeeping. The recursive calls make a quick check
165 * to see whether to bottom out to @mpx_umul@ which should help quite a
166 * lot, but sometimes the only way to know is to make sure...
172 if (avl
- av
<= KARATSUBA_CUTOFF
|| bvl
- bv
<= KARATSUBA_CUTOFF
) {
173 mpx_umul(dv
, dvl
, av
, avl
, bv
, bvl
);
177 /* --- How the algorithm works --- *
179 * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding, %$AB
180 * = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because I've
181 * got four multiplications, each four times easier than the one I started
182 * with. However, note that I can rewrite the coefficient of %$b$% as
183 * %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$%
184 * I've already calculated, and that leaves only one more multiplication to
185 * do. So now I have three multiplications, each four times easier, and
189 /* --- First things --- *
191 * Sort out where to break the factors in half. I'll choose the midpoint
192 * of the largest one, since this minimizes the amount of work I have to do
196 if (avl
- av
> bvl
- bv
) {
197 m
= (avl
- av
+ 1) >> 1;
204 m
= (bvl
- bv
+ 1) >> 1;
212 /* --- Sort out the middle term --- *
214 * I'm going to keep track of the carry by hand rather than pass it down to
215 * the next level, because it means multiplication by one or zero, which I
216 * can do easily myself.
226 mpw
*bsv
= sv
+ m
, *ssv
= bsv
+ m
;
227 mpw
*rdv
= dv
+ m
, *rdvl
= rdv
+ 2 * m
;
229 UADD2(sv
, bsv
+ 1, av
, avm
, avm
, avl
);
232 UADD2(bsv
, ssv
+ 1, bv
, bvm
, bvm
, bvl
);
236 if (m
> KARATSUBA_CUTOFF
)
237 mpx_kmul(rdv
, rdvl
, sv
, bsv
, bsv
, ssv
, ssv
, svl
);
239 mpx_umul(rdv
, rdvl
, sv
, bsv
, bsv
, ssv
);
246 if (!(~f
& (carry_a
| carry_b
)))
247 MPX_UADDN(rdv
+ m
, rdvl
, 1);
250 /* --- Sort out the other two terms --- */
253 mpw
*ssv
= sv
+ 2 * m
;
257 if (m
> KARATSUBA_CUTOFF
)
258 mpx_kmul(sv
, ssv
, avm
, avl
, bvm
, bvl
, ssv
, svl
);
260 mpx_umul(sv
, ssv
, avm
, avl
, bvm
, bvl
);
264 if (m
> KARATSUBA_CUTOFF
)
265 mpx_kmul(sv
, ssv
, av
, avm
, bv
, bvm
, ssv
, svl
);
267 mpx_umul(sv
, ssv
, av
, avm
, bv
, bvm
);
273 /*----- Test rig ----------------------------------------------------------*/
277 #include <mLib/alloc.h>
278 #include <mLib/testrig.h>
282 #define ALLOC(v, vl, sz) do { \
284 mpw *_vv = xmalloc(MPWS(_sz)); \
285 mpw *_vvl = _vv + _sz; \
290 #define LOAD(v, vl, d) do { \
291 const dstr *_d = (d); \
293 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
294 mpx_loadb(_v, _vl, _d->buf, _d->len); \
299 #define MAX(x, y) ((x) > (y) ? (x) : (y))
301 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
306 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
310 static int umul(dstr
*v
)
323 m
= MAX(al
- a
, bl
- b
) + 1;
325 ALLOC(s
, sl
, 2 * m
+ 32);
327 mpx_kmul(d
, dl
, a
, al
, b
, bl
, s
, sl
);
328 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
329 fprintf(stderr
, "\n*** umul failed\n");
332 dumpmp("expected", c
, cl
);
333 dumpmp(" result", d
, dl
);
337 free(a
); free(b
); free(c
); free(d
); free(s
);
341 static test_chunk defs
[] = {
342 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
346 int main(int argc
, char *argv
[])
348 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
354 /*----- That's all, folks -------------------------------------------------*/