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