3 * $Id: gfx-sqr.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
5 * Sqaring binary polynomials
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
34 #include "gfx-sqr-tab.h"
36 /*----- Static variables --------------------------------------------------*/
38 static const uint16 tab
[256] = GFX_SQRTAB
;
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @gfx_sqr@ --- *
44 * Arguments: @mpw *dv, *dvl@ = destination vector base and limit
45 * @const mpw *av, *avl@ = argument vector base and limit
49 * Use: Performs squaring of binary polynomials.
52 void gfx_sqr(mpw
*dv
, mpw
*dvl
, const mpw
*av
, const mpw
*avl
)
55 unsigned b
= 0, bb
= 0;
57 /* --- Simple stuff --- */
63 /* --- The main algorithm --- *
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.
76 /* --- Input buffering --- */
85 /* --- Do the work in the middle --- */
87 aa
|= (mpd
)(tab
[U8(a
)]) << bb
;
92 /* --- Output buffering --- */
103 /* --- Flush the input buffer --- */
106 aa
|= (mpd
)(tab
[U8(a
)]) << bb
;
122 /* --- Flush the output buffer --- */
135 /* --- Zero the rest of everything --- */
140 /*----- Test rig ----------------------------------------------------------*/
144 #include <mLib/alloc.h>
145 #include <mLib/dstr.h>
146 #include <mLib/quis.h>
147 #include <mLib/testrig.h>
149 #define ALLOC(v, vl, sz) do { \
151 mpw *_vv = xmalloc(MPWS(_sz)); \
152 mpw *_vvl = _vv + _sz; \
157 #define LOAD(v, vl, d) do { \
158 const dstr *_d = (d); \
160 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
161 mpx_loadb(_v, _vl, _d->buf, _d->len); \
166 #define MAX(x, y) ((x) > (y) ? (x) : (y))
168 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
173 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
177 static int vsqr(dstr
*v
)
186 ALLOC(d
, dl
, 2 * (al
- a
));
188 gfx_sqr(d
, dl
, a
, al
);
189 if (!mpx_ueq(d
, dl
, b
, bl
)) {
190 fprintf(stderr
, "\n*** vsqr failed\n");
192 dumpmp("expected", b
, bl
);
193 dumpmp(" result", d
, dl
);
197 free(a
); free(b
); free(d
);
201 static test_chunk defs
[] = {
202 { "sqr", vsqr
, { &type_hex
, &type_hex
, 0 } },
206 int main(int argc
, char *argv
[])
208 test_run(argc
, argv
, defs
, SRCDIR
"/tests/gfx");
214 /*----- That's all, folks -------------------------------------------------*/