From: mdw Date: Sat, 17 Jun 2000 11:42:11 +0000 (+0000) Subject: Moved the Karatsuba macros into a separate file for better sharing. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/7d5fa32a96bb9badc88fe1b07086c6f770c0de31 Moved the Karatsuba macros into a separate file for better sharing. Fixed some comments. --- diff --git a/karatsuba.h b/karatsuba.h new file mode 100644 index 0000000..885d907 --- /dev/null +++ b/karatsuba.h @@ -0,0 +1,128 @@ +/* -*-c-*- + * + * $Id: karatsuba.h,v 1.1 2000/06/17 11:42:11 mdw Exp $ + * + * Macros for Karatsuba functions + * + * (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: karatsuba.h,v $ + * Revision 1.1 2000/06/17 11:42:11 mdw + * Moved the Karatsuba macros into a separate file for better sharing. + * Fixed some comments. + * + */ + +#ifndef CATACOMB_MPX_KMAC_H +#define CATACOMB_MPX_KMAC_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#ifndef CATACOMB_MPW_H +# include "mpw.h" +#endif + +/*----- Macros provided ---------------------------------------------------*/ + +#define UADD(dv, av, avl) do { \ + mpw *_dv = (dv); \ + const mpw *_av = (av), *_avl = (avl); \ + mpw _c = 0; \ + \ + while (_av < _avl) { \ + mpw _a, _b; \ + mpd _x; \ + _a = *_av++; \ + _b = *_dv; \ + _x = (mpd)_a + (mpd)_b + _c; \ + *_dv++ = MPW(_x); \ + _c = _x >> MPW_BITS; \ + } \ + while (_c) { \ + mpd _x = (mpd)*_dv + (mpd)_c; \ + *_dv++ = MPW(_x); \ + _c = _x >> MPW_BITS; \ + } \ +} while (0) + +#define UADD2(dv, dvl, av, avl, bv, bvl) do { \ + mpw *_dv = (dv), *_dvl = (dvl); \ + const mpw *_av = (av), *_avl = (avl); \ + const mpw *_bv = (bv), *_bvl = (bvl); \ + mpw _c = 0; \ + \ + while (_av < _avl || _bv < _bvl) { \ + mpw _a, _b; \ + mpd _x; \ + _a = (_av < _avl) ? *_av++ : 0; \ + _b = (_bv < _bvl) ? *_bv++ : 0; \ + _x = (mpd)_a + (mpd)_b + _c; \ + *_dv++ = MPW(_x); \ + _c = _x >> MPW_BITS; \ + } \ + *_dv++ = _c; \ + while (_dv < _dvl) \ + *_dv++ = 0; \ +} while (0) + +#define USUB(dv, av, avl) do { \ + mpw *_dv = (dv); \ + const mpw *_av = (av), *_avl = (avl); \ + mpw _c = 0; \ + \ + while (_av < _avl) { \ + mpw _a, _b; \ + mpd _x; \ + _a = *_av++; \ + _b = *_dv; \ + _x = (mpd)_b - (mpd)_a - _c; \ + *_dv++ = MPW(_x); \ + if (_x >> MPW_BITS) \ + _c = 1; \ + else \ + _c = 0; \ + } \ + while (_c) { \ + mpd _x = (mpd)*_dv - (mpd)_c; \ + *_dv++ = MPW(_x); \ + if (_x >> MPW_BITS) \ + _c = 1; \ + else \ + _c = 0; \ + } \ +} while (0) + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/mpx-kmac.h b/mpx-kmac.h new file mode 100644 index 0000000..1da2860 --- /dev/null +++ b/mpx-kmac.h @@ -0,0 +1,128 @@ +/* -*-c-*- + * + * $Id: mpx-kmac.h,v 1.1 2000/06/17 11:42:11 mdw Exp $ + * + * Macros for Karatsuba functions + * + * (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: mpx-kmac.h,v $ + * Revision 1.1 2000/06/17 11:42:11 mdw + * Moved the Karatsuba macros into a separate file for better sharing. + * Fixed some comments. + * + */ + +#ifndef CATACOMB_MPX_KMAC_H +#define CATACOMB_MPX_KMAC_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#ifndef CATACOMB_MPW_H +# include "mpw.h" +#endif + +/*----- Macros provided ---------------------------------------------------*/ + +#define UADD(dv, av, avl) do { \ + mpw *_dv = (dv); \ + const mpw *_av = (av), *_avl = (avl); \ + mpw _c = 0; \ + \ + while (_av < _avl) { \ + mpw _a, _b; \ + mpd _x; \ + _a = *_av++; \ + _b = *_dv; \ + _x = (mpd)_a + (mpd)_b + _c; \ + *_dv++ = MPW(_x); \ + _c = _x >> MPW_BITS; \ + } \ + while (_c) { \ + mpd _x = (mpd)*_dv + (mpd)_c; \ + *_dv++ = MPW(_x); \ + _c = _x >> MPW_BITS; \ + } \ +} while (0) + +#define UADD2(dv, dvl, av, avl, bv, bvl) do { \ + mpw *_dv = (dv), *_dvl = (dvl); \ + const mpw *_av = (av), *_avl = (avl); \ + const mpw *_bv = (bv), *_bvl = (bvl); \ + mpw _c = 0; \ + \ + while (_av < _avl || _bv < _bvl) { \ + mpw _a, _b; \ + mpd _x; \ + _a = (_av < _avl) ? *_av++ : 0; \ + _b = (_bv < _bvl) ? *_bv++ : 0; \ + _x = (mpd)_a + (mpd)_b + _c; \ + *_dv++ = MPW(_x); \ + _c = _x >> MPW_BITS; \ + } \ + *_dv++ = _c; \ + while (_dv < _dvl) \ + *_dv++ = 0; \ +} while (0) + +#define USUB(dv, av, avl) do { \ + mpw *_dv = (dv); \ + const mpw *_av = (av), *_avl = (avl); \ + mpw _c = 0; \ + \ + while (_av < _avl) { \ + mpw _a, _b; \ + mpd _x; \ + _a = *_av++; \ + _b = *_dv; \ + _x = (mpd)_b - (mpd)_a - _c; \ + *_dv++ = MPW(_x); \ + if (_x >> MPW_BITS) \ + _c = 1; \ + else \ + _c = 0; \ + } \ + while (_c) { \ + mpd _x = (mpd)*_dv - (mpd)_c; \ + *_dv++ = MPW(_x); \ + if (_x >> MPW_BITS) \ + _c = 1; \ + else \ + _c = 0; \ + } \ +} while (0) + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/mpx-kmul.c b/mpx-kmul.c index d53622b..dd492ad 100644 --- a/mpx-kmul.c +++ b/mpx-kmul.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mpx-kmul.c,v 1.3 1999/12/13 15:35:01 mdw Exp $ + * $Id: mpx-kmul.c,v 1.4 2000/06/17 11:42:11 mdw Exp $ * * Karatsuba's multiplication algorithm * @@ -30,6 +30,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: mpx-kmul.c,v $ + * Revision 1.4 2000/06/17 11:42:11 mdw + * Moved the Karatsuba macros into a separate file for better sharing. + * Fixed some comments. + * * Revision 1.3 1999/12/13 15:35:01 mdw * Simplify and improve. * @@ -47,6 +51,7 @@ #include #include "mpx.h" +#include "mpx-kmac.h" /*----- Tweakables --------------------------------------------------------*/ @@ -55,76 +60,6 @@ # define KARATSUBA_CUTOFF 2 #endif -/*----- Addition macros ---------------------------------------------------*/ - -#define UADD(dv, av, avl) do { \ - mpw *_dv = (dv); \ - const mpw *_av = (av), *_avl = (avl); \ - mpw _c = 0; \ - \ - while (_av < _avl) { \ - mpw _a, _b; \ - mpd _x; \ - _a = *_av++; \ - _b = *_dv; \ - _x = (mpd)_a + (mpd)_b + _c; \ - *_dv++ = MPW(_x); \ - _c = _x >> MPW_BITS; \ - } \ - while (_c) { \ - mpd _x = (mpd)*_dv + (mpd)_c; \ - *_dv++ = MPW(_x); \ - _c = _x >> MPW_BITS; \ - } \ -} while (0) - -#define UADD2(dv, dvl, av, avl, bv, bvl) do { \ - mpw *_dv = (dv), *_dvl = (dvl); \ - const mpw *_av = (av), *_avl = (avl); \ - const mpw *_bv = (bv), *_bvl = (bvl); \ - mpw _c = 0; \ - \ - while (_av < _avl || _bv < _bvl) { \ - mpw _a, _b; \ - mpd _x; \ - _a = (_av < _avl) ? *_av++ : 0; \ - _b = (_bv < _bvl) ? *_bv++ : 0; \ - _x = (mpd)_a + (mpd)_b + _c; \ - *_dv++ = MPW(_x); \ - _c = _x >> MPW_BITS; \ - } \ - *_dv++ = _c; \ - while (_dv < _dvl) \ - *_dv++ = 0; \ -} while (0) - -#define USUB(dv, av, avl) do { \ - mpw *_dv = (dv); \ - const mpw *_av = (av), *_avl = (avl); \ - mpw _c = 0; \ - \ - while (_av < _avl) { \ - mpw _a, _b; \ - mpd _x; \ - _a = *_av++; \ - _b = *_dv; \ - _x = (mpd)_b - (mpd)_a - _c; \ - *_dv++ = MPW(_x); \ - if (_x >> MPW_BITS) \ - _c = 1; \ - else \ - _c = 0; \ - } \ - while (_c) { \ - mpd _x = (mpd)*_dv - (mpd)_c; \ - *_dv++ = MPW(_x); \ - if (_x >> MPW_BITS) \ - _c = 1; \ - else \ - _c = 0; \ - } \ -} while (0) - /*----- Main code ---------------------------------------------------------*/ /* --- @mpx_kmul@ --- * @@ -172,11 +107,11 @@ void mpx_kmul(mpw *dv, mpw *dvl, /* --- How the algorithm works --- * * - * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding, %$AB - * = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because I've - * got four multiplications, each four times easier than the one I started - * with. However, note that I can rewrite the coefficient of %$b$% as - * %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$% + * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding, + * %$AB = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because + * I've got four multiplications, each four times easier than the one I + * started with. However, note that I can rewrite the coefficient of %$b$% + * as %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$% * I've already calculated, and that leaves only one more multiplication to * do. So now I have three multiplications, each four times easier, and * that's a win.