X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/b817bfc642225b8c3c0b6a7e42d1fb949b61a606..a6864ad907239985bd1f6eab414cec6171930d46:/calc/gfx.cal diff --git a/calc/gfx.cal b/calc/gfx.cal index 45f534b..01d5c10 100644 --- a/calc/gfx.cal +++ b/calc/gfx.cal @@ -7,7 +7,7 @@ * (c) 2000 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,12 +15,12 @@ * 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, @@ -97,24 +97,36 @@ define gf_mod(x, y) return gf(l[[1]]); } -define gf_inv(a, b) +define gf_gcd(a, b) { - local g, x, y, X, Y, u, v, t, q, r; - x = gf(1); X = gf(0); - y = gf(0); Y = gf(1); - - if (b == gf(0)) { g = a; } else if (a == gf(0)) { g = b; } + local swap = 0; + local g, x = 1, X = 0, y = 0, Y = 1, q, r, t; + if (a.x < b.x) { + t = a; a = b; b = t; + swap = 1; + } + if (b == gf(0)) + g = a; else { while (b != gf(0)) { - q = gf_div(b, a); r = gf_mod(b, a); + q = gf_div(a, b); r = gf_mod(a, b); t = X * q + x; x = X; X = t; t = Y * q + y; y = Y; Y = t; - b = a; a = r; + a = b; b = r; } g = a; } - if (g != gf(1)) quit "not coprime in gf_inv"; - return Y; + if (swap) { + t = x; x = y; y = t; + } + return list(g, x, y); +} + +define gf_inv(a, b) +{ + local l = gf_gcd(b, a); + if (l[[0]] != gf(1)) quit "not coprime in gf_inv"; + return l[[2]]; } /*----- That's all, folks -------------------------------------------------*/