Rename MP_IS* to MP_*P, for consistency's sake. Use these macros more often.
[u/mdw/catacomb] / mpint.h
CommitLineData
70b904c5 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
70b904c5 4 *
5 * Conversion between MPs and standard C integers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
92c2a290 30#ifndef CATACOMB_MPINT_H
31#define CATACOMB_MPINT_H
70b904c5 32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
39#include <limits.h>
40
92c2a290 41#ifndef CATACOMB_MP_H
70b904c5 42# include "mp.h"
43#endif
44
45/*----- Generic translation macros ----------------------------------------*/
46
47/* --- @MP_FROMINT@ --- *
48 *
49 * Arguments: @d@ = destination multiprecision integer
50 * @type@ = type of integer which @i@ is
51 * @i@ = a standard C integer
52 *
53 * Use: Stores the value of @i@ in @d@. This macro is actually
54 * rather subtle in places. Be careful what you change.
55 */
56
57#define MP_FROMINT(d, type, i) do { \
58 type _i = (i); \
59 size_t _o = 0; \
60 mp *_d = (d); \
61 size_t _sz = 4; \
62 \
d34decd2 63 MP_DEST(_d, _sz, 0); \
70b904c5 64 _d->f &= ~(MP_NEG | MP_UNDEF); \
65 \
66 /* --- Set the sign on the MP --- * \
67 * \
68 * If the input integer is *not* negative, then negate it. This \
69 * fixes a problem with two's complement machines where the most \
70 * negative value actually has larger magnitude than the most \
71 * positive, and hence -TYPE_MIN == TYPE_MIN but TYPE_MIN != 0. If \
72 * all the work is carried out on negative numbers there isn't a \
73 * problem. \
74 */ \
75 \
76 if (_i >= 0) \
77 _i = -_i; \
78 else \
79 _d->f |= MP_NEG; \
80 \
81 while (_i) { \
82 if (_o == _sz) { \
83 _sz <<= 1; \
84 MP_ENSURE(_d, _sz); \
85 } \
86 _d->v[_o++] = MPW(-_i); \
87 \
88 /* --- More subtlety --- * \
89 * \
90 * Ideally, I'd like to just shift @i@ right by @MPW_BITS@. But I \
91 * can't, because that might be more than I'm allowed. I can't \
92 * divide by @MPW_MAX + 1@ because that might turn out to be zero \
93 * in my current type, and besides which it's unsigned which messes \
94 * up all of my negative arithmetic. So do an explicit test here. \
95 */ \
96 \
8646ee38 97 if (_i >= -MPW_MAX) \
70b904c5 98 break; \
99 else \
100 _i /= (type)MPW_MAX + 1; \
101 } \
102 _d->vl = _d->v + _o; \
103 (d) = _d; \
104} while (0)
105
106/* --- @MP_TOINT@ --- *
107 *
108 * Arguments: @m@ = a multiprecision integer
109 * @type@ = the type of @i@
110 * @max@ = the largest value @i@ can represent
111 * @i@ = an integer variable
112 *
113 * Use: Stores the value of a multiprecision integer in a standard C
114 * integer. If the value won't fit, the behaviour is determined
115 * by the type of @i@: if @i@ is unsigned, the value of the
116 * multiprecision integer modulo @max + 1@ is stored; if @i@ is
117 * signed, the behaviour is undefined.
118 *
119 * If you don't want to be bitten by these sorts of things, keep
120 * copies of @INT_MAX@ or whatever is appropriate in
121 * multiprecision form and compare before conversion.
122 */
123
124#define MP_TOINT(m, type, max, i) do { \
125 type _i = 0; \
126 type _max = (max); \
127 unsigned _s = 0; \
128 const mp *_m = (m); \
129 const mpw *_v = _m->v, *_vl = _m->vl; \
130 \
131 /* --- Do all the arithmetic in negative numbers --- */ \
132 \
133 while (_v < _vl && _max > 0) { \
134 _i -= *_v << _s; \
135 _s += MPW_BITS; \
136 _v++; \
137 _max /= (mpd)MPW_MAX + 1; \
138 } \
a69a3efd 139 if (!MP_NEGP(_m)) \
70b904c5 140 _i = -_i; \
141 (i) = _i; \
142} while (0)
143
144/*----- Functions provided ------------------------------------------------*/
145
146/* --- @mp_fromINT@ --- *
147 *
148 * Arguments: @mp *d@ = pointer to destination multiprecision integer
149 * @INT i@ = standard C integer to convert
150 *
151 * Returns: The resulting multiprecision integer.
152 *
153 * Use: Converts a standard C integer to a multiprecision integer.
154 */
155
156#define mp_fromINT(name, type) \
157 extern mp *mp_from##name(mp */*d*/, type /*i*/)
158
159mp_fromINT(short, short);
160mp_fromINT(ushort, unsigned short);
161mp_fromINT(int, int);
162mp_fromINT(uint, unsigned);
92c2a290 163mp_fromINT(uint32, uint32);
70b904c5 164mp_fromINT(long, long);
165mp_fromINT(ulong, unsigned long);
166
167#undef mp_fromINT
168
169/* --- @mp_toINT@ --- *
170 *
171 * Arguments: @const mp *m@ = pointer to a multiprecision integer
172 *
173 * Returns: The value of the integer @m@ as a C integer.
174 *
175 * Use: Converts a multiprecision integer to a standard C integer.
176 * If the value of the multiprecision integer cannot be
177 * represented in the return type, and the return type is
178 * unsigned, it is reduced modulo @TYPE_MAX + 1@; if the return
179 * type is signed, the behaviour is undefined.
180 */
181
182#define mp_toINT(name, type) \
cfcaafa7 183 extern type mp_to##name(const mp */*m*/)
70b904c5 184
185mp_toINT(short, short);
186mp_toINT(ushort, unsigned short);
187mp_toINT(int, int);
188mp_toINT(uint, unsigned);
92c2a290 189mp_toINT(uint32, uint32);
70b904c5 190mp_toINT(long, long);
191mp_toINT(ulong, unsigned long);
192
193#undef mp_toINT
194
195/*----- That's all, folks -------------------------------------------------*/
196
197#ifdef __cplusplus
198 }
199#endif
200
201#endif