diff options
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | src/mpfr-gmp.c | 50 | ||||
-rw-r--r-- | src/mpfr-gmp.h | 255 | ||||
-rw-r--r-- | src/mpfr-impl.h | 83 | ||||
-rw-r--r-- | src/mulders.c | 4 | ||||
-rw-r--r-- | tests/memory.c | 45 | ||||
-rw-r--r-- | tests/mpfr-test.h | 46 | ||||
-rw-r--r-- | tests/tests.c | 14 |
8 files changed, 249 insertions, 254 deletions
diff --git a/configure.ac b/configure.ac index 9e4d2941b..a3911de63 100644 --- a/configure.ac +++ b/configure.ac @@ -481,13 +481,13 @@ dnl see http://gmplib.org/list-archives/gmp-bugs/2011-August/002345.html. AC_MSG_CHECKING(for recent GMP) AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ #include "gmp.h" -#if (__GNU_MP_VERSION*100+__GNU_MP_VERSION_MINOR*10 < 410) -# error "min GMP version is 4.1.0" +#if (__GNU_MP_VERSION*100+__GNU_MP_VERSION_MINOR*10 < 420) +# error "min GMP version is 4.2.0" error #endif ]])],[AC_MSG_RESULT(yes)],[ AC_MSG_RESULT(no) - AC_MSG_ERROR([GMP 4.1.0 min required]) + AC_MSG_ERROR([GMP 4.2.0 or newer is required]) ]) dnl Check if we can use internal header files of GMP (only --with-gmp-build) diff --git a/src/mpfr-gmp.c b/src/mpfr-gmp.c index 257ab009e..b0e84ce2d 100644 --- a/src/mpfr-gmp.c +++ b/src/mpfr-gmp.c @@ -25,9 +25,6 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., #ifndef MPFR_HAVE_GMP_IMPL -char mpfr_rands_initialized = 0; -gmp_randstate_t mpfr_rands; - const struct bases mpfr_bases[257] = { /* 0 */ {0.0}, @@ -303,8 +300,6 @@ mpfr_assert_fail (const char *filename, int linenum, abort(); } -#ifdef mp_get_memory_functions - /* putting 0 as initial values forces those symbols to be fully defined, and always resolved, otherwise they are only tentatively defined, which leads to problems on e.g. MacOS, cf @@ -316,51 +311,14 @@ void * (*mpfr_allocate_func) (size_t) = 0; void * (*mpfr_reallocate_func) (void *,size_t, size_t) = 0; void (*mpfr_free_func) (void *, size_t) = 0; -#endif - -void * -mpfr_default_allocate (size_t size) -{ - void *ret; - ret = malloc (size); - if (ret == NULL) - { - fprintf (stderr, "MPFR: Can't allocate memory (size=%lu)\n", - (unsigned long) size); - abort (); - } - return ret; -} - -void * -mpfr_default_reallocate (void *oldptr, size_t old_size, size_t new_size) -{ - void *ret; - ret = realloc (oldptr, new_size); - if (ret == NULL) - { - fprintf (stderr, - "MPFR: Can't reallocate memory (old_size=%lu new_size=%lu)\n", - (unsigned long) old_size, (unsigned long) new_size); - abort (); - } - return ret; -} - -void -mpfr_default_free (void *blk_ptr, size_t blk_size) -{ - free (blk_ptr); -} - void * mpfr_tmp_allocate (struct tmp_marker **tmp_marker, size_t size) { struct tmp_marker *head; head = (struct tmp_marker *) - mpfr_default_allocate (sizeof (struct tmp_marker)); - head->ptr = mpfr_default_allocate (size); + (*mpfr_allocate_func) (sizeof (struct tmp_marker)); + head->ptr = (*mpfr_allocate_func) (size); head->size = size; head->next = *tmp_marker; *tmp_marker = head; @@ -375,9 +333,9 @@ mpfr_tmp_free (struct tmp_marker *tmp_marker) while (tmp_marker != NULL) { t = tmp_marker; - mpfr_default_free (t->ptr, t->size); + (*mpfr_free_func) (t->ptr, t->size); tmp_marker = t->next; - mpfr_default_free (t, sizeof (struct tmp_marker)); + (*mpfr_free_func) (t, sizeof (struct tmp_marker)); } } diff --git a/src/mpfr-gmp.h b/src/mpfr-gmp.h index 946605f6e..8b111b822 100644 --- a/src/mpfr-gmp.h +++ b/src/mpfr-gmp.h @@ -1,4 +1,4 @@ -/* Interface to replace gmp-impl.h +/* Uniform Interface to GMP. Copyright 2004-2014 Free Software Foundation, Inc. Contributed by the AriC and Caramel projects, INRIA. @@ -27,8 +27,66 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., # error "mpfr-impl.h not included" #endif -#include <limits.h> /* For INT_MAX, ... */ -#include <string.h> /* For memcpy, memset and memmove */ + +/****************************************************** + ******************** C++ Compatibility *************** + ******************************************************/ +#if defined (__cplusplus) +extern "C" { +#endif + + +/****************************************************** + ******************** Identify GMP ******************** + ******************************************************/ + +/* Macro to detect the GMP version */ +#if defined(__GNU_MP_VERSION) && \ + defined(__GNU_MP_VERSION_MINOR) && \ + defined(__GNU_MP_VERSION_PATCHLEVEL) +# define __MPFR_GMP(a,b,c) \ + (MPFR_VERSION_NUM(__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR,__GNU_MP_VERSION_PATCHLEVEL) >= MPFR_VERSION_NUM(a,b,c)) +#else +# define __MPFR_GMP(a,b,c) 0 +#endif + + + +/****************************************************** + ******************** Check GMP *********************** + ******************************************************/ + +#if !__MPFR_GMP(4,2,0) +# error "GMP 4.2.0 or newer needed" +#endif + +#if GMP_NAIL_BITS != 0 +# error "MPFR doesn't support nonzero values of GMP_NAIL_BITS" +#endif + +#if (GMP_NUMB_BITS<32) || (GMP_NUMB_BITS & (GMP_NUMB_BITS - 1)) +# error "GMP_NUMB_BITS must be a power of 2, and >= 32" +#endif + +#if GMP_NUMB_BITS == 32 +# define MPFR_LOG2_GMP_NUMB_BITS 5 +#elif GMP_NUMB_BITS == 64 +# define MPFR_LOG2_GMP_NUMB_BITS 6 +#elif GMP_NUMB_BITS == 128 +# define MPFR_LOG2_GMP_NUMB_BITS 7 +#elif GMP_NUMB_BITS == 256 +# define MPFR_LOG2_GMP_NUMB_BITS 8 +#else +# error "Can't compute log2(GMP_NUMB_BITS)" +#endif + + + +/****************************************************** + ************* Define GMP Internal Interface ********* + ******************************************************/ + +#ifndef MPFR_HAVE_GMP_IMPL /* Build with gmp internals */ /* The following tries to get a good version of alloca. See gmp-impl.h for implementation details and original version */ @@ -51,26 +109,6 @@ void *alloca (size_t); # endif #endif -#if defined (__cplusplus) -extern "C" { -#endif - -/* Define GMP_NUMB_BITS - Can't use sizeof(mp_limb_t) since it should be a preprocessor constant */ -#if defined(GMP_NUMB_BITS) /* GMP 4.1.2 or above */ -#ifndef GMP_NUMB_BITS -# define GMP_NUMB_BITS (GMP_NUMB_BITS+GMP_NAIL_BITS) -#endif -#elif defined (__GMP_GMP_NUMB_BITS) /* Older versions 4.x.x */ -# define GMP_NUMB_BITS __GMP_GMP_NUMB_BITS -# define GMP_NUMB_BITS GMP_NUMB_BITS -# ifndef GMP_NAIL_BITS -# define GMP_NAIL_BITS 0 -# endif -#else -# error "Could not detect GMP_NUMB_BITS. Try with gmp internal files." -#endif - /* Define some macros */ #define BYTES_PER_MP_LIMB (GMP_NUMB_BITS/CHAR_BIT) @@ -221,47 +259,8 @@ __MPFR_DECLSPEC extern const struct bases mpfr_bases[257]; #define MAX(h,i) ((h) > (i) ? (h) : (i)) #define numberof(x) (sizeof (x) / sizeof ((x)[0])) -/* Random */ -/* TODO: these variables and macros seem to be used only in the tests. - Remove them from the MPFR source (src directory) so that the symbols - mpfr_rands_initialized and mpfr_rands are not defined in the MPFR - library? Moreover, whether the GMP build directory is used or not, - we could use our own rand state, so that __gmp_rands_initialized and - __gmp_rands would no longer be needed. */ -#undef __gmp_rands_initialized -#undef __gmp_rands -#define __gmp_rands_initialized mpfr_rands_initialized -#define __gmp_rands mpfr_rands - -__MPFR_DECLSPEC extern char mpfr_rands_initialized; -__MPFR_DECLSPEC extern gmp_randstate_t mpfr_rands; - -#undef RANDS -#define RANDS \ - ((__gmp_rands_initialized ? 0 \ - : (__gmp_rands_initialized = 1, \ - gmp_randinit_default (__gmp_rands), 0)), \ - __gmp_rands) - -#undef RANDS_CLEAR -#define RANDS_CLEAR() \ - do { \ - if (__gmp_rands_initialized) \ - { \ - __gmp_rands_initialized = 0; \ - gmp_randclear (__gmp_rands); \ - } \ - } while (0) - -typedef __gmp_randstate_struct *gmp_randstate_ptr; - /* Allocate func are defined in gmp-impl.h */ -/* In newer GMP, there aren't anymore __gmp_allocate_func, - __gmp_reallocate_func & __gmp_free_func in gmp.h - Just getting the correct value by calling mp_get_memory_functions */ -#ifdef mp_get_memory_functions - #undef __gmp_allocate_func #undef __gmp_reallocate_func #undef __gmp_free_func @@ -275,19 +274,6 @@ __MPFR_DECLSPEC extern void * (*mpfr_reallocate_func) _MPFR_PROTO ((void *, __MPFR_DECLSPEC extern void (*mpfr_free_func) _MPFR_PROTO ((void *, size_t)); -#endif - -#undef __gmp_default_allocate -#undef __gmp_default_reallocate -#undef __gmp_default_free -#define __gmp_default_allocate mpfr_default_allocate -#define __gmp_default_reallocate mpfr_default_reallocate -#define __gmp_default_free mpfr_default_free -__MPFR_DECLSPEC void *__gmp_default_allocate _MPFR_PROTO ((size_t)); -__MPFR_DECLSPEC void *__gmp_default_reallocate _MPFR_PROTO ((void *, size_t, - size_t)); -__MPFR_DECLSPEC void __gmp_default_free _MPFR_PROTO ((void *, size_t)); - #if defined(WANT_GMP_INTERNALS) && defined(HAVE___GMPN_ROOTREM) #ifndef __gmpn_rootrem __MPFR_DECLSPEC mp_size_t __gmpn_rootrem _MPFR_PROTO ((mp_limb_t*, @@ -302,8 +288,8 @@ __MPFR_DECLSPEC void __gmp_default_free _MPFR_PROTO ((void *, size_t)); #endif #endif -/* Temp memory allocate */ +/* Temp memory allocate */ struct tmp_marker { void *ptr; @@ -322,51 +308,35 @@ __MPFR_DECLSPEC void mpfr_tmp_free _MPFR_PROTO ((struct tmp_marker *)); #define TMP_MARK(m) (tmp_marker = 0) #define TMP_FREE(m) mpfr_tmp_free (tmp_marker) +#endif /* GMP Internal replacement */ + + + +/****************************************************** + ****** GMP Interface which changes with versions ***** + ****** to other versions of GMP. Add missing ***** + ****** interfaces. ***** + ******************************************************/ + +/* If a mpn_sqr_n macro is not defined, use mpn_mul. GMP 4.x defines a + mpn_sqr_n macro in gmp-impl.h (and this macro disappeared in GMP 5), + so that GMP's macro can only be used when MPFR has been configured + with --with-gmp-build (and only with GMP 4.x). */ +#ifndef mpn_sqr_n +# define mpn_sqr_n(dst,src,n) mpn_mul((dst),(src),(n),(src),(n)) +#endif + /* invert_limb macro, copied from GMP 5.0.2, file gmp-impl.h. It returns invxl = floor((B^2-1)/xl)-B, where B=2^BITS_PER_LIMB, assuming the most significant bit of xl is set. */ -#undef invert_limb +#ifndef invert_limb #define invert_limb(invxl,xl) \ do { \ mp_limb_t dummy MPFR_MAYBE_UNUSED; \ MPFR_ASSERTD ((xl) != 0); \ udiv_qrnnd (invxl, dummy, ~(xl), ~(mp_limb_t)0, xl); \ } while (0) - -typedef struct {mp_limb_t inv32;} mpfr_pi1_t; /* We changed gmp_pi1_t into - mpfr_pi1_t to avoid using - GMP's namespace. */ -/* invert_pi1 macro, adapted from GMP 5.0.2, file gmp-impl.h. - It returns dinv = floor((B^3-1)/(d1*B+d0))-B, where B=2^BITS_PER_LIMB, - assuming the most significant bit of d1 is set. */ -#undef invert_pi1 -#define invert_pi1(dinv, d1, d0) \ - do { \ - mp_limb_t _v, _p, _t1, _t0, _mask; \ - invert_limb (_v, d1); \ - _p = d1 * _v; \ - _p += d0; \ - if (_p < d0) \ - { \ - _v--; \ - _mask = -(_p >= d1); \ - _p -= d1; \ - _v += _mask; \ - _p -= _mask & d1; \ - } \ - umul_ppmm (_t1, _t0, d0, _v); \ - _p += _t1; \ - if (_p < _t1) \ - { \ - _v--; \ - if (MPFR_UNLIKELY (_p >= d1)) \ - { \ - if (_p > d1 || _t0 >= d0) \ - _v--; \ - } \ - } \ - (dinv).inv32 = _v; \ - } while (0) +#endif /* udiv_qr_3by2 macro, adapted from GMP 5.0.2, file gmp-impl.h. Compute quotient the quotient and remainder for n / d. Requires d @@ -377,7 +347,7 @@ typedef struct {mp_limb_t inv32;} mpfr_pi1_t; /* We changed gmp_pi1_t into NOTE: Output variables are updated multiple times. Only some inputs and outputs may overlap. */ -#undef udiv_qr_3by2 +#ifndef udiv_qr_3by2 #define udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \ do { \ mp_limb_t _q0, _t1, _t0, _mask; \ @@ -405,7 +375,66 @@ typedef struct {mp_limb_t inv32;} mpfr_pi1_t; /* We changed gmp_pi1_t into } \ } \ } while (0) +#endif + +/* invert_pi1 macro adapted from GMP 5 */ +typedef struct {mp_limb_t inv32;} mpfr_pi1_t; +#ifndef invert_pi1 +#define invert_pi1(dinv, d1, d0) \ + do { \ + mp_limb_t _v, _p, _t1, _t0, _mask; \ + invert_limb (_v, d1); \ + _p = (d1) * _v; \ + _p += (d0); \ + if (_p < (d0)) \ + { \ + _v--; \ + _mask = -(mp_limb_t) (_p >= (d1)); \ + _p -= (d1); \ + _v += _mask; \ + _p -= _mask & (d1); \ + } \ + umul_ppmm (_t1, _t0, d0, _v); \ + _p += _t1; \ + if (_p < _t1) \ + { \ + _v--; \ + if (MPFR_UNLIKELY (_p >= (d1))) \ + { \ + if (_p > (d1) || _t0 >= (d0)) \ + _v--; \ + } \ + } \ + (dinv).inv32 = _v; \ + } while (0) +#endif + +/* mpn_copyd is a new exported function in GMP 5. + It existed in GMP 4 in the internal header, but still may not be + defined if HAVE_NATIVE_mpn_copyd is not defined */ +#if !__MPFR_GMP(5,0,0) +# undef mpn_copyd +# define mpn_copyd MPN_COPY +#endif + + + +/****************************************************** + ************* GMP Basic Pointer Types **************** + ******************************************************/ +/* Compatibility with old GMP versions. */ +#if !__MPFR_GMP(5,0,0) +typedef unsigned long mp_bitcnt_t; +#endif + +typedef mp_limb_t *mpfr_limb_ptr; +typedef __gmp_const mp_limb_t *mpfr_limb_srcptr; + + +/****************************************************** + ******************** C++ Compatibility *************** + ******************************************************/ #if defined (__cplusplus) } #endif diff --git a/src/mpfr-impl.h b/src/mpfr-impl.h index bba905eb2..3ed97a425 100644 --- a/src/mpfr-impl.h +++ b/src/mpfr-impl.h @@ -58,6 +58,8 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., # define __MPFR_WITHIN_MPFR 1 #endif + + /****************************************************** ****************** Include files ********************* ******************************************************/ @@ -82,6 +84,7 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., # ifndef __MPFR_H # include "mpfr.h" # endif +# include "mpfr-gmp.h" #else /* Build without gmp internals */ @@ -95,48 +98,15 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., # ifndef __MPFR_H # include "mpfr.h" # endif -# ifndef __GMPFR_GMP_H__ -# include "mpfr-gmp.h" -# endif +# include "mpfr-gmp.h" # ifdef MPFR_NEED_LONGLONG_H # define LONGLONG_STANDALONE # include "mpfr-longlong.h" # endif #endif -#undef MPFR_NEED_LONGLONG_H - -/* If a mpn_sqr_n macro is not defined, use mpn_mul. GMP 4.x defines a - mpn_sqr_n macro in gmp-impl.h (and this macro disappeared in GMP 5), - so that GMP's macro can only be used when MPFR has been configured - with --with-gmp-build (and only with GMP 4.x). */ -#ifndef mpn_sqr_n -# define mpn_sqr_n(dst,src,n) mpn_mul((dst),(src),(n),(src),(n)) -#endif - -/* For the definition of MPFR_THREAD_ATTR. GCC/ICC detection macros are - no longer used, as they sometimes gave incorrect information about - the support of thread-local variables. A configure check is now done. */ -#include "mpfr-thread.h" -/* Macro to detect the GMP version */ -#if defined(__GNU_MP_VERSION) && \ - defined(__GNU_MP_VERSION_MINOR) && \ - defined(__GNU_MP_VERSION_PATCHLEVEL) -# define __MPFR_GMP(a,b,c) \ - (MPFR_VERSION_NUM(__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR,__GNU_MP_VERSION_PATCHLEVEL) >= MPFR_VERSION_NUM(a,b,c)) -#else -# define __MPFR_GMP(a,b,c) 0 -#endif - - - -/****************************************************** - ************* GMP Basic Pointer Types **************** - ******************************************************/ - -typedef mp_limb_t *mpfr_limb_ptr; -typedef __gmp_const mp_limb_t *mpfr_limb_srcptr; +#undef MPFR_NEED_LONGLONG_H @@ -162,34 +132,13 @@ typedef __gmp_const mp_limb_t *mpfr_limb_srcptr; /****************************************************** - ******************** Check GMP *********************** + ************** Attributes definition ***************** ******************************************************/ -#if !__MPFR_GMP(4,1,0) -# error "GMP 4.1.0 or newer needed" -#endif - -#if GMP_NAIL_BITS != 0 -# error "MPFR doesn't support nonzero values of GMP_NAIL_BITS" -#endif - -#if (GMP_NUMB_BITS<32) || (GMP_NUMB_BITS & (GMP_NUMB_BITS - 1)) -# error "GMP_NUMB_BITS must be a power of 2, and >= 32" -#endif - -#if GMP_NUMB_BITS == 16 -# define MPFR_LOG2_GMP_NUMB_BITS 4 -#elif GMP_NUMB_BITS == 32 -# define MPFR_LOG2_GMP_NUMB_BITS 5 -#elif GMP_NUMB_BITS == 64 -# define MPFR_LOG2_GMP_NUMB_BITS 6 -#elif GMP_NUMB_BITS == 128 -# define MPFR_LOG2_GMP_NUMB_BITS 7 -#elif GMP_NUMB_BITS == 256 -# define MPFR_LOG2_GMP_NUMB_BITS 8 -#else -# error "Can't compute log2(GMP_NUMB_BITS)" -#endif +/* For the definition of MPFR_THREAD_ATTR. GCC/ICC detection macros are + no longer used, as they sometimes gave incorrect information about + the support of thread-local variables. A configure check is now done. */ +#include "mpfr-thread.h" #if defined(MPFR_HAVE_NORETURN) /* _Noreturn is specified by ISO C11 (Section 6.7.4); @@ -225,16 +174,16 @@ typedef __gmp_const mp_limb_t *mpfr_limb_srcptr; #define MPFR_MAYBE_UNUSED #endif -/* Compatibility with old GMP versions. */ -#if __GNU_MP_VERSION < 5 -typedef unsigned long mp_bitcnt_t; -#endif /****************************************************** ************* Global Internal Variables ************** ******************************************************/ +#if defined (__cplusplus) +extern "C" { +#endif + /* Cache struct */ struct __gmpfr_cache_s { mpfr_t x; @@ -244,10 +193,6 @@ struct __gmpfr_cache_s { typedef struct __gmpfr_cache_s mpfr_cache_t[1]; typedef struct __gmpfr_cache_s *mpfr_cache_ptr; -#if defined (__cplusplus) -extern "C" { -#endif - __MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_flags_t __gmpfr_flags; __MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_exp_t __gmpfr_emin; __MPFR_DECLSPEC extern MPFR_THREAD_ATTR mpfr_exp_t __gmpfr_emax; diff --git a/src/mulders.c b/src/mulders.c index 2da87f78c..a9e049f13 100644 --- a/src/mulders.c +++ b/src/mulders.c @@ -196,10 +196,6 @@ static short divhigh_ktab[] = {MPFR_DIVHIGH_TAB}; #define MPFR_DIVHIGH_TAB_SIZE (sizeof(divhigh_ktab) / sizeof(divhigh_ktab[0])) #endif -#ifndef __GMPFR_GMP_H__ -#define mpfr_pi1_t gmp_pi1_t /* with a GMP build */ -#endif - #if !(defined(WANT_GMP_INTERNALS) && defined(HAVE___GMPN_SBPI1_DIVAPPR_Q)) /* Put in Q={qp, n} an approximation of N={np, 2*n} divided by D={dp, n}, with the most significant limb of the quotient as return value (0 or 1). diff --git a/tests/memory.c b/tests/memory.c index 025e4127e..dc1594736 100644 --- a/tests/memory.c +++ b/tests/memory.c @@ -77,12 +77,12 @@ tests_allocate (size_t size) abort (); } - h = (struct header *) __gmp_default_allocate (sizeof (*h)); + h = (struct header *) mpfr_default_allocate (sizeof (*h)); h->next = tests_memory_list; tests_memory_list = h; h->size = size; - h->ptr = __gmp_default_allocate (size); + h->ptr = mpfr_default_allocate (size); return h->ptr; } @@ -117,7 +117,7 @@ tests_reallocate (void *ptr, size_t old_size, size_t new_size) } h->size = new_size; - h->ptr = __gmp_default_reallocate (ptr, old_size, new_size); + h->ptr = mpfr_default_reallocate (ptr, old_size, new_size); return h->ptr; } @@ -142,8 +142,8 @@ tests_free_nosize (void *ptr) *hp = h->next; /* unlink */ - __gmp_default_free (ptr, h->size); - __gmp_default_free (h, sizeof (*h)); + mpfr_default_free (ptr, h->size); + mpfr_default_free (h, sizeof (*h)); } static void @@ -164,6 +164,41 @@ tests_free (void *ptr, size_t size) tests_free_nosize (ptr); } +void * +mpfr_default_allocate (size_t size) +{ + void *ret; + ret = malloc (size); + if (MPFR_UNLIKELY (ret == NULL)) + { + fprintf (stderr, "MPFR: Can't allocate memory (size=%lu)\n", + (unsigned long) size); + abort (); + } + return ret; +} + +void * +mpfr_default_reallocate (void *oldptr, size_t old_size, size_t new_size) +{ + void *ret; + ret = realloc (oldptr, new_size); + if (MPFR_UNLIKELY(ret == NULL)) + { + fprintf (stderr, + "MPFR: Can't reallocate memory (old_size=%lu new_size=%lu)\n", + (unsigned long) old_size, (unsigned long) new_size); + abort (); + } + return ret; +} + +void +mpfr_default_free (void *blk_ptr, size_t blk_size) +{ + free (blk_ptr); +} + void tests_memory_start (void) { diff --git a/tests/mpfr-test.h b/tests/mpfr-test.h index dafe98df0..ee1014812 100644 --- a/tests/mpfr-test.h +++ b/tests/mpfr-test.h @@ -34,6 +34,10 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., #include "mpfr-impl.h" +#if defined (__cplusplus) +extern "C" { +#endif + /* generates a random long int, a random double, and corresponding seed initializing */ #define DBL_RAND() ((double) randlimb() / (double) MP_LIMB_T_MAX) @@ -70,10 +74,6 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., #define FLIST mpfr_ptr, mpfr_srcptr, mpfr_rnd_t -#if defined (__cplusplus) -extern "C" { -#endif - void test_version _MPFR_PROTO ((void)); void tests_memory_start _MPFR_PROTO ((void)); @@ -113,10 +113,6 @@ int mpfr_cmp_str _MPFR_PROTO ((mpfr_srcptr x, const char *, int, mpfr_rnd_t)); #define mpfr_cmp0(x,y) (MPFR_ASSERTN (!MPFR_IS_NAN (x) && !MPFR_IS_NAN (y)), mpfr_cmp (x,y)) #define mpfr_cmp_ui0(x,i) (MPFR_ASSERTN (!MPFR_IS_NAN (x)), mpfr_cmp_ui (x,i)) -#if defined (__cplusplus) -} -#endif - /* define CHECK_EXTERNAL if you want to check mpfr against another library with correct rounding. You'll probably have to modify mpfr_print_raw() and/or test_add() below: @@ -179,4 +175,38 @@ mpfr_print_raw (mpfr_srcptr x) } #endif +/* Random */ +__MPFR_DECLSPEC extern char mpfr_rands_initialized; +__MPFR_DECLSPEC extern gmp_randstate_t mpfr_rands; + +#undef RANDS +#define RANDS \ + ((mpfr_rands_initialized ? 0 \ + : (mpfr_rands_initialized = 1, \ + gmp_randinit_default (mpfr_rands), 0)), \ + mpfr_rands) + +#undef RANDS_CLEAR +#define RANDS_CLEAR() \ + do { \ + if (mpfr_rands_initialized) \ + { \ + mpfr_rands_initialized = 0; \ + gmp_randclear (mpfr_rands); \ + } \ + } while (0) + +typedef __gmp_randstate_struct *gmp_randstate_ptr; + +/* Allocation */ +__MPFR_DECLSPEC void *mpfr_default_allocate _MPFR_PROTO ((size_t)); +__MPFR_DECLSPEC void *mpfr_default_reallocate _MPFR_PROTO ((void *, size_t, + size_t)); +__MPFR_DECLSPEC void mpfr_default_free _MPFR_PROTO ((void *, size_t)); + + +#if defined (__cplusplus) +} +#endif + #endif diff --git a/tests/tests.c b/tests/tests.c index 604c2a18c..db109bfe8 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -101,6 +101,9 @@ set_fpu_prec (void) #endif +char mpfr_rands_initialized = 0; +gmp_randstate_t mpfr_rands; + static mpfr_exp_t default_emin, default_emax; static void tests_rand_start (void); @@ -123,7 +126,6 @@ test_version (void) /* VL: I get the following error on an OpenSUSE machine, and changing the value of shlibpath_overrides_runpath in the libtool file from 'no' to 'yes' fixes the problem. */ - version = mpfr_get_version (); if (strcmp (MPFR_VERSION_STRING, version) == 0) { @@ -271,17 +273,17 @@ tests_rand_start (void) char *perform_seed; unsigned long seed; - if (__gmp_rands_initialized) + if (mpfr_rands_initialized) { printf ( - "Please let tests_start() initialize the global __gmp_rands, i.e.\n" + "Please let tests_start() initialize the global mpfr_rands, i.e.\n" "ensure that function is called before the first use of RANDS.\n"); exit (1); } - gmp_randinit_default (__gmp_rands); - __gmp_rands_initialized = 1; - rands = __gmp_rands; + gmp_randinit_default (mpfr_rands); + mpfr_rands_initialized = 1; + rands = mpfr_rands; perform_seed = getenv ("GMP_CHECK_RANDOMIZE"); if (perform_seed != NULL) |