a86e33af |
1 | /* -*-c-*- |
2 | * |
52cdaca9 |
3 | * $Id: mpx-kmul.c,v 1.7 2000/10/08 15:48:35 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 $ |
52cdaca9 |
33 | * Revision 1.7 2000/10/08 15:48:35 mdw |
34 | * Rename Karatsuba constants now that we have @gfx_kmul@ too. |
35 | * |
c9060100 |
36 | * Revision 1.6 2000/10/08 12:11:01 mdw |
37 | * Use @mpx_ueq@ instead of @MPX_UCMP@. |
38 | * |
07dc33b2 |
39 | * Revision 1.5 2000/07/29 17:04:02 mdw |
40 | * Remove useless header `mpscan.h'. |
41 | * |
7d5fa32a |
42 | * Revision 1.4 2000/06/17 11:42:11 mdw |
43 | * Moved the Karatsuba macros into a separate file for better sharing. |
44 | * Fixed some comments. |
45 | * |
4468424e |
46 | * Revision 1.3 1999/12/13 15:35:01 mdw |
47 | * Simplify and improve. |
48 | * |
1b756626 |
49 | * Revision 1.2 1999/12/11 10:58:02 mdw |
50 | * Remove tweakable comments. |
51 | * |
a86e33af |
52 | * Revision 1.1 1999/12/10 23:23:51 mdw |
53 | * Karatsuba-Ofman multiplication algorithm. |
54 | * |
55 | */ |
56 | |
57 | /*----- Header files ------------------------------------------------------*/ |
58 | |
4468424e |
59 | #include <assert.h> |
a86e33af |
60 | #include <stdio.h> |
61 | |
62 | #include "mpx.h" |
52cdaca9 |
63 | #include "karatsuba.h" |
a86e33af |
64 | |
65 | /*----- Tweakables --------------------------------------------------------*/ |
66 | |
a86e33af |
67 | #ifdef TEST_RIG |
52cdaca9 |
68 | # undef MPK_THRESH |
69 | # define MPK_THRESH 2 |
a86e33af |
70 | #endif |
71 | |
a86e33af |
72 | /*----- Main code ---------------------------------------------------------*/ |
73 | |
74 | /* --- @mpx_kmul@ --- * |
75 | * |
76 | * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer |
77 | * @const mpw *av, *avl@ = pointer to first argument |
78 | * @const mpw *bv, *bvl@ = pointer to second argument |
79 | * @mpw *sv, *svl@ = pointer to scratch workspace |
80 | * |
81 | * Returns: --- |
82 | * |
83 | * Use: Multiplies two multiprecision integers using Karatsuba's |
84 | * algorithm. This is rather faster than traditional long |
85 | * multiplication (e.g., @mpx_umul@) on large numbers, although |
86 | * more expensive on small ones. |
87 | * |
88 | * The destination must be twice as large as the larger |
89 | * argument. The scratch space must be twice as large as the |
52cdaca9 |
90 | * larger argument, plus the magic number @MPK_SLOP@. |
a86e33af |
91 | */ |
92 | |
93 | void mpx_kmul(mpw *dv, mpw *dvl, |
94 | const mpw *av, const mpw *avl, |
95 | const mpw *bv, const mpw *bvl, |
96 | mpw *sv, mpw *svl) |
97 | { |
98 | const mpw *avm, *bvm; |
99 | size_t m; |
100 | |
101 | /* --- Dispose of easy cases to @mpx_umul@ --- * |
102 | * |
103 | * Karatsuba is only a win on large numbers, because of all the |
104 | * recursiveness and bookkeeping. The recursive calls make a quick check |
105 | * to see whether to bottom out to @mpx_umul@ which should help quite a |
106 | * lot, but sometimes the only way to know is to make sure... |
107 | */ |
108 | |
109 | MPX_SHRINK(av, avl); |
110 | MPX_SHRINK(bv, bvl); |
111 | |
52cdaca9 |
112 | if (avl - av <= MPK_THRESH || bvl - bv <= MPK_THRESH) { |
a86e33af |
113 | mpx_umul(dv, dvl, av, avl, bv, bvl); |
114 | return; |
115 | } |
116 | |
117 | /* --- How the algorithm works --- * |
118 | * |
7d5fa32a |
119 | * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding, |
120 | * %$AB = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because |
121 | * I've got four multiplications, each four times easier than the one I |
122 | * started with. However, note that I can rewrite the coefficient of %$b$% |
123 | * as %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$% |
a86e33af |
124 | * I've already calculated, and that leaves only one more multiplication to |
125 | * do. So now I have three multiplications, each four times easier, and |
126 | * that's a win. |
127 | */ |
128 | |
129 | /* --- First things --- * |
130 | * |
131 | * Sort out where to break the factors in half. I'll choose the midpoint |
52cdaca9 |
132 | * of the larger one, since this minimizes the amount of work I have to do |
a86e33af |
133 | * most effectively. |
134 | */ |
135 | |
136 | if (avl - av > bvl - bv) { |
137 | m = (avl - av + 1) >> 1; |
138 | avm = av + m; |
139 | if (bvl - bv > m) |
140 | bvm = bv + m; |
141 | else |
142 | bvm = bvl; |
143 | } else { |
144 | m = (bvl - bv + 1) >> 1; |
145 | bvm = bv + m; |
146 | if (avl - av > m) |
147 | avm = av + m; |
148 | else |
149 | avm = avl; |
150 | } |
151 | |
4468424e |
152 | assert(((void)"Destination too small for Karatsuba multiply", |
153 | dvl - dv >= 4 * m)); |
154 | assert(((void)"Not enough workspace for Karatsuba multiply", |
155 | svl - sv >= 4 * m)); |
156 | |
157 | /* --- Sort out the middle term --- */ |
a86e33af |
158 | |
159 | { |
4468424e |
160 | mpw *bsv = sv + m + 1, *ssv = bsv + m + 1; |
161 | mpw *rdv = dv + m, *rdvl = rdv + 2 * (m + 2); |
162 | |
163 | UADD2(sv, bsv, av, avm, avm, avl); |
164 | UADD2(bsv, ssv, bv, bvm, bvm, bvl); |
52cdaca9 |
165 | if (m > MPK_THRESH) |
a86e33af |
166 | mpx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl); |
167 | else |
168 | mpx_umul(rdv, rdvl, sv, bsv, bsv, ssv); |
a86e33af |
169 | } |
170 | |
171 | /* --- Sort out the other two terms --- */ |
172 | |
173 | { |
4468424e |
174 | mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4; |
a86e33af |
175 | mpw *tdv = dv + m; |
176 | mpw *rdv = tdv + m; |
177 | |
4468424e |
178 | if (avl == avm || bvl == bvm) |
179 | MPX_ZERO(rdv + m + 1, dvl); |
180 | else { |
52cdaca9 |
181 | if (m > MPK_THRESH) |
4468424e |
182 | mpx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl); |
183 | else |
184 | mpx_umul(sv, ssv, avm, avl, bvm, bvl); |
185 | MPX_COPY(rdv + m + 1, dvl, svm + 1, svn); |
186 | UADD(rdv, sv, svm + 1); |
187 | USUB(tdv, sv, svn); |
188 | } |
189 | |
52cdaca9 |
190 | if (m > MPK_THRESH) |
a86e33af |
191 | mpx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl); |
192 | else |
193 | mpx_umul(sv, ssv, av, avm, bv, bvm); |
4468424e |
194 | MPX_COPY(dv, tdv, sv, svm); |
195 | USUB(tdv, sv, svn); |
196 | UADD(tdv, svm, svn); |
a86e33af |
197 | } |
198 | } |
199 | |
200 | /*----- Test rig ----------------------------------------------------------*/ |
201 | |
202 | #ifdef TEST_RIG |
203 | |
204 | #include <mLib/alloc.h> |
205 | #include <mLib/testrig.h> |
206 | |
a86e33af |
207 | #define ALLOC(v, vl, sz) do { \ |
208 | size_t _sz = (sz); \ |
209 | mpw *_vv = xmalloc(MPWS(_sz)); \ |
210 | mpw *_vvl = _vv + _sz; \ |
211 | (v) = _vv; \ |
212 | (vl) = _vvl; \ |
213 | } while (0) |
214 | |
215 | #define LOAD(v, vl, d) do { \ |
216 | const dstr *_d = (d); \ |
217 | mpw *_v, *_vl; \ |
218 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ |
219 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ |
220 | (v) = _v; \ |
221 | (vl) = _vl; \ |
222 | } while (0) |
223 | |
224 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
225 | |
226 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) |
227 | { |
228 | fputs(msg, stderr); |
229 | MPX_SHRINK(v, vl); |
230 | while (v < vl) |
231 | fprintf(stderr, " %08lx", (unsigned long)*--vl); |
232 | fputc('\n', stderr); |
233 | } |
234 | |
235 | static int umul(dstr *v) |
236 | { |
237 | mpw *a, *al; |
238 | mpw *b, *bl; |
239 | mpw *c, *cl; |
240 | mpw *d, *dl; |
241 | mpw *s, *sl; |
242 | size_t m; |
243 | int ok = 1; |
244 | |
245 | LOAD(a, al, &v[0]); |
246 | LOAD(b, bl, &v[1]); |
247 | LOAD(c, cl, &v[2]); |
248 | m = MAX(al - a, bl - b) + 1; |
249 | ALLOC(d, dl, 2 * m); |
250 | ALLOC(s, sl, 2 * m + 32); |
251 | |
252 | mpx_kmul(d, dl, a, al, b, bl, s, sl); |
c9060100 |
253 | if (!mpx_ueq(d, dl, c, cl)) { |
a86e33af |
254 | fprintf(stderr, "\n*** umul failed\n"); |
255 | dumpmp(" a", a, al); |
256 | dumpmp(" b", b, bl); |
257 | dumpmp("expected", c, cl); |
258 | dumpmp(" result", d, dl); |
259 | ok = 0; |
260 | } |
261 | |
262 | free(a); free(b); free(c); free(d); free(s); |
263 | return (ok); |
264 | } |
265 | |
266 | static test_chunk defs[] = { |
267 | { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } }, |
268 | { 0, 0, { 0 } } |
269 | }; |
270 | |
271 | int main(int argc, char *argv[]) |
272 | { |
273 | test_run(argc, argv, defs, SRCDIR"/tests/mpx"); |
274 | return (0); |
275 | } |
276 | |
277 | #endif |
278 | |
279 | /*----- That's all, folks -------------------------------------------------*/ |