| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpint.h,v 1.3 2000/06/17 11:45:09 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.3 2000/06/17 11:45:09 mdw |
| 34 | * Major memory management overhaul. Added arena support. Use the secure |
| 35 | * arena for secret integers. Replace and improve the MP management macros |
| 36 | * (e.g., replace MP_MODIFY by MP_DEST). |
| 37 | * |
| 38 | * Revision 1.2 1999/12/10 23:22:53 mdw |
| 39 | * Support for uint32. |
| 40 | * |
| 41 | * Revision 1.1 1999/11/25 11:38:31 mdw |
| 42 | * Support for conversions between MPs and C integers. |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | #ifndef CATACOMB_MPINT_H |
| 47 | #define CATACOMB_MPINT_H |
| 48 | |
| 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
| 53 | /*----- Header files ------------------------------------------------------*/ |
| 54 | |
| 55 | #include <limits.h> |
| 56 | |
| 57 | #ifndef CATACOMB_MP_H |
| 58 | # include "mp.h" |
| 59 | #endif |
| 60 | |
| 61 | /*----- Generic translation macros ----------------------------------------*/ |
| 62 | |
| 63 | /* --- @MP_FROMINT@ --- * |
| 64 | * |
| 65 | * Arguments: @d@ = destination multiprecision integer |
| 66 | * @type@ = type of integer which @i@ is |
| 67 | * @i@ = a standard C integer |
| 68 | * |
| 69 | * Use: Stores the value of @i@ in @d@. This macro is actually |
| 70 | * rather subtle in places. Be careful what you change. |
| 71 | */ |
| 72 | |
| 73 | #define MP_FROMINT(d, type, i) do { \ |
| 74 | type _i = (i); \ |
| 75 | size_t _o = 0; \ |
| 76 | mp *_d = (d); \ |
| 77 | size_t _sz = 4; \ |
| 78 | \ |
| 79 | MP_DEST(_d, _sz, 0); \ |
| 80 | _d->f &= ~(MP_NEG | MP_UNDEF); \ |
| 81 | \ |
| 82 | /* --- Set the sign on the MP --- * \ |
| 83 | * \ |
| 84 | * If the input integer is *not* negative, then negate it. This \ |
| 85 | * fixes a problem with two's complement machines where the most \ |
| 86 | * negative value actually has larger magnitude than the most \ |
| 87 | * positive, and hence -TYPE_MIN == TYPE_MIN but TYPE_MIN != 0. If \ |
| 88 | * all the work is carried out on negative numbers there isn't a \ |
| 89 | * problem. \ |
| 90 | */ \ |
| 91 | \ |
| 92 | if (_i >= 0) \ |
| 93 | _i = -_i; \ |
| 94 | else \ |
| 95 | _d->f |= MP_NEG; \ |
| 96 | \ |
| 97 | while (_i) { \ |
| 98 | if (_o == _sz) { \ |
| 99 | _sz <<= 1; \ |
| 100 | MP_ENSURE(_d, _sz); \ |
| 101 | } \ |
| 102 | _d->v[_o++] = MPW(-_i); \ |
| 103 | \ |
| 104 | /* --- More subtlety --- * \ |
| 105 | * \ |
| 106 | * Ideally, I'd like to just shift @i@ right by @MPW_BITS@. But I \ |
| 107 | * can't, because that might be more than I'm allowed. I can't \ |
| 108 | * divide by @MPW_MAX + 1@ because that might turn out to be zero \ |
| 109 | * in my current type, and besides which it's unsigned which messes \ |
| 110 | * up all of my negative arithmetic. So do an explicit test here. \ |
| 111 | */ \ |
| 112 | \ |
| 113 | if (_i > -MPW_MAX) \ |
| 114 | break; \ |
| 115 | else \ |
| 116 | _i /= (type)MPW_MAX + 1; \ |
| 117 | } \ |
| 118 | _d->vl = _d->v + _o; \ |
| 119 | (d) = _d; \ |
| 120 | } while (0) |
| 121 | |
| 122 | /* --- @MP_TOINT@ --- * |
| 123 | * |
| 124 | * Arguments: @m@ = a multiprecision integer |
| 125 | * @type@ = the type of @i@ |
| 126 | * @max@ = the largest value @i@ can represent |
| 127 | * @i@ = an integer variable |
| 128 | * |
| 129 | * Use: Stores the value of a multiprecision integer in a standard C |
| 130 | * integer. If the value won't fit, the behaviour is determined |
| 131 | * by the type of @i@: if @i@ is unsigned, the value of the |
| 132 | * multiprecision integer modulo @max + 1@ is stored; if @i@ is |
| 133 | * signed, the behaviour is undefined. |
| 134 | * |
| 135 | * If you don't want to be bitten by these sorts of things, keep |
| 136 | * copies of @INT_MAX@ or whatever is appropriate in |
| 137 | * multiprecision form and compare before conversion. |
| 138 | */ |
| 139 | |
| 140 | #define MP_TOINT(m, type, max, i) do { \ |
| 141 | type _i = 0; \ |
| 142 | type _max = (max); \ |
| 143 | unsigned _s = 0; \ |
| 144 | const mp *_m = (m); \ |
| 145 | const mpw *_v = _m->v, *_vl = _m->vl; \ |
| 146 | \ |
| 147 | /* --- Do all the arithmetic in negative numbers --- */ \ |
| 148 | \ |
| 149 | while (_v < _vl && _max > 0) { \ |
| 150 | _i -= *_v << _s; \ |
| 151 | _s += MPW_BITS; \ |
| 152 | _v++; \ |
| 153 | _max /= (mpd)MPW_MAX + 1; \ |
| 154 | } \ |
| 155 | if (!(_m->f & MP_NEG)) \ |
| 156 | _i = -_i; \ |
| 157 | (i) = _i; \ |
| 158 | } while (0) |
| 159 | |
| 160 | /*----- Functions provided ------------------------------------------------*/ |
| 161 | |
| 162 | /* --- @mp_fromINT@ --- * |
| 163 | * |
| 164 | * Arguments: @mp *d@ = pointer to destination multiprecision integer |
| 165 | * @INT i@ = standard C integer to convert |
| 166 | * |
| 167 | * Returns: The resulting multiprecision integer. |
| 168 | * |
| 169 | * Use: Converts a standard C integer to a multiprecision integer. |
| 170 | */ |
| 171 | |
| 172 | #define mp_fromINT(name, type) \ |
| 173 | extern mp *mp_from##name(mp */*d*/, type /*i*/) |
| 174 | |
| 175 | mp_fromINT(short, short); |
| 176 | mp_fromINT(ushort, unsigned short); |
| 177 | mp_fromINT(int, int); |
| 178 | mp_fromINT(uint, unsigned); |
| 179 | mp_fromINT(uint32, uint32); |
| 180 | mp_fromINT(long, long); |
| 181 | mp_fromINT(ulong, unsigned long); |
| 182 | |
| 183 | #undef mp_fromINT |
| 184 | |
| 185 | /* --- @mp_toINT@ --- * |
| 186 | * |
| 187 | * Arguments: @const mp *m@ = pointer to a multiprecision integer |
| 188 | * |
| 189 | * Returns: The value of the integer @m@ as a C integer. |
| 190 | * |
| 191 | * Use: Converts a multiprecision integer to a standard C integer. |
| 192 | * If the value of the multiprecision integer cannot be |
| 193 | * represented in the return type, and the return type is |
| 194 | * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return |
| 195 | * type is signed, the behaviour is undefined. |
| 196 | */ |
| 197 | |
| 198 | #define mp_toINT(name, type) \ |
| 199 | extern type mp_to##name(const mp */*m*/); |
| 200 | |
| 201 | mp_toINT(short, short); |
| 202 | mp_toINT(ushort, unsigned short); |
| 203 | mp_toINT(int, int); |
| 204 | mp_toINT(uint, unsigned); |
| 205 | mp_toINT(uint32, uint32); |
| 206 | mp_toINT(long, long); |
| 207 | mp_toINT(ulong, unsigned long); |
| 208 | |
| 209 | #undef mp_toINT |
| 210 | |
| 211 | /*----- That's all, folks -------------------------------------------------*/ |
| 212 | |
| 213 | #ifdef __cplusplus |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | #endif |