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