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