X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/10607a708e616a3c911e2b07d412d1b1895845b2..70b904c575877c683e6c79cac4fbf7c6d89d0bde:/mpint.h diff --git a/mpint.h b/mpint.h new file mode 100644 index 0000000..733292b --- /dev/null +++ b/mpint.h @@ -0,0 +1,207 @@ +/* -*-c-*- + * + * $Id: mpint.h,v 1.1 1999/11/25 11:38:31 mdw Exp $ + * + * Conversion between MPs and standard C integers + * + * (c) 1999 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: mpint.h,v $ + * Revision 1.1 1999/11/25 11:38:31 mdw + * Support for conversions between MPs and C integers. + * + */ + +#ifndef MPINT_H +#define MPINT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include + +#ifndef MP_H +# include "mp.h" +#endif + +/*----- Generic translation macros ----------------------------------------*/ + +/* --- @MP_FROMINT@ --- * + * + * Arguments: @d@ = destination multiprecision integer + * @type@ = type of integer which @i@ is + * @i@ = a standard C integer + * + * Use: Stores the value of @i@ in @d@. This macro is actually + * rather subtle in places. Be careful what you change. + */ + +#define MP_FROMINT(d, type, i) do { \ + type _i = (i); \ + size_t _o = 0; \ + mp *_d = (d); \ + size_t _sz = 4; \ + \ + MP_MODIFY(_d, _sz); \ + _d->f &= ~(MP_NEG | MP_UNDEF); \ + \ + /* --- Set the sign on the MP --- * \ + * \ + * If the input integer is *not* negative, then negate it. This \ + * fixes a problem with two's complement machines where the most \ + * negative value actually has larger magnitude than the most \ + * positive, and hence -TYPE_MIN == TYPE_MIN but TYPE_MIN != 0. If \ + * all the work is carried out on negative numbers there isn't a \ + * problem. \ + */ \ + \ + if (_i >= 0) \ + _i = -_i; \ + else \ + _d->f |= MP_NEG; \ + \ + while (_i) { \ + if (_o == _sz) { \ + _sz <<= 1; \ + MP_ENSURE(_d, _sz); \ + } \ + _d->v[_o++] = MPW(-_i); \ + \ + /* --- More subtlety --- * \ + * \ + * Ideally, I'd like to just shift @i@ right by @MPW_BITS@. But I \ + * can't, because that might be more than I'm allowed. I can't \ + * divide by @MPW_MAX + 1@ because that might turn out to be zero \ + * in my current type, and besides which it's unsigned which messes \ + * up all of my negative arithmetic. So do an explicit test here. \ + */ \ + \ + if (_i > -MPW_MAX) \ + break; \ + else \ + _i /= (type)MPW_MAX + 1; \ + } \ + _d->vl = _d->v + _o; \ + (d) = _d; \ +} while (0) + +/* --- @MP_TOINT@ --- * + * + * Arguments: @m@ = a multiprecision integer + * @type@ = the type of @i@ + * @max@ = the largest value @i@ can represent + * @i@ = an integer variable + * + * Use: Stores the value of a multiprecision integer in a standard C + * integer. If the value won't fit, the behaviour is determined + * by the type of @i@: if @i@ is unsigned, the value of the + * multiprecision integer modulo @max + 1@ is stored; if @i@ is + * signed, the behaviour is undefined. + * + * If you don't want to be bitten by these sorts of things, keep + * copies of @INT_MAX@ or whatever is appropriate in + * multiprecision form and compare before conversion. + */ + +#define MP_TOINT(m, type, max, i) do { \ + type _i = 0; \ + type _max = (max); \ + unsigned _s = 0; \ + const mp *_m = (m); \ + const mpw *_v = _m->v, *_vl = _m->vl; \ + \ + /* --- Do all the arithmetic in negative numbers --- */ \ + \ + while (_v < _vl && _max > 0) { \ + _i -= *_v << _s; \ + _s += MPW_BITS; \ + _v++; \ + _max /= (mpd)MPW_MAX + 1; \ + } \ + if (!(_m->f & MP_NEG)) \ + _i = -_i; \ + (i) = _i; \ +} while (0) + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @mp_fromINT@ --- * + * + * Arguments: @mp *d@ = pointer to destination multiprecision integer + * @INT i@ = standard C integer to convert + * + * Returns: The resulting multiprecision integer. + * + * Use: Converts a standard C integer to a multiprecision integer. + */ + +#define mp_fromINT(name, type) \ + extern mp *mp_from##name(mp */*d*/, type /*i*/) + +mp_fromINT(short, short); +mp_fromINT(ushort, unsigned short); +mp_fromINT(int, int); +mp_fromINT(uint, unsigned); +mp_fromINT(long, long); +mp_fromINT(ulong, unsigned long); + +#undef mp_fromINT + +/* --- @mp_toINT@ --- * + * + * Arguments: @const mp *m@ = pointer to a multiprecision integer + * + * Returns: The value of the integer @m@ as a C integer. + * + * Use: Converts a multiprecision integer to a standard C integer. + * If the value of the multiprecision integer cannot be + * represented in the return type, and the return type is + * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return + * type is signed, the behaviour is undefined. + */ + +#define mp_toINT(name, type) \ + extern type mp_to##name(const mp */*m*/); + +mp_toINT(short, short); +mp_toINT(ushort, unsigned short); +mp_toINT(int, int); +mp_toINT(uint, unsigned); +mp_toINT(long, long); +mp_toINT(ulong, unsigned long); + +#undef mp_toINT + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif