1/* Utilities for MPFR developers, not exported.
2
3Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4Contributed by the AriC and Caramel projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#ifndef __MPFR_IMPL_H__
24#define __MPFR_IMPL_H__
25
26/* Let's include some standard headers unconditionally as they are
27   already needed by several source files or when some options are
28   enabled/disabled, and it is easy to forget them (some configure
29   options may hide the error).
30   Note: If some source file must not have such a header included
31   (which is very unlikely and probably means something broken in
32   this source file), we should do that with some macro (that would
33   also force to disable incompatible features). */
34#if defined (__cplusplus)
35#include <cstdio>
36#include <cstring>
37#else
38#include <stdio.h>
39#include <string.h>
40#endif
41#include <limits.h>
42
43#if _MPFR_EXP_FORMAT == 4
44/* mpfr_exp_t will be defined as intmax_t */
45# include "mpfr-intmax.h"
46#endif
47
48/* Check if we are inside a build of MPFR or inside the test suite.
49   This is needed in mpfr.h to export or import the functions.
50   It matters only for Windows DLL */
51#ifndef __MPFR_TEST_H__
52# define __MPFR_WITHIN_MPFR 1
53#endif
54
55/******************************************************
56 ****************** Include files *********************
57 ******************************************************/
58
59/* Include 'config.h' before using ANY configure macros if needed
60   NOTE: It isn't MPFR 'config.h', but GMP's one! */
61#ifdef HAVE_CONFIG_H
62# include "config.h"
63#endif
64
65#ifdef  MPFR_HAVE_GMP_IMPL /* Build with gmp internals*/
66
67# ifndef __GMP_H__
68#  include "gmp.h"
69# endif
70# ifndef __GMP_IMPL_H__
71#  include "gmp-impl.h"
72# endif
73# ifdef MPFR_NEED_LONGLONG_H
74#  include "longlong.h"
75# endif
76# ifndef __MPFR_H
77#  include "mpfr.h"
78# endif
79
80#else /* Build without gmp internals */
81
82# ifndef __GMP_H__
83#  include "gmp.h"
84# endif
85# ifndef __MPFR_H
86#  include "mpfr.h"
87# endif
88# ifndef __GMPFR_GMP_H__
89#  include "mpfr-gmp.h"
90# endif
91# ifdef MPFR_NEED_LONGLONG_H
92#  define LONGLONG_STANDALONE
93#  include "mpfr-longlong.h"
94# endif
95
96#endif
97#undef MPFR_NEED_LONGLONG_H
98
99/* If a mpn_sqr_n macro is not defined, use mpn_mul. GMP 4.x defines a
100   mpn_sqr_n macro in gmp-impl.h (and this macro disappeared in GMP 5),
101   so that GMP's macro can only be used when MPFR has been configured
102   with --with-gmp-build (and only with GMP 4.x). */
103#ifndef mpn_sqr_n
104# define mpn_sqr_n(dst,src,n) mpn_mul((dst),(src),(n),(src),(n))
105#endif
106
107/* For the definition of MPFR_THREAD_ATTR. GCC/ICC detection macros are
108   no longer used, as they sometimes gave incorrect information about
109   the support of thread-local variables. A configure check is now done.
110   If the use of detection macros is needed in the future, this could be
111   moved below (after the detection macros are defined). */
112#include "mpfr-thread.h"
113
114
115/******************************************************
116 ***************** Detection macros *******************
117 ******************************************************/
118
119/* Macros to detect STDC, GCC, GLIBC, GMP and ICC version */
120#if defined(__STDC_VERSION__)
121# define __MPFR_STDC(version) (__STDC_VERSION__>=(version))
122#elif defined(__STDC__)
123# define __MPFR_STDC(version) (0 == (version))
124#else
125# define __MPFR_STDC(version) 0
126#endif
127
128#if defined(_WIN32)
129/* Under MS Windows (e.g. with VS2008 or VS2010), Intel's compiler doesn't
130   support/enable extensions like the ones seen under GNU/Linux.
131   https://sympa.inria.fr/sympa/arc/mpfr/2011-02/msg00032.html */
132# define __MPFR_ICC(a,b,c) 0
133#elif defined(__ICC)
134# define __MPFR_ICC(a,b,c) (__ICC >= (a)*100+(b)*10+(c))
135#elif defined(__INTEL_COMPILER)
136# define __MPFR_ICC(a,b,c) (__INTEL_COMPILER >= (a)*100+(b)*10+(c))
137#else
138# define __MPFR_ICC(a,b,c) 0
139#endif
140
141#if defined(__GNUC__) && defined(__GNUC_MINOR__) && ! __MPFR_ICC(0,0,0)
142# define __MPFR_GNUC(a,i) \
143 (MPFR_VERSION_NUM(__GNUC__,__GNUC_MINOR__,0) >= MPFR_VERSION_NUM(a,i,0))
144#else
145# define __MPFR_GNUC(a,i) 0
146#endif
147
148#if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
149# define __MPFR_GLIBC(a,i) \
150 (MPFR_VERSION_NUM(__GLIBC__,__GLIBC_MINOR__,0) >= MPFR_VERSION_NUM(a,i,0))
151#else
152# define __MPFR_GLIBC(a,i) 0
153#endif
154
155#if defined(__GNU_MP_VERSION) && \
156    defined(__GNU_MP_VERSION_MINOR) && \
157    defined(__GNU_MP_VERSION_PATCHLEVEL)
158# define __MPFR_GMP(a,b,c) \
159  (MPFR_VERSION_NUM(__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR,__GNU_MP_VERSION_PATCHLEVEL) >= MPFR_VERSION_NUM(a,b,c))
160#else
161# define __MPFR_GMP(a,b,c) 0
162#endif
163
164
165
166/******************************************************
167 ************* GMP Basic Pointer Types ****************
168 ******************************************************/
169
170typedef mp_limb_t *mpfr_limb_ptr;
171typedef __gmp_const mp_limb_t *mpfr_limb_srcptr;
172
173
174
175/******************************************************
176 ****************** (U)INTMAX_MAX *********************
177 ******************************************************/
178
179/* Let's try to fix UINTMAX_MAX and INTMAX_MAX if these macros don't work
180   (e.g. with gcc -ansi -pedantic-errors in 32-bit mode under GNU/Linux),
181   see <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=582698>. */
182#ifdef _MPFR_H_HAVE_INTMAX_T
183# ifdef MPFR_HAVE_INTMAX_MAX
184#  define MPFR_UINTMAX_MAX UINTMAX_MAX
185#  define MPFR_INTMAX_MAX INTMAX_MAX
186#  define MPFR_INTMAX_MIN INTMAX_MIN
187# else
188#  define MPFR_UINTMAX_MAX ((uintmax_t) -1)
189#  define MPFR_INTMAX_MAX ((intmax_t) (MPFR_UINTMAX_MAX >> 1))
190#  define MPFR_INTMAX_MIN (INT_MIN + INT_MAX - MPFR_INTMAX_MAX)
191# endif
192#endif
193
194
195
196/******************************************************
197 ******************** Check GMP ***********************
198 ******************************************************/
199
200#if !__MPFR_GMP(4,1,0)
201# error "GMP 4.1.0 or newer needed"
202#endif
203
204#if GMP_NAIL_BITS != 0
205# error "MPFR doesn't support nonzero values of GMP_NAIL_BITS"
206#endif
207
208#if (GMP_NUMB_BITS<32) || (GMP_NUMB_BITS & (GMP_NUMB_BITS - 1))
209# error "GMP_NUMB_BITS must be a power of 2, and >= 32"
210#endif
211
212#if GMP_NUMB_BITS == 16
213# define MPFR_LOG2_GMP_NUMB_BITS 4
214#elif GMP_NUMB_BITS == 32
215# define MPFR_LOG2_GMP_NUMB_BITS 5
216#elif GMP_NUMB_BITS == 64
217# define MPFR_LOG2_GMP_NUMB_BITS 6
218#elif GMP_NUMB_BITS == 128
219# define MPFR_LOG2_GMP_NUMB_BITS 7
220#elif GMP_NUMB_BITS == 256
221# define MPFR_LOG2_GMP_NUMB_BITS 8
222#else
223# error "Can't compute log2(GMP_NUMB_BITS)"
224#endif
225
226#if __MPFR_GNUC(3,0) || __MPFR_ICC(8,1,0)
227/* For the future: N1478: Supporting the 'noreturn' property in C1x
228   http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1478.htm */
229# define MPFR_NORETURN_ATTR __attribute__ ((noreturn))
230# define MPFR_CONST_ATTR    __attribute__ ((const))
231#else
232# define MPFR_NORETURN_ATTR
233# define MPFR_CONST_ATTR
234#endif
235
236/******************************************************
237 ************* Global Internal Variables **************
238 ******************************************************/
239
240/* Cache struct */
241struct __gmpfr_cache_s {
242  mpfr_t x;
243  int inexact;
244  int (*func)(mpfr_ptr, mpfr_rnd_t);
245};
246typedef struct __gmpfr_cache_s mpfr_cache_t[1];
247typedef struct __gmpfr_cache_s *mpfr_cache_ptr;
248
249#if defined (__cplusplus)
250extern "C" {
251#endif
252
253__MPFR_DECLSPEC extern MPFR_THREAD_ATTR unsigned int __gmpfr_flags;
254__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_exp_t   __gmpfr_emin;
255__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_exp_t   __gmpfr_emax;
256__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_prec_t  __gmpfr_default_fp_bit_precision;
257__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_rnd_t   __gmpfr_default_rounding_mode;
258__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_euler;
259__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_catalan;
260
261#ifndef MPFR_USE_LOGGING
262__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_pi;
263__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_cache_const_log2;
264#else
265/* Two constants are used by the logging functions (via mpfr_fprintf,
266   then mpfr_log, for the base conversion): pi and log(2). Since the
267   mpfr_cache function isn't re-entrant when working on the same cache,
268   we need to define two caches for each constant. */
269__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_normal_pi;
270__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_normal_log2;
271__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_logging_pi;
272__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_t __gmpfr_logging_log2;
273__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_ptr __gmpfr_cache_const_pi;
274__MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_cache_ptr __gmpfr_cache_const_log2;
275#endif
276
277#define BASE_MAX 62
278__MPFR_DECLSPEC extern const __mpfr_struct __gmpfr_l2b[BASE_MAX-1][2];
279
280/* Note: do not use the following values when they can be outside the
281   current exponent range, e.g. when the exponent range has not been
282   extended yet; under such a condition, they can be used only in
283   mpfr_cmpabs. */
284__MPFR_DECLSPEC extern const mpfr_t __gmpfr_one;
285__MPFR_DECLSPEC extern const mpfr_t __gmpfr_two;
286__MPFR_DECLSPEC extern const mpfr_t __gmpfr_four;
287
288
289#if defined (__cplusplus)
290 }
291#endif
292
293/* Flags of __gmpfr_flags */
294#define MPFR_FLAGS_UNDERFLOW 1
295#define MPFR_FLAGS_OVERFLOW 2
296#define MPFR_FLAGS_NAN 4
297#define MPFR_FLAGS_INEXACT 8
298#define MPFR_FLAGS_ERANGE 16
299#define MPFR_FLAGS_DIVBY0 32
300#define MPFR_FLAGS_ALL 63
301
302/* Replace some common functions for direct access to the global vars */
303#define mpfr_get_emin() (__gmpfr_emin + 0)
304#define mpfr_get_emax() (__gmpfr_emax + 0)
305#define mpfr_get_default_rounding_mode() (__gmpfr_default_rounding_mode + 0)
306#define mpfr_get_default_prec() (__gmpfr_default_fp_bit_precision + 0)
307
308#define mpfr_clear_flags() \
309  ((void) (__gmpfr_flags = 0))
310#define mpfr_clear_underflow() \
311  ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_UNDERFLOW))
312#define mpfr_clear_overflow() \
313  ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_OVERFLOW))
314#define mpfr_clear_nanflag() \
315  ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_NAN))
316#define mpfr_clear_inexflag() \
317  ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_INEXACT))
318#define mpfr_clear_erangeflag() \
319  ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE))
320#define mpfr_clear_divby0() \
321  ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_DIVBY0))
322#define mpfr_underflow_p() \
323  ((int) (__gmpfr_flags & MPFR_FLAGS_UNDERFLOW))
324#define mpfr_overflow_p() \
325  ((int) (__gmpfr_flags & MPFR_FLAGS_OVERFLOW))
326#define mpfr_nanflag_p() \
327  ((int) (__gmpfr_flags & MPFR_FLAGS_NAN))
328#define mpfr_inexflag_p() \
329  ((int) (__gmpfr_flags & MPFR_FLAGS_INEXACT))
330#define mpfr_erangeflag_p() \
331  ((int) (__gmpfr_flags & MPFR_FLAGS_ERANGE))
332#define mpfr_divby0_p() \
333  ((int) (__gmpfr_flags & MPFR_FLAGS_DIVBY0))
334
335/* Testing an exception flag correctly is tricky. There are mainly two
336   pitfalls: First, one needs to remember to clear the corresponding
337   flag, in case it was set before the function call or during some
338   intermediate computations (in practice, one can clear all the flags).
339   Secondly, one needs to test the flag early enough, i.e. before it
340   can be modified by another function. Moreover, it is quite difficult
341   (if not impossible) to reliably check problems with "make check". To
342   avoid these pitfalls, it is recommended to use the following macros.
343   Other use of the exception-flag predicate functions/macros will be
344   detected by mpfrlint.
345   Note: _op can be either a statement or an expression.
346   MPFR_BLOCK_EXCEP should be used only inside a block; it is useful to
347   detect some exception in order to exit the block as soon as possible. */
348#define MPFR_BLOCK_DECL(_flags) unsigned int _flags
349/* The (void) (_flags) makes sure that _flags is read at least once (it
350   makes sense to use MPFR_BLOCK while _flags will never be read in the
351   source, so that we wish to avoid the corresponding warning). */
352#define MPFR_BLOCK(_flags,_op)          \
353  do                                    \
354    {                                   \
355      mpfr_clear_flags ();              \
356      _op;                              \
357      (_flags) = __gmpfr_flags;         \
358      (void) (_flags);                  \
359    }                                   \
360  while (0)
361#define MPFR_BLOCK_TEST(_flags,_f) MPFR_UNLIKELY ((_flags) & (_f))
362#define MPFR_BLOCK_EXCEP (__gmpfr_flags & (MPFR_FLAGS_UNDERFLOW | \
363                                           MPFR_FLAGS_OVERFLOW | \
364                                           MPFR_FLAGS_DIVBY0 | \
365                                           MPFR_FLAGS_NAN))
366/* Let's use a MPFR_ prefix, because e.g. OVERFLOW is defined by glibc's
367   math.h, though this is not a reserved identifier! */
368#define MPFR_UNDERFLOW(_flags)  MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_UNDERFLOW)
369#define MPFR_OVERFLOW(_flags)   MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_OVERFLOW)
370#define MPFR_NANFLAG(_flags)    MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_NAN)
371#define MPFR_INEXFLAG(_flags)   MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_INEXACT)
372#define MPFR_ERANGEFLAG(_flags) MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_ERANGE)
373#define MPFR_DIVBY0(_flags)     MPFR_BLOCK_TEST (_flags, MPFR_FLAGS_DIVBY0)
374
375
376/******************************************************
377 ******************** Assertions **********************
378 ******************************************************/
379
380/* Compile with -DWANT_ASSERT to check all assert statements */
381
382/* Note: do not use GMP macros ASSERT_ALWAYS and ASSERT as they are not
383   expressions, and as a consequence, they cannot be used in a for(),
384   with a comma operator and so on. */
385
386/* MPFR_ASSERTN(expr): assertions that should always be checked */
387#define MPFR_ASSERTN(expr)  \
388   ((void) ((MPFR_UNLIKELY(expr)) || MPFR_UNLIKELY( (ASSERT_FAIL(expr),0) )))
389
390/* MPFR_ASSERTD(expr): assertions that should be checked when testing */
391#ifdef WANT_ASSERT
392# define MPFR_EXP_CHECK 1
393# define MPFR_ASSERTD(expr)  MPFR_ASSERTN (expr)
394#else
395# define MPFR_ASSERTD(expr)  ((void) 0)
396#endif
397
398/* Code to deal with impossible
399   WARNING: It doesn't use do { } while (0) for Insure++*/
400#define MPFR_RET_NEVER_GO_HERE()  {MPFR_ASSERTN(0); return 0;}
401
402
403/******************************************************
404 ******************** Warnings ************************
405 ******************************************************/
406
407/* MPFR_WARNING is no longer useful, but let's keep the macro in case
408   it needs to be used again in the future. */
409
410#ifdef MPFR_USE_WARNINGS
411# include <stdlib.h>
412# define MPFR_WARNING(W)                    \
413  do                                        \
414    {                                       \
415      char *q = getenv ("MPFR_QUIET");      \
416      if (q == NULL || *q == 0)             \
417        fprintf (stderr, "MPFR: %s\n", W);  \
418    }                                       \
419  while (0)
420#else
421# define MPFR_WARNING(W)  ((void) 0)
422#endif
423
424
425/******************************************************
426 ****************** double macros *********************
427 ******************************************************/
428
429/* Precision used for lower precision computations */
430#define MPFR_SMALL_PRECISION 32
431
432/* Definition of constants */
433#define LOG2 0.69314718055994528622 /* log(2) rounded to zero on 53 bits */
434#define ALPHA 4.3191365662914471407 /* a+2 = a*log(a), rounded to +infinity */
435#define EXPM1 0.36787944117144227851 /* exp(-1), rounded to zero */
436
437/* MPFR_DOUBLE_SPEC = 1 if the C type 'double' corresponds to IEEE-754
438   double precision, 0 if it doesn't, and undefined if one doesn't know.
439   On all the tested machines, MPFR_DOUBLE_SPEC = 1. To have this macro
440   defined here, #include <float.h> is needed. If need be, other values
441   could be defined for other specs (once they are known). */
442#if !defined(MPFR_DOUBLE_SPEC) && defined(FLT_RADIX) && \
443    defined(DBL_MANT_DIG) && defined(DBL_MIN_EXP) && defined(DBL_MAX_EXP)
444# if FLT_RADIX == 2 && DBL_MANT_DIG == 53 && \
445     DBL_MIN_EXP == -1021 && DBL_MAX_EXP == 1024
446#  define MPFR_DOUBLE_SPEC 1
447# else
448#  define MPFR_DOUBLE_SPEC 0
449# endif
450#endif
451
452/* Debug non IEEE floats */
453#ifdef XDEBUG
454# undef _GMP_IEEE_FLOATS
455#endif
456#ifndef _GMP_IEEE_FLOATS
457# define _GMP_IEEE_FLOATS 0
458#endif
459
460#ifndef IEEE_DBL_MANT_DIG
461#define IEEE_DBL_MANT_DIG 53
462#endif
463#define MPFR_LIMBS_PER_DOUBLE ((IEEE_DBL_MANT_DIG-1)/GMP_NUMB_BITS+1)
464
465#ifndef IEEE_FLT_MANT_DIG
466#define IEEE_FLT_MANT_DIG 24
467#endif
468#define MPFR_LIMBS_PER_FLT ((IEEE_FLT_MANT_DIG-1)/GMP_NUMB_BITS+1)
469
470/* Visual C++ doesn't support +1.0/0.0, -1.0/0.0 and 0.0/0.0
471   at compile time. */
472#if defined(_MSC_VER) && defined(_WIN32) && (_MSC_VER >= 1200)
473static double double_zero = 0.0;
474# define DBL_NAN (double_zero/double_zero)
475# define DBL_POS_INF ((double) 1.0/double_zero)
476# define DBL_NEG_INF ((double)-1.0/double_zero)
477# define DBL_NEG_ZERO (-double_zero)
478#else
479# define DBL_POS_INF ((double) 1.0/0.0)
480# define DBL_NEG_INF ((double)-1.0/0.0)
481# define DBL_NAN     ((double) 0.0/0.0)
482# define DBL_NEG_ZERO (-0.0)
483#endif
484
485/* Note: In the past, there was specific code for _GMP_IEEE_FLOATS, which
486   was based on NaN and Inf memory representations. This code was breaking
487   the aliasing rules (see ISO C99, 6.5#6 and 6.5#7 on the effective type)
488   and for this reason it did not behave correctly with GCC 4.5.0 20091119.
489   The code needed a memory transfer and was probably not better than the
490   macros below with a good compiler (a fix based on the NaN / Inf memory
491   representation would be even worse due to C limitations), and this code
492   could be selected only when MPFR was built with --with-gmp-build, thus
493   introducing a difference (bad for maintaining/testing MPFR); therefore
494   it has been removed. The old code required that the argument x be an
495   lvalue of type double. We still require that, in case one would need
496   to change the macros below, e.g. for some broken compiler. But the
497   LVALUE(x) condition could be removed if really necessary. */
498/* Below, the &(x) == &(x) || &(x) != &(x) allows to make sure that x
499   is a lvalue without (probably) any warning from the compiler.  The
500   &(x) != &(x) is needed to avoid a failure under Mac OS X 10.4.11
501   (with Xcode 2.4.1, i.e. the latest one). */
502#define LVALUE(x) (&(x) == &(x) || &(x) != &(x))
503#define DOUBLE_ISINF(x) (LVALUE(x) && ((x) > DBL_MAX || (x) < -DBL_MAX))
504#ifdef MPFR_NANISNAN
505/* Avoid MIPSpro / IRIX64 / gcc -ffast-math (incorrect) optimizations.
506   The + must not be replaced by a ||. With gcc -ffast-math, NaN is
507   regarded as a positive number or something like that; the second
508   test catches this case. */
509# define DOUBLE_ISNAN(x) \
510    (LVALUE(x) && !((((x) >= 0.0) + ((x) <= 0.0)) && -(x)*(x) <= 0.0))
511#else
512# define DOUBLE_ISNAN(x) (LVALUE(x) && (x) != (x))
513#endif
514
515/******************************************************
516 *************** Long double macros *******************
517 ******************************************************/
518
519/* We try to get the exact value of the precision of long double
520   (provided by the implementation) in order to provide correct
521   rounding in this case (not guaranteed if the C implementation
522   does not have an adequate long double arithmetic). Note that
523   it may be lower than the precision of some numbers that can
524   be represented in a long double; e.g. on FreeBSD/x86, it is
525   53 because the processor is configured to round in double
526   precision (even when using the long double type -- this is a
527   limitation of the x87 arithmetic), and on Mac OS X, it is 106
528   because the implementation is a double-double arithmetic.
529   Otherwise (e.g. in base 10), we get an upper bound of the
530   precision, and correct rounding isn't currently provided.
531*/
532#if defined(LDBL_MANT_DIG) && FLT_RADIX == 2
533# define MPFR_LDBL_MANT_DIG LDBL_MANT_DIG
534#else
535# define MPFR_LDBL_MANT_DIG \
536  (sizeof(long double)*GMP_NUMB_BITS/sizeof(mp_limb_t))
537#endif
538#define MPFR_LIMBS_PER_LONG_DOUBLE \
539  ((sizeof(long double)-1)/sizeof(mp_limb_t)+1)
540
541/* LONGDOUBLE_NAN_ACTION executes the code "action" if x is a NaN. */
542
543/* On hppa2.0n-hp-hpux10 with the unbundled HP cc, the test x!=x on a NaN
544   has been seen false, meaning NaNs are not detected.  This seemed to
545   happen only after other comparisons, not sure what's really going on.  In
546   any case we can pick apart the bytes to identify a NaN.  */
547#ifdef HAVE_LDOUBLE_IEEE_QUAD_BIG
548# define LONGDOUBLE_NAN_ACTION(x, action)                       \
549  do {                                                          \
550    union {                                                     \
551      long double    ld;                                        \
552      struct {                                                  \
553        unsigned int sign : 1;                                  \
554        unsigned int exp  : 15;                                 \
555        unsigned int man3 : 16;                                 \
556        unsigned int man2 : 32;                                 \
557        unsigned int man1 : 32;                                 \
558        unsigned int man0 : 32;                                 \
559      } s;                                                      \
560    } u;                                                        \
561    u.ld = (x);                                                 \
562    if (u.s.exp == 0x7FFFL                                      \
563        && (u.s.man0 | u.s.man1 | u.s.man2 | u.s.man3) != 0)    \
564      { action; }                                               \
565  } while (0)
566#endif
567
568#ifdef HAVE_LDOUBLE_IEEE_QUAD_LITTLE
569# define LONGDOUBLE_NAN_ACTION(x, action)                       \
570  do {                                                          \
571    union {                                                     \
572      long double    ld;                                        \
573      struct {                                                  \
574        unsigned int man0 : 32;                                 \
575        unsigned int man1 : 32;                                 \
576        unsigned int man2 : 32;                                 \
577        unsigned int man3 : 16;                                 \
578        unsigned int exp  : 15;                                 \
579        unsigned int sign : 1;                                  \
580      } s;                                                      \
581    } u;                                                        \
582    u.ld = (x);                                                 \
583    if (u.s.exp == 0x7FFFL                                      \
584        && (u.s.man0 | u.s.man1 | u.s.man2 | u.s.man3) != 0)    \
585      { action; }                                               \
586  } while (0)
587#endif
588
589/* Under IEEE rules, NaN is not equal to anything, including itself.
590   "volatile" here stops "cc" on mips64-sgi-irix6.5 from optimizing away
591   x!=x. */
592#ifndef LONGDOUBLE_NAN_ACTION
593# define LONGDOUBLE_NAN_ACTION(x, action)               \
594  do {                                                  \
595    volatile long double __x = LONGDOUBLE_VOLATILE (x); \
596    if ((x) != __x)                                     \
597      { action; }                                       \
598  } while (0)
599# define WANT_LONGDOUBLE_VOLATILE 1
600#endif
601
602/* If we don't have a proper "volatile" then volatile is #defined to empty,
603   in this case call through an external function to stop the compiler
604   optimizing anything. */
605#ifdef WANT_LONGDOUBLE_VOLATILE
606# ifdef volatile
607__MPFR_DECLSPEC long double __gmpfr_longdouble_volatile _MPFR_PROTO ((long double)) MPFR_CONST_ATTR;
608#  define LONGDOUBLE_VOLATILE(x)  (__gmpfr_longdouble_volatile (x))
609#  define WANT_GMPFR_LONGDOUBLE_VOLATILE 1
610# else
611#  define LONGDOUBLE_VOLATILE(x)  (x)
612# endif
613#endif
614
615/* Some special case for IEEE_EXT Litle Endian */
616#if HAVE_LDOUBLE_IEEE_EXT_LITTLE
617
618typedef union {
619  long double    ld;
620  struct {
621    unsigned int manl : 32;
622    unsigned int manh : 32;
623    unsigned int expl : 8 ;
624    unsigned int exph : 7;
625    unsigned int sign : 1;
626  } s;
627} mpfr_long_double_t;
628
629/* #undef MPFR_LDBL_MANT_DIG */
630#undef MPFR_LIMBS_PER_LONG_DOUBLE
631/* #define MPFR_LDBL_MANT_DIG   64 */
632#define MPFR_LIMBS_PER_LONG_DOUBLE ((64-1)/GMP_NUMB_BITS+1)
633
634#endif
635
636/******************************************************
637 *************** _Decimal64 support *******************
638 ******************************************************/
639
640#ifdef MPFR_WANT_DECIMAL_FLOATS
641/* to cast between binary64 and decimal64 */
642union ieee_double_decimal64 { double d; _Decimal64 d64; };
643#endif
644
645/******************************************************
646 **************** mpfr_t properties *******************
647 ******************************************************/
648
649/* In the following macro, p is usually a mpfr_prec_t, but this macro
650   works with other integer types (without integer overflow). Checking
651   that p >= 1 in debug mode is useful here because this macro can be
652   used on a computed precision (in particular, this formula does not
653   work for a degenerate case p = 0, and could give different results
654   on different platforms). But let us not use an assertion checking
655   in the MPFR_LAST_LIMB() and MPFR_LIMB_SIZE() macros below to avoid
656   too much expansion for assertions (in practice, this should be a
657   problem just when testing MPFR with the --enable-assert configure
658   option and the -ansi -pedantic-errors gcc compiler flags). */
659#define MPFR_PREC2LIMBS(p) \
660  (MPFR_ASSERTD ((p) >= 1), ((p) - 1) / GMP_NUMB_BITS + 1)
661
662#define MPFR_PREC(x)      ((x)->_mpfr_prec)
663#define MPFR_EXP(x)       ((x)->_mpfr_exp)
664#define MPFR_MANT(x)      ((x)->_mpfr_d)
665#define MPFR_LAST_LIMB(x) ((MPFR_PREC (x) - 1) / GMP_NUMB_BITS)
666#define MPFR_LIMB_SIZE(x) (MPFR_LAST_LIMB (x) + 1)
667
668
669/******************************************************
670 **************** exponent properties *****************
671 ******************************************************/
672
673/* Limits of the mpfr_exp_t type (NOT those of valid exponent values).
674   These macros can be used in preprocessor directives. */
675#if   _MPFR_EXP_FORMAT == 1
676# define MPFR_EXP_MAX (SHRT_MAX)
677# define MPFR_EXP_MIN (SHRT_MIN)
678#elif _MPFR_EXP_FORMAT == 2
679# define MPFR_EXP_MAX (INT_MAX)
680# define MPFR_EXP_MIN (INT_MIN)
681#elif _MPFR_EXP_FORMAT == 3
682# define MPFR_EXP_MAX (LONG_MAX)
683# define MPFR_EXP_MIN (LONG_MIN)
684#elif _MPFR_EXP_FORMAT == 4
685# define MPFR_EXP_MAX (MPFR_INTMAX_MAX)
686# define MPFR_EXP_MIN (MPFR_INTMAX_MIN)
687#else
688# error "Invalid MPFR Exp format"
689#endif
690
691/* Before doing a cast to mpfr_uexp_t, make sure that the value is
692   nonnegative. */
693#define MPFR_UEXP(X) (MPFR_ASSERTD ((X) >= 0), (mpfr_uexp_t) (X))
694
695#if MPFR_EXP_MIN >= LONG_MIN && MPFR_EXP_MAX <= LONG_MAX
696typedef long int mpfr_eexp_t;
697# define mpfr_get_exp_t(x,r) mpfr_get_si((x),(r))
698# define mpfr_set_exp_t(x,e,r) mpfr_set_si((x),(e),(r))
699# define MPFR_EXP_FSPEC "l"
700#elif defined (_MPFR_H_HAVE_INTMAX_T)
701typedef intmax_t mpfr_eexp_t;
702# define mpfr_get_exp_t(x,r) mpfr_get_sj((x),(r))
703# define mpfr_set_exp_t(x,e,r) mpfr_set_sj((x),(e),(r))
704# define MPFR_EXP_FSPEC "j"
705#else
706# error "Cannot define mpfr_get_exp_t and mpfr_set_exp_t"
707#endif
708
709/* Invalid exponent value (to track bugs...) */
710#define MPFR_EXP_INVALID \
711 ((mpfr_exp_t) 1 << (GMP_NUMB_BITS*sizeof(mpfr_exp_t)/sizeof(mp_limb_t)-2))
712
713/* Definition of the exponent limits for MPFR numbers.
714 * These limits are chosen so that if e is such an exponent, then 2e-1 and
715 * 2e+1 are representable. This is useful for intermediate computations,
716 * in particular the multiplication.
717 */
718#undef MPFR_EMIN_MIN
719#undef MPFR_EMIN_MAX
720#undef MPFR_EMAX_MIN
721#undef MPFR_EMAX_MAX
722#define MPFR_EMIN_MIN (1-MPFR_EXP_INVALID)
723#define MPFR_EMIN_MAX (MPFR_EXP_INVALID-1)
724#define MPFR_EMAX_MIN (1-MPFR_EXP_INVALID)
725#define MPFR_EMAX_MAX (MPFR_EXP_INVALID-1)
726
727/* Use MPFR_GET_EXP and MPFR_SET_EXP instead of MPFR_EXP directly,
728   unless when the exponent may be out-of-range, for instance when
729   setting the exponent before calling mpfr_check_range.
730   MPFR_EXP_CHECK is defined when WANT_ASSERT is defined, but if you
731   don't use WANT_ASSERT (for speed reasons), you can still define
732   MPFR_EXP_CHECK by setting -DMPFR_EXP_CHECK in $CFLAGS. */
733
734#ifdef MPFR_EXP_CHECK
735# define MPFR_GET_EXP(x)          (mpfr_get_exp) (x)
736# define MPFR_SET_EXP(x, exp)     MPFR_ASSERTN (!mpfr_set_exp ((x), (exp)))
737# define MPFR_SET_INVALID_EXP(x)  ((void) (MPFR_EXP (x) = MPFR_EXP_INVALID))
738#else
739# define MPFR_GET_EXP(x)          MPFR_EXP (x)
740# define MPFR_SET_EXP(x, exp)     ((void) (MPFR_EXP (x) = (exp)))
741# define MPFR_SET_INVALID_EXP(x)  ((void) 0)
742#endif
743
744
745
746/******************************************************
747 ********** Singular Values (NAN, INF, ZERO) **********
748 ******************************************************/
749
750/* Enum special value of exponent. */
751# define MPFR_EXP_ZERO (MPFR_EXP_MIN+1)
752# define MPFR_EXP_NAN  (MPFR_EXP_MIN+2)
753# define MPFR_EXP_INF  (MPFR_EXP_MIN+3)
754
755#define MPFR_IS_NAN(x)   (MPFR_EXP(x) == MPFR_EXP_NAN)
756#define MPFR_SET_NAN(x)  (MPFR_EXP(x) =  MPFR_EXP_NAN)
757#define MPFR_IS_INF(x)   (MPFR_EXP(x) == MPFR_EXP_INF)
758#define MPFR_SET_INF(x)  (MPFR_EXP(x) =  MPFR_EXP_INF)
759#define MPFR_IS_ZERO(x)  (MPFR_EXP(x) == MPFR_EXP_ZERO)
760#define MPFR_SET_ZERO(x) (MPFR_EXP(x) =  MPFR_EXP_ZERO)
761#define MPFR_NOTZERO(x)  (MPFR_EXP(x) != MPFR_EXP_ZERO)
762
763#define MPFR_IS_FP(x)       (!MPFR_IS_NAN(x) && !MPFR_IS_INF(x))
764#define MPFR_IS_SINGULAR(x) (MPFR_EXP(x) <= MPFR_EXP_INF)
765#define MPFR_IS_PURE_FP(x)  (!MPFR_IS_SINGULAR(x) && \
766  (MPFR_ASSERTD ((MPFR_MANT(x)[MPFR_LAST_LIMB(x)]  \
767                  & MPFR_LIMB_HIGHBIT) != 0), 1))
768
769#define MPFR_ARE_SINGULAR(x,y) \
770  (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)) || MPFR_UNLIKELY(MPFR_IS_SINGULAR(y)))
771
772#define MPFR_IS_POWER_OF_2(x) \
773  (mpfr_cmp_ui_2exp ((x), 1, MPFR_GET_EXP (x) - 1) == 0)
774
775
776/******************************************************
777 ********************* Sign Macros ********************
778 ******************************************************/
779
780#define MPFR_SIGN_POS (1)
781#define MPFR_SIGN_NEG (-1)
782
783#define MPFR_IS_STRICTPOS(x)  (MPFR_NOTZERO((x)) && MPFR_SIGN(x) > 0)
784#define MPFR_IS_STRICTNEG(x)  (MPFR_NOTZERO((x)) && MPFR_SIGN(x) < 0)
785
786#define MPFR_IS_NEG(x) (MPFR_SIGN(x) < 0)
787#define MPFR_IS_POS(x) (MPFR_SIGN(x) > 0)
788
789#define MPFR_SET_POS(x) (MPFR_SIGN(x) = MPFR_SIGN_POS)
790#define MPFR_SET_NEG(x) (MPFR_SIGN(x) = MPFR_SIGN_NEG)
791
792#define MPFR_CHANGE_SIGN(x) (MPFR_SIGN(x) = -MPFR_SIGN(x))
793#define MPFR_SET_SAME_SIGN(x, y) (MPFR_SIGN(x) = MPFR_SIGN(y))
794#define MPFR_SET_OPPOSITE_SIGN(x, y) (MPFR_SIGN(x) = -MPFR_SIGN(y))
795#define MPFR_ASSERT_SIGN(s) \
796 (MPFR_ASSERTD((s) == MPFR_SIGN_POS || (s) == MPFR_SIGN_NEG))
797#define MPFR_SET_SIGN(x, s) \
798  (MPFR_ASSERT_SIGN(s), MPFR_SIGN(x) = s)
799#define MPFR_IS_POS_SIGN(s1) (s1 > 0)
800#define MPFR_IS_NEG_SIGN(s1) (s1 < 0)
801#define MPFR_MULT_SIGN(s1, s2) ((s1) * (s2))
802/* Transform a sign to 1 or -1 */
803#define MPFR_FROM_SIGN_TO_INT(s) (s)
804#define MPFR_INT_SIGN(x) MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(x))
805
806
807
808/******************************************************
809 ***************** Ternary Value Macros ***************
810 ******************************************************/
811
812/* Special inexact value */
813#define MPFR_EVEN_INEX 2
814
815/* Macros for functions returning two inexact values in an 'int' */
816#define INEXPOS(y) ((y) == 0 ? 0 : (((y) > 0) ? 1 : 2))
817#define INEX(y,z) (INEXPOS(y) | (INEXPOS(z) << 2))
818
819/* When returning the ternary inexact value, ALWAYS use one of the
820   following two macros, unless the flag comes from another function
821   returning the ternary inexact value */
822#define MPFR_RET(I) return \
823  (I) ? ((__gmpfr_flags |= MPFR_FLAGS_INEXACT), (I)) : 0
824#define MPFR_RET_NAN return (__gmpfr_flags |= MPFR_FLAGS_NAN), 0
825
826#define MPFR_SET_ERANGE() (__gmpfr_flags |= MPFR_FLAGS_ERANGE)
827
828#define SIGN(I) ((I) < 0 ? -1 : (I) > 0)
829#define SAME_SIGN(I1,I2) (SIGN (I1) == SIGN (I2))
830
831
832
833/******************************************************
834 ************** Rounding mode macros  *****************
835 ******************************************************/
836
837/* MPFR_RND_MAX gives the number of supported rounding modes by all functions.
838 * Once faithful rounding is implemented, MPFR_RNDA should be changed
839 * to MPFR_RNDF. But this will also require more changes in the tests.
840 */
841#define MPFR_RND_MAX ((mpfr_rnd_t)((MPFR_RNDA)+1))
842
843/* We want to test this :
844 *  (rnd == MPFR_RNDU && test) || (rnd == RNDD && !test)
845 * ie it transforms RNDU or RNDD to Away or Zero according to the sign */
846#define MPFR_IS_RNDUTEST_OR_RNDDNOTTEST(rnd, test) \
847  (((rnd) + (test)) == MPFR_RNDD)
848
849/* We want to test if rnd = Zero, or Away.
850   'test' is 1 if negative, and 0 if positive. */
851#define MPFR_IS_LIKE_RNDZ(rnd, test) \
852  ((rnd==MPFR_RNDZ) || MPFR_IS_RNDUTEST_OR_RNDDNOTTEST (rnd, test))
853
854#define MPFR_IS_LIKE_RNDU(rnd, sign) \
855  ((rnd==MPFR_RNDU) || (rnd==MPFR_RNDZ && sign<0) || (rnd==MPFR_RNDA && sign>0))
856
857#define MPFR_IS_LIKE_RNDD(rnd, sign) \
858  ((rnd==MPFR_RNDD) || (rnd==MPFR_RNDZ && sign>0) || (rnd==MPFR_RNDA && sign<0))
859
860/* Invert a rounding mode, RNDZ and RNDA are unchanged */
861#define MPFR_INVERT_RND(rnd) ((rnd == MPFR_RNDU) ? MPFR_RNDD : \
862                             ((rnd == MPFR_RNDD) ? MPFR_RNDU : rnd))
863
864/* Transform RNDU and RNDD to RNDZ according to test */
865#define MPFR_UPDATE_RND_MODE(rnd, test)                            \
866  do {                                                             \
867    if (MPFR_UNLIKELY(MPFR_IS_RNDUTEST_OR_RNDDNOTTEST(rnd, test))) \
868      rnd = MPFR_RNDZ;                                              \
869  } while (0)
870
871/* Transform RNDU and RNDD to RNDZ or RNDA according to sign,
872   leave the other modes unchanged */
873#define MPFR_UPDATE2_RND_MODE(rnd, sign)        \
874  do {                                          \
875  if (rnd == MPFR_RNDU)                          \
876    rnd = (sign > 0) ? MPFR_RNDA : MPFR_RNDZ;     \
877  else if (rnd == MPFR_RNDD)                     \
878    rnd = (sign < 0) ? MPFR_RNDA : MPFR_RNDZ;     \
879  } while (0)
880
881
882/******************************************************
883 ******************* Limb Macros **********************
884 ******************************************************/
885
886 /* Definition of MPFR_LIMB_HIGHBIT */
887#if defined(GMP_LIMB_HIGHBIT)
888# define MPFR_LIMB_HIGHBIT GMP_LIMB_HIGHBIT
889#elif defined(MP_LIMB_T_HIGHBIT)
890# define MPFR_LIMB_HIGHBIT MP_LIMB_T_HIGHBIT
891#else
892# error "Neither GMP_LIMB_HIGHBIT nor MP_LIMB_T_HIGHBIT defined in GMP"
893#endif
894
895/* Mask to get the Most Significant Bit of a limb */
896#define MPFR_LIMB_MSB(l) ((l)&MPFR_LIMB_HIGHBIT)
897
898/* Definition of MPFR_LIMB_ONE & MPFR_LIMB_ZERO */
899#ifdef CNST_LIMB
900# define MPFR_LIMB_ONE  CNST_LIMB(1)
901# define MPFR_LIMB_ZERO CNST_LIMB(0)
902#else
903# define MPFR_LIMB_ONE  ((mp_limb_t) 1L)
904# define MPFR_LIMB_ZERO ((mp_limb_t) 0L)
905#endif
906
907/* Mask for the low 's' bits of a limb */
908#define MPFR_LIMB_MASK(s) ((MPFR_LIMB_ONE<<(s))-MPFR_LIMB_ONE)
909
910
911
912/******************************************************
913 ********************** Memory ************************
914 ******************************************************/
915
916/* Heap Memory gestion */
917typedef union { mp_size_t s; mp_limb_t l; } mpfr_size_limb_t;
918#define MPFR_GET_ALLOC_SIZE(x) \
919 ( ((mp_size_t*) MPFR_MANT(x))[-1] + 0)
920#define MPFR_SET_ALLOC_SIZE(x, n) \
921 ( ((mp_size_t*) MPFR_MANT(x))[-1] = n)
922#define MPFR_MALLOC_SIZE(s) \
923  ( sizeof(mpfr_size_limb_t) + BYTES_PER_MP_LIMB * ((size_t) s) )
924#define MPFR_SET_MANT_PTR(x,p) \
925   (MPFR_MANT(x) = (mp_limb_t*) ((mpfr_size_limb_t*) p + 1))
926#define MPFR_GET_REAL_PTR(x) \
927   ((mp_limb_t*) ((mpfr_size_limb_t*) MPFR_MANT(x) - 1))
928
929/* Temporary memory gestion */
930#ifndef TMP_SALLOC
931/* GMP 4.1.x or below or internals */
932#define MPFR_TMP_DECL TMP_DECL
933#define MPFR_TMP_MARK TMP_MARK
934#define MPFR_TMP_ALLOC TMP_ALLOC
935#define MPFR_TMP_FREE TMP_FREE
936#else
937#define MPFR_TMP_DECL(x) TMP_DECL
938#define MPFR_TMP_MARK(x) TMP_MARK
939#define MPFR_TMP_ALLOC(s) TMP_ALLOC(s)
940#define MPFR_TMP_FREE(x) TMP_FREE
941#endif
942
943/* This code is experimental: don't use it */
944#ifdef MPFR_USE_OWN_MPFR_TMP_ALLOC
945extern unsigned char *mpfr_stack;
946#undef MPFR_TMP_DECL
947#undef MPFR_TMP_MARK
948#undef MPFR_TMP_ALLOC
949#undef MPFR_TMP_FREE
950#define MPFR_TMP_DECL(_x) unsigned char *(_x)
951#define MPFR_TMP_MARK(_x) ((_x) = mpfr_stack)
952#define MPFR_TMP_ALLOC(_s) (mpfr_stack += (_s), mpfr_stack - (_s))
953#define MPFR_TMP_FREE(_x) (mpfr_stack = (_x))
954#endif
955
956#define MPFR_TMP_LIMBS_ALLOC(N) \
957  ((mp_limb_t *) MPFR_TMP_ALLOC ((size_t) (N) * BYTES_PER_MP_LIMB))
958
959/* temporary allocate 1 limb at xp, and initialize mpfr variable x */
960/* The temporary var doesn't have any size field, but it doesn't matter
961 * since only functions dealing with the Heap care about it */
962#define MPFR_TMP_INIT1(xp, x, p)                                     \
963 ( MPFR_PREC(x) = (p),                                               \
964   MPFR_MANT(x) = (xp),                                              \
965   MPFR_SET_POS(x),                                                  \
966   MPFR_SET_INVALID_EXP(x))
967
968#define MPFR_TMP_INIT(xp, x, p, s)                                   \
969  (xp = MPFR_TMP_LIMBS_ALLOC(s),                                     \
970   MPFR_TMP_INIT1(xp, x, p))
971
972#define MPFR_TMP_INIT_ABS(d, s)                                      \
973 ( MPFR_PREC(d) = MPFR_PREC(s),                                      \
974   MPFR_MANT(d) = MPFR_MANT(s),                                      \
975   MPFR_SET_POS(d),                                                  \
976   MPFR_EXP(d)  = MPFR_EXP(s))
977
978
979
980/******************************************************
981 *****************  Cache macros **********************
982 ******************************************************/
983
984#define mpfr_const_pi(_d,_r)    mpfr_cache(_d, __gmpfr_cache_const_pi,_r)
985#define mpfr_const_log2(_d,_r)  mpfr_cache(_d, __gmpfr_cache_const_log2, _r)
986#define mpfr_const_euler(_d,_r) mpfr_cache(_d, __gmpfr_cache_const_euler, _r)
987#define mpfr_const_catalan(_d,_r) mpfr_cache(_d,__gmpfr_cache_const_catalan,_r)
988
989#define MPFR_DECL_INIT_CACHE(_cache,_func)                           \
990 mpfr_cache_t MPFR_THREAD_ATTR _cache =                              \
991    {{{{0,MPFR_SIGN_POS,0,(mp_limb_t*)0}},0,_func}}
992
993
994
995/******************************************************
996 *******************  Threshold ***********************
997 ******************************************************/
998
999#include "mparam.h"
1000
1001/******************************************************
1002 *****************  Useful macros *********************
1003 ******************************************************/
1004
1005/* Theses macros help the compiler to determine if a test is
1006   likely or unlikely. The !! is necessary in case x is larger
1007   than a long. */
1008#if __MPFR_GNUC(3,0) || __MPFR_ICC(8,1,0)
1009# define MPFR_LIKELY(x) (__builtin_expect(!!(x),1))
1010# define MPFR_UNLIKELY(x) (__builtin_expect(!!(x),0))
1011#else
1012# define MPFR_LIKELY(x) (x)
1013# define MPFR_UNLIKELY(x) (x)
1014#endif
1015
1016/* Declare that some variable is initialized before being used (without a
1017   dummy initialization) in order to avoid some compiler warnings. Use the
1018   VAR = VAR trick (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36296)
1019   only with gcc as this is undefined behavior, and we don't know what
1020   other compilers do (they may also be smarter). This trick could be
1021   disabled with future gcc versions. */
1022#if defined(__GNUC__)
1023# define INITIALIZED(VAR) VAR = VAR
1024#else
1025# define INITIALIZED(VAR) VAR
1026#endif
1027
1028/* Ceil log 2: If GCC, uses a GCC extension, otherwise calls a function */
1029/* Warning:
1030 *   Needs to define MPFR_NEED_LONGLONG.
1031 *   Computes ceil(log2(x)) only for x integer (unsigned long)
1032 *   Undefined if x is 0 */
1033#if __MPFR_GNUC(2,95) || __MPFR_ICC(8,1,0)
1034# define MPFR_INT_CEIL_LOG2(x)                            \
1035    (MPFR_UNLIKELY ((x) == 1) ? 0 :                       \
1036     __extension__ ({ int _b; mp_limb_t _limb;            \
1037      MPFR_ASSERTN ((x) > 1);                             \
1038      _limb = (x) - 1;                                    \
1039      MPFR_ASSERTN (_limb == (x) - 1);                    \
1040      count_leading_zeros (_b, _limb);                    \
1041      (GMP_NUMB_BITS - _b); }))
1042#else
1043# define MPFR_INT_CEIL_LOG2(x) (__gmpfr_int_ceil_log2(x))
1044#endif
1045
1046/* Add two integers with overflow handling */
1047/* Example: MPFR_SADD_OVERFLOW (c, a, b, long, unsigned long,
1048 *                              LONG_MIN, LONG_MAX,
1049 *                              goto overflow, goto underflow); */
1050#define MPFR_UADD_OVERFLOW(c,a,b,ACTION_IF_OVERFLOW)                  \
1051 do {                                                                 \
1052  (c) = (a) + (b);                                                    \
1053  if ((c) < (a)) ACTION_IF_OVERFLOW;                                  \
1054 } while (0)
1055
1056#define MPFR_SADD_OVERFLOW(c,a,b,STYPE,UTYPE,MIN,MAX,ACTION_IF_POS_OVERFLOW,ACTION_IF_NEG_OVERFLOW) \
1057  do {                                                                \
1058  if ((a) >= 0 && (b) >= 0) {                                         \
1059         UTYPE uc,ua,ub;                                              \
1060         ua = (UTYPE) (a); ub = (UTYPE) (b);                          \
1061         MPFR_UADD_OVERFLOW (uc, ua, ub, ACTION_IF_POS_OVERFLOW);     \
1062         if (uc > (UTYPE)(MAX)) ACTION_IF_POS_OVERFLOW;               \
1063         else (c) = (STYPE) uc;                                       \
1064  } else if ((a) < 0 && (b) < 0) {                                    \
1065         UTYPE uc,ua,ub;                                              \
1066         ua = -(UTYPE) (a); ub = -(UTYPE) (b);                        \
1067         MPFR_UADD_OVERFLOW (uc, ua, ub, ACTION_IF_NEG_OVERFLOW);     \
1068         if (uc >= -(UTYPE)(MIN) || uc > (UTYPE)(MAX)) {              \
1069           if (uc ==  -(UTYPE)(MIN)) (c) = (MIN);                     \
1070           else  ACTION_IF_NEG_OVERFLOW; }                            \
1071         else (c) = -(STYPE) uc;                                      \
1072  } else (c) = (a) + (b);                                             \
1073 } while (0)
1074
1075
1076/* Set a number to 1 (Fast) - It doesn't check if 1 is in the exponent range */
1077#define MPFR_SET_ONE(x)                                               \
1078do {                                                                  \
1079  mp_size_t _size = MPFR_LAST_LIMB(x);                                \
1080  MPFR_SET_POS(x);                                                    \
1081  MPFR_EXP(x) = 1;                                                    \
1082  MPN_ZERO ( MPFR_MANT(x), _size);                                    \
1083  MPFR_MANT(x)[_size] = MPFR_LIMB_HIGHBIT;                            \
1084} while (0)
1085
1086/* Compute s = (-a) % GMP_NUMB_BITS as unsigned */
1087#define MPFR_UNSIGNED_MINUS_MODULO(s, a)                              \
1088  do                                                                  \
1089    {                                                                 \
1090      if (IS_POW2 (GMP_NUMB_BITS))                                    \
1091        (s) = (- (unsigned int) (a)) % GMP_NUMB_BITS;                 \
1092      else                                                            \
1093        {                                                             \
1094          (s) = (a) % GMP_NUMB_BITS;                                  \
1095          if ((s) != 0)                                               \
1096            (s) = GMP_NUMB_BITS - (s);                                \
1097        }                                                             \
1098      MPFR_ASSERTD ((s) >= 0 && (s) < GMP_NUMB_BITS);                 \
1099    }                                                                 \
1100  while (0)
1101
1102/* Use it only for debug reasons */
1103/*   MPFR_TRACE (operation) : execute operation iff DEBUG flag is set */
1104/*   MPFR_DUMP (x) : print x (a mpfr_t) on stdout */
1105#ifdef DEBUG
1106# define MPFR_TRACE(x) x
1107#else
1108# define MPFR_TRACE(x) (void) 0
1109#endif
1110#define MPFR_DUMP(x) ( printf(#x"="), mpfr_dump(x) )
1111
1112/* Test if X (positive) is a power of 2 */
1113#define IS_POW2(X) (((X) & ((X) - 1)) == 0)
1114#define NOT_POW2(X) (((X) & ((X) - 1)) != 0)
1115
1116/* Safe absolute value (to avoid possible integer overflow) */
1117/* type is the target (unsigned) type */
1118#define SAFE_ABS(type,x) ((x) >= 0 ? (type)(x) : -(type)(x))
1119
1120#define mpfr_get_d1(x) mpfr_get_d(x,__gmpfr_default_rounding_mode)
1121
1122/* Store in r the size in bits of the mpz_t z */
1123#define MPFR_MPZ_SIZEINBASE2(r, z)              \
1124  do {                                          \
1125   int _cnt;                                    \
1126   mp_size_t _size;                             \
1127   MPFR_ASSERTD (mpz_sgn (z) != 0);             \
1128   _size = ABSIZ(z);                            \
1129   count_leading_zeros (_cnt, PTR(z)[_size-1]); \
1130   (r) = _size * GMP_NUMB_BITS - _cnt;       \
1131  } while (0)
1132
1133/* Needs <locale.h> */
1134#ifdef HAVE_LOCALE_H
1135#include <locale.h>
1136/* Warning! In case of signed char, the value of MPFR_DECIMAL_POINT may
1137   be negative (the ISO C99 does not seem to forbid negative values). */
1138#define MPFR_DECIMAL_POINT (localeconv()->decimal_point[0])
1139#define MPFR_THOUSANDS_SEPARATOR (localeconv()->thousands_sep[0])
1140#else
1141#define MPFR_DECIMAL_POINT ((char) '.')
1142#define MPFR_THOUSANDS_SEPARATOR ('\0')
1143#endif
1144
1145
1146/* Set y to s*significand(x)*2^e, for example MPFR_ALIAS(y,x,1,MPFR_EXP(x))
1147   sets y to |x|, and MPFR_ALIAS(y,x,MPFR_SIGN(x),0) sets y to x*2^f such
1148   that 1/2 <= |y| < 1. Does not check y is in the valid exponent range.
1149   WARNING! x and y share the same mantissa. So, some operations are
1150   not valid if x has been provided via an argument, e.g., trying to
1151   modify the mantissa of y, even temporarily, or calling mpfr_clear on y.
1152*/
1153#define MPFR_ALIAS(y,x,s,e)                     \
1154  do                                            \
1155    {                                           \
1156      MPFR_PREC(y) = MPFR_PREC(x);              \
1157      MPFR_SIGN(y) = (s);                       \
1158      MPFR_EXP(y) = (e);                        \
1159      MPFR_MANT(y) = MPFR_MANT(x);              \
1160    } while (0)
1161
1162
1163/******************************************************
1164 **************  Save exponent macros  ****************
1165 ******************************************************/
1166
1167/* See README.dev for details on how to use the macros.
1168   They are used to set the exponent range to the maximum
1169   temporarily */
1170
1171typedef struct {
1172  unsigned int saved_flags;
1173  mpfr_exp_t saved_emin;
1174  mpfr_exp_t saved_emax;
1175} mpfr_save_expo_t;
1176
1177/* Minimum and maximum exponents of the extended exponent range. */
1178#define MPFR_EXT_EMIN MPFR_EMIN_MIN
1179#define MPFR_EXT_EMAX MPFR_EMAX_MAX
1180
1181#define MPFR_SAVE_EXPO_DECL(x) mpfr_save_expo_t x
1182#define MPFR_SAVE_EXPO_MARK(x)     \
1183 ((x).saved_flags = __gmpfr_flags, \
1184  (x).saved_emin = __gmpfr_emin,   \
1185  (x).saved_emax = __gmpfr_emax,   \
1186  __gmpfr_emin = MPFR_EXT_EMIN,    \
1187  __gmpfr_emax = MPFR_EXT_EMAX)
1188#define MPFR_SAVE_EXPO_FREE(x)     \
1189 (__gmpfr_flags = (x).saved_flags, \
1190  __gmpfr_emin = (x).saved_emin,   \
1191  __gmpfr_emax = (x).saved_emax)
1192#define MPFR_SAVE_EXPO_UPDATE_FLAGS(x, flags)  \
1193  (x).saved_flags |= (flags)
1194
1195/* Speed up final checking */
1196#define mpfr_check_range(x,t,r) \
1197 (MPFR_LIKELY (MPFR_EXP (x) >= __gmpfr_emin && MPFR_EXP (x) <= __gmpfr_emax) \
1198  ? ((t) ? (__gmpfr_flags |= MPFR_FLAGS_INEXACT, (t)) : 0)                   \
1199  : mpfr_check_range(x,t,r))
1200
1201
1202/******************************************************
1203 *****************  Inline Rounding *******************
1204 ******************************************************/
1205
1206/*
1207 * Note: due to the labels, one cannot use a macro MPFR_RNDRAW* more than
1208 * once in a function (otherwise these labels would not be unique).
1209 */
1210
1211/*
1212 * Round mantissa (srcp, sprec) to mpfr_t dest using rounding mode rnd
1213 * assuming dest's sign is sign.
1214 * In rounding to nearest mode, execute MIDDLE_HANDLER when the value
1215 * is the middle of two consecutive numbers in dest precision.
1216 * Execute OVERFLOW_HANDLER in case of overflow when rounding.
1217 */
1218#define MPFR_RNDRAW_GEN(inexact, dest, srcp, sprec, rnd, sign,              \
1219                        MIDDLE_HANDLER, OVERFLOW_HANDLER)                   \
1220  do {                                                                      \
1221    mp_size_t _dests, _srcs;                                                \
1222    mp_limb_t *_destp;                                                      \
1223    mpfr_prec_t _destprec, _srcprec;                                        \
1224                                                                            \
1225    /* Check Trivial Case when Dest Mantissa has more bits than source */   \
1226    _srcprec = (sprec);                                                     \
1227    _destprec = MPFR_PREC (dest);                                           \
1228    _destp = MPFR_MANT (dest);                                              \
1229    if (MPFR_UNLIKELY (_destprec >= _srcprec))                              \
1230      {                                                                     \
1231        _srcs  = MPFR_PREC2LIMBS (_srcprec);                                \
1232        _dests = MPFR_PREC2LIMBS (_destprec) - _srcs;                       \
1233        MPN_COPY (_destp + _dests, srcp, _srcs);                            \
1234        MPN_ZERO (_destp, _dests);                                          \
1235        inexact = 0;                                                        \
1236      }                                                                     \
1237    else                                                                    \
1238      {                                                                     \
1239        /* Non trivial case: rounding needed */                             \
1240        mpfr_prec_t _sh;                                                    \
1241        mp_limb_t *_sp;                                                     \
1242        mp_limb_t _rb, _sb, _ulp;                                           \
1243                                                                            \
1244        /* Compute Position and shift */                                    \
1245        _srcs  = MPFR_PREC2LIMBS (_srcprec);                                \
1246        _dests = MPFR_PREC2LIMBS (_destprec);                               \
1247        MPFR_UNSIGNED_MINUS_MODULO (_sh, _destprec);                        \
1248        _sp = (srcp) + _srcs - _dests;                                      \
1249                                                                            \
1250        /* General case when prec % GMP_NUMB_BITS != 0 */                   \
1251        if (MPFR_LIKELY (_sh != 0))                                         \
1252          {                                                                 \
1253            mp_limb_t _mask;                                                \
1254            /* Compute Rounding Bit and Sticky Bit */                       \
1255            /* Note: in directed rounding modes, if the rounding bit */     \
1256            /* is 1, the behavior does not depend on the sticky bit; */     \
1257            /* thus we will not try to compute it in this case (this */     \
1258            /* can be much faster and avoids to read uninitialized   */     \
1259            /* data in the current mpfr_mul implementation). We just */     \
1260            /* make sure that _sb is initialized.                    */     \
1261            _mask = MPFR_LIMB_ONE << (_sh - 1);                             \
1262            _rb = _sp[0] & _mask;                                           \
1263            _sb = _sp[0] & (_mask - 1);                                     \
1264            if (MPFR_UNLIKELY (_sb == 0) &&                                 \
1265                ((rnd) == MPFR_RNDN || _rb == 0))                           \
1266              { /* TODO: Improve it */                                      \
1267                mp_limb_t *_tmp;                                            \
1268                mp_size_t _n;                                               \
1269                for (_tmp = _sp, _n = _srcs - _dests ;                      \
1270                     _n != 0 && _sb == 0 ; _n--)                            \
1271                  _sb = *--_tmp;                                            \
1272              }                                                             \
1273            _ulp = 2 * _mask;                                               \
1274          }                                                                 \
1275        else /* _sh == 0 */                                                 \
1276          {                                                                 \
1277            MPFR_ASSERTD (_dests < _srcs);                                  \
1278            /* Compute Rounding Bit and Sticky Bit - see note above */      \
1279            _rb = _sp[-1] & MPFR_LIMB_HIGHBIT;                              \
1280            _sb = _sp[-1] & (MPFR_LIMB_HIGHBIT-1);                          \
1281            if (MPFR_UNLIKELY (_sb == 0) &&                                 \
1282                ((rnd) == MPFR_RNDN || _rb == 0))                           \
1283              {                                                             \
1284                mp_limb_t *_tmp;                                            \
1285                mp_size_t _n;                                               \
1286                for (_tmp = _sp - 1, _n = _srcs - _dests - 1 ;              \
1287                     _n != 0 && _sb == 0 ; _n--)                            \
1288                  _sb = *--_tmp;                                            \
1289              }                                                             \
1290            _ulp = MPFR_LIMB_ONE;                                           \
1291          }                                                                 \
1292        /* Rounding */                                                      \
1293        if (MPFR_LIKELY (rnd == MPFR_RNDN))                                 \
1294          {                                                                 \
1295            if (_rb == 0)                                                   \
1296              {                                                             \
1297              trunc:                                                        \
1298                inexact = MPFR_LIKELY ((_sb | _rb) != 0) ? -sign : 0;       \
1299              trunc_doit:                                                   \
1300                MPN_COPY (_destp, _sp, _dests);                             \
1301                _destp[0] &= ~(_ulp - 1);                                   \
1302              }                                                             \
1303            else if (MPFR_UNLIKELY (_sb == 0))                              \
1304              { /* Middle of two consecutive representable numbers */       \
1305                MIDDLE_HANDLER;                                             \
1306              }                                                             \
1307            else                                                            \
1308              {                                                             \
1309                if (0)                                                      \
1310                  goto addoneulp_doit; /* dummy code to avoid warning */    \
1311              addoneulp:                                                    \
1312                inexact = sign;                                             \
1313              addoneulp_doit:                                               \
1314                if (MPFR_UNLIKELY (mpn_add_1 (_destp, _sp, _dests, _ulp)))  \
1315                  {                                                         \
1316                    _destp[_dests - 1] = MPFR_LIMB_HIGHBIT;                 \
1317                    OVERFLOW_HANDLER;                                       \
1318                  }                                                         \
1319                _destp[0] &= ~(_ulp - 1);                                   \
1320              }                                                             \
1321          }                                                                 \
1322        else                                                                \
1323          { /* Directed rounding mode */                                    \
1324            if (MPFR_LIKELY (MPFR_IS_LIKE_RNDZ (rnd,                        \
1325                                                MPFR_IS_NEG_SIGN (sign))))  \
1326              goto trunc;                                                   \
1327             else if (MPFR_UNLIKELY ((_sb | _rb) == 0))                     \
1328               {                                                            \
1329                 inexact = 0;                                               \
1330                 goto trunc_doit;                                           \
1331               }                                                            \
1332             else                                                           \
1333              goto addoneulp;                                               \
1334          }                                                                 \
1335      }                                                                     \
1336  } while (0)
1337
1338/*
1339 * Round mantissa (srcp, sprec) to mpfr_t dest using rounding mode rnd
1340 * assuming dest's sign is sign.
1341 * Execute OVERFLOW_HANDLER in case of overflow when rounding.
1342 */
1343#define MPFR_RNDRAW(inexact, dest, srcp, sprec, rnd, sign, OVERFLOW_HANDLER) \
1344   MPFR_RNDRAW_GEN (inexact, dest, srcp, sprec, rnd, sign,                   \
1345     if ((_sp[0] & _ulp) == 0)                                               \
1346       {                                                                     \
1347         inexact = -sign;                                                    \
1348         goto trunc_doit;                                                    \
1349       }                                                                     \
1350     else                                                                    \
1351       goto addoneulp;                                                       \
1352     , OVERFLOW_HANDLER)
1353
1354/*
1355 * Round mantissa (srcp, sprec) to mpfr_t dest using rounding mode rnd
1356 * assuming dest's sign is sign.
1357 * Execute OVERFLOW_HANDLER in case of overflow when rounding.
1358 * Set inexact to +/- MPFR_EVEN_INEX in case of even rounding.
1359 */
1360#define MPFR_RNDRAW_EVEN(inexact, dest, srcp, sprec, rnd, sign, \
1361                         OVERFLOW_HANDLER)                      \
1362   MPFR_RNDRAW_GEN (inexact, dest, srcp, sprec, rnd, sign,      \
1363     if ((_sp[0] & _ulp) == 0)                                  \
1364       {                                                        \
1365         inexact = -MPFR_EVEN_INEX * sign;                      \
1366         goto trunc_doit;                                       \
1367       }                                                        \
1368     else                                                       \
1369       {                                                        \
1370         inexact = MPFR_EVEN_INEX * sign;                       \
1371         goto addoneulp_doit;                                   \
1372       }                                                        \
1373     , OVERFLOW_HANDLER)
1374
1375/* Return TRUE if b is non singular and we can round it to precision 'prec'
1376   and determine the ternary value, with rounding mode 'rnd', and with
1377   error at most 'error' */
1378#define MPFR_CAN_ROUND(b,err,prec,rnd)                                       \
1379 (!MPFR_IS_SINGULAR (b) && mpfr_round_p (MPFR_MANT (b), MPFR_LIMB_SIZE (b),  \
1380                                         (err), (prec) + ((rnd)==MPFR_RNDN)))
1381
1382/* Copy the sign and the significand, and handle the exponent in exp. */
1383#define MPFR_SETRAW(inexact,dest,src,exp,rnd)                           \
1384  if (MPFR_UNLIKELY (dest != src))                                      \
1385    {                                                                   \
1386      MPFR_SET_SIGN (dest, MPFR_SIGN (src));                            \
1387      if (MPFR_LIKELY (MPFR_PREC (dest) == MPFR_PREC (src)))            \
1388        {                                                               \
1389          MPN_COPY (MPFR_MANT (dest), MPFR_MANT (src),                  \
1390                    MPFR_LIMB_SIZE (src));                              \
1391          inexact = 0;                                                  \
1392        }                                                               \
1393      else                                                              \
1394        {                                                               \
1395          MPFR_RNDRAW (inexact, dest, MPFR_MANT (src), MPFR_PREC (src), \
1396                       rnd, MPFR_SIGN (src), exp++);                    \
1397        }                                                               \
1398    }                                                                   \
1399  else                                                                  \
1400    inexact = 0;
1401
1402/* TODO: fix this description (see round_near_x.c). */
1403/* Assuming that the function has a Taylor expansion which looks like:
1404    y=o(f(x)) = o(v + g(x)) with |g(x)| <= 2^(EXP(v)-err)
1405   we can quickly set y to v if x is small (ie err > prec(y)+1) in most
1406   cases. It assumes that f(x) is not representable exactly as a FP number.
1407   v must not be a singular value (NAN, INF or ZERO); usual values are
1408   v=1 or v=x.
1409
1410   y is the destination (a mpfr_t), v the value to set (a mpfr_t),
1411   err1+err2 with err2 <= 3 the error term (mpfr_exp_t's), dir (an int) is
1412   the direction of the committed error (if dir = 0, it rounds toward 0,
1413   if dir=1, it rounds away from 0), rnd the rounding mode.
1414
1415   It returns from the function a ternary value in case of success.
1416   If you want to free something, you must fill the "extra" field
1417   in consequences, otherwise put nothing in it.
1418
1419   The test is less restrictive than necessary, but the function
1420   will finish the check itself.
1421
1422   Note: err1 + err2 is allowed to overflow as mpfr_exp_t, but it must give
1423   its real value as mpfr_uexp_t.
1424*/
1425#define MPFR_FAST_COMPUTE_IF_SMALL_INPUT(y,v,err1,err2,dir,rnd,extra)   \
1426  do {                                                                  \
1427    mpfr_ptr _y = (y);                                                  \
1428    mpfr_exp_t _err1 = (err1);                                          \
1429    mpfr_exp_t _err2 = (err2);                                          \
1430    if (_err1 > 0)                                                      \
1431      {                                                                 \
1432        mpfr_uexp_t _err = (mpfr_uexp_t) _err1 + _err2;                 \
1433        if (MPFR_UNLIKELY (_err > MPFR_PREC (_y) + 1))                  \
1434          {                                                             \
1435            int _inexact = mpfr_round_near_x (_y,(v),_err,(dir),(rnd)); \
1436            if (_inexact != 0)                                          \
1437              {                                                         \
1438                extra;                                                  \
1439                return _inexact;                                        \
1440              }                                                         \
1441          }                                                             \
1442      }                                                                 \
1443  } while (0)
1444
1445/* Variant, to be called somewhere after MPFR_SAVE_EXPO_MARK. This variant
1446   is needed when there are some computations before or when some non-zero
1447   real constant is used, such as __gmpfr_one for mpfr_cos. */
1448#define MPFR_SMALL_INPUT_AFTER_SAVE_EXPO(y,v,err1,err2,dir,rnd,expo,extra) \
1449  do {                                                                  \
1450    mpfr_ptr _y = (y);                                                  \
1451    mpfr_exp_t _err1 = (err1);                                          \
1452    mpfr_exp_t _err2 = (err2);                                          \
1453    if (_err1 > 0)                                                      \
1454      {                                                                 \
1455        mpfr_uexp_t _err = (mpfr_uexp_t) _err1 + _err2;                 \
1456        if (MPFR_UNLIKELY (_err > MPFR_PREC (_y) + 1))                  \
1457          {                                                             \
1458            int _inexact;                                               \
1459            mpfr_clear_flags ();                                        \
1460            _inexact = mpfr_round_near_x (_y,(v),_err,(dir),(rnd));     \
1461            if (_inexact != 0)                                          \
1462              {                                                         \
1463                extra;                                                  \
1464                MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);      \
1465                MPFR_SAVE_EXPO_FREE (expo);                             \
1466                return mpfr_check_range (_y, _inexact, (rnd));          \
1467              }                                                         \
1468          }                                                             \
1469      }                                                                 \
1470  } while (0)
1471
1472/******************************************************
1473 ***************  Ziv Loop Macro  *********************
1474 ******************************************************/
1475
1476#ifndef MPFR_USE_LOGGING
1477
1478#define MPFR_ZIV_DECL(_x) mpfr_prec_t _x
1479#define MPFR_ZIV_INIT(_x, _p) (_x) = GMP_NUMB_BITS
1480#define MPFR_ZIV_NEXT(_x, _p) ((_p) += (_x), (_x) = (_p)/2)
1481#define MPFR_ZIV_FREE(x)
1482
1483#else
1484
1485/* The following test on glibc is there mainly for Darwin (Mac OS X), to
1486   obtain a better error message. The real test should have been a test
1487   concerning nested functions in gcc, which are disabled by default on
1488   Darwin; but it is not possible to do that without a configure test. */
1489# if defined (__cplusplus) || !(__MPFR_GNUC(3,0) && __MPFR_GLIBC(2,0))
1490#  error "Logging not supported (needs gcc >= 3.0 and GNU C Library >= 2.0)."
1491# endif
1492
1493/* Use LOGGING */
1494
1495/* Note: the mpfr_log_level >= 0 below avoids to take into account
1496   Ziv loops used by the MPFR functions called by the mpfr_fprintf
1497   in LOG_PRINT. */
1498
1499#define MPFR_ZIV_DECL(_x)                                               \
1500  mpfr_prec_t _x;                                                       \
1501  int _x ## _cpt = 1;                                                   \
1502  static unsigned long  _x ## _loop = 0, _x ## _bad = 0;                \
1503  static const char *_x ## _fname = __func__;                           \
1504  auto void __attribute__ ((destructor)) x ## _f  (void);               \
1505  void __attribute__ ((destructor)) x ## _f  (void) {                   \
1506    if (_x ## _loop != 0 && (MPFR_LOG_STAT_F & mpfr_log_type))          \
1507      fprintf (mpfr_log_file,                                           \
1508               "%s: Ziv failed %2.2f%% (%lu bad cases / %lu calls)\n",  \
1509               _x ## _fname, (double) 100.0 * _x ## _bad / _x ## _loop, \
1510               _x ## _bad, _x ## _loop ); }
1511
1512#define MPFR_ZIV_INIT(_x, _p)                                           \
1513  do                                                                    \
1514    {                                                                   \
1515      (_x) = GMP_NUMB_BITS;                                             \
1516      if (mpfr_log_level >= 0)                                          \
1517        _x ## _loop ++;                                                 \
1518      if ((MPFR_LOG_BADCASE_F & mpfr_log_type) &&                       \
1519          (mpfr_log_current <= mpfr_log_level))                         \
1520        LOG_PRINT ("%s:ZIV 1st prec=%Pd\n",                             \
1521                   __func__, (mpfr_prec_t) (_p));                       \
1522    }                                                                   \
1523  while (0)
1524
1525#define MPFR_ZIV_NEXT(_x, _p)                                           \
1526  do                                                                    \
1527    {                                                                   \
1528      (_p) += (_x);                                                     \
1529      (_x) = (_p) / 2;                                                  \
1530      if (mpfr_log_level >= 0)                                          \
1531        _x ## _bad += (_x ## _cpt == 1);                                \
1532      _x ## _cpt ++;                                                    \
1533      if ((MPFR_LOG_BADCASE_F & mpfr_log_type) &&                       \
1534          (mpfr_log_current <= mpfr_log_level))                         \
1535        LOG_PRINT ("%s:ZIV new prec=%Pd\n",                             \
1536                   __func__, (mpfr_prec_t) (_p));                       \
1537    }                                                                   \
1538  while (0)
1539
1540#define MPFR_ZIV_FREE(_x)                                               \
1541  do                                                                    \
1542    {                                                                   \
1543      if ((MPFR_LOG_BADCASE_F & mpfr_log_type) && _x ## _cpt > 1 &&     \
1544          (mpfr_log_current <= mpfr_log_level))                         \
1545        fprintf (mpfr_log_file, "%s:ZIV %d loops\n",                    \
1546                 __func__, _x ## _cpt);                                 \
1547    }                                                                   \
1548  while (0)
1549
1550#endif
1551
1552
1553/******************************************************
1554 ***************  Logging Macros  *********************
1555 ******************************************************/
1556
1557/* The different kind of LOG */
1558#define MPFR_LOG_INPUT_F    1
1559#define MPFR_LOG_OUTPUT_F   2
1560#define MPFR_LOG_INTERNAL_F 4
1561#define MPFR_LOG_TIME_F     8
1562#define MPFR_LOG_BADCASE_F  16
1563#define MPFR_LOG_MSG_F      32
1564#define MPFR_LOG_STAT_F     64
1565
1566#ifdef MPFR_USE_LOGGING
1567
1568/* Check if we can support this feature */
1569# ifdef MPFR_USE_THREAD_SAFE
1570#  error "Enable either `Logging' or `thread-safe', not both"
1571# endif
1572# if !__MPFR_GNUC(3,0)
1573#  error "Logging not supported (GCC >= 3.0)"
1574# endif
1575
1576#if defined (__cplusplus)
1577extern "C" {
1578#endif
1579
1580__MPFR_DECLSPEC extern FILE *mpfr_log_file;
1581__MPFR_DECLSPEC extern int   mpfr_log_type;
1582__MPFR_DECLSPEC extern int   mpfr_log_level;
1583__MPFR_DECLSPEC extern int   mpfr_log_current;
1584__MPFR_DECLSPEC extern mpfr_prec_t mpfr_log_prec;
1585
1586#if defined (__cplusplus)
1587 }
1588#endif
1589
1590/* LOG_PRINT calls mpfr_fprintf on mpfr_log_file with logging disabled
1591   (recursive logging is not wanted and freezes MPFR). */
1592#define LOG_PRINT(format, ...)                                          \
1593  do                                                                    \
1594    {                                                                   \
1595      int old_level = mpfr_log_level;                                   \
1596      mpfr_log_level = -1;  /* disable logging in mpfr_fprintf */       \
1597      __gmpfr_cache_const_pi = __gmpfr_logging_pi;                      \
1598      __gmpfr_cache_const_log2 = __gmpfr_logging_log2;                  \
1599      mpfr_fprintf (mpfr_log_file, format, __VA_ARGS__);                \
1600      mpfr_log_level = old_level;                                       \
1601      __gmpfr_cache_const_pi = __gmpfr_normal_pi;                       \
1602      __gmpfr_cache_const_log2 = __gmpfr_normal_log2;                   \
1603    }                                                                   \
1604  while (0)
1605
1606#define MPFR_LOG_VAR(x)                                                 \
1607  do                                                                    \
1608    if ((MPFR_LOG_INTERNAL_F & mpfr_log_type) &&                        \
1609        (mpfr_log_current <= mpfr_log_level))                           \
1610      LOG_PRINT ("%s.%d:%s[%#Pu]=%.*Rg\n", __func__, __LINE__,          \
1611                 #x, mpfr_get_prec (x), mpfr_log_prec, x);              \
1612  while (0)
1613
1614#define MPFR_LOG_MSG2(format, ...)                                      \
1615  do                                                                    \
1616    if ((MPFR_LOG_MSG_F & mpfr_log_type) &&                             \
1617        (mpfr_log_current <= mpfr_log_level))                           \
1618      LOG_PRINT ("%s.%d: "format, __func__, __LINE__, __VA_ARGS__);     \
1619  while (0)
1620#define MPFR_LOG_MSG(x) MPFR_LOG_MSG2 x
1621
1622#define MPFR_LOG_BEGIN2(format, ...)                                    \
1623  mpfr_log_current ++;                                                  \
1624  if ((MPFR_LOG_INPUT_F & mpfr_log_type) &&                             \
1625      (mpfr_log_current <= mpfr_log_level))                             \
1626    LOG_PRINT ("%s:IN  "format"\n", __func__, __VA_ARGS__);             \
1627  if ((MPFR_LOG_TIME_F & mpfr_log_type) &&                              \
1628      (mpfr_log_current <= mpfr_log_level))                             \
1629    __gmpfr_log_time = mpfr_get_cputime ();
1630#define MPFR_LOG_BEGIN(x)                                               \
1631  int __gmpfr_log_time = 0;                                             \
1632  MPFR_LOG_BEGIN2 x
1633
1634#define MPFR_LOG_END2(format, ...)                                      \
1635  if ((MPFR_LOG_TIME_F & mpfr_log_type) &&                              \
1636      (mpfr_log_current <= mpfr_log_level))                             \
1637    fprintf (mpfr_log_file, "%s:TIM %dms\n", __mpfr_log_fname,          \
1638             mpfr_get_cputime () - __gmpfr_log_time);                   \
1639  if ((MPFR_LOG_OUTPUT_F & mpfr_log_type) &&                            \
1640      (mpfr_log_current <= mpfr_log_level))                             \
1641    LOG_PRINT ("%s:OUT "format"\n", __mpfr_log_fname, __VA_ARGS__);     \
1642  mpfr_log_current --;
1643#define MPFR_LOG_END(x)                                                 \
1644  static const char *__mpfr_log_fname = __func__;                       \
1645  MPFR_LOG_END2 x
1646
1647#define MPFR_LOG_FUNC(begin,end)                                        \
1648  static const char *__mpfr_log_fname = __func__;                       \
1649  auto void __mpfr_log_cleanup (int *time);                             \
1650  void __mpfr_log_cleanup (int *time) {                                 \
1651    int __gmpfr_log_time = *time;                                       \
1652    MPFR_LOG_END2 end; }                                                \
1653  int __gmpfr_log_time __attribute__ ((cleanup (__mpfr_log_cleanup)));  \
1654  __gmpfr_log_time = 0;                                                 \
1655  MPFR_LOG_BEGIN2 begin
1656
1657#else /* MPFR_USE_LOGGING */
1658
1659/* Define void macro for logging */
1660
1661#define MPFR_LOG_VAR(x)
1662#define MPFR_LOG_BEGIN(x)
1663#define MPFR_LOG_END(x)
1664#define MPFR_LOG_MSG(x)
1665#define MPFR_LOG_FUNC(x,y)
1666
1667#endif /* MPFR_USE_LOGGING */
1668
1669
1670/**************************************************************
1671 ************  Group Initialize Functions Macros  *************
1672 **************************************************************/
1673
1674#ifndef MPFR_GROUP_STATIC_SIZE
1675# define MPFR_GROUP_STATIC_SIZE 16
1676#endif
1677
1678struct mpfr_group_t {
1679  size_t     alloc;
1680  mp_limb_t *mant;
1681  mp_limb_t  tab[MPFR_GROUP_STATIC_SIZE];
1682};
1683
1684#define MPFR_GROUP_DECL(g) struct mpfr_group_t g
1685#define MPFR_GROUP_CLEAR(g) do {                                 \
1686 MPFR_LOG_MSG (("GROUP_CLEAR: ptr = 0x%lX, size = %lu\n",        \
1687                (unsigned long) (g).mant,                        \
1688                (unsigned long) (g).alloc));                     \
1689 if (MPFR_UNLIKELY ((g).alloc != 0)) {                           \
1690   MPFR_ASSERTD ((g).mant != (g).tab);                           \
1691   (*__gmp_free_func) ((g).mant, (g).alloc);                     \
1692 }} while (0)
1693
1694#define MPFR_GROUP_INIT_TEMPLATE(g, prec, num, handler) do {            \
1695 mpfr_prec_t _prec = (prec);                                            \
1696 mp_size_t _size;                                                       \
1697 MPFR_ASSERTD (_prec >= MPFR_PREC_MIN);                                 \
1698 if (MPFR_UNLIKELY (_prec > MPFR_PREC_MAX))                             \
1699   mpfr_abort_prec_max ();                                              \
1700 _size = MPFR_PREC2LIMBS (_prec);                                       \
1701 if (MPFR_UNLIKELY (_size * (num) > MPFR_GROUP_STATIC_SIZE))            \
1702   {                                                                    \
1703     (g).alloc = (num) * _size * sizeof (mp_limb_t);                    \
1704     (g).mant = (mp_limb_t *) (*__gmp_allocate_func) ((g).alloc);       \
1705   }                                                                    \
1706 else                                                                   \
1707   {                                                                    \
1708     (g).alloc = 0;                                                     \
1709     (g).mant = (g).tab;                                                \
1710   }                                                                    \
1711 MPFR_LOG_MSG (("GROUP_INIT: ptr = 0x%lX, size = %lu\n",                \
1712                (unsigned long) (g).mant, (unsigned long) (g).alloc));  \
1713 handler;                                                               \
1714 } while (0)
1715#define MPFR_GROUP_TINIT(g, n, x)                       \
1716  MPFR_TMP_INIT1 ((g).mant + _size * (n), x, _prec)
1717
1718#define MPFR_GROUP_INIT_1(g, prec, x)                            \
1719 MPFR_GROUP_INIT_TEMPLATE(g, prec, 1, MPFR_GROUP_TINIT(g, 0, x))
1720#define MPFR_GROUP_INIT_2(g, prec, x, y)                         \
1721 MPFR_GROUP_INIT_TEMPLATE(g, prec, 2,                            \
1722   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y))
1723#define MPFR_GROUP_INIT_3(g, prec, x, y, z)                      \
1724 MPFR_GROUP_INIT_TEMPLATE(g, prec, 3,                            \
1725   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1726   MPFR_GROUP_TINIT(g, 2, z))
1727#define MPFR_GROUP_INIT_4(g, prec, x, y, z, t)                   \
1728 MPFR_GROUP_INIT_TEMPLATE(g, prec, 4,                            \
1729   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1730   MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t))
1731#define MPFR_GROUP_INIT_5(g, prec, x, y, z, t, a)                \
1732 MPFR_GROUP_INIT_TEMPLATE(g, prec, 5,                            \
1733   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1734   MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t);          \
1735   MPFR_GROUP_TINIT(g, 4, a))
1736#define MPFR_GROUP_INIT_6(g, prec, x, y, z, t, a, b)             \
1737 MPFR_GROUP_INIT_TEMPLATE(g, prec, 6,                            \
1738   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1739   MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t);          \
1740   MPFR_GROUP_TINIT(g, 4, a);MPFR_GROUP_TINIT(g, 5, b))
1741
1742#define MPFR_GROUP_REPREC_TEMPLATE(g, prec, num, handler) do {          \
1743 mpfr_prec_t _prec = (prec);                                            \
1744 size_t    _oalloc = (g).alloc;                                         \
1745 mp_size_t _size;                                                       \
1746 MPFR_LOG_MSG (("GROUP_REPREC: oldptr = 0x%lX, oldsize = %lu\n",        \
1747                (unsigned long) (g).mant, (unsigned long) _oalloc));    \
1748 MPFR_ASSERTD (_prec >= MPFR_PREC_MIN);                                 \
1749 if (MPFR_UNLIKELY (_prec > MPFR_PREC_MAX))                             \
1750   mpfr_abort_prec_max ();                                              \
1751 _size = MPFR_PREC2LIMBS (_prec);                                       \
1752 (g).alloc = (num) * _size * sizeof (mp_limb_t);                        \
1753 if (MPFR_LIKELY (_oalloc == 0))                                        \
1754   (g).mant = (mp_limb_t *) (*__gmp_allocate_func) ((g).alloc);         \
1755 else                                                                   \
1756   (g).mant = (mp_limb_t *)                                             \
1757     (*__gmp_reallocate_func) ((g).mant, _oalloc, (g).alloc);           \
1758 MPFR_LOG_MSG (("GROUP_REPREC: newptr = 0x%lX, newsize = %lu\n",        \
1759                (unsigned long) (g).mant, (unsigned long) (g).alloc));  \
1760 handler;                                                               \
1761 } while (0)
1762
1763#define MPFR_GROUP_REPREC_1(g, prec, x)                          \
1764 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 1, MPFR_GROUP_TINIT(g, 0, x))
1765#define MPFR_GROUP_REPREC_2(g, prec, x, y)                       \
1766 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 2,                          \
1767   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y))
1768#define MPFR_GROUP_REPREC_3(g, prec, x, y, z)                    \
1769 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 3,                          \
1770   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1771   MPFR_GROUP_TINIT(g, 2, z))
1772#define MPFR_GROUP_REPREC_4(g, prec, x, y, z, t)                 \
1773 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 4,                          \
1774   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1775   MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t))
1776#define MPFR_GROUP_REPREC_5(g, prec, x, y, z, t, a)              \
1777 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 5,                          \
1778   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1779   MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t);          \
1780   MPFR_GROUP_TINIT(g, 4, a))
1781#define MPFR_GROUP_REPREC_6(g, prec, x, y, z, t, a, b)           \
1782 MPFR_GROUP_REPREC_TEMPLATE(g, prec, 6,                          \
1783   MPFR_GROUP_TINIT(g, 0, x);MPFR_GROUP_TINIT(g, 1, y);          \
1784   MPFR_GROUP_TINIT(g, 2, z);MPFR_GROUP_TINIT(g, 3, t);          \
1785   MPFR_GROUP_TINIT(g, 4, a);MPFR_GROUP_TINIT(g, 5, b))
1786
1787
1788/******************************************************
1789 ***************  Internal Functions  *****************
1790 ******************************************************/
1791
1792#if defined (__cplusplus)
1793extern "C" {
1794#endif
1795
1796__MPFR_DECLSPEC int mpfr_underflow _MPFR_PROTO ((mpfr_ptr, mpfr_rnd_t, int));
1797__MPFR_DECLSPEC int mpfr_overflow _MPFR_PROTO ((mpfr_ptr, mpfr_rnd_t, int));
1798
1799__MPFR_DECLSPEC int mpfr_add1 _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,
1800                                            mpfr_srcptr, mpfr_rnd_t));
1801__MPFR_DECLSPEC int mpfr_sub1 _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,
1802                                            mpfr_srcptr, mpfr_rnd_t));
1803__MPFR_DECLSPEC int mpfr_add1sp _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,
1804                                              mpfr_srcptr, mpfr_rnd_t));
1805__MPFR_DECLSPEC int mpfr_sub1sp _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,
1806                                              mpfr_srcptr, mpfr_rnd_t));
1807__MPFR_DECLSPEC int mpfr_can_round_raw _MPFR_PROTO ((const mp_limb_t *,
1808             mp_size_t, int, mpfr_exp_t, mpfr_rnd_t, mpfr_rnd_t, mpfr_prec_t));
1809
1810__MPFR_DECLSPEC int mpfr_cmp2 _MPFR_PROTO ((mpfr_srcptr, mpfr_srcptr,
1811                                            mpfr_prec_t *));
1812
1813__MPFR_DECLSPEC long          __gmpfr_ceil_log2     _MPFR_PROTO ((double));
1814__MPFR_DECLSPEC long          __gmpfr_floor_log2    _MPFR_PROTO ((double));
1815__MPFR_DECLSPEC double        __gmpfr_ceil_exp2     _MPFR_PROTO ((double));
1816__MPFR_DECLSPEC unsigned long __gmpfr_isqrt     _MPFR_PROTO ((unsigned long));
1817__MPFR_DECLSPEC unsigned long __gmpfr_cuberoot  _MPFR_PROTO ((unsigned long));
1818__MPFR_DECLSPEC int       __gmpfr_int_ceil_log2 _MPFR_PROTO ((unsigned long));
1819
1820__MPFR_DECLSPEC mpfr_exp_t mpfr_ceil_mul _MPFR_PROTO ((mpfr_exp_t, int, int));
1821
1822__MPFR_DECLSPEC int mpfr_exp_2 _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,mpfr_rnd_t));
1823__MPFR_DECLSPEC int mpfr_exp_3 _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,mpfr_rnd_t));
1824__MPFR_DECLSPEC int mpfr_powerof2_raw _MPFR_PROTO ((mpfr_srcptr));
1825
1826__MPFR_DECLSPEC int mpfr_pow_general _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,
1827                           mpfr_srcptr, mpfr_rnd_t, int, mpfr_save_expo_t *));
1828
1829__MPFR_DECLSPEC void mpfr_setmax _MPFR_PROTO ((mpfr_ptr, mpfr_exp_t));
1830__MPFR_DECLSPEC void mpfr_setmin _MPFR_PROTO ((mpfr_ptr, mpfr_exp_t));
1831
1832__MPFR_DECLSPEC long mpfr_mpn_exp _MPFR_PROTO ((mp_limb_t *, mpfr_exp_t *, int,
1833                                                mpfr_exp_t, size_t));
1834
1835#ifdef _MPFR_H_HAVE_FILE
1836__MPFR_DECLSPEC void mpfr_fprint_binary _MPFR_PROTO ((FILE *, mpfr_srcptr));
1837#endif
1838__MPFR_DECLSPEC void mpfr_print_binary _MPFR_PROTO ((mpfr_srcptr));
1839__MPFR_DECLSPEC void mpfr_print_mant_binary _MPFR_PROTO ((const char*,
1840                                          const mp_limb_t*, mpfr_prec_t));
1841__MPFR_DECLSPEC void mpfr_set_str_binary _MPFR_PROTO((mpfr_ptr, const char*));
1842
1843__MPFR_DECLSPEC int mpfr_round_raw _MPFR_PROTO ((mp_limb_t *,
1844       const mp_limb_t *, mpfr_prec_t, int, mpfr_prec_t, mpfr_rnd_t, int *));
1845__MPFR_DECLSPEC int mpfr_round_raw_2 _MPFR_PROTO ((const mp_limb_t *,
1846             mpfr_prec_t, int, mpfr_prec_t, mpfr_rnd_t));
1847/* No longer defined (see round_prec.c).
1848   Uncomment if it needs to be defined again.
1849__MPFR_DECLSPEC int mpfr_round_raw_3 _MPFR_PROTO ((const mp_limb_t *,
1850             mpfr_prec_t, int, mpfr_prec_t, mpfr_rnd_t, int *));
1851*/
1852__MPFR_DECLSPEC int mpfr_round_raw_4 _MPFR_PROTO ((mp_limb_t *,
1853       const mp_limb_t *, mpfr_prec_t, int, mpfr_prec_t, mpfr_rnd_t));
1854
1855#define mpfr_round_raw2(xp, xn, neg, r, prec) \
1856  mpfr_round_raw_2((xp),(xn)*GMP_NUMB_BITS,(neg),(prec),(r))
1857
1858__MPFR_DECLSPEC int mpfr_check _MPFR_PROTO ((mpfr_srcptr));
1859
1860__MPFR_DECLSPEC int mpfr_sum_sort _MPFR_PROTO ((mpfr_srcptr *const,
1861                                                unsigned long, mpfr_srcptr *));
1862
1863__MPFR_DECLSPEC int mpfr_get_cputime _MPFR_PROTO ((void));
1864
1865__MPFR_DECLSPEC void mpfr_nexttozero _MPFR_PROTO ((mpfr_ptr));
1866__MPFR_DECLSPEC void mpfr_nexttoinf _MPFR_PROTO ((mpfr_ptr));
1867
1868__MPFR_DECLSPEC int mpfr_const_pi_internal _MPFR_PROTO ((mpfr_ptr,mpfr_rnd_t));
1869__MPFR_DECLSPEC int mpfr_const_log2_internal _MPFR_PROTO((mpfr_ptr,mpfr_rnd_t));
1870__MPFR_DECLSPEC int mpfr_const_euler_internal _MPFR_PROTO((mpfr_ptr, mpfr_rnd_t));
1871__MPFR_DECLSPEC int mpfr_const_catalan_internal _MPFR_PROTO((mpfr_ptr, mpfr_rnd_t));
1872
1873#if 0
1874__MPFR_DECLSPEC void mpfr_init_cache _MPFR_PROTO ((mpfr_cache_t,
1875                                           int(*)(mpfr_ptr,mpfr_rnd_t)));
1876#endif
1877__MPFR_DECLSPEC void mpfr_clear_cache _MPFR_PROTO ((mpfr_cache_t));
1878__MPFR_DECLSPEC int  mpfr_cache _MPFR_PROTO ((mpfr_ptr, mpfr_cache_t,
1879                                              mpfr_rnd_t));
1880
1881__MPFR_DECLSPEC void mpfr_mulhigh_n _MPFR_PROTO ((mpfr_limb_ptr,
1882                        mpfr_limb_srcptr, mpfr_limb_srcptr, mp_size_t));
1883__MPFR_DECLSPEC void mpfr_mullow_n  _MPFR_PROTO ((mpfr_limb_ptr,
1884                        mpfr_limb_srcptr, mpfr_limb_srcptr, mp_size_t));
1885__MPFR_DECLSPEC void mpfr_sqrhigh_n _MPFR_PROTO ((mpfr_limb_ptr,
1886                        mpfr_limb_srcptr, mp_size_t));
1887__MPFR_DECLSPEC mp_limb_t mpfr_divhigh_n _MPFR_PROTO ((mpfr_limb_ptr,
1888                        mpfr_limb_ptr, mpfr_limb_ptr, mp_size_t));
1889
1890__MPFR_DECLSPEC int mpfr_round_p _MPFR_PROTO ((mp_limb_t *, mp_size_t,
1891                                               mpfr_exp_t, mpfr_prec_t));
1892
1893__MPFR_DECLSPEC void mpfr_dump_mant _MPFR_PROTO ((const mp_limb_t *,
1894                                                  mpfr_prec_t, mpfr_prec_t,
1895                                                  mpfr_prec_t));
1896
1897__MPFR_DECLSPEC int mpfr_round_near_x _MPFR_PROTO ((mpfr_ptr, mpfr_srcptr,
1898                                                    mpfr_uexp_t, int,
1899                                                    mpfr_rnd_t));
1900__MPFR_DECLSPEC void mpfr_abort_prec_max _MPFR_PROTO ((void))
1901       MPFR_NORETURN_ATTR;
1902
1903__MPFR_DECLSPEC void mpfr_rand_raw _MPFR_PROTO((mpfr_limb_ptr, gmp_randstate_t,
1904                                                mpfr_prec_t));
1905
1906__MPFR_DECLSPEC mpz_t* mpfr_bernoulli_internal _MPFR_PROTO((mpz_t*,
1907                                                            unsigned long));
1908
1909__MPFR_DECLSPEC int mpfr_sincos_fast _MPFR_PROTO((mpfr_t, mpfr_t,
1910                                                  mpfr_srcptr, mpfr_rnd_t));
1911
1912__MPFR_DECLSPEC double mpfr_scale2 _MPFR_PROTO((double, int));
1913
1914__MPFR_DECLSPEC void mpfr_div_ui2 _MPFR_PROTO((mpfr_ptr, mpfr_srcptr,
1915                                               unsigned long int, unsigned long int,
1916                                               mpfr_rnd_t));
1917
1918__MPFR_DECLSPEC void mpfr_gamma_one_and_two_third _MPFR_PROTO((mpfr_ptr, mpfr_ptr, mpfr_prec_t));
1919
1920#if defined (__cplusplus)
1921}
1922#endif
1923
1924#endif
1925