checkin of 0.3.0
[pspp-builds.git] / lib / gmp / gmp-impl.h
1 /* Include file for internal GNU MP types and definitions.
2
3 Copyright (C) 1991, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
15 License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
21
22 #if 0 /* PSPP has its own alloca */
23 /* When using gcc, make sure to use its builtin alloca.  */
24 #if ! defined (alloca) && defined (__GNUC__)
25 #define alloca __builtin_alloca
26 #define HAVE_ALLOCA
27 #endif
28
29 /* When using cc, do whatever necessary to allow use of alloca.  For many
30    machines, this means including alloca.h.  IBM's compilers need a #pragma
31    in "each module that needs to use alloca".  */
32 #if ! defined (alloca)
33 /* We need lots of variants for MIPS, to cover all versions and perversions
34    of OSes for MIPS.  */
35 #if defined (__mips) || defined (MIPSEL) || defined (MIPSEB) \
36  || defined (_MIPSEL) || defined (_MIPSEB) || defined (__sgi) \
37  || defined (__alpha) || defined (__sparc) || defined (sparc) \
38  || defined (__ksr__)
39 #include <alloca.h>
40 #define HAVE_ALLOCA
41 #endif
42 #if defined (_IBMR2)
43 #pragma alloca
44 #define HAVE_ALLOCA
45 #endif
46 #if defined (__DECC)
47 #define alloca(x) __ALLOCA(x)
48 #define HAVE_ALLOCA
49 #endif
50 #endif
51 #endif /* 0 */
52
53 #if ! defined (HAVE_ALLOCA) || USE_STACK_ALLOC
54 #include "stack-alloc.h"
55 #else
56 #define TMP_DECL(m)
57 #define TMP_ALLOC(x) alloca(x)
58 #define TMP_MARK(m)
59 #define TMP_FREE(m)
60 #endif
61
62 #ifndef NULL
63 #define NULL ((void *) 0)
64 #endif
65
66 #if ! defined (__GNUC__)
67 #define inline                  /* Empty */
68 #endif
69
70 #define ABS(x) (x >= 0 ? x : -x)
71 #define MIN(l,o) ((l) < (o) ? (l) : (o))
72 #define MAX(h,i) ((h) > (i) ? (h) : (i))
73
74 /* Field access macros.  */
75 #define SIZ(x) ((x)->_mp_size)
76 #define ABSIZ(x) ABS (SIZ (x))
77 #define PTR(x) ((x)->_mp_d)
78 #define EXP(x) ((x)->_mp_exp)
79 #define PREC(x) ((x)->_mp_prec)
80 #define ALLOC(x) ((x)->_mp_alloc)
81
82 #include "gmp-mparam.h"
83 /* #include "longlong.h" */
84
85 #if defined (__STDC__)  || defined (__cplusplus)
86 void *malloc (size_t);
87 void *realloc (void *, size_t);
88 void free (void *);
89
90 extern void *   (*_mp_allocate_func) (size_t);
91 extern void *   (*_mp_reallocate_func) (void *, size_t, size_t);
92 extern void     (*_mp_free_func) (void *, size_t);
93
94 void *_mp_default_allocate (size_t);
95 void *_mp_default_reallocate (void *, size_t, size_t);
96 void _mp_default_free (void *, size_t);
97
98 #else
99
100 #define const                   /* Empty */
101 #define signed                  /* Empty */
102
103 void *malloc ();
104 void *realloc ();
105 void free ();
106
107 extern void *   (*_mp_allocate_func) ();
108 extern void *   (*_mp_reallocate_func) ();
109 extern void     (*_mp_free_func) ();
110
111 void *_mp_default_allocate ();
112 void *_mp_default_reallocate ();
113 void _mp_default_free ();
114 #endif
115
116 /* Copy NLIMBS *limbs* from SRC to DST.  */
117 #define MPN_COPY_INCR(DST, SRC, NLIMBS) \
118   do {                                                                  \
119     mp_size_t __i;                                                      \
120     for (__i = 0; __i < (NLIMBS); __i++)                                \
121       (DST)[__i] = (SRC)[__i];                                          \
122   } while (0)
123 #define MPN_COPY_DECR(DST, SRC, NLIMBS) \
124   do {                                                                  \
125     mp_size_t __i;                                                      \
126     for (__i = (NLIMBS) - 1; __i >= 0; __i--)                           \
127       (DST)[__i] = (SRC)[__i];                                          \
128   } while (0)
129 #define MPN_COPY MPN_COPY_INCR
130
131 /* Zero NLIMBS *limbs* AT DST.  */
132 #define MPN_ZERO(DST, NLIMBS) \
133   do {                                                                  \
134     mp_size_t __i;                                                      \
135     for (__i = 0; __i < (NLIMBS); __i++)                                \
136       (DST)[__i] = 0;                                                   \
137   } while (0)
138
139 #define MPN_NORMALIZE(DST, NLIMBS) \
140   do {                                                                  \
141     while (NLIMBS > 0)                                                  \
142       {                                                                 \
143         if ((DST)[(NLIMBS) - 1] != 0)                                   \
144           break;                                                        \
145         NLIMBS--;                                                       \
146       }                                                                 \
147   } while (0)
148 #define MPN_NORMALIZE_NOT_ZERO(DST, NLIMBS) \
149   do {                                                                  \
150     while (1)                                                           \
151       {                                                                 \
152         if ((DST)[(NLIMBS) - 1] != 0)                                   \
153           break;                                                        \
154         NLIMBS--;                                                       \
155       }                                                                 \
156   } while (0)
157
158 /* Initialize the MP_INT X with space for NLIMBS limbs.
159    X should be a temporary variable, and it will be automatically
160    cleared out when the running function returns.
161    We use __x here to make it possible to accept both mpz_ptr and mpz_t
162    arguments.  */
163 #define MPZ_TMP_INIT(X, NLIMBS) \
164   do {                                                                  \
165     mpz_ptr __x = (X);                                                  \
166     __x->_mp_alloc = (NLIMBS);                                          \
167     __x->_mp_d = (mp_ptr) TMP_ALLOC ((NLIMBS) * BYTES_PER_MP_LIMB);     \
168   } while (0)
169
170 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
171   do {                                                                  \
172     if ((size) < KARATSUBA_THRESHOLD)                                   \
173       impn_mul_n_basecase (prodp, up, vp, size);                        \
174     else                                                                \
175       impn_mul_n (prodp, up, vp, size, tspace);                 \
176   } while (0);
177 #define MPN_SQR_N_RECURSE(prodp, up, size, tspace) \
178   do {                                                                  \
179     if ((size) < KARATSUBA_THRESHOLD)                                   \
180       impn_sqr_n_basecase (prodp, up, size);                            \
181     else                                                                \
182       impn_sqr_n (prodp, up, size, tspace);                             \
183   } while (0);
184
185 /* Structure for conversion between internal binary format and
186    strings in base 2..36.  */
187 struct bases
188 {
189   /* Number of digits in the conversion base that always fits in an mp_limb_t.
190      For example, for base 10 on a machine where a mp_limb_t has 32 bits this
191      is 9, since 10**9 is the largest number that fits into a mp_limb_t.  */
192   int chars_per_limb;
193
194   /* log(2)/log(conversion_base) */
195   float chars_per_bit_exactly;
196
197   /* base**chars_per_limb, i.e. the biggest number that fits a word, built by
198      factors of base.  Exception: For 2, 4, 8, etc, big_base is log2(base),
199      i.e. the number of bits used to represent each digit in the base.  */
200   mp_limb_t big_base;
201
202   /* A BITS_PER_MP_LIMB bit approximation to 1/big_base, represented as a
203      fixed-point number.  Instead of dividing by big_base an application can
204      choose to multiply by big_base_inverted.  */
205   mp_limb_t big_base_inverted;
206 };
207
208 extern const struct bases __mp_bases[];
209 extern mp_size_t __gmp_default_fp_limb_precision;
210
211 /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
212    limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
213    If this would yield overflow, DI should be the largest possible number
214    (i.e., only ones).  For correct operation, the most significant bit of D
215    has to be set.  Put the quotient in Q and the remainder in R.  */
216 #define udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
217   do {                                                                  \
218     mp_limb_t _q, _ql, _r;                                              \
219     mp_limb_t _xh, _xl;                                                 \
220     umul_ppmm (_q, _ql, (nh), (di));                                    \
221     _q += (nh);                 /* DI is 2**BITS_PER_MP_LIMB too small */\
222     umul_ppmm (_xh, _xl, _q, (d));                                      \
223     sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl);                         \
224     if (_xh != 0)                                                       \
225       {                                                                 \
226         sub_ddmmss (_xh, _r, _xh, _r, 0, (d));                          \
227         _q += 1;                                                        \
228         if (_xh != 0)                                                   \
229           {                                                             \
230             sub_ddmmss (_xh, _r, _xh, _r, 0, (d));                      \
231             _q += 1;                                                    \
232           }                                                             \
233       }                                                                 \
234     if (_r >= (d))                                                      \
235       {                                                                 \
236         _r -= (d);                                                      \
237         _q += 1;                                                        \
238       }                                                                 \
239     (r) = _r;                                                           \
240     (q) = _q;                                                           \
241   } while (0)
242 /* Like udiv_qrnnd_preinv, but for for any value D.  DNORM is D shifted left
243    so that its most significant bit is set.  LGUP is ceil(log2(D)).  */
244 #define udiv_qrnnd_preinv2gen(q, r, nh, nl, d, di, dnorm, lgup) \
245   do {                                                                  \
246     mp_limb_t n2, n10, n1, nadj, q1;                                    \
247     mp_limb_t _xh, _xl;                                                 \
248     n2 = ((nh) << (BITS_PER_MP_LIMB - (lgup))) + ((nl) >> 1 >> (l - 1));\
249     n10 = (nl) << (BITS_PER_MP_LIMB - (lgup));                          \
250     n1 = ((mp_limb_signed_t) n10 >> (BITS_PER_MP_LIMB - 1));            \
251     nadj = n10 + (n1 & (dnorm));                                        \
252     umul_ppmm (_xh, _xl, di, n2 - n1);                                  \
253     add_ssaaaa (_xh, _xl, _xh, _xl, 0, nadj);                           \
254     q1 = ~(n2 + _xh);                                                   \
255     umul_ppmm (_xh, _xl, q1, d);                                        \
256     add_ssaaaa (_xh, _xl, _xh, _xl, nh, nl);                            \
257     _xh -= (d);                                                         \
258     (r) = _xl + ((d) & _xh);                                            \
259     (q) = _xh - q1;                                                     \
260   } while (0)
261 /* Exactly like udiv_qrnnd_preinv, but branch-free.  It is not clear which
262    version to use.  */
263 #define udiv_qrnnd_preinv2norm(q, r, nh, nl, d, di) \
264   do {                                                                  \
265     mp_limb_t n2, n10, n1, nadj, q1;                                    \
266     mp_limb_t _xh, _xl;                                                 \
267     n2 = (nh);                                                          \
268     n10 = (nl);                                                         \
269     n1 = ((mp_limb_signed_t) n10 >> (BITS_PER_MP_LIMB - 1));            \
270     nadj = n10 + (n1 & (d));                                            \
271     umul_ppmm (_xh, _xl, di, n2 - n1);                                  \
272     add_ssaaaa (_xh, _xl, _xh, _xl, 0, nadj);                           \
273     q1 = ~(n2 + _xh);                                                   \
274     umul_ppmm (_xh, _xl, q1, d);                                        \
275     add_ssaaaa (_xh, _xl, _xh, _xl, nh, nl);                            \
276     _xh -= (d);                                                         \
277     (r) = _xl + ((d) & _xh);                                            \
278     (q) = _xh - q1;                                                     \
279   } while (0)
280
281 #if defined (__GNUC__)
282 /* Define stuff for longlong.h.  */
283 typedef unsigned int UQItype    __attribute__ ((mode (QI)));
284 typedef          int SItype     __attribute__ ((mode (SI)));
285 typedef unsigned int USItype    __attribute__ ((mode (SI)));
286 typedef          int DItype     __attribute__ ((mode (DI)));
287 typedef unsigned int UDItype    __attribute__ ((mode (DI)));
288 #else
289 typedef unsigned char UQItype;
290 typedef          long SItype;
291 typedef unsigned long USItype;
292 #endif
293
294 typedef mp_limb_t UWtype;
295 typedef unsigned int UHWtype;
296 #define W_TYPE_SIZE BITS_PER_MP_LIMB
297
298 /* Internal mpn calls */
299 #define impn_mul_n_basecase     __MPN(impn_mul_n_basecase)
300 #define impn_mul_n              __MPN(impn_mul_n)
301 #define impn_sqr_n_basecase     __MPN(impn_sqr_n_basecase)
302 #define impn_sqr_n              __MPN(impn_sqr_n)
303
304 void impn_mul_n_basecase (mp_ptr prodp, mp_srcptr up, mp_srcptr vp,
305                           mp_size_t size);
306 void impn_mul_n (mp_ptr prodp, mp_srcptr up, mp_srcptr vp, mp_size_t size,
307                  mp_ptr tspace);
308
309 /* Define ieee_double_extract and _GMP_IEEE_FLOATS.  */
310
311 #if defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)             \
312  || defined (__alpha)                                                   \
313  || (defined (__arm__) && defined (__ARMWEL__))                         \
314  || defined (__clipper__)                                               \
315  || defined (__cris)                                                    \
316  || defined (__i386__)                                                  \
317  || defined (__i860__)                                                  \
318  || defined (__i960__)                                                  \
319  || defined (MIPSEL) || defined (_MIPSEL)                               \
320  || defined (__ns32000__)                                               \
321  || defined (__WINNT) || defined (_WIN32)
322 #define _GMP_IEEE_FLOATS 1
323 union ieee_double_extract
324 {
325   struct
326     {
327       unsigned int manl:32;
328       unsigned int manh:20;
329       unsigned int exp:11;
330       unsigned int sig:1;
331     } s;
332   double d;
333 };
334 #else /* Need this as an #else since the tests aren't made exclusive.  */
335 #if defined (_BIG_ENDIAN)                                               \
336  || defined (__a29k__) || defined (_AM29K)                              \
337  || defined (__arm__)                                                   \
338  || (defined (__convex__) && defined (_IEEE_FLOAT_))                    \
339  || defined (__i370__) || defined (__mvs__)                             \
340  || defined (__mc68000__) || defined (__mc68020__) || defined (__NeXT__)\
341     || defined(mc68020)                                                 \
342  || defined (__m88000__)                                                \
343  || defined (MIPSEB) || defined (_MIPSEB)                               \
344  || defined (__hppa)                                                    \
345  || defined (__pyr__)                                                   \
346  || defined (__ibm032__)                                                \
347  || defined (_IBMR2) || defined (_ARCH_PPC)                             \
348  || defined (__sh__)                                                    \
349  || defined (__sparc) || defined (sparc)                                \
350  || defined (__we32k__)
351 #define _GMP_IEEE_FLOATS 1
352 union ieee_double_extract
353 {
354   struct
355     {
356       unsigned int sig:1;
357       unsigned int exp:11;
358       unsigned int manh:20;
359       unsigned int manl:32;
360     } s;
361   double d;
362 };
363 #endif
364 #endif
365
366 #define MP_BASE_AS_DOUBLE (2.0 * ((mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1)))
367 #if BITS_PER_MP_LIMB == 64
368 #define LIMBS_PER_DOUBLE 2
369 #else
370 #define LIMBS_PER_DOUBLE 3
371 #endif
372
373 double __gmp_scale2 _PROTO ((double, int));
374 int __gmp_extract_double _PROTO((mp_ptr, double));