xref: /dragonfly/contrib/mpfr/src/mpfr-gmp.h (revision ab6d115f)
14a238c70SJohn Marino /* Interface to replace gmp-impl.h
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino #ifndef __GMPFR_GMP_H__
244a238c70SJohn Marino #define __GMPFR_GMP_H__
254a238c70SJohn Marino 
264a238c70SJohn Marino #ifndef __MPFR_IMPL_H__
274a238c70SJohn Marino # error  "mpfr-impl.h not included"
284a238c70SJohn Marino #endif
294a238c70SJohn Marino 
304a238c70SJohn Marino #include <limits.h> /* For INT_MAX, ... */
314a238c70SJohn Marino #include <string.h> /* For memcpy, memset and memmove */
324a238c70SJohn Marino 
334a238c70SJohn Marino /* The following tries to get a good version of alloca.
344a238c70SJohn Marino    See gmp-impl.h for implementation details and original version */
354a238c70SJohn Marino /* FIXME: the autoconf manual gives a different piece of code under the
364a238c70SJohn Marino    documentation of the AC_FUNC_ALLOCA macro. Should we switch to it? */
374a238c70SJohn Marino #ifndef alloca
384a238c70SJohn Marino # if defined ( __GNUC__ )
394a238c70SJohn Marino #  define alloca __builtin_alloca
404a238c70SJohn Marino # elif defined (__DECC)
414a238c70SJohn Marino #  define alloca(x) __ALLOCA(x)
424a238c70SJohn Marino # elif defined (_MSC_VER)
434a238c70SJohn Marino #  include <malloc.h>
444a238c70SJohn Marino #  define alloca _alloca
454a238c70SJohn Marino # elif defined (HAVE_ALLOCA_H)
464a238c70SJohn Marino #  include <alloca.h>
474a238c70SJohn Marino # elif defined (_AIX) || defined (_IBMR2)
484a238c70SJohn Marino #  pragma alloca
494a238c70SJohn Marino # else
504a238c70SJohn Marino void *alloca (size_t);
514a238c70SJohn Marino # endif
524a238c70SJohn Marino #endif
534a238c70SJohn Marino 
544a238c70SJohn Marino #if defined (__cplusplus)
554a238c70SJohn Marino extern "C" {
564a238c70SJohn Marino #endif
574a238c70SJohn Marino 
584a238c70SJohn Marino /* Define GMP_NUMB_BITS
594a238c70SJohn Marino    Can't use sizeof(mp_limb_t) since it should be a preprocessor constant */
604a238c70SJohn Marino #if defined(GMP_NUMB_BITS) /* GMP 4.1.2 or above */
614a238c70SJohn Marino #ifndef GMP_NUMB_BITS
624a238c70SJohn Marino # define GMP_NUMB_BITS  (GMP_NUMB_BITS+GMP_NAIL_BITS)
634a238c70SJohn Marino #endif
644a238c70SJohn Marino #elif defined (__GMP_GMP_NUMB_BITS) /* Older versions 4.x.x */
654a238c70SJohn Marino # define GMP_NUMB_BITS  __GMP_GMP_NUMB_BITS
664a238c70SJohn Marino # define GMP_NUMB_BITS GMP_NUMB_BITS
674a238c70SJohn Marino # ifndef GMP_NAIL_BITS
684a238c70SJohn Marino #  define GMP_NAIL_BITS 0
694a238c70SJohn Marino # endif
704a238c70SJohn Marino #else
714a238c70SJohn Marino # error "Could not detect GMP_NUMB_BITS. Try with gmp internal files."
724a238c70SJohn Marino #endif
734a238c70SJohn Marino 
744a238c70SJohn Marino /* Define some macros */
754a238c70SJohn Marino #define BYTES_PER_MP_LIMB (GMP_NUMB_BITS/CHAR_BIT)
764a238c70SJohn Marino 
774a238c70SJohn Marino #define MP_LIMB_T_MAX (~(mp_limb_t)0)
784a238c70SJohn Marino 
794a238c70SJohn Marino #define ULONG_HIGHBIT (ULONG_MAX ^ ((unsigned long) ULONG_MAX >> 1))
804a238c70SJohn Marino #define UINT_HIGHBIT  (UINT_MAX ^ ((unsigned) UINT_MAX >> 1))
814a238c70SJohn Marino #define USHRT_HIGHBIT ((unsigned short) (USHRT_MAX ^ ((unsigned short) USHRT_MAX >> 1)))
824a238c70SJohn Marino 
834a238c70SJohn Marino #define GMP_LIMB_HIGHBIT (MP_LIMB_T_MAX ^ (MP_LIMB_T_MAX >> 1))
844a238c70SJohn Marino 
854a238c70SJohn Marino 
864a238c70SJohn Marino #if __GMP_MP_SIZE_T_INT
874a238c70SJohn Marino #define MP_SIZE_T_MAX      INT_MAX
884a238c70SJohn Marino #define MP_SIZE_T_MIN      INT_MIN
894a238c70SJohn Marino #else
904a238c70SJohn Marino #define MP_SIZE_T_MAX      LONG_MAX
914a238c70SJohn Marino #define MP_SIZE_T_MIN      LONG_MIN
924a238c70SJohn Marino #endif
934a238c70SJohn Marino 
944a238c70SJohn Marino #define LONG_HIGHBIT       LONG_MIN
954a238c70SJohn Marino #define INT_HIGHBIT        INT_MIN
964a238c70SJohn Marino #define SHRT_HIGHBIT       SHRT_MIN
974a238c70SJohn Marino 
984a238c70SJohn Marino /* MP_LIMB macros */
994a238c70SJohn Marino #define MPN_ZERO(dst, n) memset((dst), 0, (n)*BYTES_PER_MP_LIMB)
1004a238c70SJohn Marino #define MPN_COPY_DECR(dst,src,n) memmove((dst),(src),(n)*BYTES_PER_MP_LIMB)
1014a238c70SJohn Marino #define MPN_COPY_INCR(dst,src,n) memmove((dst),(src),(n)*BYTES_PER_MP_LIMB)
1024a238c70SJohn Marino #define MPN_COPY(dst,src,n) \
1034a238c70SJohn Marino   do                                                                  \
1044a238c70SJohn Marino     {                                                                 \
1054a238c70SJohn Marino       if ((dst) != (src))                                             \
1064a238c70SJohn Marino         {                                                             \
1074a238c70SJohn Marino           MPFR_ASSERTD ((char *) (dst) >= (char *) (src) +            \
1084a238c70SJohn Marino                                           (n) * BYTES_PER_MP_LIMB ||  \
1094a238c70SJohn Marino                         (char *) (src) >= (char *) (dst) +            \
1104a238c70SJohn Marino                                           (n) * BYTES_PER_MP_LIMB);   \
1114a238c70SJohn Marino           memcpy ((dst), (src), (n) * BYTES_PER_MP_LIMB);             \
1124a238c70SJohn Marino         }                                                             \
1134a238c70SJohn Marino     }                                                                 \
1144a238c70SJohn Marino   while (0)
1154a238c70SJohn Marino 
1164a238c70SJohn Marino /* MPN macros taken from gmp-impl.h */
1174a238c70SJohn Marino #define MPN_NORMALIZE(DST, NLIMBS) \
1184a238c70SJohn Marino   do {                                        \
1194a238c70SJohn Marino     while (NLIMBS > 0)                        \
1204a238c70SJohn Marino       {                                       \
1214a238c70SJohn Marino         if ((DST)[(NLIMBS) - 1] != 0)         \
1224a238c70SJohn Marino           break;                              \
1234a238c70SJohn Marino         NLIMBS--;                             \
1244a238c70SJohn Marino       }                                       \
1254a238c70SJohn Marino   } while (0)
1264a238c70SJohn Marino #define MPN_NORMALIZE_NOT_ZERO(DST, NLIMBS)     \
1274a238c70SJohn Marino   do {                                          \
1284a238c70SJohn Marino     MPFR_ASSERTD ((NLIMBS) >= 1);               \
1294a238c70SJohn Marino     while (1)                                   \
1304a238c70SJohn Marino       {                                         \
1314a238c70SJohn Marino         if ((DST)[(NLIMBS) - 1] != 0)           \
1324a238c70SJohn Marino           break;                                \
1334a238c70SJohn Marino         NLIMBS--;                               \
1344a238c70SJohn Marino       }                                         \
1354a238c70SJohn Marino   } while (0)
1364a238c70SJohn Marino #define MPN_OVERLAP_P(xp, xsize, yp, ysize) \
1374a238c70SJohn Marino   ((xp) + (xsize) > (yp) && (yp) + (ysize) > (xp))
1384a238c70SJohn Marino #define MPN_SAME_OR_INCR2_P(dst, dsize, src, ssize)             \
1394a238c70SJohn Marino   ((dst) <= (src) || ! MPN_OVERLAP_P (dst, dsize, src, ssize))
1404a238c70SJohn Marino #define MPN_SAME_OR_INCR_P(dst, src, size)      \
1414a238c70SJohn Marino   MPN_SAME_OR_INCR2_P(dst, size, src, size)
1424a238c70SJohn Marino #define MPN_SAME_OR_DECR2_P(dst, dsize, src, ssize)             \
1434a238c70SJohn Marino   ((dst) >= (src) || ! MPN_OVERLAP_P (dst, dsize, src, ssize))
1444a238c70SJohn Marino #define MPN_SAME_OR_DECR_P(dst, src, size)      \
1454a238c70SJohn Marino   MPN_SAME_OR_DECR2_P(dst, size, src, size)
1464a238c70SJohn Marino 
1474a238c70SJohn Marino /* If mul_basecase or mpn_sqr_basecase are not exported, used mpn_mul instead */
1484a238c70SJohn Marino #ifndef mpn_mul_basecase
1494a238c70SJohn Marino # define mpn_mul_basecase(dst,s1,n1,s2,n2) mpn_mul((dst),(s1),(n1),(s2),(n2))
1504a238c70SJohn Marino #endif
1514a238c70SJohn Marino #ifndef mpn_sqr_basecase
1524a238c70SJohn Marino # define mpn_sqr_basecase(dst,src,n) mpn_mul((dst),(src),(n),(src),(n))
1534a238c70SJohn Marino #endif
1544a238c70SJohn Marino 
1554a238c70SJohn Marino /* ASSERT */
1564a238c70SJohn Marino __MPFR_DECLSPEC void mpfr_assert_fail _MPFR_PROTO((const char *, int,
1574a238c70SJohn Marino                                                    const char *));
1584a238c70SJohn Marino 
1594a238c70SJohn Marino #define ASSERT_FAIL(expr)  mpfr_assert_fail (__FILE__, __LINE__, #expr)
1604a238c70SJohn Marino #define ASSERT(expr)       MPFR_ASSERTD(expr)
1614a238c70SJohn Marino 
1624a238c70SJohn Marino /* Access fileds of GMP struct */
1634a238c70SJohn Marino #define SIZ(x) ((x)->_mp_size)
1644a238c70SJohn Marino #define ABSIZ(x) ABS (SIZ (x))
1654a238c70SJohn Marino #define PTR(x) ((x)->_mp_d)
1664a238c70SJohn Marino #define EXP(x) ((x)->_mp_exp)
1674a238c70SJohn Marino #define PREC(x) ((x)->_mp_prec)
1684a238c70SJohn Marino #define ALLOC(x) ((x)->_mp_alloc)
1694a238c70SJohn Marino #define MPZ_REALLOC(z,n) ((n) > ALLOC(z) ? _mpz_realloc(z,n) : PTR(z))
1704a238c70SJohn Marino 
1714a238c70SJohn Marino /* Non IEEE float supports -- needs to detect them with proper configure */
1724a238c70SJohn Marino #undef  XDEBUG
1734a238c70SJohn Marino #define XDEBUG
1744a238c70SJohn Marino 
1754a238c70SJohn Marino /* For longlong.h */
1764a238c70SJohn Marino #ifdef HAVE_ATTRIBUTE_MODE
1774a238c70SJohn Marino typedef unsigned int UQItype    __attribute__ ((mode (QI)));
1784a238c70SJohn Marino typedef          int SItype     __attribute__ ((mode (SI)));
1794a238c70SJohn Marino typedef unsigned int USItype    __attribute__ ((mode (SI)));
1804a238c70SJohn Marino typedef          int DItype     __attribute__ ((mode (DI)));
1814a238c70SJohn Marino typedef unsigned int UDItype    __attribute__ ((mode (DI)));
1824a238c70SJohn Marino #else
1834a238c70SJohn Marino typedef unsigned char UQItype;
1844a238c70SJohn Marino typedef          long SItype;
1854a238c70SJohn Marino typedef unsigned long USItype;
1864a238c70SJohn Marino #ifdef HAVE_LONG_LONG
1874a238c70SJohn Marino typedef long long int DItype;
1884a238c70SJohn Marino typedef unsigned long long int UDItype;
1894a238c70SJohn Marino #else /* Assume `long' gives us a wide enough type.  Needed for hppa2.0w.  */
1904a238c70SJohn Marino typedef long int DItype;
1914a238c70SJohn Marino typedef unsigned long int UDItype;
1924a238c70SJohn Marino #endif
1934a238c70SJohn Marino #endif
1944a238c70SJohn Marino typedef mp_limb_t UWtype;
1954a238c70SJohn Marino typedef unsigned int UHWtype;
1964a238c70SJohn Marino #define W_TYPE_SIZE GMP_NUMB_BITS
1974a238c70SJohn Marino 
1984a238c70SJohn Marino /* Remap names of internal mpn functions (for longlong.h).  */
1994a238c70SJohn Marino #undef  __clz_tab
2004a238c70SJohn Marino #define __clz_tab               mpfr_clz_tab
2014a238c70SJohn Marino 
2024a238c70SJohn Marino /* Use (4.0 * ...) instead of (2.0 * ...) to work around buggy compilers
2034a238c70SJohn Marino    that don't convert ulong->double correctly (eg. SunOS 4 native cc).  */
2044a238c70SJohn Marino #undef MP_BASE_AS_DOUBLE
2054a238c70SJohn Marino #define MP_BASE_AS_DOUBLE (4.0 * ((mp_limb_t) 1 << (GMP_NUMB_BITS - 2)))
2064a238c70SJohn Marino 
2074a238c70SJohn Marino /* Structure for conversion between internal binary format and
2084a238c70SJohn Marino    strings in base 2..36.  */
2094a238c70SJohn Marino struct bases
2104a238c70SJohn Marino {
2114a238c70SJohn Marino   /* log(2)/log(conversion_base) */
2124a238c70SJohn Marino   double chars_per_bit_exactly;
2134a238c70SJohn Marino };
2144a238c70SJohn Marino #undef  __mp_bases
2154a238c70SJohn Marino #define __mp_bases mpfr_bases
2164a238c70SJohn Marino __MPFR_DECLSPEC extern const struct bases mpfr_bases[257];
2174a238c70SJohn Marino 
2184a238c70SJohn Marino /* Standard macros */
2194a238c70SJohn Marino #undef ABS
2204a238c70SJohn Marino #undef MIN
2214a238c70SJohn Marino #undef MAX
2224a238c70SJohn Marino #undef numberof
2234a238c70SJohn Marino #define ABS(x) ((x) >= 0 ? (x) : -(x))
2244a238c70SJohn Marino #define MIN(l,o) ((l) < (o) ? (l) : (o))
2254a238c70SJohn Marino #define MAX(h,i) ((h) > (i) ? (h) : (i))
2264a238c70SJohn Marino #define numberof(x)  (sizeof (x) / sizeof ((x)[0]))
2274a238c70SJohn Marino 
2284a238c70SJohn Marino /* Random */
2294a238c70SJohn Marino #undef  __gmp_rands_initialized
2304a238c70SJohn Marino #undef  __gmp_rands
2314a238c70SJohn Marino #define __gmp_rands_initialized mpfr_rands_initialized
2324a238c70SJohn Marino #define __gmp_rands             mpfr_rands
2334a238c70SJohn Marino 
2344a238c70SJohn Marino __MPFR_DECLSPEC extern char             mpfr_rands_initialized;
2354a238c70SJohn Marino __MPFR_DECLSPEC extern gmp_randstate_t  mpfr_rands;
2364a238c70SJohn Marino 
2374a238c70SJohn Marino #undef RANDS
2384a238c70SJohn Marino #define RANDS                                   \
2394a238c70SJohn Marino   ((__gmp_rands_initialized ? 0                 \
2404a238c70SJohn Marino     : (__gmp_rands_initialized = 1,             \
2414a238c70SJohn Marino        gmp_randinit_default (__gmp_rands), 0)), \
2424a238c70SJohn Marino    __gmp_rands)
2434a238c70SJohn Marino 
2444a238c70SJohn Marino #undef RANDS_CLEAR
2454a238c70SJohn Marino #define RANDS_CLEAR()                   \
2464a238c70SJohn Marino   do {                                  \
2474a238c70SJohn Marino     if (__gmp_rands_initialized)        \
2484a238c70SJohn Marino       {                                 \
2494a238c70SJohn Marino         __gmp_rands_initialized = 0;    \
2504a238c70SJohn Marino         gmp_randclear (__gmp_rands);    \
2514a238c70SJohn Marino       }                                 \
2524a238c70SJohn Marino   } while (0)
2534a238c70SJohn Marino 
2544a238c70SJohn Marino typedef __gmp_randstate_struct *gmp_randstate_ptr;
2554a238c70SJohn Marino 
2564a238c70SJohn Marino /* Allocate func are defined in gmp-impl.h */
2574a238c70SJohn Marino 
2584a238c70SJohn Marino /* In newer GMP, there aren't anymore __gmp_allocate_func,
2594a238c70SJohn Marino    __gmp_reallocate_func & __gmp_free_func in gmp.h
2604a238c70SJohn Marino    Just getting the correct value by calling mp_get_memory_functions */
2614a238c70SJohn Marino #ifdef mp_get_memory_functions
2624a238c70SJohn Marino 
2634a238c70SJohn Marino #undef __gmp_allocate_func
2644a238c70SJohn Marino #undef __gmp_reallocate_func
2654a238c70SJohn Marino #undef __gmp_free_func
2664a238c70SJohn Marino #define MPFR_GET_MEMFUNC mp_get_memory_functions(&mpfr_allocate_func, &mpfr_reallocate_func, &mpfr_free_func)
2674a238c70SJohn Marino #define __gmp_allocate_func   (MPFR_GET_MEMFUNC, mpfr_allocate_func)
2684a238c70SJohn Marino #define __gmp_reallocate_func (MPFR_GET_MEMFUNC, mpfr_reallocate_func)
2694a238c70SJohn Marino #define __gmp_free_func       (MPFR_GET_MEMFUNC, mpfr_free_func)
2704a238c70SJohn Marino __MPFR_DECLSPEC extern void * (*mpfr_allocate_func)   _MPFR_PROTO ((size_t));
2714a238c70SJohn Marino __MPFR_DECLSPEC extern void * (*mpfr_reallocate_func) _MPFR_PROTO ((void *,
2724a238c70SJohn Marino                                                           size_t, size_t));
2734a238c70SJohn Marino __MPFR_DECLSPEC extern void   (*mpfr_free_func)       _MPFR_PROTO ((void *,
2744a238c70SJohn Marino                                                                     size_t));
2754a238c70SJohn Marino 
2764a238c70SJohn Marino #endif
2774a238c70SJohn Marino 
2784a238c70SJohn Marino #undef __gmp_default_allocate
2794a238c70SJohn Marino #undef __gmp_default_reallocate
2804a238c70SJohn Marino #undef __gmp_default_free
2814a238c70SJohn Marino #define __gmp_default_allocate   mpfr_default_allocate
2824a238c70SJohn Marino #define __gmp_default_reallocate mpfr_default_reallocate
2834a238c70SJohn Marino #define __gmp_default_free       mpfr_default_free
2844a238c70SJohn Marino __MPFR_DECLSPEC void *__gmp_default_allocate _MPFR_PROTO ((size_t));
2854a238c70SJohn Marino __MPFR_DECLSPEC void *__gmp_default_reallocate _MPFR_PROTO ((void *, size_t,
2864a238c70SJohn Marino                                                              size_t));
2874a238c70SJohn Marino __MPFR_DECLSPEC void __gmp_default_free _MPFR_PROTO ((void *, size_t));
2884a238c70SJohn Marino 
2894a238c70SJohn Marino #if defined(WANT_GMP_INTERNALS) && defined(HAVE___GMPN_ROOTREM)
2904a238c70SJohn Marino #ifndef __gmpn_rootrem
2914a238c70SJohn Marino   __MPFR_DECLSPEC mp_size_t __gmpn_rootrem _MPFR_PROTO ((mp_limb_t*,
2924a238c70SJohn Marino                     mp_limb_t*, mp_limb_t*, mp_size_t, mp_limb_t));
2934a238c70SJohn Marino #endif
2944a238c70SJohn Marino #endif
2954a238c70SJohn Marino 
2964a238c70SJohn Marino #if defined(WANT_GMP_INTERNALS) && defined(HAVE___GMPN_SBPI1_DIVAPPR_Q)
2974a238c70SJohn Marino #ifndef __gmpn_sbpi1_divappr_q
2984a238c70SJohn Marino   __MPFR_DECLSPEC mp_limb_t __gmpn_sbpi1_divappr_q _MPFR_PROTO ((mp_limb_t*,
2994a238c70SJohn Marino                 mp_limb_t*, mp_size_t, mp_limb_t*, mp_size_t, mp_limb_t));
3004a238c70SJohn Marino #endif
3014a238c70SJohn Marino #endif
3024a238c70SJohn Marino 
3034a238c70SJohn Marino /* Temp memory allocate */
3044a238c70SJohn Marino 
3054a238c70SJohn Marino struct tmp_marker
3064a238c70SJohn Marino {
3074a238c70SJohn Marino   void *ptr;
3084a238c70SJohn Marino   size_t size;
3094a238c70SJohn Marino   struct tmp_marker *next;
3104a238c70SJohn Marino };
3114a238c70SJohn Marino 
3124a238c70SJohn Marino __MPFR_DECLSPEC void *mpfr_tmp_allocate _MPFR_PROTO ((struct tmp_marker **,
3134a238c70SJohn Marino                                                       size_t));
3144a238c70SJohn Marino __MPFR_DECLSPEC void mpfr_tmp_free _MPFR_PROTO ((struct tmp_marker *));
3154a238c70SJohn Marino 
3164a238c70SJohn Marino /* Do not define TMP_SALLOC (see the test in mpfr-impl.h)! */
3174a238c70SJohn Marino #define TMP_ALLOC(n) (MPFR_LIKELY ((n) < 16384) ?       \
3184a238c70SJohn Marino                       alloca (n) : mpfr_tmp_allocate (&tmp_marker, (n)))
3194a238c70SJohn Marino #define TMP_DECL(m) struct tmp_marker *tmp_marker
3204a238c70SJohn Marino #define TMP_MARK(m) (tmp_marker = 0)
3214a238c70SJohn Marino #define TMP_FREE(m) mpfr_tmp_free (tmp_marker)
3224a238c70SJohn Marino 
3234a238c70SJohn Marino /* invert_limb macro, copied from GMP 5.0.2, file gmp-impl.h.
3244a238c70SJohn Marino    It returns invxl = floor((B^2-1)/xl)-B, where B=2^BITS_PER_LIMB,
3254a238c70SJohn Marino    assuming the most significant bit of xl is set. */
3264a238c70SJohn Marino #undef invert_limb
3274a238c70SJohn Marino #define invert_limb(invxl,xl)                             \
3284a238c70SJohn Marino   do {                                                    \
3294a238c70SJohn Marino     mp_limb_t dummy;                                      \
3304a238c70SJohn Marino     MPFR_ASSERTD ((xl) != 0);                             \
3314a238c70SJohn Marino     udiv_qrnnd (invxl, dummy, ~(xl), ~(mp_limb_t)0, xl);  \
3324a238c70SJohn Marino   } while (0)
3334a238c70SJohn Marino 
3344a238c70SJohn Marino typedef struct {mp_limb_t inv32;} mpfr_pi1_t; /* We changed gmp_pi1_t into
3354a238c70SJohn Marino                                                  mpfr_pi1_t to avoid using
3364a238c70SJohn Marino                                                  GMP's namespace. */
3374a238c70SJohn Marino /* invert_pi1 macro, adapted from GMP 5.0.2, file gmp-impl.h.
3384a238c70SJohn Marino    It returns dinv = floor((B^3-1)/(d1*B+d0))-B, where B=2^BITS_PER_LIMB,
3394a238c70SJohn Marino    assuming the most significant bit of d1 is set. */
3404a238c70SJohn Marino #undef invert_pi1
3414a238c70SJohn Marino #define invert_pi1(dinv, d1, d0)                                \
3424a238c70SJohn Marino   do {                                                          \
3434a238c70SJohn Marino     mp_limb_t _v, _p, _t1, _t0, _mask;                          \
3444a238c70SJohn Marino     invert_limb (_v, d1);                                       \
3454a238c70SJohn Marino     _p = d1 * _v;                                               \
3464a238c70SJohn Marino     _p += d0;                                                   \
3474a238c70SJohn Marino     if (_p < d0)                                                \
3484a238c70SJohn Marino       {                                                         \
3494a238c70SJohn Marino         _v--;                                                   \
3504a238c70SJohn Marino         _mask = -(_p >= d1);                                    \
3514a238c70SJohn Marino         _p -= d1;                                               \
3524a238c70SJohn Marino         _v += _mask;                                            \
3534a238c70SJohn Marino         _p -= _mask & d1;                                       \
3544a238c70SJohn Marino       }                                                         \
3554a238c70SJohn Marino     umul_ppmm (_t1, _t0, d0, _v);                               \
3564a238c70SJohn Marino     _p += _t1;                                                  \
3574a238c70SJohn Marino     if (_p < _t1)                                               \
3584a238c70SJohn Marino       {                                                         \
3594a238c70SJohn Marino         _v--;                                                   \
3604a238c70SJohn Marino         if (MPFR_UNLIKELY (_p >= d1))                           \
3614a238c70SJohn Marino           {                                                     \
3624a238c70SJohn Marino             if (_p > d1 || _t0 >= d0)                           \
3634a238c70SJohn Marino               _v--;                                             \
3644a238c70SJohn Marino           }                                                     \
3654a238c70SJohn Marino       }                                                         \
3664a238c70SJohn Marino     (dinv).inv32 = _v;                                          \
3674a238c70SJohn Marino   } while (0)
3684a238c70SJohn Marino 
3694a238c70SJohn Marino /* udiv_qr_3by2 macro, adapted from GMP 5.0.2, file gmp-impl.h.
3704a238c70SJohn Marino    Compute quotient the quotient and remainder for n / d. Requires d
3714a238c70SJohn Marino    >= B^2 / 2 and n < d B. dinv is the inverse
3724a238c70SJohn Marino 
3734a238c70SJohn Marino      floor ((B^3 - 1) / (d0 + d1 B)) - B.
3744a238c70SJohn Marino 
3754a238c70SJohn Marino    NOTE: Output variables are updated multiple times. Only some inputs
3764a238c70SJohn Marino    and outputs may overlap.
3774a238c70SJohn Marino */
3784a238c70SJohn Marino #undef udiv_qr_3by2
3794a238c70SJohn Marino #define udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv)               \
3804a238c70SJohn Marino   do {                                                                  \
3814a238c70SJohn Marino     mp_limb_t _q0, _t1, _t0, _mask;                                     \
3824a238c70SJohn Marino     umul_ppmm ((q), _q0, (n2), (dinv));                                 \
3834a238c70SJohn Marino     add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1));                        \
3844a238c70SJohn Marino                                                                         \
3854a238c70SJohn Marino     /* Compute the two most significant limbs of n - q'd */             \
3864a238c70SJohn Marino     (r1) = (n1) - (d1) * (q);                                           \
3874a238c70SJohn Marino     (r0) = (n0);                                                        \
3884a238c70SJohn Marino     sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0));                    \
3894a238c70SJohn Marino     umul_ppmm (_t1, _t0, (d0), (q));                                    \
3904a238c70SJohn Marino     sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0);                      \
3914a238c70SJohn Marino     (q)++;                                                              \
3924a238c70SJohn Marino                                                                         \
3934a238c70SJohn Marino     /* Conditionally adjust q and the remainders */                     \
3944a238c70SJohn Marino     _mask = - (mp_limb_t) ((r1) >= _q0);                                \
3954a238c70SJohn Marino     (q) += _mask;                                                       \
3964a238c70SJohn Marino     add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0));    \
3974a238c70SJohn Marino     if (MPFR_UNLIKELY ((r1) >= (d1)))                                   \
3984a238c70SJohn Marino       {                                                                 \
3994a238c70SJohn Marino         if ((r1) > (d1) || (r0) >= (d0))                                \
4004a238c70SJohn Marino           {                                                             \
4014a238c70SJohn Marino             (q)++;                                                      \
4024a238c70SJohn Marino             sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0));            \
4034a238c70SJohn Marino           }                                                             \
4044a238c70SJohn Marino       }                                                                 \
4054a238c70SJohn Marino   } while (0)
4064a238c70SJohn Marino 
4074a238c70SJohn Marino #if defined (__cplusplus)
4084a238c70SJohn Marino }
4094a238c70SJohn Marino #endif
4104a238c70SJohn Marino 
4114a238c70SJohn Marino #endif /* Gmp internal emulator */
412