a86e33af |
1 | /* -*-c-*- |
2 | * |
4468424e |
3 | * $Id: mpx-kmul.c,v 1.3 1999/12/13 15:35:01 mdw Exp $ |
a86e33af |
4 | * |
5 | * Karatsuba's multiplication algorithm |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: mpx-kmul.c,v $ |
4468424e |
33 | * Revision 1.3 1999/12/13 15:35:01 mdw |
34 | * Simplify and improve. |
35 | * |
1b756626 |
36 | * Revision 1.2 1999/12/11 10:58:02 mdw |
37 | * Remove tweakable comments. |
38 | * |
a86e33af |
39 | * Revision 1.1 1999/12/10 23:23:51 mdw |
40 | * Karatsuba-Ofman multiplication algorithm. |
41 | * |
42 | */ |
43 | |
44 | /*----- Header files ------------------------------------------------------*/ |
45 | |
4468424e |
46 | #include <assert.h> |
a86e33af |
47 | #include <stdio.h> |
48 | |
49 | #include "mpx.h" |
50 | |
51 | /*----- Tweakables --------------------------------------------------------*/ |
52 | |
a86e33af |
53 | #ifdef TEST_RIG |
54 | # undef KARATSUBA_CUTOFF |
55 | # define KARATSUBA_CUTOFF 2 |
56 | #endif |
57 | |
58 | /*----- Addition macros ---------------------------------------------------*/ |
59 | |
60 | #define UADD(dv, av, avl) do { \ |
61 | mpw *_dv = (dv); \ |
62 | const mpw *_av = (av), *_avl = (avl); \ |
63 | mpw _c = 0; \ |
64 | \ |
65 | while (_av < _avl) { \ |
66 | mpw _a, _b; \ |
67 | mpd _x; \ |
68 | _a = *_av++; \ |
69 | _b = *_dv; \ |
70 | _x = (mpd)_a + (mpd)_b + _c; \ |
71 | *_dv++ = MPW(_x); \ |
72 | _c = _x >> MPW_BITS; \ |
73 | } \ |
74 | while (_c) { \ |
75 | mpd _x = (mpd)*_dv + (mpd)_c; \ |
76 | *_dv++ = MPW(_x); \ |
77 | _c = _x >> MPW_BITS; \ |
78 | } \ |
79 | } while (0) |
80 | |
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); \ |
85 | mpw _c = 0; \ |
86 | \ |
87 | while (_av < _avl || _bv < _bvl) { \ |
88 | mpw _a, _b; \ |
89 | mpd _x; \ |
90 | _a = (_av < _avl) ? *_av++ : 0; \ |
91 | _b = (_bv < _bvl) ? *_bv++ : 0; \ |
92 | _x = (mpd)_a + (mpd)_b + _c; \ |
93 | *_dv++ = MPW(_x); \ |
94 | _c = _x >> MPW_BITS; \ |
95 | } \ |
96 | *_dv++ = _c; \ |
97 | while (_dv < _dvl) \ |
98 | *_dv++ = 0; \ |
99 | } while (0) |
100 | |
101 | #define USUB(dv, av, avl) do { \ |
102 | mpw *_dv = (dv); \ |
103 | const mpw *_av = (av), *_avl = (avl); \ |
104 | mpw _c = 0; \ |
105 | \ |
106 | while (_av < _avl) { \ |
107 | mpw _a, _b; \ |
108 | mpd _x; \ |
109 | _a = *_av++; \ |
110 | _b = *_dv; \ |
111 | _x = (mpd)_b - (mpd)_a - _c; \ |
112 | *_dv++ = MPW(_x); \ |
113 | if (_x >> MPW_BITS) \ |
114 | _c = 1; \ |
115 | else \ |
116 | _c = 0; \ |
117 | } \ |
118 | while (_c) { \ |
119 | mpd _x = (mpd)*_dv - (mpd)_c; \ |
120 | *_dv++ = MPW(_x); \ |
121 | if (_x >> MPW_BITS) \ |
122 | _c = 1; \ |
123 | else \ |
124 | _c = 0; \ |
125 | } \ |
126 | } while (0) |
127 | |
128 | /*----- Main code ---------------------------------------------------------*/ |
129 | |
130 | /* --- @mpx_kmul@ --- * |
131 | * |
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 |
136 | * |
137 | * Returns: --- |
138 | * |
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. |
143 | * |
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@. |
a86e33af |
147 | */ |
148 | |
149 | void mpx_kmul(mpw *dv, mpw *dvl, |
150 | const mpw *av, const mpw *avl, |
151 | const mpw *bv, const mpw *bvl, |
152 | mpw *sv, mpw *svl) |
153 | { |
154 | const mpw *avm, *bvm; |
155 | size_t m; |
156 | |
157 | /* --- Dispose of easy cases to @mpx_umul@ --- * |
158 | * |
159 | * Karatsuba is only a win on large numbers, because of all the |
160 | * recursiveness and bookkeeping. The recursive calls make a quick check |
161 | * to see whether to bottom out to @mpx_umul@ which should help quite a |
162 | * lot, but sometimes the only way to know is to make sure... |
163 | */ |
164 | |
165 | MPX_SHRINK(av, avl); |
166 | MPX_SHRINK(bv, bvl); |
167 | |
168 | if (avl - av <= KARATSUBA_CUTOFF || bvl - bv <= KARATSUBA_CUTOFF) { |
169 | mpx_umul(dv, dvl, av, avl, bv, bvl); |
170 | return; |
171 | } |
172 | |
173 | /* --- How the algorithm works --- * |
174 | * |
175 | * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding, %$AB |
176 | * = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because I've |
177 | * got four multiplications, each four times easier than the one I started |
178 | * with. However, note that I can rewrite the coefficient of %$b$% as |
179 | * %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$% |
180 | * I've already calculated, and that leaves only one more multiplication to |
181 | * do. So now I have three multiplications, each four times easier, and |
182 | * that's a win. |
183 | */ |
184 | |
185 | /* --- First things --- * |
186 | * |
187 | * Sort out where to break the factors in half. I'll choose the midpoint |
188 | * of the largest one, since this minimizes the amount of work I have to do |
189 | * most effectively. |
190 | */ |
191 | |
192 | if (avl - av > bvl - bv) { |
193 | m = (avl - av + 1) >> 1; |
194 | avm = av + m; |
195 | if (bvl - bv > m) |
196 | bvm = bv + m; |
197 | else |
198 | bvm = bvl; |
199 | } else { |
200 | m = (bvl - bv + 1) >> 1; |
201 | bvm = bv + m; |
202 | if (avl - av > m) |
203 | avm = av + m; |
204 | else |
205 | avm = avl; |
206 | } |
207 | |
4468424e |
208 | assert(((void)"Destination too small for Karatsuba multiply", |
209 | dvl - dv >= 4 * m)); |
210 | assert(((void)"Not enough workspace for Karatsuba multiply", |
211 | svl - sv >= 4 * m)); |
212 | |
213 | /* --- Sort out the middle term --- */ |
a86e33af |
214 | |
215 | { |
4468424e |
216 | mpw *bsv = sv + m + 1, *ssv = bsv + m + 1; |
217 | mpw *rdv = dv + m, *rdvl = rdv + 2 * (m + 2); |
218 | |
219 | UADD2(sv, bsv, av, avm, avm, avl); |
220 | UADD2(bsv, ssv, bv, bvm, bvm, bvl); |
a86e33af |
221 | if (m > KARATSUBA_CUTOFF) |
222 | mpx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl); |
223 | else |
224 | mpx_umul(rdv, rdvl, sv, bsv, bsv, ssv); |
a86e33af |
225 | } |
226 | |
227 | /* --- Sort out the other two terms --- */ |
228 | |
229 | { |
4468424e |
230 | mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4; |
a86e33af |
231 | mpw *tdv = dv + m; |
232 | mpw *rdv = tdv + m; |
233 | |
4468424e |
234 | if (avl == avm || bvl == bvm) |
235 | MPX_ZERO(rdv + m + 1, dvl); |
236 | else { |
237 | if (m > KARATSUBA_CUTOFF) |
238 | mpx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl); |
239 | else |
240 | mpx_umul(sv, ssv, avm, avl, bvm, bvl); |
241 | MPX_COPY(rdv + m + 1, dvl, svm + 1, svn); |
242 | UADD(rdv, sv, svm + 1); |
243 | USUB(tdv, sv, svn); |
244 | } |
245 | |
a86e33af |
246 | if (m > KARATSUBA_CUTOFF) |
247 | mpx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl); |
248 | else |
249 | mpx_umul(sv, ssv, av, avm, bv, bvm); |
4468424e |
250 | MPX_COPY(dv, tdv, sv, svm); |
251 | USUB(tdv, sv, svn); |
252 | UADD(tdv, svm, svn); |
a86e33af |
253 | } |
254 | } |
255 | |
256 | /*----- Test rig ----------------------------------------------------------*/ |
257 | |
258 | #ifdef TEST_RIG |
259 | |
260 | #include <mLib/alloc.h> |
261 | #include <mLib/testrig.h> |
262 | |
263 | #include "mpscan.h" |
264 | |
265 | #define ALLOC(v, vl, sz) do { \ |
266 | size_t _sz = (sz); \ |
267 | mpw *_vv = xmalloc(MPWS(_sz)); \ |
268 | mpw *_vvl = _vv + _sz; \ |
269 | (v) = _vv; \ |
270 | (vl) = _vvl; \ |
271 | } while (0) |
272 | |
273 | #define LOAD(v, vl, d) do { \ |
274 | const dstr *_d = (d); \ |
275 | mpw *_v, *_vl; \ |
276 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ |
277 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ |
278 | (v) = _v; \ |
279 | (vl) = _vl; \ |
280 | } while (0) |
281 | |
282 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
283 | |
284 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) |
285 | { |
286 | fputs(msg, stderr); |
287 | MPX_SHRINK(v, vl); |
288 | while (v < vl) |
289 | fprintf(stderr, " %08lx", (unsigned long)*--vl); |
290 | fputc('\n', stderr); |
291 | } |
292 | |
293 | static int umul(dstr *v) |
294 | { |
295 | mpw *a, *al; |
296 | mpw *b, *bl; |
297 | mpw *c, *cl; |
298 | mpw *d, *dl; |
299 | mpw *s, *sl; |
300 | size_t m; |
301 | int ok = 1; |
302 | |
303 | LOAD(a, al, &v[0]); |
304 | LOAD(b, bl, &v[1]); |
305 | LOAD(c, cl, &v[2]); |
306 | m = MAX(al - a, bl - b) + 1; |
307 | ALLOC(d, dl, 2 * m); |
308 | ALLOC(s, sl, 2 * m + 32); |
309 | |
310 | mpx_kmul(d, dl, a, al, b, bl, s, sl); |
311 | if (MPX_UCMP(d, dl, !=, c, cl)) { |
312 | fprintf(stderr, "\n*** umul failed\n"); |
313 | dumpmp(" a", a, al); |
314 | dumpmp(" b", b, bl); |
315 | dumpmp("expected", c, cl); |
316 | dumpmp(" result", d, dl); |
317 | ok = 0; |
318 | } |
319 | |
320 | free(a); free(b); free(c); free(d); free(s); |
321 | return (ok); |
322 | } |
323 | |
324 | static test_chunk defs[] = { |
325 | { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } }, |
326 | { 0, 0, { 0 } } |
327 | }; |
328 | |
329 | int main(int argc, char *argv[]) |
330 | { |
331 | test_run(argc, argv, defs, SRCDIR"/tests/mpx"); |
332 | return (0); |
333 | } |
334 | |
335 | #endif |
336 | |
337 | /*----- That's all, folks -------------------------------------------------*/ |