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