X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/7c0acba656af3823577d1341f39c0bbe86632681..a2a74efe4b1276b1efec12f25fd65f0df060f38a:/calc/gfx.cal diff --git a/calc/gfx.cal b/calc/gfx.cal new file mode 100644 index 0000000..8d8fd00 --- /dev/null +++ b/calc/gfx.cal @@ -0,0 +1,104 @@ +/* -*-apcalc-*- + * + * $Id: gfx.cal,v 1.1 2000/10/08 16:01:37 mdw Exp $ + * + * Testbed for %$\gf{2}$% poltnomial arithmetic + * + * (c) 2000 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: gfx.cal,v $ + * Revision 1.1 2000/10/08 16:01:37 mdw + * Prototypes of various bits of code. + * + */ + +/*----- Object types ------------------------------------------------------*/ + +obj gf { x }; + +/*----- Static variables --------------------------------------------------*/ + +static obj gf example_gf_object; + +/*----- Main code ---------------------------------------------------------*/ + +dummy = config("lib_debug", -1); + +define gf(x) +{ + local obj gf g; + g.x = x; + return (g); +} + +define gfint(x) +{ + if (istype(x, example_gf_object)) + return (x.x); + else + return (x); +} + +define gf_add(x, y) = gf(xor(gfint(x), gfint(y))); +define gf_sub(x, y) = gf(xor(gfint(x), gfint(y))); +define gf_neg(x) = x; + +define gf_mul(x, y) +{ + local a = gfint(x), b = gfint(y), z = 0, i, bits = highbit(a); + for (i = 0; i <= bits; i++) { + if (bit(a, i)) + z = xor(z, b << i); + } + return gf(z); +} + +define gfx_div(rx, dx) +{ + local r = gfint(rx), d = gfint(dx), i; + local q = 0, dbits = highbit(d), rbits = highbit(r); + for (i = rbits - dbits; i >= 0; i--) { + if (bit(r, i + dbits)) { + r = xor(r, d << i); + q |= (1 << i); + } + } + return list(q, r); +} + +define gf_div(x, y) +{ + local l = gfx_div(x, y); + return gf(l[[0]]); +} + +define gf_mod(x, y) +{ + local l = gfx_div(x, y); + return gf(l[[1]]); +} + +/*----- That's all, folks -------------------------------------------------*/