chiark / gitweb /
Merge branch '2.4.x' into 2.5.x
[catacomb] / math / mpx-ksqr.c
CommitLineData
5bf74dea 1/* -*-c-*-
5bf74dea 2 *
3 * Karatsuba-based squaring algorithm
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
5bf74dea 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 *
5bf74dea 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 *
5bf74dea 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
5bf74dea 28/*----- Header files ------------------------------------------------------*/
29
4468424e 30#include <assert.h>
5bf74dea 31#include <stdio.h>
32
33#include "mpx.h"
52cdaca9 34#include "karatsuba.h"
5bf74dea 35
36/*----- Tweakables --------------------------------------------------------*/
37
38#ifdef TEST_RIG
52cdaca9 39# undef MPK_THRESH
dd22938e 40# define MPK_THRESH 4
5bf74dea 41#endif
42
5bf74dea 43/*----- Main code ---------------------------------------------------------*/
44
45/* --- @mpx_ksqr@ --- *
46 *
47 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
48 * @const mpw *av, *avl@ = pointer to first argument
49 * @mpw *sv, *svl@ = pointer to scratch workspace
50 *
51 * Returns: ---
52 *
53 * Use: Squares a multiprecision integers using something similar to
54 * Karatsuba's multiplication algorithm. This is rather faster
55 * than traditional long multiplication (e.g., @mpx_umul@) on
56 * large numbers, although more expensive on small ones, and
57 * rather simpler than full-blown Karatsuba multiplication.
58 *
dd22938e 59 * The destination must be three times as large as the larger
60 * argument. The scratch space must be five times as large as
61 * the larger argument.
5bf74dea 62 */
63
64void mpx_ksqr(mpw *dv, mpw *dvl,
65 const mpw *av, const mpw *avl,
66 mpw *sv, mpw *svl)
67{
68 const mpw *avm;
69 size_t m;
70
71 /* --- Dispose of easy cases to @mpx_usqr@ --- *
72 *
73 * Karatsuba is only a win on large numbers, because of all the
74 * recursiveness and bookkeeping. The recursive calls make a quick check
75 * to see whether to bottom out to @mpx_usqr@ which should help quite a
76 * lot, but sometimes the only way to know is to make sure...
77 */
78
79 MPX_SHRINK(av, avl);
80
52cdaca9 81 if (avl - av <= MPK_THRESH) {
5bf74dea 82 mpx_usqr(dv, dvl, av, avl);
83 return;
84 }
85
86 /* --- How the algorithm works --- *
87 *
d2d86297 88 * The identity for squaring is known to all schoolchildren.
89 * Let %$A = xb + y$%. Then %$A^2 = x^2 b^2 + 2 x y b + y^2$%. Now,
90 * %$(x + y)^2 - x^2 - y^2 = 2 x y$%, which means I only need to do three
91 * squarings.
5bf74dea 92 */
93
94 /* --- First things --- *
95 *
96 * Sort out where to break the factor in half.
97 */
98
99 m = (avl - av + 1) >> 1;
100 avm = av + m;
101
102 /* --- Sort out everything --- */
103
104 {
4468424e 105 mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4;
5bf74dea 106 mpw *tdv = dv + m;
107 mpw *rdv = tdv + m;
108
dd22938e 109 assert(rdv + m + 4 < dvl);
110 assert(ssv < svl);
d2d86297 111 UADD2(sv, svm, av, avm, avm, avl);
52cdaca9 112 if (m > MPK_THRESH)
d2d86297 113 mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl);
5bf74dea 114 else
d2d86297 115 mpx_usqr(tdv, rdv + m + 4, sv, svm + 1);
5bf74dea 116
52cdaca9 117 if (m > MPK_THRESH)
5bf74dea 118 mpx_ksqr(sv, ssv, avm, avl, ssv, svl);
119 else
120 mpx_usqr(sv, ssv, avm, avl);
4468424e 121 MPX_COPY(rdv + m + 1, dvl, svm + 1, svn);
122 UADD(rdv, sv, svm + 1);
d2d86297 123 USUB(tdv, sv, svn);
45c0fd36 124
52cdaca9 125 if (m > MPK_THRESH)
5bf74dea 126 mpx_ksqr(sv, ssv, av, avm, ssv, svl);
127 else
128 mpx_usqr(sv, ssv, av, avm);
4468424e 129 MPX_COPY(dv, tdv, sv, svm);
130 UADD(tdv, svm, svn);
d2d86297 131 USUB(tdv, sv, svn);
5bf74dea 132 }
133}
134
135/*----- Test rig ----------------------------------------------------------*/
136
137#ifdef TEST_RIG
138
139#include <mLib/alloc.h>
140#include <mLib/testrig.h>
141
45c0fd36
MW
142#define ALLOC(v, vl, sz) do { \
143 size_t _sz = (sz); \
144 mpw *_vv = xmalloc(MPWS(_sz)); \
145 mpw *_vvl = _vv + _sz; \
146 (v) = _vv; \
147 (vl) = _vvl; \
5bf74dea 148} while (0)
149
45c0fd36
MW
150#define LOAD(v, vl, d) do { \
151 const dstr *_d = (d); \
152 mpw *_v, *_vl; \
153 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
154 mpx_loadb(_v, _vl, _d->buf, _d->len); \
155 (v) = _v; \
156 (vl) = _vl; \
5bf74dea 157} while (0)
158
159#define MAX(x, y) ((x) > (y) ? (x) : (y))
160
161static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
162{
163 fputs(msg, stderr);
164 MPX_SHRINK(v, vl);
165 while (v < vl)
166 fprintf(stderr, " %08lx", (unsigned long)*--vl);
167 fputc('\n', stderr);
168}
169
170static int usqr(dstr *v)
171{
172 mpw *a, *al;
173 mpw *c, *cl;
174 mpw *d, *dl;
175 mpw *s, *sl;
176 size_t m;
177 int ok = 1;
178
179 LOAD(a, al, &v[0]);
180 LOAD(c, cl, &v[1]);
181 m = al - a + 1;
dd22938e 182 ALLOC(d, dl, 3 * m);
183 ALLOC(s, sl, 5 * m);
5bf74dea 184
185 mpx_ksqr(d, dl, a, al, s, sl);
c9060100 186 if (!mpx_ueq(d, dl, c, cl)) {
5bf74dea 187 fprintf(stderr, "\n*** usqr failed\n");
45c0fd36 188 dumpmp(" a", a, al);
5bf74dea 189 dumpmp("expected", c, cl);
190 dumpmp(" result", d, dl);
191 ok = 0;
192 }
193
12ed8a1f 194 xfree(a); xfree(c); xfree(d); xfree(s);
5bf74dea 195 return (ok);
196}
197
198static test_chunk defs[] = {
199 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
200 { 0, 0, { 0 } }
201};
202
203int main(int argc, char *argv[])
204{
0f00dc4c 205 test_run(argc, argv, defs, SRCDIR"/t/mpx");
5bf74dea 206 return (0);
207}
208
209#endif
210
211/*----- That's all, folks -------------------------------------------------*/