Home | History | Annotate | Line # | Download | only in tests
mpfr-test.h revision 1.1.1.3
      1      1.1  mrg /* auxiliary functions for MPFR tests.
      2      1.1  mrg 
      3  1.1.1.3  mrg Copyright 1999-2016 Free Software Foundation, Inc.
      4  1.1.1.3  mrg Contributed by the AriC and Caramba projects, INRIA.
      5      1.1  mrg 
      6      1.1  mrg This file is part of the GNU MPFR Library.
      7      1.1  mrg 
      8      1.1  mrg The GNU MPFR Library is free software; you can redistribute it and/or modify
      9      1.1  mrg it under the terms of the GNU Lesser General Public License as published by
     10      1.1  mrg the Free Software Foundation; either version 3 of the License, or (at your
     11      1.1  mrg option) any later version.
     12      1.1  mrg 
     13      1.1  mrg The GNU MPFR Library is distributed in the hope that it will be useful, but
     14      1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15      1.1  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16      1.1  mrg License for more details.
     17      1.1  mrg 
     18      1.1  mrg You should have received a copy of the GNU Lesser General Public License
     19      1.1  mrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20      1.1  mrg http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21      1.1  mrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22      1.1  mrg 
     23      1.1  mrg #ifndef __MPFR_TEST_H__
     24      1.1  mrg #define __MPFR_TEST_H__
     25      1.1  mrg 
     26      1.1  mrg #include <stdio.h>
     27      1.1  mrg 
     28      1.1  mrg #include "mpfr-impl.h"
     29      1.1  mrg 
     30      1.1  mrg /* generates a random long int, a random double,
     31      1.1  mrg    and corresponding seed initializing */
     32      1.1  mrg #define DBL_RAND() ((double) randlimb() / (double) MP_LIMB_T_MAX)
     33      1.1  mrg 
     34      1.1  mrg #define MINNORM 2.2250738585072013831e-308 /* 2^(-1022), smallest normalized */
     35      1.1  mrg #define MAXNORM 1.7976931348623157081e308 /* 2^(1023)*(2-2^(-52)) */
     36      1.1  mrg 
     37      1.1  mrg /* Generates a random rounding mode */
     38      1.1  mrg #define RND_RAND() ((mpfr_rnd_t) (randlimb() % MPFR_RND_MAX))
     39      1.1  mrg 
     40      1.1  mrg /* Generates a random sign */
     41      1.1  mrg #define SIGN_RAND() ( (randlimb()%2) ? MPFR_SIGN_POS : MPFR_SIGN_NEG)
     42      1.1  mrg 
     43      1.1  mrg /* Loop for all rounding modes */
     44      1.1  mrg #define RND_LOOP(_r) for((_r) = 0 ; (_r) < MPFR_RND_MAX ; (_r)++)
     45      1.1  mrg 
     46      1.1  mrg /* Test whether two floating-point data have the same value,
     47      1.1  mrg    seen as an element of the set of the floating-point data
     48      1.1  mrg    (Level 2 in the IEEE 754-2008 standard). */
     49      1.1  mrg #define SAME_VAL(X,Y)                                                   \
     50      1.1  mrg   ((MPFR_IS_NAN (X) && MPFR_IS_NAN (Y)) ||                              \
     51      1.1  mrg    (mpfr_equal_p ((X), (Y)) && MPFR_INT_SIGN (X) == MPFR_INT_SIGN (Y)))
     52      1.1  mrg 
     53      1.1  mrg /* The MAX, MIN and ABS macros may already be defined if gmp-impl.h has
     54      1.1  mrg    been included. They have the same semantics as in gmp-impl.h, but the
     55      1.1  mrg    expressions may be slightly different. So, it's better to undefine
     56      1.1  mrg    them first, as required by the ISO C standard. */
     57      1.1  mrg #undef MAX
     58      1.1  mrg #undef MIN
     59      1.1  mrg #undef ABS
     60      1.1  mrg #define MAX(a, b) (((a) > (b)) ? (a) : (b))
     61      1.1  mrg #define MIN(a, b) (((a) < (b)) ? (a) : (b))
     62      1.1  mrg #define ABS(x) (((x)>0) ? (x) : -(x))
     63      1.1  mrg 
     64      1.1  mrg #define FLIST mpfr_ptr, mpfr_srcptr, mpfr_rnd_t
     65      1.1  mrg 
     66      1.1  mrg #if defined (__cplusplus)
     67      1.1  mrg extern "C" {
     68      1.1  mrg #endif
     69      1.1  mrg 
     70  1.1.1.3  mrg int test_version _MPFR_PROTO ((void));
     71      1.1  mrg 
     72      1.1  mrg void tests_memory_start _MPFR_PROTO ((void));
     73      1.1  mrg void tests_memory_end _MPFR_PROTO ((void));
     74      1.1  mrg 
     75      1.1  mrg void tests_start_mpfr _MPFR_PROTO ((void));
     76      1.1  mrg void tests_end_mpfr _MPFR_PROTO ((void));
     77      1.1  mrg 
     78      1.1  mrg int mpfr_set_machine_rnd_mode _MPFR_PROTO ((mpfr_rnd_t));
     79      1.1  mrg void mpfr_test_init _MPFR_PROTO ((void));
     80      1.1  mrg mp_limb_t randlimb _MPFR_PROTO ((void));
     81      1.1  mrg void randseed _MPFR_PROTO ((unsigned int));
     82      1.1  mrg void mpfr_random2 _MPFR_PROTO ((mpfr_ptr, mp_size_t, mpfr_exp_t, gmp_randstate_t));
     83      1.1  mrg int ulp _MPFR_PROTO ((double, double));
     84      1.1  mrg double dbl _MPFR_PROTO ((double, int));
     85      1.1  mrg double Ulp _MPFR_PROTO ((double));
     86      1.1  mrg int Isnan _MPFR_PROTO ((double));
     87      1.1  mrg void d_trace _MPFR_PROTO ((const char *, double));
     88      1.1  mrg void ld_trace _MPFR_PROTO ((const char *, long double));
     89      1.1  mrg 
     90      1.1  mrg FILE *src_fopen _MPFR_PROTO ((const char *, const char *));
     91      1.1  mrg void set_emin _MPFR_PROTO ((mpfr_exp_t));
     92      1.1  mrg void set_emax _MPFR_PROTO ((mpfr_exp_t));
     93  1.1.1.3  mrg void tests_default_random _MPFR_PROTO ((mpfr_ptr, int, mpfr_exp_t, mpfr_exp_t,
     94  1.1.1.3  mrg                                         int));
     95  1.1.1.2  mrg void data_check _MPFR_PROTO ((const char *, int (*) (FLIST), const char *));
     96      1.1  mrg void bad_cases _MPFR_PROTO ((int (*)(FLIST), int (*)(FLIST),
     97  1.1.1.2  mrg                              const char *, int, mpfr_exp_t, mpfr_exp_t,
     98      1.1  mrg                              mpfr_prec_t, mpfr_prec_t, mpfr_prec_t, int));
     99  1.1.1.2  mrg void flags_out _MPFR_PROTO ((unsigned int));
    100      1.1  mrg 
    101      1.1  mrg int mpfr_cmp_str _MPFR_PROTO ((mpfr_srcptr x, const char *, int, mpfr_rnd_t));
    102      1.1  mrg #define mpfr_cmp_str1(x,s) mpfr_cmp_str(x,s,10,MPFR_RNDN)
    103      1.1  mrg #define mpfr_set_str1(x,s) mpfr_set_str(x,s,10,MPFR_RNDN)
    104      1.1  mrg 
    105      1.1  mrg #define mpfr_cmp0(x,y) (MPFR_ASSERTN (!MPFR_IS_NAN (x) && !MPFR_IS_NAN (y)), mpfr_cmp (x,y))
    106      1.1  mrg #define mpfr_cmp_ui0(x,i) (MPFR_ASSERTN (!MPFR_IS_NAN (x)), mpfr_cmp_ui (x,i))
    107      1.1  mrg 
    108  1.1.1.3  mrg /* Allocation */
    109  1.1.1.3  mrg void *tests_allocate _MPFR_PROTO ((size_t));
    110  1.1.1.3  mrg void *tests_reallocate _MPFR_PROTO ((void *, size_t, size_t));
    111  1.1.1.3  mrg void tests_free _MPFR_PROTO ((void *, size_t));
    112  1.1.1.3  mrg 
    113      1.1  mrg #if defined (__cplusplus)
    114      1.1  mrg }
    115      1.1  mrg #endif
    116      1.1  mrg 
    117      1.1  mrg /* define CHECK_EXTERNAL if you want to check mpfr against another library
    118      1.1  mrg    with correct rounding. You'll probably have to modify mpfr_print_raw()
    119      1.1  mrg    and/or test_add() below:
    120      1.1  mrg    * mpfr_print_raw() prints each number as "p m e" where p is the precision,
    121      1.1  mrg      m the mantissa (as a binary integer with sign), and e the exponent.
    122      1.1  mrg      The corresponding number is m*2^e. Example: "2 10 -6" represents
    123      1.1  mrg      2*2^(-6) with a precision of 2 bits.
    124      1.1  mrg    * test_add() outputs "b c a" on one line, for each addition a <- b + c.
    125      1.1  mrg      Currently it only prints such a line for rounding to nearest, when
    126      1.1  mrg      the inputs b and c are not NaN and/or Inf.
    127      1.1  mrg */
    128      1.1  mrg #ifdef CHECK_EXTERNAL
    129      1.1  mrg static void
    130      1.1  mrg mpfr_print_raw (mpfr_srcptr x)
    131      1.1  mrg {
    132      1.1  mrg   printf ("%lu ", MPFR_PREC (x));
    133      1.1  mrg   if (MPFR_IS_NAN (x))
    134      1.1  mrg     {
    135      1.1  mrg       printf ("@NaN@");
    136      1.1  mrg       return;
    137      1.1  mrg     }
    138      1.1  mrg 
    139      1.1  mrg   if (MPFR_SIGN (x) < 0)
    140      1.1  mrg     printf ("-");
    141      1.1  mrg 
    142      1.1  mrg   if (MPFR_IS_INF (x))
    143      1.1  mrg     printf ("@Inf@");
    144      1.1  mrg   else if (MPFR_IS_ZERO (x))
    145      1.1  mrg     printf ("0 0");
    146      1.1  mrg   else
    147      1.1  mrg     {
    148      1.1  mrg       mp_limb_t *mx;
    149      1.1  mrg       mpfr_prec_t px;
    150      1.1  mrg       mp_size_t n;
    151      1.1  mrg 
    152      1.1  mrg       mx = MPFR_MANT (x);
    153      1.1  mrg       px = MPFR_PREC (x);
    154      1.1  mrg 
    155      1.1  mrg       for (n = (px - 1) / GMP_NUMB_BITS; ; n--)
    156      1.1  mrg         {
    157      1.1  mrg           mp_limb_t wd, t;
    158      1.1  mrg 
    159      1.1  mrg           MPFR_ASSERTN (n >= 0);
    160      1.1  mrg           wd = mx[n];
    161      1.1  mrg           for (t = MPFR_LIMB_HIGHBIT; t != 0; t >>= 1)
    162      1.1  mrg             {
    163      1.1  mrg               printf ((wd & t) == 0 ? "0" : "1");
    164      1.1  mrg               if (--px == 0)
    165      1.1  mrg                 {
    166      1.1  mrg                   mpfr_exp_t ex;
    167      1.1  mrg 
    168      1.1  mrg                   ex = MPFR_GET_EXP (x);
    169      1.1  mrg                   MPFR_ASSERTN (ex >= LONG_MIN && ex <= LONG_MAX);
    170      1.1  mrg                   printf (" %ld", (long) ex - (long) MPFR_PREC (x));
    171      1.1  mrg                   return;
    172      1.1  mrg                 }
    173      1.1  mrg             }
    174      1.1  mrg         }
    175      1.1  mrg     }
    176      1.1  mrg }
    177      1.1  mrg #endif
    178      1.1  mrg 
    179      1.1  mrg #endif
    180