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