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 ------------------------------------------------------*/
43 /*----- Generic translation macros ----------------------------------------*/
45 /* --- Warning damage control --- *
47 * GCC (at least) isn't clever enough to work out that the division in
48 * @MP_FROMINT@ is actually safe (since it will only be executed if @_i >
49 * MPW_MAX@, which would prove that @(type)MPW_MAX + 1 != 0@). So here's
50 * some machinery to shut it up.
53 #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
54 # define MP_FROMINT_MUFFLE_WARNING(x) do { \
55 _Pragma("GCC diagnostic push") \
56 _Pragma("GCC diagnostic ignored \"-Wdiv-by-zero\"") \
58 _Pragma("GCC diagnostic pop") \
61 # define MP_FROMINT_MUFFLE_WARNING(x) do { x } while (0)
64 /* --- @MP_FROMINT@ --- *
66 * Arguments: @d@ = destination multiprecision integer
67 * @type@ = type of integer which @i@ is
68 * @i@ = a standard C integer
70 * Use: Stores the value of @i@ in @d@. This macro is actually
71 * rather subtle in places. Be careful what you change.
74 #define MP_FROMINT(d, type, i) do { \
80 MP_DEST(_d, _sz, 0); \
81 _d->f &= ~(MP_NEG | MP_UNDEF); \
89 _d->v[_o++] = MPW(_i); \
93 MP_FROMINT_MUFFLE_WARNING({ _i /= (type)MPW_MAX + 1; }); \
100 MP_ENSURE(_d, _sz); \
102 _d->v[_o++] = MPW(-_i); \
103 if (_i >= -MPW_MAX) \
106 MP_FROMINT_MUFFLE_WARNING({ _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) { \
145 _max /= (mpd)MPW_MAX + 1; \
152 /*----- Functions provided ------------------------------------------------*/
154 /* --- @mp_fromINT@ --- *
156 * Arguments: @mp *d@ = pointer to destination multiprecision integer
157 * @INT i@ = standard C integer to convert
159 * Returns: The resulting multiprecision integer.
161 * Use: Converts a standard C integer to a multiprecision integer.
164 #define mp_fromINT(name, type) \
165 extern mp *mp_from##name(mp */*d*/, type /*i*/)
167 mp_fromINT(short, short);
168 mp_fromINT(ushort, unsigned short);
169 mp_fromINT(int, int);
170 mp_fromINT(uint, unsigned);
171 mp_fromINT(uint32, uint32);
172 mp_fromINT(long, long);
173 mp_fromINT(ulong, unsigned long);
177 /* --- @mp_toINT@ --- *
179 * Arguments: @const mp *m@ = pointer to a multiprecision integer
181 * Returns: The value of the integer @m@ as a C integer.
183 * Use: Converts a multiprecision integer to a standard C integer.
184 * If the value of the multiprecision integer cannot be
185 * represented in the return type, and the return type is
186 * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return
187 * type is signed, the behaviour is undefined.
190 #define mp_toINT(name, type) \
191 extern type mp_to##name(const mp */*m*/)
193 mp_toINT(short, short);
194 mp_toINT(ushort, unsigned short);
196 mp_toINT(uint, unsigned);
197 mp_toINT(uint32, uint32);
198 mp_toINT(long, long);
199 mp_toINT(ulong, unsigned long);
203 /*----- That's all, folks -------------------------------------------------*/