[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / gmp-impl.h
1 /* Include file for internal GNU MP types and definitions.
2
3 Copyright (C) 1991 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 General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.   */
20
21 #if defined (__GNUC__) || defined (__sparc__) || defined (sparc)
22 #define alloca __builtin_alloca
23 #endif
24
25 #ifndef NULL
26 #define NULL 0L
27 #endif
28
29 #if defined (__GNUC__)
30 volatile void abort (void);
31 #else
32 #define inline                  /* Empty */
33 void *alloca();
34 #endif
35
36 #define ABS(x) (x >= 0 ? x : -x)
37
38 #include "gmp-mparam.h"
39
40 /* 
41  Cosmetic, but when running mkdependC on this under linux-2.x,
42  we're warned about redefinition of signed (done in sys/cdefs.h).
43  To avoid this wibble, we've weakened the #if below.
44  */
45 #if  defined(__STDC__) || defined(__linux__)
46 void *malloc (size_t);
47 void *realloc (void *, size_t);
48 void free (void *);
49
50 extern void *   (*_mp_allocate_func) (size_t);
51 extern void *   (*_mp_reallocate_func) (void *, size_t, size_t);
52 extern void     (*_mp_free_func) (void *, size_t);
53
54 void *_mp_default_allocate (size_t);
55 void *_mp_default_reallocate (void *, size_t, size_t);
56 void _mp_default_free (void *, size_t);
57
58 char *_mpz_get_str (char *, int, const MP_INT *);
59 int _mpz_set_str (MP_INT *, const char *, int);
60 void _mpz_impl_sqrt (MP_INT *, MP_INT *, const MP_INT *);
61 #else
62 #define const                   /* Empty */
63 #define signed                  /* Empty */
64
65 void *malloc ();
66 void *realloc ();
67 void free ();
68
69 extern void *   (*_mp_allocate_func) ();
70 extern void *   (*_mp_reallocate_func) ();
71 extern void     (*_mp_free_func) ();
72
73 void *_mp_default_allocate ();
74 void *_mp_default_reallocate ();
75 void _mp_default_free ();
76
77 char *_mpz_get_str ();
78 int _mpz_set_str ();
79 void _mpz_impl_sqrt ();
80 #endif
81
82 /* Copy NLIMBS *limbs* from SRC to DST.  */
83 #define MPN_COPY(DST, SRC, NLIMBS) \
84   do {                                                                  \
85     mp_size i;                                                          \
86     for (i = 0; i < (NLIMBS); i++)                                      \
87       (DST)[i] = (SRC)[i];                                              \
88   } while (0)
89 /* Zero NLIMBS *limbs* AT DST.  */
90 #define MPN_ZERO(DST, NLIMBS) \
91   do {                                                                  \
92     mp_size i;                                                          \
93     for (i = 0; i < (NLIMBS); i++)                                      \
94       (DST)[i] = 0;                                                     \
95   } while (0)
96
97 /* Initialize the MP_INT X with space for NLIMBS limbs.
98    X should be a temporary variable, and it will be automatically
99    cleared out when the running function returns.  */
100 #define MPZ_TMP_INIT(X, NLIMBS) \
101   do {                                                                  \
102     (X)->alloc = (NLIMBS);                                              \
103     (X)->d = (mp_ptr) alloca ((NLIMBS) * BYTES_PER_MP_LIMB);            \
104   } while (0)
105
106 /* Structure for conversion between internal binary format and
107    strings in base 2..36.  */
108 struct bases
109 {
110   /* Number of digits in the conversion base that always fits in
111      an mp_limb.  For example, for base 10 this is 10, since
112      2**32 = 4294967296 has ten digits.  */
113   int chars_per_limb;
114
115   /* big_base is conversion_base**chars_per_limb, i.e. the biggest
116      number that fits a word, built by factors of conversion_base.
117      Exception: For 2, 4, 8, etc, big_base is log2(base), i.e. the
118      number of bits used to represent each digit in the base.  */
119   mp_limb big_base;
120
121   /* big_base_inverted is a BITS_PER_MP_LIMB bit approximation to
122      1/big_base, represented as a fixed-point number.  Instead of
123      dividing by big_base an application can choose to multiply
124      by big_base_inverted.  */
125   mp_limb big_base_inverted;
126
127   /* log(2)/log(conversion_base) */
128   float chars_per_bit_exactly;
129 };
130
131 extern const struct bases __mp_bases[37];