[project @ 1996-01-08 20:28:12 by partain]
[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 #ifdef __STDC__
41 void *malloc (size_t);
42 void *realloc (void *, size_t);
43 void free (void *);
44
45 extern void *   (*_mp_allocate_func) (size_t);
46 extern void *   (*_mp_reallocate_func) (void *, size_t, size_t);
47 extern void     (*_mp_free_func) (void *, size_t);
48
49 void *_mp_default_allocate (size_t);
50 void *_mp_default_reallocate (void *, size_t, size_t);
51 void _mp_default_free (void *, size_t);
52
53 char *_mpz_get_str (char *, int, const MP_INT *);
54 int _mpz_set_str (MP_INT *, const char *, int);
55 void _mpz_impl_sqrt (MP_INT *, MP_INT *, const MP_INT *);
56 #else
57 #define const                   /* Empty */
58 #define signed                  /* Empty */
59
60 void *malloc ();
61 void *realloc ();
62 void free ();
63
64 extern void *   (*_mp_allocate_func) ();
65 extern void *   (*_mp_reallocate_func) ();
66 extern void     (*_mp_free_func) ();
67
68 void *_mp_default_allocate ();
69 void *_mp_default_reallocate ();
70 void _mp_default_free ();
71
72 char *_mpz_get_str ();
73 int _mpz_set_str ();
74 void _mpz_impl_sqrt ();
75 #endif
76
77 /* Copy NLIMBS *limbs* from SRC to DST.  */
78 #define MPN_COPY(DST, SRC, NLIMBS) \
79   do {                                                                  \
80     mp_size i;                                                          \
81     for (i = 0; i < (NLIMBS); i++)                                      \
82       (DST)[i] = (SRC)[i];                                              \
83   } while (0)
84 /* Zero NLIMBS *limbs* AT DST.  */
85 #define MPN_ZERO(DST, NLIMBS) \
86   do {                                                                  \
87     mp_size i;                                                          \
88     for (i = 0; i < (NLIMBS); i++)                                      \
89       (DST)[i] = 0;                                                     \
90   } while (0)
91
92 /* Initialize the MP_INT X with space for NLIMBS limbs.
93    X should be a temporary variable, and it will be automatically
94    cleared out when the running function returns.  */
95 #define MPZ_TMP_INIT(X, NLIMBS) \
96   do {                                                                  \
97     (X)->alloc = (NLIMBS);                                              \
98     (X)->d = (mp_ptr) alloca ((NLIMBS) * BYTES_PER_MP_LIMB);            \
99   } while (0)
100
101 /* Structure for conversion between internal binary format and
102    strings in base 2..36.  */
103 struct bases
104 {
105   /* Number of digits in the conversion base that always fits in
106      an mp_limb.  For example, for base 10 this is 10, since
107      2**32 = 4294967296 has ten digits.  */
108   int chars_per_limb;
109
110   /* big_base is conversion_base**chars_per_limb, i.e. the biggest
111      number that fits a word, built by factors of conversion_base.
112      Exception: For 2, 4, 8, etc, big_base is log2(base), i.e. the
113      number of bits used to represent each digit in the base.  */
114   mp_limb big_base;
115
116   /* big_base_inverted is a BITS_PER_MP_LIMB bit approximation to
117      1/big_base, represented as a fixed-point number.  Instead of
118      dividing by big_base an application can choose to multiply
119      by big_base_inverted.  */
120   mp_limb big_base_inverted;
121
122   /* log(2)/log(conversion_base) */
123   float chars_per_bit_exactly;
124 };
125
126 extern const struct bases __mp_bases[37];