1 /* Utilities for MPFR developers, not exported.
3 Copyright 1999-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #ifndef __MPFR_IMPL_H__
24 #define __MPFR_IMPL_H__
26 /* Include MPFR 'config.h' before ANY system headers */
31 /* Let's include some standard headers unconditionally as they are
32 already needed by several source files or when some options are
33 enabled/disabled, and it is easy to forget them (some configure
34 options may hide the error).
35 Note: If some source file must not have such a header included
36 (which is very unlikely and probably means something broken in
37 this source file), we should do that with some macro (that would
38 also force to disable incompatible features). */
39 #if defined (__cplusplus)
48 #if _MPFR_EXP_FORMAT == 4
49 /* mpfr_exp_t will be defined as intmax_t */
50 # include "mpfr-intmax.h"
53 /* Check if we are inside a build of MPFR or inside the test suite.
54 This is needed in mpfr.h to export or import the functions.
55 It matters only for Windows DLL */
56 #ifndef __MPFR_TEST_H__
57 # define __MPFR_WITHIN_MPFR 1
60 /******************************************************
61 ****************** Include files *********************
62 ******************************************************/
64 /* For the definition of MPFR_THREAD_ATTR. GCC/ICC detection macros are
65 no longer used, as they sometimes gave incorrect information about
66 the support of thread-local variables. A configure check is now done. */
67 #include "mpfr-thread.h"
69 #ifdef MPFR_HAVE_GMP_IMPL /* Build with gmp internals */
74 # ifndef __GMP_IMPL_H__
75 # include "gmp-impl.h"
77 # ifdef MPFR_NEED_LONGLONG_H
78 # include "longlong.h"
84 #else /* Build without gmp internals */
92 # ifndef __GMPFR_GMP_H__
93 # include "mpfr-gmp.h"
95 # ifdef MPFR_NEED_LONGLONG_H
96 # define LONGLONG_STANDALONE
97 # include "mpfr-longlong.h"
101 #undef MPFR_NEED_LONGLONG_H
103 /* If a mpn_sqr_n macro is not defined, use mpn_mul. GMP 4.x defines a
104 mpn_sqr_n macro in gmp-impl.h (and this macro disappeared in GMP 5),
105 so that GMP's macro can only be used when MPFR has been configured
106 with --with-gmp-build (and only with GMP 4.x). */
108 # define mpn_sqr_n(dst,src,n) mpn_mul((dst),(src),(n),(src),(n))
112 /******************************************************
113 ***************** Detection macros *******************
114 ******************************************************/
116 /* Macros to detect STDC, GCC, GLIBC, GMP and ICC version */
117 #if defined(__STDC_VERSION__)
118 # define __MPFR_STDC(version) (__STDC_VERSION__>=(version))
119 #elif defined(__STDC__)
120 # define __MPFR_STDC(version) (0 == (version))
122 # define __MPFR_STDC(version) 0
126 /* Under MS Windows (e.g. with VS2008 or VS2010), Intel's compiler doesn't
127 support/enable extensions like the ones seen under GNU/Linux.
128 https://sympa.inria.fr/sympa/arc/mpfr/2011-02/msg00032.html */
129 # define __MPFR_ICC(a,b,c) 0
131 # define __MPFR_ICC(a,b,c) (__ICC >= (a)*100+(b)*10+(c))
132 #elif defined(__INTEL_COMPILER)
133 # define __MPFR_ICC(a,b,c) (__INTEL_COMPILER >= (a)*100+(b)*10+(c))
135 # define __MPFR_ICC(a,b,c) 0
138 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && ! __MPFR_ICC(0,0,0)
139 # define __MPFR_GNUC(a,i) \
140 (MPFR_VERSION_NUM(__GNUC__,__GNUC_MINOR__,0) >= MPFR_VERSION_NUM(a,i,0))
142 # define __MPFR_GNUC(a,i) 0
145 #if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
146 # define __MPFR_GLIBC(a,i) \
147 (MPFR_VERSION_NUM(__GLIBC__,__GLIBC_MINOR__,0) >= MPFR_VERSION_NUM(a,i,0))
149 # define __MPFR_GLIBC(a,i) 0
152 #if defined(__GNU_MP_VERSION) && \
153 defined(__GNU_MP_VERSION_MINOR) && \
154 defined(__GNU_MP_VERSION_PATCHLEVEL)
155 # define __MPFR_GMP(a,b,c) \
156 (MPFR_VERSION_NUM(__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR,__GNU_MP_VERSION_PATCHLEVEL) >= MPFR_VERSION_NUM(a,b,c))
158 # define __MPFR_GMP(a,b,c) 0
163 /******************************************************
164 ************* GMP Basic Pointer Types ****************
165 ******************************************************/
167 typedef mp_limb_t
*mpfr_limb_ptr
;
168 typedef __gmp_const mp_limb_t
*mpfr_limb_srcptr
;
172 /******************************************************
173 ****************** (U)INTMAX_MAX *********************
174 ******************************************************/
176 /* Let's try to fix UINTMAX_MAX and INTMAX_MAX if these macros don't work
177 (e.g. with gcc -ansi -pedantic-errors in 32-bit mode under GNU/Linux),
178 see <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=582698>. */
179 #ifdef _MPFR_H_HAVE_INTMAX_T
180 # ifdef MPFR_HAVE_INTMAX_MAX
181 # define MPFR_UINTMAX_MAX UINTMAX_MAX
182 # define MPFR_INTMAX_MAX INTMAX_MAX
183 # define MPFR_INTMAX_MIN INTMAX_MIN
185 # define MPFR_UINTMAX_MAX ((uintmax_t) -1)
186 # define MPFR_INTMAX_MAX ((intmax_t) (MPFR_UINTMAX_MAX >> 1))
187 # define MPFR_INTMAX_MIN (INT_MIN + INT_MAX - MPFR_INTMAX_MAX)
191 #define MPFR_BYTES_PER_MP_LIMB (GMP_NUMB_BITS/CHAR_BIT)
193 /******************************************************
194 ******************** Check GMP ***********************
195 ******************************************************/
197 #if !__MPFR_GMP(4,1,0)
198 # error "GMP 4.1.0 or newer needed"
201 #if GMP_NAIL_BITS != 0
202 # error "MPFR doesn't support nonzero values of GMP_NAIL_BITS"
205 #if (GMP_NUMB_BITS<32) || (GMP_NUMB_BITS & (GMP_NUMB_BITS - 1))
206 # error "GMP_NUMB_BITS must be a power of 2, and >= 32"
209 #if GMP_NUMB_BITS == 16
210 # define MPFR_LOG2_GMP_NUMB_BITS 4
211 #elif GMP_NUMB_BITS == 32
212 # define MPFR_LOG2_GMP_NUMB_BITS 5
213 #elif GMP_NUMB_BITS == 64
214 # define MPFR_LOG2_GMP_NUMB_BITS 6
215 #elif GMP_NUMB_BITS == 128
216 # define MPFR_LOG2_GMP_NUMB_BITS 7
217 #elif GMP_NUMB_BITS == 256
218 # define MPFR_LOG2_GMP_NUMB_BITS 8
220 # error "Can't compute log2(GMP_NUMB_BITS)"
223 #if __MPFR_GNUC(3,0) || __MPFR_ICC(8,1,0)
224 /* For the future: N1478: Supporting the 'noreturn' property in C1x
225 http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1478.htm */
226 # define MPFR_NORETURN_ATTR __attribute__ ((noreturn))
227 # define MPFR_CONST_ATTR __attribute__ ((const))
229 # define MPFR_NORETURN_ATTR
230 # define MPFR_CONST_ATTR
233 /******************************************************
234 ************* Global Internal Variables **************
235 ******************************************************/
238 struct __gmpfr_cache_s
{
241 int (*func
)(mpfr_ptr
, mpfr_rnd_t
);
243 typedef struct __gmpfr_cache_s mpfr_cache_t
[1];
244 typedef struct __gmpfr_cache_s
*mpfr_cache_ptr
;
246 #if defined (__cplusplus)
250 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR
unsigned int __gmpfr_flags
;
251 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_exp_t __gmpfr_emin
;
252 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_exp_t __gmpfr_emax
;
253 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_prec_t __gmpfr_default_fp_bit_precision
;
254 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_rnd_t __gmpfr_default_rounding_mode
;
255 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_euler
;
256 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_catalan
;
258 #ifndef MPFR_USE_LOGGING
259 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_pi
;
260 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_log2
;
262 /* Two constants are used by the logging functions (via mpfr_fprintf,
263 then mpfr_log, for the base conversion): pi and log(2). Since the
264 mpfr_cache function isn't re-entrant when working on the same cache,
265 we need to define two caches for each constant. */
266 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_normal_pi
;
267 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_normal_log2
;
268 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_logging_pi
;
269 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_logging_log2
;
270 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_ptr __gmpfr_cache_const_pi
;
271 __MPFR_DECLSPEC
extern MPFR_THREAD_ATTR mpfr_cache_ptr __gmpfr_cache_const_log2
;
275 __MPFR_DECLSPEC
extern const __mpfr_struct __gmpfr_l2b
[BASE_MAX
-1][2];
277 /* Note: do not use the following values when they can be outside the
278 current exponent range, e.g. when the exponent range has not been
279 extended yet; under such a condition, they can be used only in
281 __MPFR_DECLSPEC
extern const mpfr_t __gmpfr_one
;
282 __MPFR_DECLSPEC
extern const mpfr_t __gmpfr_two
;
283 __MPFR_DECLSPEC
extern const mpfr_t __gmpfr_four
;
286 #if defined (__cplusplus)
290 /* Flags of __gmpfr_flags */
291 #define MPFR_FLAGS_UNDERFLOW 1
292 #define MPFR_FLAGS_OVERFLOW 2
293 #define MPFR_FLAGS_NAN 4
294 #define MPFR_FLAGS_INEXACT 8
295 #define MPFR_FLAGS_ERANGE 16
296 #define MPFR_FLAGS_DIVBY0 32
297 #define MPFR_FLAGS_ALL 63
299 /* Replace some common functions for direct access to the global vars */
300 #define mpfr_get_emin() (__gmpfr_emin + 0)
301 #define mpfr_get_emax() (__gmpfr_emax + 0)
302 #define mpfr_get_default_rounding_mode() (__gmpfr_default_rounding_mode + 0)
303 #define mpfr_get_default_prec() (__gmpfr_default_fp_bit_precision + 0)
305 #define mpfr_clear_flags() \
306 ((void) (__gmpfr_flags = 0))
307 #define mpfr_clear_underflow() \
308 ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_UNDERFLOW))
309 #define mpfr_clear_overflow() \
310 ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_OVERFLOW))
311 #define mpfr_clear_nanflag() \
312 ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_NAN))
313 #define mpfr_clear_inexflag() \
314 ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_INEXACT))
315 #define mpfr_clear_erangeflag() \
316 ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE))
317 #define mpfr_clear_divby0() \
318 ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_DIVBY0))
319 #define mpfr_underflow_p() \
320 ((int) (__gmpfr_flags & MPFR_FLAGS_UNDERFLOW))
321 #define mpfr_overflow_p() \
322 ((int) (__gmpfr_flags & MPFR_FLAGS_OVERFLOW))
323 #define mpfr_nanflag_p() \
324 ((int) (__gmpfr_flags & MPFR_FLAGS_NAN))
325 #define mpfr_inexflag_p() \
326 ((int) (__gmpfr_flags & MPFR_FLAGS_INEXACT))
327 #define mpfr_erangeflag_p() \
328 ((int) (__gmpfr_flags & MPFR_FLAGS_ERANGE))
329 #define mpfr_divby0_p() \
330 ((int) (__gmpfr_flags & MPFR_FLAGS_DIVBY0))
332 /* Testing an exception flag correctly is tricky. There are mainly two
333 pitfalls: First, one needs to remember to clear the corresponding
334 flag, in case it was set before the function call or during some
335 intermediate computations (in practice, one can clear all the flags).
336 Secondly, one needs to test the flag early enough, i.e. before it
337 can be modified by another function. Moreover, it is quite difficult
338 (if not impossible) to reliably check problems with "make check". To
339 avoid these pitfalls, it is recommended to use the following macros.
340 Other use of the exception-flag predicate functions/macros will be
341 detected by mpfrlint.
342 Note: _op can be either a statement or an expression.
343 MPFR_BLOCK_EXCEP should be used only inside a block; it is useful to
344 detect some exception in order to exit the block as soon as possible. */
345 #define MPFR_BLOCK_DECL(_flags) unsigned int _flags
346 /* The (void) (_flags) makes sure that _flags is read at least once (it
347 makes sense to use MPFR_BLOCK while _flags will never be read in the
348 source, so that we wish to avoid the corresponding warning). */
349 #define MPFR_BLOCK(_flags,_op) \
352 mpfr_clear_flags (); \
354 (_flags) = __gmpfr_flags; \
358 #define MPFR_BLOCK_TEST(_flags,_f) MPFR_UNLIKELY ((_flags) & (_f))
359 #define MPFR_BLOCK_EXCEP (__gmpfr_flags & (MPFR_FLAGS_UNDERFLOW | \
360 MPFR_FLAGS_OVERFLOW | \
361 MPFR_FLAGS_DIVBY0 | \
363 /* Let's use a MPFR_ prefix, because e.g. OVERFLOW is defined by glibc's
364 math.h, though this is not a reserved identifier! */
365 #define MPFR_UNDERFLOW(_flags) MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_UNDERFLOW)
366 #define MPFR_OVERFLOW(_flags) MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_OVERFLOW)
367 #define MPFR_NANFLAG(_flags) MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_NAN)
368 #define MPFR_INEXFLAG(_flags) MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_INEXACT)
369 #define MPFR_ERANGEFLAG(_flags) MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_ERANGE)
370 #define MPFR_DIVBY0(_flags) MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_DIVBY0)
373 /******************************************************
374 ******************** Assertions **********************
375 ******************************************************/
377 /* Compile with -DMPFR_WANT_ASSERT to check all assert statements */
379 /* Note: do not use GMP macros ASSERT_ALWAYS and ASSERT as they are not
380 expressions, and as a consequence, they cannot be used in a for(),
381 with a comma operator and so on. */
383 /* MPFR_ASSERTN(expr): assertions that should always be checked */
384 #define MPFR_ASSERTN(expr) \
385 ((void) ((MPFR_UNLIKELY(expr)) || MPFR_UNLIKELY( (ASSERT_FAIL(expr),0) )))
387 /* MPFR_ASSERTD(expr): assertions that should be checked when testing */
388 #ifdef MPFR_WANT_ASSERT
389 # define MPFR_EXP_CHECK 1
390 # define MPFR_ASSERTD(expr) MPFR_ASSERTN (expr)
392 # define MPFR_ASSERTD(expr) ((void) 0)
395 /* Code to deal with impossible
396 WARNING: It doesn't use do { } while (0) for Insure++*/
397 #define MPFR_RET_NEVER_GO_HERE() {MPFR_ASSERTN(0); return 0;}
400 /******************************************************
401 ******************** Warnings ************************
402 ******************************************************/
404 /* MPFR_WARNING is no longer useful, but let's keep the macro in case
405 it needs to be used again in the future. */
407 #ifdef MPFR_USE_WARNINGS
409 # define MPFR_WARNING(W) \
412 char *q = getenv ("MPFR_QUIET"); \
413 if (q == NULL || *q == 0) \
414 fprintf (stderr, "MPFR: %s\n", W); \
418 # define MPFR_WARNING(W) ((void) 0)
422 /******************************************************
423 ****************** double macros *********************
424 ******************************************************/
426 /* Precision used for lower precision computations */
427 #define MPFR_SMALL_PRECISION 32
429 /* Definition of constants */
430 #define LOG2 0.69314718055994528622 /* log(2) rounded to zero on 53 bits */
431 #define ALPHA 4.3191365662914471407 /* a+2 = a*log(a), rounded to +infinity */
432 #define EXPM1 0.36787944117144227851 /* exp(-1), rounded to zero */
434 /* MPFR_DOUBLE_SPEC = 1 if the C type 'double' corresponds to IEEE-754
435 double precision, 0 if it doesn't, and undefined if one doesn't know.
436 On all the tested machines, MPFR_DOUBLE_SPEC = 1. To have this macro
437 defined here, #include <float.h> is needed. If need be, other values
438 could be defined for other specs (once they are known). */
439 #if !defined(MPFR_DOUBLE_SPEC) && defined(FLT_RADIX) && \
440 defined(DBL_MANT_DIG) && defined(DBL_MIN_EXP) && defined(DBL_MAX_EXP)
441 # if FLT_RADIX == 2 && DBL_MANT_DIG == 53 && \
442 DBL_MIN_EXP == -1021 && DBL_MAX_EXP == 1024
443 # define MPFR_DOUBLE_SPEC 1
445 # define MPFR_DOUBLE_SPEC 0
449 /* Debug non IEEE floats */
451 # undef _GMP_IEEE_FLOATS
453 #ifndef _GMP_IEEE_FLOATS
454 # define _GMP_IEEE_FLOATS 0
457 #ifndef IEEE_DBL_MANT_DIG
458 #define IEEE_DBL_MANT_DIG 53
460 #define MPFR_LIMBS_PER_DOUBLE ((IEEE_DBL_MANT_DIG-1)/GMP_NUMB_BITS+1)
462 #ifndef IEEE_FLT_MANT_DIG
463 #define IEEE_FLT_MANT_DIG 24
465 #define MPFR_LIMBS_PER_FLT ((IEEE_FLT_MANT_DIG-1)/GMP_NUMB_BITS+1)
467 /* Visual C++ doesn't support +1.0/0.0, -1.0/0.0 and 0.0/0.0
469 Clang with -fsanitize=undefined is a bit similar due to a bug:
470 http://llvm.org/bugs/show_bug.cgi?id=17381
471 but even without its sanitizer, it may be better to use the
472 double_zero version until IEEE 754 division by zero is properly
474 http://llvm.org/bugs/show_bug.cgi?id=17000
476 #if (defined(_MSC_VER) && defined(_WIN32) && (_MSC_VER >= 1200)) || \
478 static double double_zero
= 0.0;
479 # define DBL_NAN (double_zero/double_zero)
480 # define DBL_POS_INF ((double) 1.0/double_zero)
481 # define DBL_NEG_INF ((double)-1.0/double_zero)
482 # define DBL_NEG_ZERO (-double_zero)
484 # define DBL_POS_INF ((double) 1.0/0.0)
485 # define DBL_NEG_INF ((double)-1.0/0.0)
486 # define DBL_NAN ((double) 0.0/0.0)
487 # define DBL_NEG_ZERO (-0.0)
490 /* Note: In the past, there was specific code for _GMP_IEEE_FLOATS, which
491 was based on NaN and Inf memory representations. This code was breaking
492 the aliasing rules (see ISO C99, 6.5#6 and 6.5#7 on the effective type)
493 and for this reason it did not behave correctly with GCC 4.5.0 20091119.
494 The code needed a memory transfer and was probably not better than the
495 macros below with a good compiler (a fix based on the NaN / Inf memory
496 representation would be even worse due to C limitations), and this code
497 could be selected only when MPFR was built with --with-gmp-build, thus
498 introducing a difference (bad for maintaining/testing MPFR); therefore
499 it has been removed. The old code required that the argument x be an
500 lvalue of type double. We still require that, in case one would need
501 to change the macros below, e.g. for some broken compiler. But the
502 LVALUE(x) condition could be removed if really necessary. */
503 /* Below, the &(x) == &(x) || &(x) != &(x) allows to make sure that x
504 is a lvalue without (probably) any warning from the compiler. The
505 &(x) != &(x) is needed to avoid a failure under Mac OS X 10.4.11
506 (with Xcode 2.4.1, i.e. the latest one). */
507 #define LVALUE(x) (&(x) == &(x) || &(x) != &(x))
508 #define DOUBLE_ISINF(x) (LVALUE(x) && ((x) > DBL_MAX || (x) < -DBL_MAX))
509 /* The DOUBLE_ISNAN(x) macro is also valid on long double x
510 (assuming that the compiler isn't too broken). */
512 /* Avoid MIPSpro / IRIX64 / gcc -ffast-math (incorrect) optimizations.
513 The + must not be replaced by a ||. With gcc -ffast-math, NaN is
514 regarded as a positive number or something like that; the second
515 test catches this case. */
516 # define DOUBLE_ISNAN(x) \
517 (LVALUE(x) && !((((x) >= 0.0) + ((x) <= 0.0)) && -(x)*(x) <= 0.0))
519 # define DOUBLE_ISNAN(x) (LVALUE(x) && (x) != (x))
522 /******************************************************
523 *************** Long double macros *******************
524 ******************************************************/
526 /* We try to get the exact value of the precision of long double
527 (provided by the implementation) in order to provide correct
528 rounding in this case (not guaranteed if the C implementation
529 does not have an adequate long double arithmetic). Note that
530 it may be lower than the precision of some numbers that can
531 be represented in a long double; e.g. on FreeBSD/x86, it is
532 53 because the processor is configured to round in double
533 precision (even when using the long double type -- this is a
534 limitation of the x87 arithmetic), and on Mac OS X, it is 106
535 because the implementation is a double-double arithmetic.
536 Otherwise (e.g. in base 10), we get an upper bound of the
537 precision, and correct rounding isn't currently provided.
539 #if defined(LDBL_MANT_DIG) && FLT_RADIX == 2
540 # define MPFR_LDBL_MANT_DIG LDBL_MANT_DIG
542 # define MPFR_LDBL_MANT_DIG \
543 (sizeof(long double)*GMP_NUMB_BITS/sizeof(mp_limb_t))
545 #define MPFR_LIMBS_PER_LONG_DOUBLE \
546 ((sizeof(long double)-1)/sizeof(mp_limb_t)+1)
548 /* LONGDOUBLE_NAN_ACTION executes the code "action" if x is a NaN. */
550 /* On hppa2.0n-hp-hpux10 with the unbundled HP cc, the test x!=x on a NaN
551 has been seen false, meaning NaNs are not detected. This seemed to
552 happen only after other comparisons, not sure what's really going on. In
553 any case we can pick apart the bytes to identify a NaN. */
554 #ifdef HAVE_LDOUBLE_IEEE_QUAD_BIG
555 # define LONGDOUBLE_NAN_ACTION(x, action) \
560 unsigned int sign : 1; \
561 unsigned int exp : 15; \
562 unsigned int man3 : 16; \
563 unsigned int man2 : 32; \
564 unsigned int man1 : 32; \
565 unsigned int man0 : 32; \
569 if (u.s.exp == 0x7FFFL \
570 && (u.s.man0 | u.s.man1 | u.s.man2 | u.s.man3) != 0) \
575 #ifdef HAVE_LDOUBLE_IEEE_QUAD_LITTLE
576 # define LONGDOUBLE_NAN_ACTION(x, action) \
581 unsigned int man0 : 32; \
582 unsigned int man1 : 32; \
583 unsigned int man2 : 32; \
584 unsigned int man3 : 16; \
585 unsigned int exp : 15; \
586 unsigned int sign : 1; \
590 if (u.s.exp == 0x7FFFL \
591 && (u.s.man0 | u.s.man1 | u.s.man2 | u.s.man3) != 0) \
596 /* Under IEEE rules, NaN is not equal to anything, including itself.
597 "volatile" here stops "cc" on mips64-sgi-irix6.5 from optimizing away
599 #ifndef LONGDOUBLE_NAN_ACTION
600 # define LONGDOUBLE_NAN_ACTION(x, action) \
602 volatile long double __x = LONGDOUBLE_VOLATILE (x); \
606 # define WANT_LONGDOUBLE_VOLATILE 1
609 /* If we don't have a proper "volatile" then volatile is #defined to empty,
610 in this case call through an external function to stop the compiler
611 optimizing anything. */
612 #ifdef WANT_LONGDOUBLE_VOLATILE
614 __MPFR_DECLSPEC
long double __gmpfr_longdouble_volatile
_MPFR_PROTO ((long double)) MPFR_CONST_ATTR
;
615 # define LONGDOUBLE_VOLATILE(x) (__gmpfr_longdouble_volatile (x))
616 # define WANT_GMPFR_LONGDOUBLE_VOLATILE 1
618 # define LONGDOUBLE_VOLATILE(x) (x)
622 /* Some special case for IEEE_EXT Litle Endian */
623 #if HAVE_LDOUBLE_IEEE_EXT_LITTLE
628 unsigned int manl
: 32;
629 unsigned int manh
: 32;
630 unsigned int expl
: 8 ;
631 unsigned int exph
: 7;
632 unsigned int sign
: 1;
634 } mpfr_long_double_t
;
636 /* #undef MPFR_LDBL_MANT_DIG */
637 #undef MPFR_LIMBS_PER_LONG_DOUBLE
638 /* #define MPFR_LDBL_MANT_DIG 64 */
639 #define MPFR_LIMBS_PER_LONG_DOUBLE ((64-1)/GMP_NUMB_BITS+1)
643 /******************************************************
644 *************** _Decimal64 support *******************
645 ******************************************************/
647 #ifdef MPFR_WANT_DECIMAL_FLOATS
648 /* to cast between binary64 and decimal64 */
649 union ieee_double_decimal64
{ double d
; _Decimal64 d64
; };
652 /******************************************************
653 **************** mpfr_t properties *******************
654 ******************************************************/
656 /* In the following macro, p is usually a mpfr_prec_t, but this macro
657 works with other integer types (without integer overflow). Checking
658 that p >= 1 in debug mode is useful here because this macro can be
659 used on a computed precision (in particular, this formula does not
660 work for a degenerate case p = 0, and could give different results
661 on different platforms). But let us not use an assertion checking
662 in the MPFR_LAST_LIMB() and MPFR_LIMB_SIZE() macros below to avoid
663 too much expansion for assertions (in practice, this should be a
664 problem just when testing MPFR with the --enable-assert configure
665 option and the -ansi -pedantic-errors gcc compiler flags). */
666 #define MPFR_PREC2LIMBS(p) \
667 (MPFR_ASSERTD ((p) >= 1), ((p) - 1) / GMP_NUMB_BITS + 1)
669 #define MPFR_PREC(x) ((x)->_mpfr_prec)
670 #define MPFR_EXP(x) ((x)->_mpfr_exp)
671 #define MPFR_MANT(x) ((x)->_mpfr_d)
672 #define MPFR_LAST_LIMB(x) ((MPFR_PREC (x) - 1) / GMP_NUMB_BITS)
673 #define MPFR_LIMB_SIZE(x) (MPFR_LAST_LIMB (x) + 1)
676 /******************************************************
677 **************** exponent properties *****************
678 ******************************************************/
680 /* Limits of the mpfr_exp_t type (NOT those of valid exponent values).
681 These macros can be used in preprocessor directives. */
682 #if _MPFR_EXP_FORMAT == 1
683 # define MPFR_EXP_MAX (SHRT_MAX)
684 # define MPFR_EXP_MIN (SHRT_MIN)
685 #elif _MPFR_EXP_FORMAT == 2
686 # define MPFR_EXP_MAX (INT_MAX)
687 # define MPFR_EXP_MIN (INT_MIN)
688 #elif _MPFR_EXP_FORMAT == 3
689 # define MPFR_EXP_MAX (LONG_MAX)
690 # define MPFR_EXP_MIN (LONG_MIN)
691 #elif _MPFR_EXP_FORMAT == 4
692 # define MPFR_EXP_MAX (MPFR_INTMAX_MAX)
693 # define MPFR_EXP_MIN (MPFR_INTMAX_MIN)
695 # error "Invalid MPFR Exp format"
698 /* Before doing a cast to mpfr_uexp_t, make sure that the value is
700 #define MPFR_UEXP(X) (MPFR_ASSERTD ((X) >= 0), (mpfr_uexp_t) (X))
702 #if MPFR_EXP_MIN >= LONG_MIN && MPFR_EXP_MAX <= LONG_MAX
703 typedef long int mpfr_eexp_t
;
704 # define mpfr_get_exp_t(x,r) mpfr_get_si((x),(r))
705 # define mpfr_set_exp_t(x,e,r) mpfr_set_si((x),(e),(r))
706 # define MPFR_EXP_FSPEC "l"
707 #elif defined (_MPFR_H_HAVE_INTMAX_T)
708 typedef intmax_t mpfr_eexp_t
;
709 # define mpfr_get_exp_t(x,r) mpfr_get_sj((x),(r))
710 # define mpfr_set_exp_t(x,e,r) mpfr_set_sj((x),(e),(r))
711 # define MPFR_EXP_FSPEC "j"
713 # error "Cannot define mpfr_get_exp_t and mpfr_set_exp_t"
716 /* Invalid exponent value (to track bugs...) */
717 #define MPFR_EXP_INVALID \
718 ((mpfr_exp_t) 1 << (GMP_NUMB_BITS*sizeof(mpfr_exp_t)/sizeof(mp_limb_t)-2))
720 /* Definition of the exponent limits for MPFR numbers.
721 * These limits are chosen so that if e is such an exponent, then 2e-1 and
722 * 2e+1 are representable. This is useful for intermediate computations,
723 * in particular the multiplication.
729 #define MPFR_EMIN_MIN (1-MPFR_EXP_INVALID)
730 #define MPFR_EMIN_MAX (MPFR_EXP_INVALID-1)
731 #define MPFR_EMAX_MIN (1-MPFR_EXP_INVALID)
732 #define MPFR_EMAX_MAX (MPFR_EXP_INVALID-1)
734 /* Use MPFR_GET_EXP and MPFR_SET_EXP instead of MPFR_EXP directly,
735 unless when the exponent may be out-of-range, for instance when
736 setting the exponent before calling mpfr_check_range.
737 MPFR_EXP_CHECK is defined when MPFR_WANT_ASSERT is defined, but if you
738 don't use MPFR_WANT_ASSERT (for speed reasons), you can still define
739 MPFR_EXP_CHECK by setting -DMPFR_EXP_CHECK in $CFLAGS. */
741 #ifdef MPFR_EXP_CHECK
742 # define MPFR_GET_EXP(x) (mpfr_get_exp) (x)
743 # define MPFR_SET_EXP(x, exp) MPFR_ASSERTN (!mpfr_set_exp ((x), (exp)))
744 # define MPFR_SET_INVALID_EXP(x) ((void) (MPFR_EXP (x) = MPFR_EXP_INVALID))
746 # define MPFR_GET_EXP(x) MPFR_EXP (x)
747 # define MPFR_SET_EXP(x, exp) ((void) (MPFR_EXP (x) = (exp)))
748 # define MPFR_SET_INVALID_EXP(x) ((void) 0)
753 /******************************************************
754 ********** Singular Values (NAN, INF, ZERO) **********
755 ******************************************************/
757 /* Enum special value of exponent. */
758 # define MPFR_EXP_ZERO (MPFR_EXP_MIN+1)
759 # define MPFR_EXP_NAN (MPFR_EXP_MIN+2)
760 # define MPFR_EXP_INF (MPFR_EXP_MIN+3)
762 #define MPFR_IS_NAN(x) (MPFR_EXP(x) == MPFR_EXP_NAN)
763 #define MPFR_SET_NAN(x) (MPFR_EXP(x) = MPFR_EXP_NAN)
764 #define MPFR_IS_INF(x) (MPFR_EXP(x) == MPFR_EXP_INF)
765 #define MPFR_SET_INF(x) (MPFR_EXP(x) = MPFR_EXP_INF)
766 #define MPFR_IS_ZERO(x) (MPFR_EXP(x) == MPFR_EXP_ZERO)
767 #define MPFR_SET_ZERO(x) (MPFR_EXP(x) = MPFR_EXP_ZERO)
768 #define MPFR_NOTZERO(x) (MPFR_EXP(x) != MPFR_EXP_ZERO)
770 #define MPFR_IS_FP(x) (!MPFR_IS_NAN(x) && !MPFR_IS_INF(x))
771 #define MPFR_IS_SINGULAR(x) (MPFR_EXP(x) <= MPFR_EXP_INF)
772 #define MPFR_IS_PURE_FP(x) (!MPFR_IS_SINGULAR(x) && \
773 (MPFR_ASSERTD ((MPFR_MANT(x)[MPFR_LAST_LIMB(x)] \
774 & MPFR_LIMB_HIGHBIT) != 0), 1))
776 #define MPFR_ARE_SINGULAR(x,y) \
777 (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)) || MPFR_UNLIKELY(MPFR_IS_SINGULAR(y)))
779 #define MPFR_IS_POWER_OF_2(x) \
780 (mpfr_cmp_ui_2exp ((x), 1, MPFR_GET_EXP (x) - 1) == 0)
783 /******************************************************
784 ********************* Sign Macros ********************
785 ******************************************************/
787 #define MPFR_SIGN_POS (1)
788 #define MPFR_SIGN_NEG (-1)
790 #define MPFR_IS_STRICTPOS(x) (MPFR_NOTZERO((x)) && MPFR_SIGN(x) > 0)
791 #define MPFR_IS_STRICTNEG(x) (MPFR_NOTZERO((x)) && MPFR_SIGN(x) < 0)
793 #define MPFR_IS_NEG(x) (MPFR_SIGN(x) < 0)
794 #define MPFR_IS_POS(x) (MPFR_SIGN(x) > 0)
796 #define MPFR_SET_POS(x) (MPFR_SIGN(x) = MPFR_SIGN_POS)
797 #define MPFR_SET_NEG(x) (MPFR_SIGN(x) = MPFR_SIGN_NEG)
799 #define MPFR_CHANGE_SIGN(x) (MPFR_SIGN(x) = -MPFR_SIGN(x))
800 #define MPFR_SET_SAME_SIGN(x, y) (MPFR_SIGN(x) = MPFR_SIGN(y))
801 #define MPFR_SET_OPPOSITE_SIGN(x, y) (MPFR_SIGN(x) = -MPFR_SIGN(y))
802 #define MPFR_ASSERT_SIGN(s) \
803 (MPFR_ASSERTD((s) == MPFR_SIGN_POS || (s) == MPFR_SIGN_NEG))
804 #define MPFR_SET_SIGN(x, s) \
805 (MPFR_ASSERT_SIGN(s), MPFR_SIGN(x) = s)
806 #define MPFR_IS_POS_SIGN(s1) (s1 > 0)
807 #define MPFR_IS_NEG_SIGN(s1) (s1 < 0)
808 #define MPFR_MULT_SIGN(s1, s2) ((s1) * (s2))
809 /* Transform a sign to 1 or -1 */
810 #define MPFR_FROM_SIGN_TO_INT(s) (s)
811 #define MPFR_INT_SIGN(x) MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(x))
815 /******************************************************
816 ***************** Ternary Value Macros ***************
817 ******************************************************/
819 /* Special inexact value */
820 #define MPFR_EVEN_INEX 2
822 /* Macros for functions returning two inexact values in an 'int' */
823 #define INEXPOS(y) ((y) == 0 ? 0 : (((y) > 0) ? 1 : 2))
824 #define INEX(y,z) (INEXPOS(y) | (INEXPOS(z) << 2))
826 /* When returning the ternary inexact value, ALWAYS use one of the
827 following two macros, unless the flag comes from another function
828 returning the ternary inexact value */
829 #define MPFR_RET(I) return \
830 (I) ? ((__gmpfr_flags |= MPFR_FLAGS_INEXACT), (I)) : 0
831 #define MPFR_RET_NAN return (__gmpfr_flags |= MPFR_FLAGS_NAN), 0
833 #define MPFR_SET_ERANGE() (__gmpfr_flags |= MPFR_FLAGS_ERANGE)
835 #define SIGN(I) ((I) < 0 ? -1 : (I) > 0)
836 #define SAME_SIGN(I1,I2) (SIGN (I1) == SIGN (I2))
840 /******************************************************
841 ************** Rounding mode macros *****************
842 ******************************************************/
844 /* MPFR_RND_MAX gives the number of supported rounding modes by all functions.
845 * Once faithful rounding is implemented, MPFR_RNDA should be changed
846 * to MPFR_RNDF. But this will also require more changes in the tests.
848 #define MPFR_RND_MAX ((mpfr_rnd_t)((MPFR_RNDA)+1))
850 /* We want to test this :
851 * (rnd == MPFR_RNDU && test) || (rnd == RNDD && !test)
852 * ie it transforms RNDU or RNDD to Away or Zero according to the sign */
853 #define MPFR_IS_RNDUTEST_OR_RNDDNOTTEST(rnd, test) \
854 (((rnd) + (test)) == MPFR_RNDD)
856 /* We want to test if rnd = Zero, or Away.
857 'test' is 1 if negative, and 0 if positive. */
858 #define MPFR_IS_LIKE_RNDZ(rnd, test) \
859 ((rnd==MPFR_RNDZ) || MPFR_IS_RNDUTEST_OR_RNDDNOTTEST (rnd, test))
861 #define MPFR_IS_LIKE_RNDU(rnd, sign) \
862 ((rnd==MPFR_RNDU) || (rnd==MPFR_RNDZ && sign<0) || (rnd==MPFR_RNDA && sign>0))
864 #define MPFR_IS_LIKE_RNDD(rnd, sign) \
865 ((rnd==MPFR_RNDD) || (rnd==MPFR_RNDZ && sign>0) || (rnd==MPFR_RNDA && sign<0))
867 /* Invert a rounding mode, RNDZ and RNDA are unchanged */
868 #define MPFR_INVERT_RND(rnd) ((rnd == MPFR_RNDU) ? MPFR_RNDD : \
869 ((rnd == MPFR_RNDD) ? MPFR_RNDU : rnd))
871 /* Transform RNDU and RNDD to RNDZ according to test */
872 #define MPFR_UPDATE_RND_MODE(rnd, test) \
874 if (MPFR_UNLIKELY(MPFR_IS_RNDUTEST_OR_RNDDNOTTEST(rnd, test))) \
878 /* Transform RNDU and RNDD to RNDZ or RNDA according to sign,
879 leave the other modes unchanged */
880 #define MPFR_UPDATE2_RND_MODE(rnd, sign) \
882 if (rnd == MPFR_RNDU) \
883 rnd = (sign > 0) ? MPFR_RNDA : MPFR_RNDZ; \
884 else if (rnd == MPFR_RNDD) \
885 rnd = (sign < 0) ? MPFR_RNDA : MPFR_RNDZ; \
889 /******************************************************
890 ******************* Limb Macros **********************
891 ******************************************************/
893 /* Definition of MPFR_LIMB_HIGHBIT */
894 #if defined(GMP_LIMB_HIGHBIT)
895 # define MPFR_LIMB_HIGHBIT GMP_LIMB_HIGHBIT
896 #elif defined(MP_LIMB_T_HIGHBIT)
897 # define MPFR_LIMB_HIGHBIT MP_LIMB_T_HIGHBIT
899 # error "Neither GMP_LIMB_HIGHBIT nor MP_LIMB_T_HIGHBIT defined in GMP"
902 /* Mask to get the Most Significant Bit of a limb */
903 #define MPFR_LIMB_MSB(l) ((l)&MPFR_LIMB_HIGHBIT)
905 /* Definition of MPFR_LIMB_ONE & MPFR_LIMB_ZERO */
907 # define MPFR_LIMB_ONE CNST_LIMB(1)
908 # define MPFR_LIMB_ZERO CNST_LIMB(0)
910 # define MPFR_LIMB_ONE ((mp_limb_t) 1L)
911 # define MPFR_LIMB_ZERO ((mp_limb_t) 0L)
914 /* Mask for the low 's' bits of a limb */
915 #define MPFR_LIMB_MASK(s) ((MPFR_LIMB_ONE<<(s))-MPFR_LIMB_ONE)
919 /******************************************************
920 ********************** Memory ************************
921 ******************************************************/
923 /* Heap Memory gestion */
924 typedef union { mp_size_t s
; mp_limb_t l
; } mpfr_size_limb_t
;
925 #define MPFR_GET_ALLOC_SIZE(x) \
926 ( ((mp_size_t*) MPFR_MANT(x))[-1] + 0)
927 #define MPFR_SET_ALLOC_SIZE(x, n) \
928 ( ((mp_size_t*) MPFR_MANT(x))[-1] = n)
929 #define MPFR_MALLOC_SIZE(s) \
930 ( sizeof(mpfr_size_limb_t) + MPFR_BYTES_PER_MP_LIMB * ((size_t) s) )
931 #define MPFR_SET_MANT_PTR(x,p) \
932 (MPFR_MANT(x) = (mp_limb_t*) ((mpfr_size_limb_t*) p + 1))
933 #define MPFR_GET_REAL_PTR(x) \
934 ((mp_limb_t*) ((mpfr_size_limb_t*) MPFR_MANT(x) - 1))
936 /* Temporary memory gestion */
938 /* GMP 4.1.x or below or internals */
939 #define MPFR_TMP_DECL TMP_DECL
940 #define MPFR_TMP_MARK TMP_MARK
941 #define MPFR_TMP_ALLOC TMP_ALLOC
942 #define MPFR_TMP_FREE TMP_FREE
944 #define MPFR_TMP_DECL(x) TMP_DECL
945 #define MPFR_TMP_MARK(x) TMP_MARK
946 #define MPFR_TMP_ALLOC(s) TMP_ALLOC(s)
947 #define MPFR_TMP_FREE(x) TMP_FREE
950 /* This code is experimental: don't use it */
951 #ifdef MPFR_USE_OWN_MPFR_TMP_ALLOC
952 extern unsigned char *mpfr_stack
;
955 #undef MPFR_TMP_ALLOC
957 #define MPFR_TMP_DECL(_x) unsigned char *(_x)
958 #define MPFR_TMP_MARK(_x) ((_x) = mpfr_stack)
959 #define MPFR_TMP_ALLOC(_s) (mpfr_stack += (_s), mpfr_stack - (_s))
960 #define MPFR_TMP_FREE(_x) (mpfr_stack = (_x))
963 #define MPFR_TMP_LIMBS_ALLOC(N) \
964 ((mp_limb_t *) MPFR_TMP_ALLOC ((size_t) (N) * MPFR_BYTES_PER_MP_LIMB))
966 /* temporary allocate 1 limb at xp, and initialize mpfr variable x */
967 /* The temporary var doesn't have any size field, but it doesn't matter
968 * since only functions dealing with the Heap care about it */
969 #define MPFR_TMP_INIT1(xp, x, p) \
970 ( MPFR_PREC(x) = (p), \
971 MPFR_MANT(x) = (xp), \
973 MPFR_SET_INVALID_EXP(x))
975 #define MPFR_TMP_INIT(xp, x, p, s) \
976 (xp = MPFR_TMP_LIMBS_ALLOC(s), \
977 MPFR_TMP_INIT1(xp, x, p))
979 #define MPFR_TMP_INIT_ABS(d, s) \
980 ( MPFR_PREC(d) = MPFR_PREC(s), \
981 MPFR_MANT(d) = MPFR_MANT(s), \
983 MPFR_EXP(d) = MPFR_EXP(s))
987 /******************************************************
988 ***************** Cache macros **********************
989 ******************************************************/
991 #define mpfr_const_pi(_d,_r) mpfr_cache(_d, __gmpfr_cache_const_pi,_r)
992 #define mpfr_const_log2(_d,_r) mpfr_cache(_d, __gmpfr_cache_const_log2, _r)
993 #define mpfr_const_euler(_d,_r) mpfr_cache(_d, __gmpfr_cache_const_euler, _r)
994 #define mpfr_const_catalan(_d,_r) mpfr_cache(_d,__gmpfr_cache_const_catalan,_r)
996 #define MPFR_DECL_INIT_CACHE(_cache,_func) \
997 mpfr_cache_t MPFR_THREAD_ATTR _cache = \
998 {{{{0,MPFR_SIGN_POS,0,(mp_limb_t*)0}},0,_func}}
1002 /******************************************************
1003 ******************* Threshold ***********************
1004 ******************************************************/
1008 /******************************************************
1009 ***************** Useful macros *********************
1010 ******************************************************/
1012 /* Theses macros help the compiler to determine if a test is
1013 likely or unlikely. The !! is necessary in case x is larger
1015 #if __MPFR_GNUC(3,0) || __MPFR_ICC(8,1,0)
1016 # define MPFR_LIKELY(x) (__builtin_expect(!!(x),1))
1017 # define MPFR_UNLIKELY(x) (__builtin_expect(!!(x),0))
1019 # define MPFR_LIKELY(x) (x)
1020 # define MPFR_UNLIKELY(x) (x)
1023 /* Declare that some variable is initialized before being used (without a
1024 dummy initialization) in order to avoid some compiler warnings. Use the
1025 VAR = VAR trick (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36296)
1026 only with gcc as this is undefined behavior, and we don't know what
1027 other compilers do (they may also be smarter). This trick could be
1028 disabled with future gcc versions. */
1029 #if defined(__GNUC__)
1030 # define INITIALIZED(VAR) VAR = VAR
1032 # define INITIALIZED(VAR) VAR
1035 /* Ceil log 2: If GCC, uses a GCC extension, otherwise calls a function */
1037 * Needs to define MPFR_NEED_LONGLONG.
1038 * Computes ceil(log2(x)) only for x integer (unsigned long)
1039 * Undefined if x is 0 */
1040 #if __MPFR_GNUC(2,95) || __MPFR_ICC(8,1,0)
1041 # define MPFR_INT_CEIL_LOG2(x) \
1042 (MPFR_UNLIKELY ((x) == 1) ? 0 : \
1043 __extension__ ({ int _b; mp_limb_t _limb; \
1044 MPFR_ASSERTN ((x) > 1); \
1046 MPFR_ASSERTN (_limb == (x) - 1); \
1047 count_leading_zeros (_b, _limb); \
1048 (GMP_NUMB_BITS - _b); }))
1050 # define MPFR_INT_CEIL_LOG2(x) (__gmpfr_int_ceil_log2(x))
1053 /* Add two integers with overflow handling */
1054 /* Example: MPFR_SADD_OVERFLOW (c, a, b, long, unsigned long,
1055 * LONG_MIN, LONG_MAX,
1056 * goto overflow, goto underflow); */
1057 #define MPFR_UADD_OVERFLOW(c,a,b,ACTION_IF_OVERFLOW) \
1060 if ((c) < (a)) ACTION_IF_OVERFLOW; \
1063 #define MPFR_SADD_OVERFLOW(c,a,b,STYPE,UTYPE,MIN,MAX,ACTION_IF_POS_OVERFLOW,ACTION_IF_NEG_OVERFLOW) \
1065 if ((a) >= 0 && (b) >= 0) { \
1067 ua = (UTYPE) (a); ub = (UTYPE) (b); \
1068 MPFR_UADD_OVERFLOW (uc, ua, ub, ACTION_IF_POS_OVERFLOW); \
1069 if (uc > (UTYPE)(MAX)) ACTION_IF_POS_OVERFLOW; \
1070 else (c) = (STYPE) uc; \
1071 } else if ((a) < 0 && (b) < 0) { \
1073 ua = -(UTYPE) (a); ub = -(UTYPE) (b); \
1074 MPFR_UADD_OVERFLOW (uc, ua, ub, ACTION_IF_NEG_OVERFLOW); \
1075 if (uc >= -(UTYPE)(MIN) || uc > (UTYPE)(MAX)) { \
1076 if (uc == -(UTYPE)(MIN)) (c) = (MIN); \
1077 else ACTION_IF_NEG_OVERFLOW; } \
1078 else (c) = -(STYPE) uc; \
1079 } else (c) = (a) + (b); \
1083 /* Set a number to 1 (Fast) - It doesn't check if 1 is in the exponent range */
1084 #define MPFR_SET_ONE(x) \
1086 mp_size_t _size = MPFR_LAST_LIMB(x); \
1089 MPN_ZERO ( MPFR_MANT(x), _size); \
1090 MPFR_MANT(x)[_size] = MPFR_LIMB_HIGHBIT; \
1093 /* Compute s = (-a) % GMP_NUMB_BITS as unsigned */
1094 #define MPFR_UNSIGNED_MINUS_MODULO(s, a) \
1097 if (IS_POW2 (GMP_NUMB_BITS)) \
1098 (s) = (- (unsigned int) (a)) % GMP_NUMB_BITS; \
1101 (s) = (a) % GMP_NUMB_BITS; \
1103 (s) = GMP_NUMB_BITS - (s); \
1105 MPFR_ASSERTD ((s) >= 0 && (s) < GMP_NUMB_BITS); \
1109 /* Use it only for debug reasons */
1110 /* MPFR_TRACE (operation) : execute operation iff DEBUG flag is set */
1111 /* MPFR_DUMP (x) : print x (a mpfr_t) on stdout */
1113 # define MPFR_TRACE(x) x
1115 # define MPFR_TRACE(x) (void) 0
1117 #define MPFR_DUMP(x) ( printf(#x"="), mpfr_dump(x) )
1119 /* Test if X (positive) is a power of 2 */
1120 #define IS_POW2(X) (((X) & ((X) - 1)) == 0)
1121 #define NOT_POW2(X) (((X) & ((X) - 1)) != 0)
1123 /* Safe absolute value (to avoid possible integer overflow) */
1124 /* type is the target (unsigned) type */
1125 #define SAFE_ABS(type,x) ((x) >= 0 ? (type)(x) : -(type)(x))
1127 #define mpfr_get_d1(x) mpfr_get_d(x,__gmpfr_default_rounding_mode)
1129 /* Store in r the size in bits of the mpz_t z */
1130 #define MPFR_MPZ_SIZEINBASE2(r, z) \
1134 MPFR_ASSERTD (mpz_sgn (z) != 0); \
1136 count_leading_zeros (_cnt, PTR(z)[_size-1]); \
1137 (r) = _size * GMP_NUMB_BITS - _cnt; \
1140 /* MPFR_LCONV_DPTS can also be forced to 0 or 1 by the user. */
1141 #ifndef MPFR_LCONV_DPTS
1142 # if defined(HAVE_LOCALE_H) && \
1143 defined(HAVE_STRUCT_LCONV_DECIMAL_POINT) && \
1144 defined(HAVE_STRUCT_LCONV_THOUSANDS_SEP)
1145 # define MPFR_LCONV_DPTS 1
1147 # define MPFR_LCONV_DPTS 0
1153 /* Warning! In case of signed char, the value of MPFR_DECIMAL_POINT may
1154 be negative (the ISO C99 does not seem to forbid negative values). */
1155 #define MPFR_DECIMAL_POINT (localeconv()->decimal_point[0])
1156 #define MPFR_THOUSANDS_SEPARATOR (localeconv()->thousands_sep[0])
1158 #define MPFR_DECIMAL_POINT ((char) '.')
1159 #define MPFR_THOUSANDS_SEPARATOR ('\0')
1163 /* Set y to s*significand(x)*2^e, for example MPFR_ALIAS(y,x,1,MPFR_EXP(x))
1164 sets y to |x|, and MPFR_ALIAS(y,x,MPFR_SIGN(x),0) sets y to x*2^f such
1165 that 1/2 <= |y| < 1. Does not check y is in the valid exponent range.
1166 WARNING! x and y share the same mantissa. So, some operations are
1167 not valid if x has been provided via an argument, e.g., trying to
1168 modify the mantissa of y, even temporarily, or calling mpfr_clear on y.
1170 #define MPFR_ALIAS(y,x,s,e) \
1173 MPFR_PREC(y) = MPFR_PREC(x); \
1174 MPFR_SIGN(y) = (s); \
1175 MPFR_EXP(y) = (e); \
1176 MPFR_MANT(y) = MPFR_MANT(x); \
1180 /******************************************************
1181 ************** Save exponent macros ****************
1182 ******************************************************/
1184 /* See README.dev for details on how to use the macros.
1185 They are used to set the exponent range to the maximum
1189 unsigned int saved_flags
;
1190 mpfr_exp_t saved_emin
;
1191 mpfr_exp_t saved_emax
;
1194 /* Minimum and maximum exponents of the extended exponent range. */
1195 #define MPFR_EXT_EMIN MPFR_EMIN_MIN
1196 #define MPFR_EXT_EMAX MPFR_EMAX_MAX
1198 #define MPFR_SAVE_EXPO_DECL(x) mpfr_save_expo_t x
1199 #define MPFR_SAVE_EXPO_MARK(x) \
1200 ((x).saved_flags = __gmpfr_flags, \
1201 (x).saved_emin = __gmpfr_emin, \
1202 (x).saved_emax = __gmpfr_emax, \
1203 __gmpfr_emin = MPFR_EXT_EMIN, \
1204 __gmpfr_emax = MPFR_EXT_EMAX)
1205 #define MPFR_SAVE_EXPO_FREE(x) \
1206 (__gmpfr_flags = (x).saved_flags, \
1207 __gmpfr_emin = (x).saved_emin, \
1208 __gmpfr_emax = (x).saved_emax)
1209 #define MPFR_SAVE_EXPO_UPDATE_FLAGS(x, flags) \
1210 (x).saved_flags |= (flags)
1212 /* Speed up final checking */
1213 #define mpfr_check_range(x,t,r) \
1214 (MPFR_LIKELY (MPFR_EXP (x) >= __gmpfr_emin && MPFR_EXP (x) <= __gmpfr_emax) \
1215 ? ((t) ? (__gmpfr_flags |= MPFR_FLAGS_INEXACT, (t)) : 0) \
1216 : mpfr_check_range(x,t,r))
1219 /******************************************************
1220 ***************** Inline Rounding *******************
1221 ******************************************************/
1224 * Note: due to the labels, one cannot use a macro MPFR_RNDRAW* more than
1225 * once in a function (otherwise these labels would not be unique).
1229 * Round mantissa (srcp, sprec) to mpfr_t dest using rounding mode rnd
1230 * assuming dest's sign is sign.
1231 * In rounding to nearest mode, execute MIDDLE_HANDLER when the value
1232 * is the middle of two consecutive numbers in dest precision.
1233 * Execute OVERFLOW_HANDLER in case of overflow when rounding.
1235 #define MPFR_RNDRAW_GEN(inexact, dest, srcp, sprec, rnd, sign, \
1236 MIDDLE_HANDLER, OVERFLOW_HANDLER) \
1238 mp_size_t _dests, _srcs; \
1239 mp_limb_t *_destp; \
1240 mpfr_prec_t _destprec, _srcprec; \
1242 /* Check Trivial Case when Dest Mantissa has more bits than source */ \
1243 _srcprec = (sprec); \
1244 _destprec = MPFR_PREC (dest); \
1245 _destp = MPFR_MANT (dest); \
1246 if (MPFR_UNLIKELY (_destprec >= _srcprec)) \
1248 _srcs = MPFR_PREC2LIMBS (_srcprec); \
1249 _dests = MPFR_PREC2LIMBS (_destprec) - _srcs; \
1250 MPN_COPY (_destp + _dests, srcp, _srcs); \
1251 MPN_ZERO (_destp, _dests); \
1256 /* Non trivial case: rounding needed */ \
1259 mp_limb_t _rb, _sb, _ulp; \
1261 /* Compute Position and shift */ \
1262 _srcs = MPFR_PREC2LIMBS (_srcprec); \
1263 _dests = MPFR_PREC2LIMBS (_destprec); \
1264 MPFR_UNSIGNED_MINUS_MODULO (_sh, _destprec); \
1265 _sp = (srcp) + _srcs - _dests; \
1267 /* General case when prec % GMP_NUMB_BITS != 0 */ \
1268 if (MPFR_LIKELY (_sh != 0)) \
1271 /* Compute Rounding Bit and Sticky Bit */ \
1272 /* Note: in directed rounding modes, if the rounding bit */ \
1273 /* is 1, the behavior does not depend on the sticky bit; */ \
1274 /* thus we will not try to compute it in this case (this */ \
1275 /* can be much faster and avoids to read uninitialized */ \
1276 /* data in the current mpfr_mul implementation). We just */ \
1277 /* make sure that _sb is initialized. */ \
1278 _mask = MPFR_LIMB_ONE << (_sh - 1); \
1279 _rb = _sp[0] & _mask; \
1280 _sb = _sp[0] & (_mask - 1); \
1281 if (MPFR_UNLIKELY (_sb == 0) && \
1282 ((rnd) == MPFR_RNDN || _rb == 0)) \
1283 { /* TODO: Improve it */ \
1286 for (_tmp = _sp, _n = _srcs - _dests ; \
1287 _n != 0 && _sb == 0 ; _n--) \
1292 else /* _sh == 0 */ \
1294 MPFR_ASSERTD (_dests < _srcs); \
1295 /* Compute Rounding Bit and Sticky Bit - see note above */ \
1296 _rb = _sp[-1] & MPFR_LIMB_HIGHBIT; \
1297 _sb = _sp[-1] & (MPFR_LIMB_HIGHBIT-1); \
1298 if (MPFR_UNLIKELY (_sb == 0) && \
1299 ((rnd) == MPFR_RNDN || _rb == 0)) \
1303 for (_tmp = _sp - 1, _n = _srcs - _dests - 1 ; \
1304 _n != 0 && _sb == 0 ; _n--) \
1307 _ulp = MPFR_LIMB_ONE; \
1310 if (MPFR_LIKELY (rnd == MPFR_RNDN)) \
1315 inexact = MPFR_LIKELY ((_sb | _rb) != 0) ? -sign : 0; \
1317 MPN_COPY (_destp, _sp, _dests); \
1318 _destp[0] &= ~(_ulp - 1); \
1320 else if (MPFR_UNLIKELY (_sb == 0)) \
1321 { /* Middle of two consecutive representable numbers */ \
1327 goto addoneulp_doit; /* dummy code to avoid warning */ \
1331 if (MPFR_UNLIKELY (mpn_add_1 (_destp, _sp, _dests, _ulp))) \
1333 _destp[_dests - 1] = MPFR_LIMB_HIGHBIT; \
1336 _destp[0] &= ~(_ulp - 1); \
1340 { /* Directed rounding mode */ \
1341 if (MPFR_LIKELY (MPFR_IS_LIKE_RNDZ (rnd, \
1342 MPFR_IS_NEG_SIGN (sign)))) \
1344 else if (MPFR_UNLIKELY ((_sb | _rb) == 0)) \
1356 * Round mantissa (srcp, sprec) to mpfr_t dest using rounding mode rnd
1357 * assuming dest's sign is sign.
1358 * Execute OVERFLOW_HANDLER in case of overflow when rounding.
1360 #define MPFR_RNDRAW(inexact, dest, srcp, sprec, rnd, sign, OVERFLOW_HANDLER) \
1361 MPFR_RNDRAW_GEN (inexact, dest, srcp, sprec, rnd, sign, \
1362 if ((_sp[0] & _ulp) == 0) \
1372 * Round mantissa (srcp, sprec) to mpfr_t dest using rounding mode rnd
1373 * assuming dest's sign is sign.
1374 * Execute OVERFLOW_HANDLER in case of overflow when rounding.
1375 * Set inexact to +/- MPFR_EVEN_INEX in case of even rounding.
1377 #define MPFR_RNDRAW_EVEN(inexact, dest, srcp, sprec, rnd, sign, \
1379 MPFR_RNDRAW_GEN (inexact, dest, srcp, sprec, rnd, sign, \
1380 if ((_sp[0] & _ulp) == 0) \
1382 inexact = -MPFR_EVEN_INEX * sign; \
1387 inexact = MPFR_EVEN_INEX * sign; \
1388 goto addoneulp_doit; \
1392 /* Return TRUE if b is non singular and we can round it to precision 'prec'
1393 and determine the ternary value, with rounding mode 'rnd', and with
1394 error at most 'error' */
1395 #define MPFR_CAN_ROUND(b,err,prec,rnd) \
1396 (!MPFR_IS_SINGULAR (b) && mpfr_round_p (MPFR_MANT (b), MPFR_LIMB_SIZE (b), \
1397 (err), (prec) + ((rnd)==MPFR_RNDN)))
1399 /* Copy the sign and the significand, and handle the exponent in exp. */
1400 #define MPFR_SETRAW(inexact,dest,src,exp,rnd) \
1401 if (MPFR_UNLIKELY (dest != src)) \
1403 MPFR_SET_SIGN (dest, MPFR_SIGN (src)); \
1404 if (MPFR_LIKELY (MPFR_PREC (dest) == MPFR_PREC (src))) \
1406 MPN_COPY (MPFR_MANT (dest), MPFR_MANT (src), \
1407 MPFR_LIMB_SIZE (src)); \
1412 MPFR_RNDRAW (inexact, dest, MPFR_MANT (src), MPFR_PREC (src), \
1413 rnd, MPFR_SIGN (src), exp++); \
1419 /* TODO: fix this description (see round_near_x.c). */
1420 /* Assuming that the function has a Taylor expansion which looks like:
1421 y=o(f(x)) = o(v + g(x)) with |g(x)| <= 2^(EXP(v)-err)
1422 we can quickly set y to v if x is small (ie err > prec(y)+1) in most
1423 cases. It assumes that f(x) is not representable exactly as a FP number.
1424 v must not be a singular value (NAN, INF or ZERO); usual values are
1427 y is the destination (a mpfr_t), v the value to set (a mpfr_t),
1428 err1+err2 with err2 <= 3 the error term (mpfr_exp_t's), dir (an int) is
1429 the direction of the committed error (if dir = 0, it rounds toward 0,
1430 if dir=1, it rounds away from 0), rnd the rounding mode.
1432 It returns from the function a ternary value in case of success.
1433 If you want to free something, you must fill the "extra" field
1434 in consequences, otherwise put nothing in it.
1436 The test is less restrictive than necessary, but the function
1437 will finish the check itself.
1439 Note: err1 + err2 is allowed to overflow as mpfr_exp_t, but it must give
1440 its real value as mpfr_uexp_t.
1442 #define MPFR_FAST_COMPUTE_IF_SMALL_INPUT(y,v,err1,err2,dir,rnd,extra) \
1444 mpfr_ptr _y = (y); \
1445 mpfr_exp_t _err1 = (err1); \
1446 mpfr_exp_t _err2 = (err2); \
1449 mpfr_uexp_t _err = (mpfr_uexp_t) _err1 + _err2; \
1450 if (MPFR_UNLIKELY (_err > MPFR_PREC (_y) + 1)) \
1452 int _inexact = mpfr_round_near_x (_y,(v),_err,(dir),(rnd)); \
1453 if (_inexact != 0) \
1462 /* Variant, to be called somewhere after MPFR_SAVE_EXPO_MARK. This variant
1463 is needed when there are some computations before or when some non-zero
1464 real constant is used, such as __gmpfr_one for mpfr_cos. */
1465 #define MPFR_SMALL_INPUT_AFTER_SAVE_EXPO(y,v,err1,err2,dir,rnd,expo,extra) \
1467 mpfr_ptr _y = (y); \
1468 mpfr_exp_t _err1 = (err1); \
1469 mpfr_exp_t _err2 = (err2); \
1472 mpfr_uexp_t _err = (mpfr_uexp_t) _err1 + _err2; \
1473 if (MPFR_UNLIKELY (_err > MPFR_PREC (_y) + 1)) \
1476 mpfr_clear_flags (); \
1477 _inexact = mpfr_round_near_x (_y,(v),_err,(dir),(rnd)); \
1478 if (_inexact != 0) \
1481 MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags); \
1482 MPFR_SAVE_EXPO_FREE (expo); \
1483 return mpfr_check_range (_y, _inexact, (rnd)); \
1489 /******************************************************
1490 *************** Ziv Loop Macro *********************
1491 ******************************************************/
1493 #ifndef MPFR_USE_LOGGING
1495 #define MPFR_ZIV_DECL(_x) mpfr_prec_t _x
1496 #define MPFR_ZIV_INIT(_x, _p) (_x) = GMP_NUMB_BITS
1497 #define MPFR_ZIV_NEXT(_x, _p) ((_p) += (_x), (_x) = (_p)/2)
1498 #define MPFR_ZIV_FREE(x)
1502 /* The following test on glibc is there mainly for Darwin (Mac OS X), to
1503 obtain a better error message. The real test should have been a test
1504 concerning nested functions in gcc, which are disabled by default on
1505 Darwin; but it is not possible to do that without a configure test. */
1506 # if defined (__cplusplus) || !(__MPFR_GNUC(3,0) && __MPFR_GLIBC(2,0))
1507 # error "Logging not supported (needs gcc >= 3.0 and GNU C Library >= 2.0)."
1512 /* Note: the mpfr_log_level >= 0 below avoids to take into account
1513 Ziv loops used by the MPFR functions called by the mpfr_fprintf
1516 #define MPFR_ZIV_DECL(_x) \
1518 int _x ## _cpt = 1; \
1519 static unsigned long _x ## _loop = 0, _x ## _bad = 0; \
1520 static const char *_x ## _fname = __func__; \
1521 auto void __attribute__ ((destructor)) x ## _f (void); \
1522 void __attribute__ ((destructor)) x ## _f (void) { \
1523 if (_x ## _loop != 0 && (MPFR_LOG_STAT_F & mpfr_log_type)) \
1524 fprintf (mpfr_log_file, \
1525 "%s: Ziv failed %2.2f%% (%lu bad cases / %lu calls)\n", \
1526 _x ## _fname, (double) 100.0 * _x ## _bad / _x ## _loop, \
1527 _x ## _bad, _x ## _loop ); }
1529 #define MPFR_ZIV_INIT(_x, _p) \
1532 (_x) = GMP_NUMB_BITS; \
1533 if (mpfr_log_level >= 0) \
1535 if ((MPFR_LOG_BADCASE_F & mpfr_log_type) && \
1536 (mpfr_log_current <= mpfr_log_level)) \
1537 LOG_PRINT ("%s:ZIV 1st prec=%Pd\n", \
1538 __func__, (mpfr_prec_t) (_p)); \
1542 #define MPFR_ZIV_NEXT(_x, _p) \
1547 if (mpfr_log_level >= 0) \
1548 _x ## _bad += (_x ## _cpt == 1); \
1550 if ((MPFR_LOG_BADCASE_F & mpfr_log_type) && \
1551 (mpfr_log_current <= mpfr_log_level)) \
1552 LOG_PRINT ("%s:ZIV new prec=%Pd\n", \
1553 __func__, (mpfr_prec_t) (_p)); \
1557 #define MPFR_ZIV_FREE(_x) \
1560 if ((MPFR_LOG_BADCASE_F & mpfr_log_type) && _x ## _cpt > 1 && \
1561 (mpfr_log_current <= mpfr_log_level)) \
1562 fprintf (mpfr_log_file, "%s:ZIV %d loops\n", \
1563 __func__, _x ## _cpt); \
1570 /******************************************************
1571 *************** Logging Macros *********************
1572 ******************************************************/
1574 /* The different kind of LOG */
1575 #define MPFR_LOG_INPUT_F 1
1576 #define MPFR_LOG_OUTPUT_F 2
1577 #define MPFR_LOG_INTERNAL_F 4
1578 #define MPFR_LOG_TIME_F 8
1579 #define MPFR_LOG_BADCASE_F 16
1580 #define MPFR_LOG_MSG_F 32
1581 #define MPFR_LOG_STAT_F 64
1583 #ifdef MPFR_USE_LOGGING
1585 /* Check if we can support this feature */
1586 # ifdef MPFR_USE_THREAD_SAFE
1587 # error "Enable either `Logging' or `thread-safe', not both"
1589 # if !__MPFR_GNUC(3,0)
1590 # error "Logging not supported (GCC >= 3.0)"
1593 #if defined (__cplusplus)
1597 __MPFR_DECLSPEC
extern FILE *mpfr_log_file
;
1598 __MPFR_DECLSPEC
extern int mpfr_log_type
;
1599 __MPFR_DECLSPEC
extern int mpfr_log_level
;
1600 __MPFR_DECLSPEC
extern int mpfr_log_current
;
1601 __MPFR_DECLSPEC
extern mpfr_prec_t mpfr_log_prec
;
1603 #if defined (__cplusplus)
1607 /* LOG_PRINT calls mpfr_fprintf on mpfr_log_file with logging disabled
1608 (recursive logging is not wanted and freezes MPFR). */
1609 #define LOG_PRINT(format, ...) \
1612 int old_level = mpfr_log_level; \
1613 mpfr_log_level = -1; /* disable logging in mpfr_fprintf */ \
1614 __gmpfr_cache_const_pi = __gmpfr_logging_pi; \
1615 __gmpfr_cache_const_log2 = __gmpfr_logging_log2; \
1616 mpfr_fprintf (mpfr_log_file, format, __VA_ARGS__); \
1617 mpfr_log_level = old_level; \
1618 __gmpfr_cache_const_pi = __gmpfr_normal_pi; \
1619 __gmpfr_cache_const_log2 = __gmpfr_normal_log2; \
1623 #define MPFR_LOG_VAR(x) \
1625 if ((MPFR_LOG_INTERNAL_F & mpfr_log_type) && \
1626 (mpfr_log_current <= mpfr_log_level)) \
1627 LOG_PRINT ("%s.%d:%s[%#Pu]=%.*Rg\n", __func__, __LINE__, \
1628 #x, mpfr_get_prec (x), mpfr_log_prec, x); \
1631 #define MPFR_LOG_MSG2(format, ...) \
1633 if ((MPFR_LOG_MSG_F & mpfr_log_type) && \
1634 (mpfr_log_current <= mpfr_log_level)) \
1635 LOG_PRINT ("%s.%d: "format, __func__, __LINE__, __VA_ARGS__); \
1637 #define MPFR_LOG_MSG(x) MPFR_LOG_MSG2 x
1639 #define MPFR_LOG_BEGIN2(format, ...) \
1640 mpfr_log_current ++; \
1641 if ((MPFR_LOG_INPUT_F & mpfr_log_type) && \
1642 (mpfr_log_current <= mpfr_log_level)) \
1643 LOG_PRINT ("%s:IN "format"\n", __func__, __VA_ARGS__); \
1644 if ((MPFR_LOG_TIME_F & mpfr_log_type) && \
1645 (mpfr_log_current <= mpfr_log_level)) \
1646 __gmpfr_log_time = mpfr_get_cputime ();
1647 #define MPFR_LOG_BEGIN(x) \
1648 int __gmpfr_log_time = 0; \
1651 #define MPFR_LOG_END2(format, ...) \
1652 if ((MPFR_LOG_TIME_F & mpfr_log_type) && \
1653 (mpfr_log_current <= mpfr_log_level)) \
1654 fprintf (mpfr_log_file, "%s:TIM %dms\n", __mpfr_log_fname, \
1655 mpfr_get_cputime () - __gmpfr_log_time); \
1656 if ((MPFR_LOG_OUTPUT_F & mpfr_log_type) && \
1657 (mpfr_log_current <= mpfr_log_level)) \
1658 LOG_PRINT ("%s:OUT "format"\n", __mpfr_log_fname, __VA_ARGS__); \
1659 mpfr_log_current --;
1660 #define MPFR_LOG_END(x) \
1661 static const char *__mpfr_log_fname = __func__; \
1664 #define MPFR_LOG_FUNC(begin,end) \
1665 static const char *__mpfr_log_fname = __func__; \
1666 auto void __mpfr_log_cleanup (int *time); \
1667 void __mpfr_log_cleanup (int *time) { \
1668 int __gmpfr_log_time = *time; \
1669 MPFR_LOG_END2 end; } \
1670 int __gmpfr_log_time __attribute__ ((cleanup (__mpfr_log_cleanup))); \
1671 __gmpfr_log_time = 0; \
1672 MPFR_LOG_BEGIN2 begin
1674 #else /* MPFR_USE_LOGGING */
1676 /* Define void macro for logging */
1678 #define MPFR_LOG_VAR(x)
1679 #define MPFR_LOG_BEGIN(x)
1680 #define MPFR_LOG_END(x)
1681 #define MPFR_LOG_MSG(x)
1682 #define MPFR_LOG_FUNC(x,y)
1684 #endif /* MPFR_USE_LOGGING */
1687 /**************************************************************
1688 ************ Group Initialize Functions Macros *************
1689 **************************************************************/
1691 #ifndef MPFR_GROUP_STATIC_SIZE
1692 # define MPFR_GROUP_STATIC_SIZE 16
1695 struct mpfr_group_t
{
1698 mp_limb_t tab
[MPFR_GROUP_STATIC_SIZE
];
1701 #define MPFR_GROUP_DECL(g) struct mpfr_group_t g
1702 #define MPFR_GROUP_CLEAR(g) do { \
1703 MPFR_LOG_MSG (("GROUP_CLEAR: ptr = 0x%lX, size = %lu\n", \
1704 (unsigned long) (g).mant, \
1705 (unsigned long) (g).alloc)); \
1706 if (MPFR_UNLIKELY ((g).alloc != 0)) { \
1707 MPFR_ASSERTD ((g).mant != (g).tab); \
1708 (*__gmp_free_func) ((g).mant, (g).alloc); \
1711 #define MPFR_GROUP_INIT_TEMPLATE(g, prec, num, handler) do { \
1712 mpfr_prec_t _prec = (prec); \
1714 MPFR_ASSERTD (_prec >= MPFR_PREC_MIN); \
1715 if (MPFR_UNLIKELY (_prec > MPFR_PREC_MAX)) \
1716 mpfr_abort_prec_max (); \
1717 _size = MPFR_PREC2LIMBS (_prec); \
1718 if (MPFR_UNLIKELY (_size * (num) > MPFR_GROUP_STATIC_SIZE)) \
1720 (g).alloc = (num) * _size * sizeof (mp_limb_t); \
1721 (g).mant = (mp_limb_t *) (*__gmp_allocate_func) ((g).alloc); \
1726 (g).mant = (g).tab; \
1728 MPFR_LOG_MSG (("GROUP_INIT: ptr = 0x%lX, size = %lu\n", \
1729 (unsigned long) (g).mant, (unsigned long) (g).alloc)); \
1732 #define MPFR_GROUP_TINIT(g, n, x) \
1733 MPFR_TMP_INIT1 ((g).mant + _size * (n), x, _prec)
1735 #define MPFR_GROUP_INIT_1(g, prec, x) \
1736 MPFR_GROUP_INIT_TEMPLATE(g, prec, 1, MPFR_GROUP_TINIT(g, 0, x))
1737 #define MPFR_GROUP_INIT_2(g, prec, x, y) \
1738 MPFR_GROUP_INIT_TEMPLATE(g, prec, 2, \
1739 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y))
1740 #define MPFR_GROUP_INIT_3(g, prec, x, y, z) \
1741 MPFR_GROUP_INIT_TEMPLATE(g, prec, 3, \
1742 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1743 MPFR_GROUP_TINIT(g, 2, z))
1744 #define MPFR_GROUP_INIT_4(g, prec, x, y, z, t) \
1745 MPFR_GROUP_INIT_TEMPLATE(g, prec, 4, \
1746 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1747 MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t))
1748 #define MPFR_GROUP_INIT_5(g, prec, x, y, z, t, a) \
1749 MPFR_GROUP_INIT_TEMPLATE(g, prec, 5, \
1750 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1751 MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t); \
1752 MPFR_GROUP_TINIT(g, 4, a))
1753 #define MPFR_GROUP_INIT_6(g, prec, x, y, z, t, a, b) \
1754 MPFR_GROUP_INIT_TEMPLATE(g, prec, 6, \
1755 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1756 MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t); \
1757 MPFR_GROUP_TINIT(g, 4, a);MPFR_GROUP_TINIT(g, 5, b))
1759 #define MPFR_GROUP_REPREC_TEMPLATE(g, prec, num, handler) do { \
1760 mpfr_prec_t _prec = (prec); \
1761 size_t _oalloc = (g).alloc; \
1763 MPFR_LOG_MSG (("GROUP_REPREC: oldptr = 0x%lX, oldsize = %lu\n", \
1764 (unsigned long) (g).mant, (unsigned long) _oalloc)); \
1765 MPFR_ASSERTD (_prec >= MPFR_PREC_MIN); \
1766 if (MPFR_UNLIKELY (_prec > MPFR_PREC_MAX)) \
1767 mpfr_abort_prec_max (); \
1768 _size = MPFR_PREC2LIMBS (_prec); \
1769 (g).alloc = (num) * _size * sizeof (mp_limb_t); \
1770 if (MPFR_LIKELY (_oalloc == 0)) \
1771 (g).mant = (mp_limb_t *) (*__gmp_allocate_func) ((g).alloc); \
1773 (g).mant = (mp_limb_t *) \
1774 (*__gmp_reallocate_func) ((g).mant, _oalloc, (g).alloc); \
1775 MPFR_LOG_MSG (("GROUP_REPREC: newptr = 0x%lX, newsize = %lu\n", \
1776 (unsigned long) (g).mant, (unsigned long) (g).alloc)); \
1780 #define MPFR_GROUP_REPREC_1(g, prec, x) \
1781 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 1, MPFR_GROUP_TINIT(g, 0, x))
1782 #define MPFR_GROUP_REPREC_2(g, prec, x, y) \
1783 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 2, \
1784 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y))
1785 #define MPFR_GROUP_REPREC_3(g, prec, x, y, z) \
1786 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 3, \
1787 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1788 MPFR_GROUP_TINIT(g, 2, z))
1789 #define MPFR_GROUP_REPREC_4(g, prec, x, y, z, t) \
1790 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 4, \
1791 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1792 MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t))
1793 #define MPFR_GROUP_REPREC_5(g, prec, x, y, z, t, a) \
1794 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 5, \
1795 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1796 MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t); \
1797 MPFR_GROUP_TINIT(g, 4, a))
1798 #define MPFR_GROUP_REPREC_6(g, prec, x, y, z, t, a, b) \
1799 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 6, \
1800 MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y); \
1801 MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t); \
1802 MPFR_GROUP_TINIT(g, 4, a);MPFR_GROUP_TINIT(g, 5, b))
1805 /******************************************************
1806 *************** Internal Functions *****************
1807 ******************************************************/
1809 #if defined (__cplusplus)
1813 __MPFR_DECLSPEC
int mpfr_underflow
_MPFR_PROTO ((mpfr_ptr
, mpfr_rnd_t
, int));
1814 __MPFR_DECLSPEC
int mpfr_overflow
_MPFR_PROTO ((mpfr_ptr
, mpfr_rnd_t
, int));
1816 __MPFR_DECLSPEC
int mpfr_add1
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,
1817 mpfr_srcptr
, mpfr_rnd_t
));
1818 __MPFR_DECLSPEC
int mpfr_sub1
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,
1819 mpfr_srcptr
, mpfr_rnd_t
));
1820 __MPFR_DECLSPEC
int mpfr_add1sp
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,
1821 mpfr_srcptr
, mpfr_rnd_t
));
1822 __MPFR_DECLSPEC
int mpfr_sub1sp
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,
1823 mpfr_srcptr
, mpfr_rnd_t
));
1824 __MPFR_DECLSPEC
int mpfr_can_round_raw
_MPFR_PROTO ((const mp_limb_t
*,
1825 mp_size_t
, int, mpfr_exp_t
, mpfr_rnd_t
, mpfr_rnd_t
, mpfr_prec_t
));
1827 __MPFR_DECLSPEC
int mpfr_cmp2
_MPFR_PROTO ((mpfr_srcptr
, mpfr_srcptr
,
1830 __MPFR_DECLSPEC
long __gmpfr_ceil_log2
_MPFR_PROTO ((double));
1831 __MPFR_DECLSPEC
long __gmpfr_floor_log2
_MPFR_PROTO ((double));
1832 __MPFR_DECLSPEC
double __gmpfr_ceil_exp2
_MPFR_PROTO ((double));
1833 __MPFR_DECLSPEC
unsigned long __gmpfr_isqrt
_MPFR_PROTO ((unsigned long));
1834 __MPFR_DECLSPEC
unsigned long __gmpfr_cuberoot
_MPFR_PROTO ((unsigned long));
1835 __MPFR_DECLSPEC
int __gmpfr_int_ceil_log2
_MPFR_PROTO ((unsigned long));
1837 __MPFR_DECLSPEC mpfr_exp_t mpfr_ceil_mul
_MPFR_PROTO ((mpfr_exp_t
, int, int));
1839 __MPFR_DECLSPEC
int mpfr_exp_2
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,mpfr_rnd_t
));
1840 __MPFR_DECLSPEC
int mpfr_exp_3
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,mpfr_rnd_t
));
1841 __MPFR_DECLSPEC
int mpfr_powerof2_raw
_MPFR_PROTO ((mpfr_srcptr
));
1843 __MPFR_DECLSPEC
int mpfr_pow_general
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,
1844 mpfr_srcptr
, mpfr_rnd_t
, int, mpfr_save_expo_t
*));
1846 __MPFR_DECLSPEC
void mpfr_setmax
_MPFR_PROTO ((mpfr_ptr
, mpfr_exp_t
));
1847 __MPFR_DECLSPEC
void mpfr_setmin
_MPFR_PROTO ((mpfr_ptr
, mpfr_exp_t
));
1849 __MPFR_DECLSPEC
long mpfr_mpn_exp
_MPFR_PROTO ((mp_limb_t
*, mpfr_exp_t
*, int,
1850 mpfr_exp_t
, size_t));
1852 #ifdef _MPFR_H_HAVE_FILE
1853 __MPFR_DECLSPEC
void mpfr_fprint_binary
_MPFR_PROTO ((FILE *, mpfr_srcptr
));
1855 __MPFR_DECLSPEC
void mpfr_print_binary
_MPFR_PROTO ((mpfr_srcptr
));
1856 __MPFR_DECLSPEC
void mpfr_print_mant_binary
_MPFR_PROTO ((const char*,
1857 const mp_limb_t
*, mpfr_prec_t
));
1858 __MPFR_DECLSPEC
void mpfr_set_str_binary
_MPFR_PROTO((mpfr_ptr
, const char*));
1860 __MPFR_DECLSPEC
int mpfr_round_raw
_MPFR_PROTO ((mp_limb_t
*,
1861 const mp_limb_t
*, mpfr_prec_t
, int, mpfr_prec_t
, mpfr_rnd_t
, int *));
1862 __MPFR_DECLSPEC
int mpfr_round_raw_2
_MPFR_PROTO ((const mp_limb_t
*,
1863 mpfr_prec_t
, int, mpfr_prec_t
, mpfr_rnd_t
));
1864 /* No longer defined (see round_prec.c).
1865 Uncomment if it needs to be defined again.
1866 __MPFR_DECLSPEC int mpfr_round_raw_3 _MPFR_PROTO ((const mp_limb_t *,
1867 mpfr_prec_t, int, mpfr_prec_t, mpfr_rnd_t, int *));
1869 __MPFR_DECLSPEC
int mpfr_round_raw_4
_MPFR_PROTO ((mp_limb_t
*,
1870 const mp_limb_t
*, mpfr_prec_t
, int, mpfr_prec_t
, mpfr_rnd_t
));
1872 #define mpfr_round_raw2(xp, xn, neg, r, prec) \
1873 mpfr_round_raw_2((xp),(xn)*GMP_NUMB_BITS,(neg),(prec),(r))
1875 __MPFR_DECLSPEC
int mpfr_check
_MPFR_PROTO ((mpfr_srcptr
));
1877 __MPFR_DECLSPEC
int mpfr_sum_sort
_MPFR_PROTO ((mpfr_srcptr
*const,
1878 unsigned long, mpfr_srcptr
*));
1880 __MPFR_DECLSPEC
int mpfr_get_cputime
_MPFR_PROTO ((void));
1882 __MPFR_DECLSPEC
void mpfr_nexttozero
_MPFR_PROTO ((mpfr_ptr
));
1883 __MPFR_DECLSPEC
void mpfr_nexttoinf
_MPFR_PROTO ((mpfr_ptr
));
1885 __MPFR_DECLSPEC
int mpfr_const_pi_internal
_MPFR_PROTO ((mpfr_ptr
,mpfr_rnd_t
));
1886 __MPFR_DECLSPEC
int mpfr_const_log2_internal
_MPFR_PROTO((mpfr_ptr
,mpfr_rnd_t
));
1887 __MPFR_DECLSPEC
int mpfr_const_euler_internal
_MPFR_PROTO((mpfr_ptr
, mpfr_rnd_t
));
1888 __MPFR_DECLSPEC
int mpfr_const_catalan_internal
_MPFR_PROTO((mpfr_ptr
, mpfr_rnd_t
));
1891 __MPFR_DECLSPEC
void mpfr_init_cache
_MPFR_PROTO ((mpfr_cache_t
,
1892 int(*)(mpfr_ptr
,mpfr_rnd_t
)));
1894 __MPFR_DECLSPEC
void mpfr_clear_cache
_MPFR_PROTO ((mpfr_cache_t
));
1895 __MPFR_DECLSPEC
int mpfr_cache
_MPFR_PROTO ((mpfr_ptr
, mpfr_cache_t
,
1898 __MPFR_DECLSPEC
void mpfr_mulhigh_n
_MPFR_PROTO ((mpfr_limb_ptr
,
1899 mpfr_limb_srcptr
, mpfr_limb_srcptr
, mp_size_t
));
1900 __MPFR_DECLSPEC
void mpfr_mullow_n
_MPFR_PROTO ((mpfr_limb_ptr
,
1901 mpfr_limb_srcptr
, mpfr_limb_srcptr
, mp_size_t
));
1902 __MPFR_DECLSPEC
void mpfr_sqrhigh_n
_MPFR_PROTO ((mpfr_limb_ptr
,
1903 mpfr_limb_srcptr
, mp_size_t
));
1904 __MPFR_DECLSPEC mp_limb_t mpfr_divhigh_n
_MPFR_PROTO ((mpfr_limb_ptr
,
1905 mpfr_limb_ptr
, mpfr_limb_ptr
, mp_size_t
));
1907 __MPFR_DECLSPEC
int mpfr_round_p
_MPFR_PROTO ((mp_limb_t
*, mp_size_t
,
1908 mpfr_exp_t
, mpfr_prec_t
));
1910 __MPFR_DECLSPEC
void mpfr_dump_mant
_MPFR_PROTO ((const mp_limb_t
*,
1911 mpfr_prec_t
, mpfr_prec_t
,
1914 __MPFR_DECLSPEC
int mpfr_round_near_x
_MPFR_PROTO ((mpfr_ptr
, mpfr_srcptr
,
1917 __MPFR_DECLSPEC
void mpfr_abort_prec_max
_MPFR_PROTO ((void))
1920 __MPFR_DECLSPEC
void mpfr_rand_raw
_MPFR_PROTO((mpfr_limb_ptr
, gmp_randstate_t
,
1923 __MPFR_DECLSPEC mpz_t
* mpfr_bernoulli_internal
_MPFR_PROTO((mpz_t
*,
1926 __MPFR_DECLSPEC
int mpfr_sincos_fast
_MPFR_PROTO((mpfr_t
, mpfr_t
,
1927 mpfr_srcptr
, mpfr_rnd_t
));
1929 __MPFR_DECLSPEC
double mpfr_scale2
_MPFR_PROTO((double, int));
1931 __MPFR_DECLSPEC
void mpfr_div_ui2
_MPFR_PROTO((mpfr_ptr
, mpfr_srcptr
,
1932 unsigned long int, unsigned long int,
1935 __MPFR_DECLSPEC
void mpfr_gamma_one_and_two_third
_MPFR_PROTO((mpfr_ptr
, mpfr_ptr
, mpfr_prec_t
));
1937 #if defined (__cplusplus)