3 * Conversion between MPs and standard C integers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_MPINT_H
29 #define CATACOMB_MPINT_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/macros.h>
45 /*----- Generic translation macros ----------------------------------------*/
47 /* --- Warning damage control --- *
49 * GCC (at least) isn't clever enough to work out that the division in
50 * @MP_FROMINT@ is actually safe (since it will only be executed if @_i >
51 * MPW_MAX@, which would prove that @(type)MPW_MAX + 1 != 0@).
54 /* --- @MP_FROMINT@ --- *
56 * Arguments: @d@ = destination multiprecision integer
57 * @type@ = type of integer which @i@ is
58 * @i@ = a standard C integer
60 * Use: Stores the value of @i@ in @d@. This macro is actually
61 * rather subtle in places. Be careful what you change.
64 #define MP_FROMINT(d, type, i) do { \
70 MP_DEST(_d, _sz, 0); \
71 _d->f &= ~(MP_NEG | MP_UNDEF); \
79 _d->v[_o++] = MPW(_i); \
83 MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero"), { \
84 _i /= (type)MPW_MAX + 1; \
94 _d->v[_o++] = MPW(-_i); \
98 MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero"), { \
99 _i /= (type)MPW_MAX + 1; \
104 _d->vl = _d->v + _o; \
108 /* --- @MP_TOINT@ --- *
110 * Arguments: @m@ = a multiprecision integer
111 * @type@ = the type of @i@
112 * @max@ = the largest value @i@ can represent
113 * @i@ = an integer variable
115 * Use: Stores the value of a multiprecision integer in a standard C
116 * integer. If the value won't fit, the behaviour is determined
117 * by the type of @i@: if @i@ is unsigned, the value of the
118 * multiprecision integer modulo @max + 1@ is stored; if @i@ is
119 * signed, the behaviour is undefined.
121 * If you don't want to be bitten by these sorts of things, keep
122 * copies of @INT_MAX@ or whatever is appropriate in
123 * multiprecision form and compare before conversion.
126 #define MP_TOINT(m, type, max, i) do { \
130 const mp *_m = (m); \
131 const mpw *_v = _m->v, *_vl = _m->vl; \
133 /* --- Do all the arithmetic in negative numbers --- */ \
135 while (_v < _vl && _max > 0) { \
139 _max /= (mpd)MPW_MAX + 1; \
146 /*----- Functions provided ------------------------------------------------*/
148 /* --- @mp_fromINT@ --- *
150 * Arguments: @mp *d@ = pointer to destination multiprecision integer
151 * @INT i@ = standard C integer to convert
153 * Returns: The resulting multiprecision integer.
155 * Use: Converts a standard C integer to a multiprecision integer.
158 #define mp_fromINT(name, type) \
159 extern mp *mp_from##name(mp */*d*/, type /*i*/)
161 mp_fromINT(short, short);
162 mp_fromINT(ushort, unsigned short);
163 mp_fromINT(int, int);
164 mp_fromINT(uint, unsigned);
165 mp_fromINT(uint32, uint32);
166 mp_fromINT(long, long);
167 mp_fromINT(ulong, unsigned long);
171 /* --- @mp_toINT@ --- *
173 * Arguments: @const mp *m@ = pointer to a multiprecision integer
175 * Returns: The value of the integer @m@ as a C integer.
177 * Use: Converts a multiprecision integer to a standard C integer.
178 * If the value of the multiprecision integer cannot be
179 * represented in the return type, and the return type is
180 * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return
181 * type is signed, the behaviour is undefined.
184 #define mp_toINT(name, type) \
185 extern type mp_to##name(const mp */*m*/)
187 mp_toINT(short, short);
188 mp_toINT(ushort, unsigned short);
190 mp_toINT(uint, unsigned);
191 mp_toINT(uint32, uint32);
192 mp_toINT(long, long);
193 mp_toINT(ulong, unsigned long);
197 /*----- That's all, folks -------------------------------------------------*/