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 #ifndef CATACOMB_MPINT_H
31 #define CATACOMB_MPINT_H
37 /*----- Header files ------------------------------------------------------*/
45 /*----- Generic translation macros ----------------------------------------*/
47 /* --- @MP_FROMINT@ --- *
49 * Arguments: @d@ = destination multiprecision integer
50 * @type@ = type of integer which @i@ is
51 * @i@ = a standard C integer
53 * Use: Stores the value of @i@ in @d@. This macro is actually
54 * rather subtle in places. Be careful what you change.
57 #define MP_FROMINT(d, type, i) do { \
63 MP_DEST(_d, _sz, 0); \
64 _d->f &= ~(MP_NEG | MP_UNDEF); \
72 _d->v[_o++] = MPW(_i); \
76 _i /= (type)MPW_MAX + 1; \
85 _d->v[_o++] = MPW(-_i); \
89 _i /= (type)MPW_MAX + 1; \
93 _d->vl = _d->v + _o; \
97 /* --- @MP_TOINT@ --- *
99 * Arguments: @m@ = a multiprecision integer
100 * @type@ = the type of @i@
101 * @max@ = the largest value @i@ can represent
102 * @i@ = an integer variable
104 * Use: Stores the value of a multiprecision integer in a standard C
105 * integer. If the value won't fit, the behaviour is determined
106 * by the type of @i@: if @i@ is unsigned, the value of the
107 * multiprecision integer modulo @max + 1@ is stored; if @i@ is
108 * signed, the behaviour is undefined.
110 * If you don't want to be bitten by these sorts of things, keep
111 * copies of @INT_MAX@ or whatever is appropriate in
112 * multiprecision form and compare before conversion.
115 #define MP_TOINT(m, type, max, i) do { \
119 const mp *_m = (m); \
120 const mpw *_v = _m->v, *_vl = _m->vl; \
122 /* --- Do all the arithmetic in negative numbers --- */ \
124 while (_v < _vl && _max > 0) { \
128 _max /= (mpd)MPW_MAX + 1; \
135 /*----- Functions provided ------------------------------------------------*/
137 /* --- @mp_fromINT@ --- *
139 * Arguments: @mp *d@ = pointer to destination multiprecision integer
140 * @INT i@ = standard C integer to convert
142 * Returns: The resulting multiprecision integer.
144 * Use: Converts a standard C integer to a multiprecision integer.
147 #define mp_fromINT(name, type) \
148 extern mp *mp_from##name(mp */*d*/, type /*i*/)
150 mp_fromINT(short, short);
151 mp_fromINT(ushort
, unsigned short);
152 mp_fromINT(int, int);
153 mp_fromINT(uint
, unsigned);
154 mp_fromINT(uint32
, uint32
);
155 mp_fromINT(long, long);
156 mp_fromINT(ulong
, unsigned long);
160 /* --- @mp_toINT@ --- *
162 * Arguments: @const mp *m@ = pointer to a multiprecision integer
164 * Returns: The value of the integer @m@ as a C integer.
166 * Use: Converts a multiprecision integer to a standard C integer.
167 * If the value of the multiprecision integer cannot be
168 * represented in the return type, and the return type is
169 * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return
170 * type is signed, the behaviour is undefined.
173 #define mp_toINT(name, type) \
174 extern type mp_to##name(const mp */*m*/)
176 mp_toINT(short, short);
177 mp_toINT(ushort
, unsigned short);
179 mp_toINT(uint
, unsigned);
180 mp_toINT(uint32
, uint32
);
181 mp_toINT(long, long);
182 mp_toINT(ulong
, unsigned long);
186 /*----- That's all, folks -------------------------------------------------*/