3 * Karatsuba's multiplication algorithm
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include "karatsuba.h"
36 /*----- Tweakables --------------------------------------------------------*/
40 # define MPK_THRESH 4 /* Smallest possible correct value */
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @mpx_kmul@ --- *
47 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
48 * @const mpw *av, *avl@ = pointer to first argument
49 * @const mpw *bv, *bvl@ = pointer to second argument
50 * @mpw *sv, *svl@ = pointer to scratch workspace
54 * Use: Multiplies two multiprecision integers using Karatsuba's
55 * algorithm. This is rather faster than traditional long
56 * multiplication (e.g., @mpx_umul@) on large numbers, although
57 * more expensive on small ones.
59 * The destination must be three times as large as the larger
60 * argument. The scratch space must be five times as large as
61 * the larger argument.
64 void mpx_kmul(mpw *dv, mpw *dvl,
65 const mpw *av, const mpw *avl,
66 const mpw *bv, const mpw *bvl,
72 /* --- Dispose of easy cases to @mpx_umul@ --- *
74 * Karatsuba is only a win on large numbers, because of all the
75 * recursiveness and bookkeeping. The recursive calls make a quick check
76 * to see whether to bottom out to @mpx_umul@ which should help quite a
77 * lot, but sometimes the only way to know is to make sure...
83 if (avl - av <= MPK_THRESH || bvl - bv <= MPK_THRESH) {
84 mpx_umul(dv, dvl, av, avl, bv, bvl);
88 /* --- How the algorithm works --- *
90 * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding,
91 * %$AB = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because
92 * I've got four multiplications, each four times easier than the one I
93 * started with. However, note that I can rewrite the coefficient of %$b$%
94 * as %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$%
95 * I've already calculated, and that leaves only one more multiplication to
96 * do. So now I have three multiplications, each four times easier, and
100 /* --- First things --- *
102 * Sort out where to break the factors in half. I'll choose the midpoint
103 * of the larger one, since this minimizes the amount of work I have to do
107 if (avl - av > bvl - bv) {
108 m = (avl - av + 1) >> 1;
115 m = (bvl - bv + 1) >> 1;
123 /* --- Sort out the middle term --- */
126 mpw *bsv = sv + m + 1, *ssv = bsv + m + 1;
127 mpw *rdv = dv + m, *rdvl = rdv + 2 * (m + 2);
131 UADD2(sv, bsv, av, avm, avm, avl);
132 UADD2(bsv, ssv, bv, bvm, bvm, bvl);
134 mpx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl);
136 mpx_umul(rdv, rdvl, sv, bsv, bsv, ssv);
139 /* --- Sort out the other two terms --- */
142 mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4;
146 if (avl == avm || bvl == bvm)
147 MPX_ZERO(rdv + m + 1, dvl);
150 mpx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl);
152 mpx_umul(sv, ssv, avm, avl, bvm, bvl);
153 MPX_COPY(rdv + m + 1, dvl, svm + 1, svn);
154 UADD(rdv, sv, svm + 1);
159 mpx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl);
161 mpx_umul(sv, ssv, av, avm, bv, bvm);
162 MPX_COPY(dv, tdv, sv, svm);
168 /*----- Test rig ----------------------------------------------------------*/
172 #include <mLib/alloc.h>
173 #include <mLib/testrig.h>
175 #define ALLOC(v, vl, sz) do { \
177 mpw *_vv = xmalloc(MPWS(_sz)); \
178 mpw *_vvl = _vv + _sz; \
183 #define LOAD(v, vl, d) do { \
184 const dstr *_d = (d); \
186 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
187 mpx_loadb(_v, _vl, _d->buf, _d->len); \
192 #define MAX(x, y) ((x) > (y) ? (x) : (y))
194 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
199 fprintf(stderr, " %08lx", (unsigned long)*--vl);
203 static int umul(dstr *v)
216 m = MAX(al - a, bl - b) + 1;
220 mpx_kmul(d, dl, a, al, b, bl, s, sl);
221 if (!mpx_ueq(d, dl, c, cl)) {
222 fprintf(stderr, "\n*** umul failed\n");
225 dumpmp("expected", c, cl);
226 dumpmp(" result", d, dl);
230 xfree(a); xfree(b); xfree(c); xfree(d); xfree(s);
234 static test_chunk defs[] = {
235 { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
239 int main(int argc, char *argv[])
241 test_run(argc, argv, defs, SRCDIR"/t/mpx");
247 /*----- That's all, folks -------------------------------------------------*/