5 * Karatsuba's multiplication algorithm on binary polynomials
7 * (c) 2000 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 /*----- Header files ------------------------------------------------------*/
36 #include "karatsuba.h"
38 /*----- Tweakables --------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @gfx_kmul@ --- *
49 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
50 * @const mpw *av, *avl@ = pointer to first argument
51 * @const mpw *bv, *bvl@ = pointer to second argument
52 * @mpw *sv, *svl@ = pointer to scratch workspace
56 * Use: Multiplies two binary polynomials using Karatsuba's
57 * algorithm. This is rather faster than traditional long
58 * multiplication (e.g., @gfx_umul@) on polynomials with large
59 * degree, although more expensive on small ones.
61 * The destination must be twice as large as the larger
62 * argument. The scratch space must be twice as large as the
66 void gfx_kmul(mpw
*dv
, mpw
*dvl
,
67 const mpw
*av
, const mpw
*avl
,
68 const mpw
*bv
, const mpw
*bvl
,
74 /* --- Dispose of easy cases to @mpx_umul@ --- *
76 * Karatsuba is only a win on large numbers, because of all the
77 * recursiveness and bookkeeping. The recursive calls make a quick check
78 * to see whether to bottom out to @gfx_umul@ which should help quite a
79 * lot, but sometimes the only way to know is to make sure...
85 if (avl
- av
<= GFK_THRESH
|| bvl
- bv
<= GFK_THRESH
) {
86 gfx_mul(dv
, dvl
, av
, avl
, bv
, bvl
);
90 /* --- How the algorithm works --- *
92 * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding,
93 * %$AB = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because
94 * I've got four multiplications, each four times easier than the one I
95 * started with. However, note that I can rewrite the coefficient of %$b$%
96 * as %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$%
97 * I've already calculated, and that leaves only one more multiplication to
98 * do. So now I have three multiplications, each four times easier, and
102 /* --- First things --- *
104 * Sort out where to break the factors in half. I'll choose the midpoint
105 * of the larger one, since this minimizes the amount of work I have to do
109 if (avl
- av
> bvl
- bv
) {
110 m
= (avl
- av
+ 1) >> 1;
117 m
= (bvl
- bv
+ 1) >> 1;
125 /* --- Sort out the middle term --- */
128 mpw
*bsv
= sv
+ m
, *ssv
= bsv
+ m
;
129 mpw
*rdv
= dv
+ m
, *rdvl
= rdv
+ 2 * m
;
133 UXOR2(sv
, bsv
, av
, avm
, avm
, avl
);
134 UXOR2(bsv
, ssv
, bv
, bvm
, bvm
, bvl
);
136 gfx_kmul(rdv
, rdvl
, sv
, bsv
, bsv
, ssv
, ssv
, svl
);
138 gfx_mul(rdv
, rdvl
, sv
, bsv
, bsv
, ssv
);
141 /* --- Sort out the other two terms --- */
144 mpw
*svm
= sv
+ m
, *ssv
= svm
+ m
;
148 if (avl
== avm
|| bvl
== bvm
)
149 MPX_ZERO(rdv
+ m
, dvl
);
152 gfx_kmul(sv
, ssv
, avm
, avl
, bvm
, bvl
, ssv
, svl
);
154 gfx_mul(sv
, ssv
, avm
, avl
, bvm
, bvl
);
155 MPX_COPY(rdv
+ m
, dvl
, svm
, ssv
);
161 gfx_kmul(sv
, ssv
, av
, avm
, bv
, bvm
, ssv
, svl
);
163 gfx_mul(sv
, ssv
, av
, avm
, bv
, bvm
);
164 MPX_COPY(dv
, tdv
, sv
, svm
);
170 /*----- Test rig ----------------------------------------------------------*/
174 #include <mLib/alloc.h>
175 #include <mLib/testrig.h>
177 #define ALLOC(v, vl, sz) do { \
179 mpw *_vv = xmalloc(MPWS(_sz)); \
180 mpw *_vvl = _vv + _sz; \
185 #define LOAD(v, vl, d) do { \
186 const dstr *_d = (d); \
188 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
189 mpx_loadb(_v, _vl, _d->buf, _d->len); \
194 #define MAX(x, y) ((x) > (y) ? (x) : (y))
196 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
201 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
205 static int mul(dstr
*v
)
218 m
= MAX(al
- a
, bl
- b
) + 1;
222 gfx_kmul(d
, dl
, a
, al
, b
, bl
, s
, sl
);
223 if (!mpx_ueq(d
, dl
, c
, cl
)) {
224 fprintf(stderr
, "\n*** mul failed\n");
227 dumpmp("expected", c
, cl
);
228 dumpmp(" result", d
, dl
);
232 xfree(a
); xfree(b
); xfree(c
); xfree(d
); xfree(s
);
236 static test_chunk defs
[] = {
237 { "mul", mul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
241 int main(int argc
, char *argv
[])
243 test_run(argc
, argv
, defs
, SRCDIR
"/tests/gfx");
249 /*----- That's all, folks -------------------------------------------------*/