Commit | Line | Data |
---|---|---|
ae747c9b | 1 | /* -*-c-*- |
ae747c9b | 2 | * |
3 | * Karatsuba's multiplication algorithm on binary polynomials | |
4 | * | |
5 | * (c) 2000 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
ae747c9b | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
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. | |
45c0fd36 | 16 | * |
ae747c9b | 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. | |
45c0fd36 | 21 | * |
ae747c9b | 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, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
ae747c9b | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
30 | #include <assert.h> | |
31 | #include <stdio.h> | |
32 | ||
33 | #include "gfx.h" | |
34 | #include "karatsuba.h" | |
35 | ||
36 | /*----- Tweakables --------------------------------------------------------*/ | |
37 | ||
38 | #ifdef TEST_RIG | |
39 | # undef GFK_THRESH | |
40 | # define GFK_THRESH 1 | |
41 | #endif | |
42 | ||
43 | /*----- Main code ---------------------------------------------------------*/ | |
44 | ||
45 | /* --- @gfx_kmul@ --- * | |
46 | * | |
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 | |
51 | * | |
52 | * Returns: --- | |
53 | * | |
54 | * Use: Multiplies two binary polynomials using Karatsuba's | |
55 | * algorithm. This is rather faster than traditional long | |
56 | * multiplication (e.g., @gfx_umul@) on polynomials with large | |
57 | * degree, although more expensive on small ones. | |
58 | * | |
59 | * The destination must be twice as large as the larger | |
60 | * argument. The scratch space must be twice as large as the | |
61 | * larger argument. | |
62 | */ | |
63 | ||
64 | void gfx_kmul(mpw *dv, mpw *dvl, | |
65 | const mpw *av, const mpw *avl, | |
66 | const mpw *bv, const mpw *bvl, | |
67 | mpw *sv, mpw *svl) | |
68 | { | |
69 | const mpw *avm, *bvm; | |
70 | size_t m; | |
71 | ||
72 | /* --- Dispose of easy cases to @mpx_umul@ --- * | |
73 | * | |
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 @gfx_umul@ which should help quite a | |
77 | * lot, but sometimes the only way to know is to make sure... | |
78 | */ | |
79 | ||
80 | MPX_SHRINK(av, avl); | |
81 | MPX_SHRINK(bv, bvl); | |
82 | ||
83 | if (avl - av <= GFK_THRESH || bvl - bv <= GFK_THRESH) { | |
84 | gfx_mul(dv, dvl, av, avl, bv, bvl); | |
85 | return; | |
86 | } | |
87 | ||
88 | /* --- How the algorithm works --- * | |
89 | * | |
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 | |
97 | * that's a win. | |
98 | */ | |
99 | ||
100 | /* --- First things --- * | |
101 | * | |
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 | |
104 | * most effectively. | |
105 | */ | |
106 | ||
107 | if (avl - av > bvl - bv) { | |
108 | m = (avl - av + 1) >> 1; | |
109 | avm = av + m; | |
110 | if (bvl - bv > m) | |
111 | bvm = bv + m; | |
112 | else | |
113 | bvm = bvl; | |
114 | } else { | |
115 | m = (bvl - bv + 1) >> 1; | |
116 | bvm = bv + m; | |
117 | if (avl - av > m) | |
118 | avm = av + m; | |
119 | else | |
120 | avm = avl; | |
121 | } | |
122 | ||
ae747c9b | 123 | /* --- Sort out the middle term --- */ |
124 | ||
125 | { | |
126 | mpw *bsv = sv + m, *ssv = bsv + m; | |
127 | mpw *rdv = dv + m, *rdvl = rdv + 2 * m; | |
128 | ||
432c4e18 | 129 | assert(rdvl <= dvl); |
130 | assert(ssv <= svl); | |
ae747c9b | 131 | UXOR2(sv, bsv, av, avm, avm, avl); |
132 | UXOR2(bsv, ssv, bv, bvm, bvm, bvl); | |
133 | if (m > GFK_THRESH) | |
134 | gfx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl); | |
135 | else | |
136 | gfx_mul(rdv, rdvl, sv, bsv, bsv, ssv); | |
137 | } | |
138 | ||
139 | /* --- Sort out the other two terms --- */ | |
140 | ||
141 | { | |
142 | mpw *svm = sv + m, *ssv = svm + m; | |
143 | mpw *tdv = dv + m; | |
144 | mpw *rdv = tdv + m; | |
145 | ||
146 | if (avl == avm || bvl == bvm) | |
147 | MPX_ZERO(rdv + m, dvl); | |
148 | else { | |
149 | if (m > GFK_THRESH) | |
150 | gfx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl); | |
151 | else | |
152 | gfx_mul(sv, ssv, avm, avl, bvm, bvl); | |
153 | MPX_COPY(rdv + m, dvl, svm, ssv); | |
154 | UXOR(rdv, sv, svm); | |
155 | UXOR(tdv, sv, ssv); | |
156 | } | |
157 | ||
158 | if (m > GFK_THRESH) | |
159 | gfx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl); | |
160 | else | |
161 | gfx_mul(sv, ssv, av, avm, bv, bvm); | |
162 | MPX_COPY(dv, tdv, sv, svm); | |
163 | UXOR(tdv, sv, ssv); | |
164 | UXOR(tdv, svm, ssv); | |
165 | } | |
166 | } | |
167 | ||
168 | /*----- Test rig ----------------------------------------------------------*/ | |
169 | ||
170 | #ifdef TEST_RIG | |
171 | ||
172 | #include <mLib/alloc.h> | |
173 | #include <mLib/testrig.h> | |
174 | ||
45c0fd36 MW |
175 | #define ALLOC(v, vl, sz) do { \ |
176 | size_t _sz = (sz); \ | |
177 | mpw *_vv = xmalloc(MPWS(_sz)); \ | |
178 | mpw *_vvl = _vv + _sz; \ | |
179 | (v) = _vv; \ | |
180 | (vl) = _vvl; \ | |
ae747c9b | 181 | } while (0) |
182 | ||
45c0fd36 MW |
183 | #define LOAD(v, vl, d) do { \ |
184 | const dstr *_d = (d); \ | |
185 | mpw *_v, *_vl; \ | |
186 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ | |
187 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ | |
188 | (v) = _v; \ | |
189 | (vl) = _vl; \ | |
ae747c9b | 190 | } while (0) |
191 | ||
192 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) | |
193 | ||
194 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) | |
195 | { | |
196 | fputs(msg, stderr); | |
197 | MPX_SHRINK(v, vl); | |
198 | while (v < vl) | |
199 | fprintf(stderr, " %08lx", (unsigned long)*--vl); | |
200 | fputc('\n', stderr); | |
201 | } | |
202 | ||
203 | static int mul(dstr *v) | |
204 | { | |
205 | mpw *a, *al; | |
206 | mpw *b, *bl; | |
207 | mpw *c, *cl; | |
208 | mpw *d, *dl; | |
209 | mpw *s, *sl; | |
210 | size_t m; | |
211 | int ok = 1; | |
212 | ||
213 | LOAD(a, al, &v[0]); | |
214 | LOAD(b, bl, &v[1]); | |
215 | LOAD(c, cl, &v[2]); | |
216 | m = MAX(al - a, bl - b) + 1; | |
217 | ALLOC(d, dl, 2 * m); | |
218 | ALLOC(s, sl, 2 * m); | |
219 | ||
220 | gfx_kmul(d, dl, a, al, b, bl, s, sl); | |
221 | if (!mpx_ueq(d, dl, c, cl)) { | |
222 | fprintf(stderr, "\n*** mul failed\n"); | |
45c0fd36 MW |
223 | dumpmp(" a", a, al); |
224 | dumpmp(" b", b, bl); | |
ae747c9b | 225 | dumpmp("expected", c, cl); |
226 | dumpmp(" result", d, dl); | |
227 | ok = 0; | |
228 | } | |
229 | ||
12ed8a1f | 230 | xfree(a); xfree(b); xfree(c); xfree(d); xfree(s); |
ae747c9b | 231 | return (ok); |
232 | } | |
233 | ||
234 | static test_chunk defs[] = { | |
235 | { "mul", mul, { &type_hex, &type_hex, &type_hex, 0 } }, | |
236 | { 0, 0, { 0 } } | |
237 | }; | |
238 | ||
239 | int main(int argc, char *argv[]) | |
240 | { | |
0f00dc4c | 241 | test_run(argc, argv, defs, SRCDIR"/t/gfx"); |
ae747c9b | 242 | return (0); |
243 | } | |
244 | ||
245 | #endif | |
246 | ||
247 | /*----- That's all, folks -------------------------------------------------*/ |