X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/52cdaca9b7b8e15871a3de8b5b571c18dcc580f8..6ec3a4cf4aaa7cd375e1aa18f85861986259b8e5:/mpx.h diff --git a/mpx.h b/mpx.h index dc86cc9..19f5cc7 100644 --- a/mpx.h +++ b/mpx.h @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: mpx.h,v 1.11 2000/10/08 15:48:35 mdw Exp $ + * $Id: mpx.h,v 1.18 2004/04/08 01:36:15 mdw Exp $ * * Low level multiprecision arithmetic * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,56 +15,18 @@ * 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.h,v $ - * Revision 1.11 2000/10/08 15:48:35 mdw - * Rename Karatsuba constants now that we have @gfx_kmul@ too. - * - * Revision 1.10 2000/10/08 12:06:12 mdw - * Provide @mpx_ueq@ for rapidly testing equality of two integers. - * - * Revision 1.9 1999/12/22 15:49:07 mdw - * New function for division by a small integer. - * - * Revision 1.8 1999/12/11 10:57:43 mdw - * Karatsuba squaring algorithm. - * - * Revision 1.7 1999/12/11 01:51:28 mdw - * Change Karatsuba parameters slightly. - * - * Revision 1.6 1999/12/10 23:23:51 mdw - * Karatsuba-Ofman multiplication algorithm. - * - * Revision 1.5 1999/11/20 22:23:27 mdw - * Add function versions of some low-level macros with wider use. - * - * Revision 1.4 1999/11/17 18:04:43 mdw - * Add two's complement support. Fix a bug in MPX_UMLAN. - * - * Revision 1.3 1999/11/13 01:51:29 mdw - * Minor interface changes. Should be stable now. - * - * Revision 1.2 1999/11/11 17:47:55 mdw - * Minor changes for different `mptypes.h' format. - * - * Revision 1.1 1999/09/03 08:41:12 mdw - * Initial import. - * - */ - #ifndef CATACOMB_MPX_H #define CATACOMB_MPX_H @@ -125,9 +87,9 @@ if (_v == _vl) \ (b) = 0; \ else { \ - unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \ + unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \ mpw _w = _vl[-1]; \ - unsigned _k = MPW_BITS / 2; \ + unsigned _k = MPW_P2; \ while (_k) { \ if (_w >> _k) { \ _w >>= _k; \ @@ -148,25 +110,24 @@ */ #define MPX_OCTETS(o, v, vl) do { \ - const mpw *_v = (v), *_vl = (vl); \ - MPX_SHRINK(_v, _vl); \ - if (_v == _vl) \ - (o) = 0; \ - else { \ - size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \ - mpw _w = _vl[-1]; \ - unsigned _k = MPW_BITS / 2; \ - while (_k >= 8) { \ - if (_w >> _k) { \ - _w >>= _k; \ - _o += _k >> 3; \ - } \ - _k >>= 1; \ - } \ - if (_w) \ - _o++; \ - (o) = _o; \ - } \ + unsigned long _bb; \ + MPX_BITS(_bb, (v), (vl)); \ + (o) = (_bb + 7) >> 3; \ +} while (0) + +/* --- @MPX_OCTETS2C@ --- * + * + * Arguments: @size_t o@ = result variable + * @const mpw *v, *vl@ = pointer to array of words + * + * Use: Calculates the number of octets in a multiprecision value, if + * you represent it as two's complement. + */ + +#define MPX_OCTETS2C(o, v, vl) do { \ + unsigned long _bb; \ + MPX_BITS(_bb, (v), (vl)); \ + (o) = (_bb >> 3) + 1; \ } while (0) /* --- @MPX_COPY@ --- * @@ -206,6 +167,20 @@ memset(_v, 0, MPWS(_vl - _v)); \ } while (0) +/* --- @MPX_ONE@ --- * + * + * Arguments: @v, vl@ = base and limit of vector to clear + * + * Use: Fills the area between the two vector pointers with ones. + */ + +#define MPX_ONE(v, vl) do { \ + mpw * _v = (v); \ + const mpw *_vl = (vl); \ + while (_v < _vl) \ + *_v++ = MPW_MAX; \ +} while (0) + /*----- Loading and storing -----------------------------------------------*/ /* --- @mpx_storel@ --- * @@ -272,6 +247,75 @@ extern void mpx_storeb(const mpw */*v*/, const mpw */*vl*/, extern void mpx_loadb(mpw */*v*/, mpw */*vl*/, const void */*p*/, size_t /*sz*/); +/* --- @mpx_storel2cn@ --- * + * + * Arguments: @const mpw *v, *vl@ = base and limit of source vector + * @void *pp@ = pointer to octet array + * @size_t sz@ = size of octet array + * + * Returns: --- + * + * Use: Stores a negative MP in an octet array, least significant + * octet first, as two's complement. High-end octets are + * silently discarded if there isn't enough space for them. + * This obviously makes the output bad. + */ + +extern void mpx_storel2cn(const mpw */*v*/, const mpw */*vl*/, + void */*p*/, size_t /*sz*/); + +/* --- @mpx_loadl2cn@ --- * + * + * Arguments: @mpw *v, *vl@ = base and limit of destination vector + * @const void *pp@ = pointer to octet array + * @size_t sz@ = size of octet array + * + * Returns: --- + * + * Use: Loads a negative MP in an octet array, least significant + * octet first, as two's complement. High-end octets are + * ignored if there isn't enough space for them. This probably + * means you made the wrong choice coming here. + */ + +extern void mpx_loadl2cn(mpw */*v*/, mpw */*vl*/, + const void */*p*/, size_t /*sz*/); + +/* --- @mpx_storeb2cn@ --- * + * + * Arguments: @const mpw *v, *vl@ = base and limit of source vector + * @void *pp@ = pointer to octet array + * @size_t sz@ = size of octet array + * + * Returns: --- + * + * Use: Stores a negative MP in an octet array, most significant + * octet first, as two's complement. High-end octets are + * silently discarded if there isn't enough space for them, + * which probably isn't what you meant. + */ + +extern void mpx_storeb2cn(const mpw */*v*/, const mpw */*vl*/, + void */*p*/, size_t /*sz*/); + +/* --- @mpx_loadb2cn@ --- * + * + * Arguments: @mpw *v, *vl@ = base and limit of destination vector + * @const void *pp@ = pointer to octet array + * @size_t sz@ = size of octet array + * + * Returns: --- + * + * Use: Loads a negative MP in an octet array, most significant octet + * first as two's complement. High-end octets are ignored if + * there isn't enough space for them. This probably means you + * chose this function wrongly. + */ + +extern void mpx_loadb2cn(mpw */*v*/, mpw */*vl*/, + const void */*p*/, size_t /*sz*/); + + /*----- Logical shifting --------------------------------------------------*/ /* --- @mpx_lsl@ --- * @@ -289,6 +333,22 @@ extern void mpx_lsl(mpw */*dv*/, mpw */*dvl*/, const mpw */*av*/, const mpw */*avl*/, size_t /*n*/); +/* --- @mpx_lslc@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination vector base and limit + * @const mpw *av, *avl@ = source vector base and limit + * @size_t n@ = number of bit positions to shift by + * + * Returns: --- + * + * Use: Performs a logical shift left operation on an integer, only + * it fills in the bits with ones instead of zeroes. + */ + +extern void mpx_lslc(mpw */*dv*/, mpw */*dvl*/, + const mpw */*av*/, const mpw */*avl*/, + size_t /*n*/); + /* --- @mpx_lsr@ --- * * * Arguments: @mpw *dv, *dvl@ = destination vector base and limit @@ -304,6 +364,60 @@ extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/, const mpw */*av*/, const mpw */*avl*/, size_t /*n*/); +/*----- Bitwise operations ------------------------------------------------*/ + +/* --- @mpx_bitop@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination vector + * @const mpw *av, *avl@ = first source vector + * @const mpw *bv, *bvl@ = second source vector + * + * Returns: --- + * + * Use: Provide the dyadic boolean functions. The functions are + * named after the truth table they generate: + * + * a: 0011 + * b: 0101 + * @mpx_bitXXXX@ + */ + +#define MPX_DOBIN(what) \ + what(0000) what(0001) what(0010) what(0011) \ + what(0100) what(0101) what(0110) what(0111) \ + what(1000) what(1001) what(1010) what(1011) \ + what(1100) what(1101) what(1110) what(1111) + +#define MPX_BITDECL(string) \ + extern void mpx_bit##string(mpw */*dv*/, mpw */*dvl*/, \ + const mpw */*av*/, const mpw */*avl*/, \ + const mpw */*bv*/, const mpw */*bvl*/); +MPX_DOBIN(MPX_BITDECL) + +/* --- @mpx_[n]and@, @mpx_[n]or@, @mpx_xor@ --- * + * + * Synonyms for the commonly-used functions above. + */ + +#define mpx_and mpx_bit0001 +#define mpx_or mpx_bit0111 +#define mpx_nand mpx_bit1110 +#define mpx_nor mpx_bit1000 +#define mpx_xor mpx_bit0110 + +/* --- @mpx_not@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination vector + * @const mpw *av, *avl@ = first source vector + * + * Returns: --- + * + * Use; Bitwise NOT. + */ + +extern void mpx_not(mpw */*dv*/, mpw */*dvl*/, + const mpw */*av*/, const mpw */*avl*/); + /*----- Unsigned arithmetic -----------------------------------------------*/ /* --- @mpx_2c@ --- * @@ -393,6 +507,22 @@ extern void mpx_uadd(mpw */*dv*/, mpw */*dvl*/, extern void mpx_uaddn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/); +/* --- @mpx_uaddnlsl@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination and first argument vector + * @mpw a@ = second argument + * @unsigned o@ = offset in bits + * + * Returns: --- + * + * Use: Computes %$d + 2^o a$%. If the result overflows then + * high-order bits are discarded, as usual. We must have + * @0 < o < MPW_BITS@. + */ + +extern void mpx_uaddnlsl(mpw */*dv*/, mpw */*dvl*/, + mpw /*a*/, unsigned /*o*/); + /* --- @mpx_usub@ --- * * * Arguments: @mpw *dv, *dvl@ = destination vector base and limit @@ -440,6 +570,23 @@ extern void mpx_usub(mpw */*dv*/, mpw */*dvl*/, extern void mpx_usubn(mpw */*dv*/, mpw */*dvl*/, mpw /*n*/); +/* --- @mpx_usubnlsl@ --- * + * + * Arguments: @mpw *dv, *dvl@ = destination and first argument vector + * @mpw a@ = second argument + * @unsigned o@ = offset in bits + * + * Returns: --- + * + * Use: Computes %$d - 2^o a$%. If the result overflows then + * high-order bits are discarded, as usual, so you get two's + * complement. Which might be what you wanted... We must have + * @0 < o < MPW_BITS@. + */ + +extern void mpx_usubnlsl(mpw */*dv*/, mpw */*dvl*/, + mpw /*a*/, unsigned /*o*/); + /* --- @mpx_umul@ --- * * * Arguments: @mpw *dv, *dvl@ = destination vector base and limit @@ -543,7 +690,7 @@ extern void mpx_usqr(mpw */*dv*/, mpw */*dvl*/, * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit * @mpw *rv, *rvl@ = dividend/remainder vector base and limit * @const mpw *dv, *dvl@ = divisor vector base and limit - * @mpw *sv, *svl@ = scratch workspace + * @mpw *sv, *svl@ = scratch workspace * * Returns: --- * @@ -582,20 +729,12 @@ extern mpw mpx_udivn(mpw */*qv*/, mpw */*qvl*/, * * This is the limiting length for using Karatsuba algorithms. It's best to * use the simpler classical multiplication method on numbers smaller than - * this. + * this. It is unsafe to make this constant less than four (i.e., the + * algorithms will fail). */ #define MPK_THRESH 16 -/* --- @MPK_SLOP@ --- * - * - * The extra number of words required as scratch space by the Karatsuba - * routines. This is a (generous) guess, since the actual amount of space - * required is proportional to the recursion depth. - */ - -#define MPK_SLOP 64 - /* --- @mpx_kmul@ --- * * * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer @@ -610,10 +749,9 @@ extern mpw mpx_udivn(mpw */*qv*/, mpw */*qvl*/, * multiplication (e.g., @mpx_umul@) on large numbers, although * more expensive on small ones. * - * The destination and scratch buffers must be twice as large as - * the larger argument. The scratch space must be twice as - * large as the larger argument, plus the magic number - * @MPK_SLOP@. + * The destination must be three times as large as the larger + * argument. The scratch space must be five times as large as + * the larger argument. */ extern void mpx_kmul(mpw */*dv*/, mpw */*dvl*/, @@ -635,9 +773,9 @@ extern void mpx_kmul(mpw */*dv*/, mpw */*dvl*/, * large numbers, although more expensive on small ones, and * rather simpler than full-blown Karatsuba multiplication. * - * The destination must be twice as large as the argument. The - * scratch space must be twice as large as the argument, plus - * the magic number @MPK_SLOP@. + * The destination must be three times as large as the larger + * argument. The scratch space must be five times as large as + * the larger argument. */ extern void mpx_ksqr(mpw */*dv*/, mpw */*dvl*/,