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