Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mpfr-gmp.hpp
1 /* Interface to replace gmp-impl.h
2 
3 Copyright 2004, 2005, 2009 Free Software Foundation, Inc.
4 
5 This file is part of the MPFR Library.
6 
7 The MPFR Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3.0 of the License, or (at your
10 option) any later version.
11 
12 The MPFR 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 Lesser General Public
15 License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with the MPFR Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21 
22 #ifndef __GMPFR_GMP_H__
23 #define __GMPFR_GMP_H__
24 
25 #ifndef __MPFR_IMPL_H__
26 # error "mpfr-impl.h not included"
27 #endif
28 
29 #include <limits.h> /* For INT_MAX, ... */
30 #include <string.h> /* For memcpy, memset and memmove */
31 
32 /* The following tries to get a good version of alloca.
33  See gmp-impl.h for implementation details and original version */
34 #ifndef alloca
35 # if defined ( __GNUC__ )
36 # define alloca __builtin_alloca
37 # elif defined (__DECC)
38 # define alloca(x) __ALLOCA(x)
39 # elif defined (_MSC_VER)
40 # include <malloc.h>
41 # define alloca _alloca
42 # elif defined (FEELPP_HAS_ALLOCA_H)
43 # include <alloca.h>
44 # elif defined (_AIX) || defined (_IBMR2)
45 #pragma alloca
46 # else
47 char *alloca ();
48 # endif
49 #endif
50 
51 #if defined (__cplusplus)
52 extern "C" {
53 #endif
54 
55  /* Define BITS_PER_MP_LIMB
56  Can't use sizeof(mp_limb_t) since it should be a preprocessor constant */
57 #if defined(GMP_NUMB_BITS) /* GMP 4.1.2 or above */
58 # define BITS_PER_MP_LIMB (GMP_NUMB_BITS+GMP_NAIL_BITS)
59 #elif defined (__GMP_BITS_PER_MP_LIMB) /* Older versions 4.x.x */
60 # define BITS_PER_MP_LIMB __GMP_BITS_PER_MP_LIMB
61 # define GMP_NUMB_BITS BITS_PER_MP_LIMB
62 # ifndef GMP_NAIL_BITS
63 # define GMP_NAIL_BITS 0
64 # endif
65 #else
66 # error "Could not detect BITS_PER_MP_LIMB. Try with gmp internal files."
67 #endif
68 
69  /* Define some macros */
70 #define BYTES_PER_MP_LIMB (BITS_PER_MP_LIMB/CHAR_BIT)
71 
72 #define MP_LIMB_T_MAX (~(mp_limb_t)0)
73 
74 #define ULONG_HIGHBIT (ULONG_MAX ^ ((unsigned long) ULONG_MAX >> 1))
75 #define UINT_HIGHBIT (UINT_MAX ^ ((unsigned) UINT_MAX >> 1))
76 #define USHRT_HIGHBIT ((unsigned short) (USHRT_MAX ^ ((unsigned short) USHRT_MAX >> 1)))
77 
78 #define GMP_LIMB_HIGHBIT (MP_LIMB_T_MAX ^ (MP_LIMB_T_MAX >> 1))
79 
80 
81 #if __GMP_MP_SIZE_T_INT
82 #define MP_SIZE_T_MAX INT_MAX
83 #define MP_SIZE_T_MIN INT_MIN
84 #else
85 #define MP_SIZE_T_MAX LONG_MAX
86 #define MP_SIZE_T_MIN LONG_MIN
87 #endif
88 
89 #define LONG_HIGHBIT LONG_MIN
90 #define INT_HIGHBIT INT_MIN
91 #define SHRT_HIGHBIT SHRT_MIN
92 
93  /* MP_LIMB macros */
94 #define MPN_ZERO(dst, n) memset((dst), 0, (n)*BYTES_PER_MP_LIMB)
95 #define MPN_COPY_DECR(dst,src,n) memmove((dst),(src),(n)*BYTES_PER_MP_LIMB)
96 #define MPN_COPY_INCR(dst,src,n) memmove((dst),(src),(n)*BYTES_PER_MP_LIMB)
97 #define MPN_COPY(dst,src,n) \
98  do \
99  { \
100  if ((dst) != (src)) \
101  { \
102  MPFR_ASSERTD ((char *) (dst) >= (char *) (src) + \
103  (n) * BYTES_PER_MP_LIMB || \
104  (char *) (src) >= (char *) (dst) + \
105  (n) * BYTES_PER_MP_LIMB); \
106  memcpy ((dst), (src), (n) * BYTES_PER_MP_LIMB); \
107  } \
108  } \
109  while (0)
110 
111  /* MPN macros taken from gmp-impl.h */
112 #define MPN_NORMALIZE(DST, NLIMBS) \
113  do { \
114  while (NLIMBS > 0) \
115  { \
116  if ((DST)[(NLIMBS) - 1] != 0) \
117  break; \
118  NLIMBS--; \
119  } \
120  } while (0)
121 #define MPN_NORMALIZE_NOT_ZERO(DST, NLIMBS) \
122  do { \
123  MPFR_ASSERTD ((NLIMBS) >= 1); \
124  while (1) \
125  { \
126  if ((DST)[(NLIMBS) - 1] != 0) \
127  break; \
128  NLIMBS--; \
129  } \
130  } while (0)
131 #define MPN_OVERLAP_P(xp, xsize, yp, ysize) \
132  ((xp) + (xsize) > (yp) && (yp) + (ysize) > (xp))
133 #define MPN_SAME_OR_INCR2_P(dst, dsize, src, ssize) \
134  ((dst) <= (src) || ! MPN_OVERLAP_P (dst, dsize, src, ssize))
135 #define MPN_SAME_OR_INCR_P(dst, src, size) \
136  MPN_SAME_OR_INCR2_P(dst, size, src, size)
137 #define MPN_SAME_OR_DECR2_P(dst, dsize, src, ssize) \
138  ((dst) >= (src) || ! MPN_OVERLAP_P (dst, dsize, src, ssize))
139 #define MPN_SAME_OR_DECR_P(dst, src, size) \
140  MPN_SAME_OR_DECR2_P(dst, size, src, size)
141 
142  /* If sqr_n or mul_basecase are not exported, used mpn_mul instead */
143 #ifndef mpn_sqr_n
144 # define mpn_sqr_n(dst,src,n) mpn_mul((dst),(src),(n),(src),(n))
145 #endif
146 #ifndef mpn_mul_basecase
147 # define mpn_mul_basecase(dst,s1,n1,s2,n2) mpn_mul((dst),(s1),(n1),(s2),(n2))
148 #endif
149 #ifndef mpn_sqr_basecase
150 # define mpn_sqr_basecase(dst,src,n) mpn_mul((dst),(src),(n),(src),(n))
151 #endif
152 
153  /* ASSERT */
154  __MPFR_DECLSPEC void mpfr_assert_fail _MPFR_PROTO( ( const char *, int,
155  const char * ) );
156 
157 #define ASSERT_FAIL(expr) mpfr_assert_fail (__FILE__, __LINE__, #expr)
158 #define ASSERT(expr) MPFR_ASSERTD(expr)
159 
160  /* Access fileds of GMP struct */
161 #define SIZ(x) ((x)->_mp_size)
162 #define ABSIZ(x) ABS (SIZ (x))
163 #define PTR(x) ((x)->_mp_d)
164 #define LIMBS(x) ((x)->_mp_d)
165 #define EXP(x) ((x)->_mp_exp)
166 #define PREC(x) ((x)->_mp_prec)
167  //#define ALLOC(x) ((x)->_mp_alloc)
168 #define MPZ_REALLOC(z,n) ((n) > ALLOC(z) ? _mpz_realloc(z,n) : PTR(z))
169 
170  /* Non IEEE float supports -- needs to detect them with proper configure */
171 #undef XDEBUG
172 #define XDEBUG
173 
174  /* Temp memory allocate */
175 #define TMP_DECL(m)
176 #define TMP_ALLOC(x) alloca(x)
177 #define TMP_MARK(m)
178 #define TMP_FREE(m)
179 
180  /* For longlong.h */
181 #ifdef FEELPP_HAS_ATTRIBUTE_MODE
182  typedef unsigned int UQItype __attribute__ ( ( mode ( QI ) ) );
183  typedef int SItype __attribute__ ( ( mode ( SI ) ) );
184  typedef unsigned int USItype __attribute__ ( ( mode ( SI ) ) );
185  typedef int DItype __attribute__ ( ( mode ( DI ) ) );
186  typedef unsigned int UDItype __attribute__ ( ( mode ( DI ) ) );
187 #else
188  typedef unsigned char UQItype;
189  typedef long SItype;
190  typedef unsigned long USItype;
191 #ifdef FEELPP_HAS_LONG_LONG
192  typedef long long int DItype;
193  typedef unsigned long long int UDItype;
194 #else /* Assume `long' gives us a wide enough type. Needed for hppa2.0w. */
195  typedef long int DItype;
196  typedef unsigned long int UDItype;
197 #endif
198 #endif
199  typedef mp_limb_t UWtype;
200  typedef unsigned int UHWtype;
201 #define W_TYPE_SIZE BITS_PER_MP_LIMB
202 
203  /* Remap names of internal mpn functions (for longlong.h). */
204 #undef __clz_tab
205 #define __clz_tab mpfr_clz_tab
206 
207  /* Use (4.0 * ...) instead of (2.0 * ...) to work around buggy compilers
208  that don't convert ulong->double correctly (eg. SunOS 4 native cc). */
209 #undef MP_BASE_AS_DOUBLE
210 #define MP_BASE_AS_DOUBLE (4.0 * ((mp_limb_t) 1 << (GMP_NUMB_BITS - 2)))
211 
212  /* Structure for conversion between internal binary format and
213  strings in base 2..36. */
214  struct bases
215  {
216  /* log(2)/log(conversion_base) */
217  double chars_per_bit_exactly;
218  };
219 #undef __mp_bases
220 #define __mp_bases mpfr_bases
221  __MPFR_DECLSPEC extern const struct bases mpfr_bases[257];
222 
223  /* Standard macros */
224 #undef ABS
225 #undef MIN
226 #undef MAX
227 #undef numberof
228 #define ABS(x) ((x) >= 0 ? (x) : -(x))
229 #define MIN(l,o) ((l) < (o) ? (l) : (o))
230 #define MAX(h,i) ((h) > (i) ? (h) : (i))
231 #define numberof(x) (sizeof (x) / sizeof ((x)[0]))
232 
233  /* Random */
234 #undef __gmp_rands_initialized
235 #undef __gmp_rands
236 #define __gmp_rands_initialized mpfr_rands_initialized
237 #define __gmp_rands mpfr_rands
238 
239  __MPFR_DECLSPEC extern char mpfr_rands_initialized;
240  __MPFR_DECLSPEC extern gmp_randstate_t mpfr_rands;
241 
242 #undef RANDS
243 #define RANDS \
244  ((__gmp_rands_initialized ? 0 \
245  : (__gmp_rands_initialized = 1, \
246  gmp_randinit_default (__gmp_rands), 0)), \
247  __gmp_rands)
248 
249 #undef RANDS_CLEAR
250 #define RANDS_CLEAR() \
251  do { \
252  if (__gmp_rands_initialized) \
253  { \
254  __gmp_rands_initialized = 0; \
255  gmp_randclear (__gmp_rands); \
256  } \
257  } while (0)
258 
259  typedef __gmp_randstate_struct *gmp_randstate_ptr;
260 
261 #undef _gmp_rand
262 #define _gmp_rand mpfr_rand_raw
263  __MPFR_DECLSPEC void mpfr_rand_raw _MPFR_PROTO( ( mp_ptr, gmp_randstate_t,
264  unsigned long ) );
265 
266  /* To be called BEFORE tests_start_mpfr () if the tests used
267  mpfr_random, mpfr_random2 or any functions which uses the macro RANDS */
268  __MPFR_DECLSPEC void mpfr_init_gmp_rand _MPFR_PROTO( ( void ) );
269 #define MPFR_TEST_USE_RANDS() mpfr_init_gmp_rand ();
270 
271 
272  /* Allocate func are defined in gmp-impl.h */
273 
274  /* In newer GMP, there aren't anymore __gmp_allocate_func,
275  __gmp_reallocate_func & __gmp_free_func in gmp.h
276  Just getting the correct value by calling mp_get_memory_functions */
277 #ifdef mp_get_memory_functions
278 
279 #undef __gmp_allocate_func
280 #undef __gmp_reallocate_func
281 #undef __gmp_free_func
282 #define MPFR_GET_MEMFUNC mp_get_memory_functions(&mpfr_allocate_func, &mpfr_reallocate_func, &mpfr_free_func)
283 #define __gmp_allocate_func (MPFR_GET_MEMFUNC, mpfr_allocate_func)
284 #define __gmp_reallocate_func (MPFR_GET_MEMFUNC, mpfr_reallocate_func)
285 #define __gmp_free_func (MPFR_GET_MEMFUNC, mpfr_free_func)
286  __MPFR_DECLSPEC extern void * ( *mpfr_allocate_func ) _MPFR_PROTO ( ( size_t ) );
287  __MPFR_DECLSPEC extern void * ( *mpfr_reallocate_func ) _MPFR_PROTO ( ( void *,
288  size_t, size_t ) );
289  __MPFR_DECLSPEC extern void ( *mpfr_free_func ) _MPFR_PROTO ( ( void *,
290  size_t ) );
291 
292 #endif
293 
294 #undef __gmp_default_allocate
295 #undef __gmp_default_reallocate
296 #undef __gmp_default_free
297 #define __gmp_default_allocate mpfr_default_allocate
298 #define __gmp_default_reallocate mpfr_default_reallocate
299 #define __gmp_default_free mpfr_default_free
300  __MPFR_DECLSPEC void *__gmp_default_allocate _MPFR_PROTO ( ( size_t ) );
301  __MPFR_DECLSPEC void *__gmp_default_reallocate _MPFR_PROTO ( ( void *, size_t,
302  size_t ) );
303  __MPFR_DECLSPEC void __gmp_default_free _MPFR_PROTO ( ( void *, size_t ) );
304 
305 #if defined (__cplusplus)
306 }
307 #endif
308 
309 #endif /* Gmp internal emulator */

Generated on Fri Oct 25 2013 14:24:19 for Feel++ by doxygen 1.8.4