| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpint.h,v 1.2 1999/12/10 23:22:53 mdw Exp $ |
| 4 | * |
| 5 | * Conversion between MPs and standard C integers |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: mpint.h,v $ |
| 33 | * Revision 1.2 1999/12/10 23:22:53 mdw |
| 34 | * Support for uint32. |
| 35 | * |
| 36 | * Revision 1.1 1999/11/25 11:38:31 mdw |
| 37 | * Support for conversions between MPs and C integers. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #ifndef CATACOMB_MPINT_H |
| 42 | #define CATACOMB_MPINT_H |
| 43 | |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
| 48 | /*----- Header files ------------------------------------------------------*/ |
| 49 | |
| 50 | #include <limits.h> |
| 51 | |
| 52 | #ifndef CATACOMB_MP_H |
| 53 | # include "mp.h" |
| 54 | #endif |
| 55 | |
| 56 | /*----- Generic translation macros ----------------------------------------*/ |
| 57 | |
| 58 | /* --- @MP_FROMINT@ --- * |
| 59 | * |
| 60 | * Arguments: @d@ = destination multiprecision integer |
| 61 | * @type@ = type of integer which @i@ is |
| 62 | * @i@ = a standard C integer |
| 63 | * |
| 64 | * Use: Stores the value of @i@ in @d@. This macro is actually |
| 65 | * rather subtle in places. Be careful what you change. |
| 66 | */ |
| 67 | |
| 68 | #define MP_FROMINT(d, type, i) do { \ |
| 69 | type _i = (i); \ |
| 70 | size_t _o = 0; \ |
| 71 | mp *_d = (d); \ |
| 72 | size_t _sz = 4; \ |
| 73 | \ |
| 74 | MP_MODIFY(_d, _sz); \ |
| 75 | _d->f &= ~(MP_NEG | MP_UNDEF); \ |
| 76 | \ |
| 77 | /* --- Set the sign on the MP --- * \ |
| 78 | * \ |
| 79 | * If the input integer is *not* negative, then negate it. This \ |
| 80 | * fixes a problem with two's complement machines where the most \ |
| 81 | * negative value actually has larger magnitude than the most \ |
| 82 | * positive, and hence -TYPE_MIN == TYPE_MIN but TYPE_MIN != 0. If \ |
| 83 | * all the work is carried out on negative numbers there isn't a \ |
| 84 | * problem. \ |
| 85 | */ \ |
| 86 | \ |
| 87 | if (_i >= 0) \ |
| 88 | _i = -_i; \ |
| 89 | else \ |
| 90 | _d->f |= MP_NEG; \ |
| 91 | \ |
| 92 | while (_i) { \ |
| 93 | if (_o == _sz) { \ |
| 94 | _sz <<= 1; \ |
| 95 | MP_ENSURE(_d, _sz); \ |
| 96 | } \ |
| 97 | _d->v[_o++] = MPW(-_i); \ |
| 98 | \ |
| 99 | /* --- More subtlety --- * \ |
| 100 | * \ |
| 101 | * Ideally, I'd like to just shift @i@ right by @MPW_BITS@. But I \ |
| 102 | * can't, because that might be more than I'm allowed. I can't \ |
| 103 | * divide by @MPW_MAX + 1@ because that might turn out to be zero \ |
| 104 | * in my current type, and besides which it's unsigned which messes \ |
| 105 | * up all of my negative arithmetic. So do an explicit test here. \ |
| 106 | */ \ |
| 107 | \ |
| 108 | if (_i > -MPW_MAX) \ |
| 109 | break; \ |
| 110 | else \ |
| 111 | _i /= (type)MPW_MAX + 1; \ |
| 112 | } \ |
| 113 | _d->vl = _d->v + _o; \ |
| 114 | (d) = _d; \ |
| 115 | } while (0) |
| 116 | |
| 117 | /* --- @MP_TOINT@ --- * |
| 118 | * |
| 119 | * Arguments: @m@ = a multiprecision integer |
| 120 | * @type@ = the type of @i@ |
| 121 | * @max@ = the largest value @i@ can represent |
| 122 | * @i@ = an integer variable |
| 123 | * |
| 124 | * Use: Stores the value of a multiprecision integer in a standard C |
| 125 | * integer. If the value won't fit, the behaviour is determined |
| 126 | * by the type of @i@: if @i@ is unsigned, the value of the |
| 127 | * multiprecision integer modulo @max + 1@ is stored; if @i@ is |
| 128 | * signed, the behaviour is undefined. |
| 129 | * |
| 130 | * If you don't want to be bitten by these sorts of things, keep |
| 131 | * copies of @INT_MAX@ or whatever is appropriate in |
| 132 | * multiprecision form and compare before conversion. |
| 133 | */ |
| 134 | |
| 135 | #define MP_TOINT(m, type, max, i) do { \ |
| 136 | type _i = 0; \ |
| 137 | type _max = (max); \ |
| 138 | unsigned _s = 0; \ |
| 139 | const mp *_m = (m); \ |
| 140 | const mpw *_v = _m->v, *_vl = _m->vl; \ |
| 141 | \ |
| 142 | /* --- Do all the arithmetic in negative numbers --- */ \ |
| 143 | \ |
| 144 | while (_v < _vl && _max > 0) { \ |
| 145 | _i -= *_v << _s; \ |
| 146 | _s += MPW_BITS; \ |
| 147 | _v++; \ |
| 148 | _max /= (mpd)MPW_MAX + 1; \ |
| 149 | } \ |
| 150 | if (!(_m->f & MP_NEG)) \ |
| 151 | _i = -_i; \ |
| 152 | (i) = _i; \ |
| 153 | } while (0) |
| 154 | |
| 155 | /*----- Functions provided ------------------------------------------------*/ |
| 156 | |
| 157 | /* --- @mp_fromINT@ --- * |
| 158 | * |
| 159 | * Arguments: @mp *d@ = pointer to destination multiprecision integer |
| 160 | * @INT i@ = standard C integer to convert |
| 161 | * |
| 162 | * Returns: The resulting multiprecision integer. |
| 163 | * |
| 164 | * Use: Converts a standard C integer to a multiprecision integer. |
| 165 | */ |
| 166 | |
| 167 | #define mp_fromINT(name, type) \ |
| 168 | extern mp *mp_from##name(mp */*d*/, type /*i*/) |
| 169 | |
| 170 | mp_fromINT(short, short); |
| 171 | mp_fromINT(ushort, unsigned short); |
| 172 | mp_fromINT(int, int); |
| 173 | mp_fromINT(uint, unsigned); |
| 174 | mp_fromINT(uint32, uint32); |
| 175 | mp_fromINT(long, long); |
| 176 | mp_fromINT(ulong, unsigned long); |
| 177 | |
| 178 | #undef mp_fromINT |
| 179 | |
| 180 | /* --- @mp_toINT@ --- * |
| 181 | * |
| 182 | * Arguments: @const mp *m@ = pointer to a multiprecision integer |
| 183 | * |
| 184 | * Returns: The value of the integer @m@ as a C integer. |
| 185 | * |
| 186 | * Use: Converts a multiprecision integer to a standard C integer. |
| 187 | * If the value of the multiprecision integer cannot be |
| 188 | * represented in the return type, and the return type is |
| 189 | * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return |
| 190 | * type is signed, the behaviour is undefined. |
| 191 | */ |
| 192 | |
| 193 | #define mp_toINT(name, type) \ |
| 194 | extern type mp_to##name(const mp */*m*/); |
| 195 | |
| 196 | mp_toINT(short, short); |
| 197 | mp_toINT(ushort, unsigned short); |
| 198 | mp_toINT(int, int); |
| 199 | mp_toINT(uint, unsigned); |
| 200 | mp_toINT(uint32, uint32); |
| 201 | mp_toINT(long, long); |
| 202 | mp_toINT(ulong, unsigned long); |
| 203 | |
| 204 | #undef mp_toINT |
| 205 | |
| 206 | /*----- That's all, folks -------------------------------------------------*/ |
| 207 | |
| 208 | #ifdef __cplusplus |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | #endif |