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); \
80 if (MUFFLE_WARNINGS_EXPR( \
81 CLANG_WARNING("-Wtautological-constant-out-of-range-compare"), \
85 MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero") \
86 CLANG_WARNING("-Wdivision-by-zero"), { \
87 _i /= (type)MPW_MAX + 1; \
97 _d->v[_o++] = MPW(-_i); \
98 if (MUFFLE_WARNINGS_EXPR( \
99 CLANG_WARNING("-Wtautological-constant-out-of-range-compare"), \
103 MUFFLE_WARNINGS_STMT(GCC_WARNING("-Wdiv-by-zero") \
104 CLANG_WARNING("-Wdivision-by-zero"), { \
105 _i /= (type)MPW_MAX + 1; \
110 _d->vl = _d->v + _o; \
114 /* --- @MP_TOINT@ --- *
116 * Arguments: @m@ = a multiprecision integer
117 * @type@ = the type of @i@
118 * @max@ = the largest value @i@ can represent
119 * @i@ = an integer variable
121 * Use: Stores the value of a multiprecision integer in a standard C
122 * integer. If the value won't fit, the behaviour is determined
123 * by the type of @i@: if @i@ is unsigned, the value of the
124 * multiprecision integer modulo @max + 1@ is stored; if @i@ is
125 * signed, the behaviour is undefined.
127 * If you don't want to be bitten by these sorts of things, keep
128 * copies of @INT_MAX@ or whatever is appropriate in
129 * multiprecision form and compare before conversion.
132 #define MP_TOINT(m, type, max, i) do { \
136 const mp *_m = (m); \
137 const mpw *_v = _m->v, *_vl = _m->vl; \
139 /* --- Do all the arithmetic in negative numbers --- */ \
141 while (_v < _vl && _max > 0) { \
142 _i -= (type)*_v << _s; \
145 _max /= (mpd)MPW_MAX + 1; \
152 /*----- Functions provided ------------------------------------------------*/
154 /* --- Build up the list of conversions to be supplied --- */
158 # define LLONG_MAX LONG_LONG_MAX
160 # define MPINT_CONV_LLONG(_) \
161 _(llong, long long, LLONG_MAX) \
162 _(ullong, unsigned long long, ULLONG_MAX)
164 # define MPINT_CONV_LLONG(_)
168 # define MPINT_CONV_INTMAX(_) \
169 _(intmax, intmax_t, INTMAX_MAX) \
170 _(uintmax, uintmax_t, UINTMAX_MAX)
172 # define MPINT_CONV_INTMAX(_)
176 # define MPINT_CONV_U64(_) _(uint64, uint64, MASK64)
178 # define MPINT_CONV_U64(_)
181 #define MPINT_CONVERSIONS(_) \
182 _(short, short, SHRT_MAX) \
183 _(ushort, unsigned short, USHRT_MAX) \
184 _(int, int, INT_MAX) \
185 _(uint, unsigned, UINT_MAX) \
186 _(long, long, LONG_MAX) \
187 _(ulong, unsigned long, ULONG_MAX) \
188 MPINT_CONV_LLONG(_) \
189 _(uint8, uint8, MASK8) \
190 _(uint16, uint16, MASK16) \
191 _(uint24, uint24, MASK24) \
192 _(uint32, uint32, MASK32) \
194 MPINT_CONV_INTMAX(_) \
195 _(sizet, size_t, (size_t)-1)
197 /* --- @mp_fromINT@ --- *
199 * Arguments: @mp *d@ = pointer to destination multiprecision integer
200 * @INT i@ = standard C integer to convert
202 * Returns: The resulting multiprecision integer.
204 * Use: Converts a standard C integer to a multiprecision integer.
207 #define mp_fromINT(name, type, max) \
208 extern mp *mp_from##name(mp */*d*/, type /*i*/);
209 MPINT_CONVERSIONS(mp_fromINT)
212 /* --- @mp_toINT@ --- *
214 * Arguments: @const mp *m@ = pointer to a multiprecision integer
216 * Returns: The value of the integer @m@ as a C integer.
218 * Use: Converts a multiprecision integer to a standard C integer.
219 * If the value of the multiprecision integer cannot be
220 * represented in the return type, and the return type is
221 * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return
222 * type is signed, the behaviour is undefined.
225 #define mp_toINT(name, type, max) \
226 extern type mp_to##name(const mp */*m*/);
227 MPINT_CONVERSIONS(mp_toINT)
230 /*----- That's all, folks -------------------------------------------------*/