cc-hash.c (fhash): The FILE name may be null.
[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
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
70b904c5 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.
45c0fd36 18 *
70b904c5 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.
45c0fd36 23 *
70b904c5 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 \
3485fc41
MW
66 if (_i >= 0) { \
67 while (_i) { \
68 if (_o == _sz) { \
69 _sz <<= 1; \
70 MP_ENSURE(_d, _sz); \
71 } \
72 _d->v[_o++] = MPW(_i); \
7eaaecf5 73 if (_i <= MPW_MAX) \
3485fc41
MW
74 break; \
75 else \
76 _i /= (type)MPW_MAX + 1; \
77 } \
78 } else { \
70b904c5 79 _d->f |= MP_NEG; \
3485fc41
MW
80 while (_i) { \
81 if (_o == _sz) { \
82 _sz <<= 1; \
83 MP_ENSURE(_d, _sz); \
84 } \
85 _d->v[_o++] = MPW(-_i); \
7eaaecf5 86 if (_i >= -MPW_MAX) \
3485fc41
MW
87 break; \
88 else \
89 _i /= (type)MPW_MAX + 1; \
70b904c5 90 } \
70b904c5 91 } \
3485fc41 92 \
70b904c5 93 _d->vl = _d->v + _o; \
94 (d) = _d; \
95} while (0)
96
97/* --- @MP_TOINT@ --- *
98 *
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
103 *
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.
109 *
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.
113 */
114
115#define MP_TOINT(m, type, max, i) do { \
116 type _i = 0; \
117 type _max = (max); \
118 unsigned _s = 0; \
119 const mp *_m = (m); \
120 const mpw *_v = _m->v, *_vl = _m->vl; \
121 \
122 /* --- Do all the arithmetic in negative numbers --- */ \
123 \
124 while (_v < _vl && _max > 0) { \
125 _i -= *_v << _s; \
126 _s += MPW_BITS; \
127 _v++; \
128 _max /= (mpd)MPW_MAX + 1; \
129 } \
a69a3efd 130 if (!MP_NEGP(_m)) \
70b904c5 131 _i = -_i; \
132 (i) = _i; \
133} while (0)
134
135/*----- Functions provided ------------------------------------------------*/
136
137/* --- @mp_fromINT@ --- *
138 *
139 * Arguments: @mp *d@ = pointer to destination multiprecision integer
140 * @INT i@ = standard C integer to convert
141 *
142 * Returns: The resulting multiprecision integer.
143 *
144 * Use: Converts a standard C integer to a multiprecision integer.
145 */
146
147#define mp_fromINT(name, type) \
148 extern mp *mp_from##name(mp */*d*/, type /*i*/)
149
150mp_fromINT(short, short);
151mp_fromINT(ushort, unsigned short);
152mp_fromINT(int, int);
153mp_fromINT(uint, unsigned);
92c2a290 154mp_fromINT(uint32, uint32);
70b904c5 155mp_fromINT(long, long);
156mp_fromINT(ulong, unsigned long);
157
158#undef mp_fromINT
159
160/* --- @mp_toINT@ --- *
161 *
162 * Arguments: @const mp *m@ = pointer to a multiprecision integer
163 *
164 * Returns: The value of the integer @m@ as a C integer.
165 *
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.
171 */
172
173#define mp_toINT(name, type) \
cfcaafa7 174 extern type mp_to##name(const mp */*m*/)
70b904c5 175
176mp_toINT(short, short);
177mp_toINT(ushort, unsigned short);
178mp_toINT(int, int);
179mp_toINT(uint, unsigned);
92c2a290 180mp_toINT(uint32, uint32);
70b904c5 181mp_toINT(long, long);
182mp_toINT(ulong, unsigned long);
183
184#undef mp_toINT
185
186/*----- That's all, folks -------------------------------------------------*/
187
188#ifdef __cplusplus
189 }
190#endif
191
192#endif