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