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