8e26f361c28baa2724be6fa3792af649754bcb51
[u/mdw/catacomb] / math / gfx-sqr.c
1 /* -*-c-*-
2 *
3 * Sqaring binary polynomials
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "mpx.h"
31 #include "gfx.h"
32 #include "gfx-sqr-tab.h"
33
34 /*----- Static variables --------------------------------------------------*/
35
36 static const uint16 tab[256] = GFX_SQRTAB;
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- @gfx_sqr@ --- *
41 *
42 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
43 * @const mpw *av, *avl@ = argument vector base and limit
44 *
45 * Returns: ---
46 *
47 * Use: Performs squaring of binary polynomials.
48 */
49
50 void gfx_sqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl)
51 {
52 mpd a = 0, aa = 0;
53 unsigned b = 0, bb = 0;
54
55 /* --- Simple stuff --- */
56
57 if (dv >= dvl)
58 return;
59 MPX_SHRINK(av, avl);
60
61 /* --- The main algorithm --- *
62 *
63 * Our method depends on the fact that, in a field of characteristic 2, we
64 * have that %$(a + b)^2 = a^2 + b^2$%. Thus, to square a polynomial, it's
65 * sufficient just to put a zero bit between each of the bits of the
66 * original argument. We use a precomputed table for this, and work on
67 * entire octets at a time. Life is more complicated because we've got to
68 * be careful of bizarre architectures which don't have words with a
69 * multiple of 8 bits in them.
70 */
71
72 for (;;) {
73
74 /* --- Input buffering --- */
75
76 if (b < 8) {
77 if (av >= avl)
78 break;
79 a |= *av++ << b;
80 b += MPW_BITS;
81 }
82
83 /* --- Do the work in the middle --- */
84
85 aa |= (mpd)(tab[U8(a)]) << bb;
86 bb += 16;
87 a >>= 8;
88 b -= 8;
89
90 /* --- Output buffering --- */
91
92 if (bb >= MPW_BITS) {
93 *dv++ = MPW(aa);
94 if (dv >= dvl)
95 return;
96 aa >>= MPW_BITS;
97 bb -= MPW_BITS;
98 }
99 }
100
101 /* --- Flush the input buffer --- */
102
103 if (b) for (;;) {
104 aa |= (mpd)(tab[U8(a)]) << bb;
105 bb += 16;
106 if (bb > MPW_BITS) {
107 *dv++ = MPW(aa);
108 if (dv >= dvl)
109 return;
110 aa >>= MPW_BITS;
111 bb -= MPW_BITS;
112 }
113 a >>= 8;
114 if (b <= 8)
115 break;
116 else
117 b -= 8;
118 }
119
120 /* --- Flush the output buffer --- */
121
122 if (bb) for (;;) {
123 *dv++ = MPW(aa);
124 if (dv >= dvl)
125 return;
126 aa >>= MPW_BITS;
127 if (bb <= MPW_BITS)
128 break;
129 else
130 bb -= MPW_BITS;
131 }
132
133 /* --- Zero the rest of everything --- */
134
135 MPX_ZERO(dv, dvl);
136 }
137
138 /*----- Test rig ----------------------------------------------------------*/
139
140 #ifdef TEST_RIG
141
142 #include <mLib/alloc.h>
143 #include <mLib/dstr.h>
144 #include <mLib/quis.h>
145 #include <mLib/testrig.h>
146
147 #define ALLOC(v, vl, sz) do { \
148 size_t _sz = (sz); \
149 mpw *_vv = xmalloc(MPWS(_sz)); \
150 mpw *_vvl = _vv + _sz; \
151 (v) = _vv; \
152 (vl) = _vvl; \
153 } while (0)
154
155 #define LOAD(v, vl, d) do { \
156 const dstr *_d = (d); \
157 mpw *_v, *_vl; \
158 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
159 mpx_loadb(_v, _vl, _d->buf, _d->len); \
160 (v) = _v; \
161 (vl) = _vl; \
162 } while (0)
163
164 #define MAX(x, y) ((x) > (y) ? (x) : (y))
165
166 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
167 {
168 fputs(msg, stderr);
169 MPX_SHRINK(v, vl);
170 while (v < vl)
171 fprintf(stderr, " %08lx", (unsigned long)*--vl);
172 fputc('\n', stderr);
173 }
174
175 static int vsqr(dstr *v)
176 {
177 mpw *a, *al;
178 mpw *b, *bl;
179 mpw *d, *dl;
180 int ok = 1;
181
182 LOAD(a, al, &v[0]);
183 LOAD(b, bl, &v[1]);
184 ALLOC(d, dl, 2 * (al - a));
185
186 gfx_sqr(d, dl, a, al);
187 if (!mpx_ueq(d, dl, b, bl)) {
188 fprintf(stderr, "\n*** vsqr failed\n");
189 dumpmp(" a", a, al);
190 dumpmp("expected", b, bl);
191 dumpmp(" result", d, dl);
192 ok = 0;
193 }
194
195 xfree(a); xfree(b); xfree(d);
196 return (ok);
197 }
198
199 static test_chunk defs[] = {
200 { "sqr", vsqr, { &type_hex, &type_hex, 0 } },
201 { 0, 0, { 0 } }
202 };
203
204 int main(int argc, char *argv[])
205 {
206 test_run(argc, argv, defs, SRCDIR"/t/gfx");
207 return (0);
208 }
209
210 #endif
211
212 /*----- That's all, folks -------------------------------------------------*/