3 * $Id: mpint.h,v 1.5 2002/01/13 19:23:16 mdw Exp $
5 * Conversion between MPs and standard C integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.5 2002/01/13 19:23:16 mdw
34 * Fix division-by-zero bug translating @MPW_MAX@ to an @mp@.
36 * Revision 1.4 2000/10/08 12:04:01 mdw
37 * Remove spurious semicolon.
39 * Revision 1.3 2000/06/17 11:45:09 mdw
40 * Major memory management overhaul. Added arena support. Use the secure
41 * arena for secret integers. Replace and improve the MP management macros
42 * (e.g., replace MP_MODIFY by MP_DEST).
44 * Revision 1.2 1999/12/10 23:22:53 mdw
47 * Revision 1.1 1999/11/25 11:38:31 mdw
48 * Support for conversions between MPs and C integers.
52 #ifndef CATACOMB_MPINT_H
53 #define CATACOMB_MPINT_H
59 /*----- Header files ------------------------------------------------------*/
67 /*----- Generic translation macros ----------------------------------------*/
69 /* --- @MP_FROMINT@ --- *
71 * Arguments: @d@ = destination multiprecision integer
72 * @type@ = type of integer which @i@ is
73 * @i@ = a standard C integer
75 * Use: Stores the value of @i@ in @d@. This macro is actually
76 * rather subtle in places. Be careful what you change.
79 #define MP_FROMINT(d, type, i) do { \
85 MP_DEST(_d, _sz, 0); \
86 _d->f &= ~(MP_NEG | MP_UNDEF); \
88 /* --- Set the sign on the MP --- * \
90 * If the input integer is *not* negative, then negate it. This \
91 * fixes a problem with two's complement machines where the most \
92 * negative value actually has larger magnitude than the most \
93 * positive, and hence -TYPE_MIN == TYPE_MIN but TYPE_MIN != 0. If \
94 * all the work is carried out on negative numbers there isn't a \
106 MP_ENSURE(_d, _sz); \
108 _d->v[_o++] = MPW(-_i); \
110 /* --- More subtlety --- * \
112 * Ideally, I'd like to just shift @i@ right by @MPW_BITS@. But I \
113 * can't, because that might be more than I'm allowed. I can't \
114 * divide by @MPW_MAX + 1@ because that might turn out to be zero \
115 * in my current type, and besides which it's unsigned which messes \
116 * up all of my negative arithmetic. So do an explicit test here. \
119 if (_i >= -MPW_MAX) \
122 _i /= (type)MPW_MAX + 1; \
124 _d->vl = _d->v + _o; \
128 /* --- @MP_TOINT@ --- *
130 * Arguments: @m@ = a multiprecision integer
131 * @type@ = the type of @i@
132 * @max@ = the largest value @i@ can represent
133 * @i@ = an integer variable
135 * Use: Stores the value of a multiprecision integer in a standard C
136 * integer. If the value won't fit, the behaviour is determined
137 * by the type of @i@: if @i@ is unsigned, the value of the
138 * multiprecision integer modulo @max + 1@ is stored; if @i@ is
139 * signed, the behaviour is undefined.
141 * If you don't want to be bitten by these sorts of things, keep
142 * copies of @INT_MAX@ or whatever is appropriate in
143 * multiprecision form and compare before conversion.
146 #define MP_TOINT(m, type, max, i) do { \
150 const mp *_m = (m); \
151 const mpw *_v = _m->v, *_vl = _m->vl; \
153 /* --- Do all the arithmetic in negative numbers --- */ \
155 while (_v < _vl && _max > 0) { \
159 _max /= (mpd)MPW_MAX + 1; \
161 if (!(_m->f & MP_NEG)) \
166 /*----- Functions provided ------------------------------------------------*/
168 /* --- @mp_fromINT@ --- *
170 * Arguments: @mp *d@ = pointer to destination multiprecision integer
171 * @INT i@ = standard C integer to convert
173 * Returns: The resulting multiprecision integer.
175 * Use: Converts a standard C integer to a multiprecision integer.
178 #define mp_fromINT(name, type) \
179 extern mp *mp_from##name(mp */*d*/, type /*i*/)
181 mp_fromINT(short, short);
182 mp_fromINT(ushort
, unsigned short);
183 mp_fromINT(int, int);
184 mp_fromINT(uint
, unsigned);
185 mp_fromINT(uint32
, uint32
);
186 mp_fromINT(long, long);
187 mp_fromINT(ulong
, unsigned long);
191 /* --- @mp_toINT@ --- *
193 * Arguments: @const mp *m@ = pointer to a multiprecision integer
195 * Returns: The value of the integer @m@ as a C integer.
197 * Use: Converts a multiprecision integer to a standard C integer.
198 * If the value of the multiprecision integer cannot be
199 * represented in the return type, and the return type is
200 * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return
201 * type is signed, the behaviour is undefined.
204 #define mp_toINT(name, type) \
205 extern type mp_to##name(const mp */*m*/)
207 mp_toINT(short, short);
208 mp_toINT(ushort
, unsigned short);
210 mp_toINT(uint
, unsigned);
211 mp_toINT(uint32
, uint32
);
212 mp_toINT(long, long);
213 mp_toINT(ulong
, unsigned long);
217 /*----- That's all, folks -------------------------------------------------*/