xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/mpfr-test.h (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* auxiliary functions for MPFR tests.
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_TEST_H__
24 #define __MPFR_TEST_H__
25 
26 #include <stdio.h>
27 
28 #include "mpfr-impl.h"
29 
30 /* generates a random long int, a random double,
31    and corresponding seed initializing */
32 #define DBL_RAND() ((double) randlimb() / (double) MP_LIMB_T_MAX)
33 
34 #define MINNORM 2.2250738585072013831e-308 /* 2^(-1022), smallest normalized */
35 #define MAXNORM 1.7976931348623157081e308 /* 2^(1023)*(2-2^(-52)) */
36 
37 /* Generates a random rounding mode */
38 #define RND_RAND() ((mpfr_rnd_t) (randlimb() % MPFR_RND_MAX))
39 
40 /* Generates a random sign */
41 #define SIGN_RAND() ( (randlimb()%2) ? MPFR_SIGN_POS : MPFR_SIGN_NEG)
42 
43 /* Loop for all rounding modes */
44 #define RND_LOOP(_r) for((_r) = 0 ; (_r) < MPFR_RND_MAX ; (_r)++)
45 
46 /* Test whether two floating-point data have the same value,
47    seen as an element of the set of the floating-point data
48    (Level 2 in the IEEE 754-2008 standard). */
49 #define SAME_VAL(X,Y)                                                   \
50   ((MPFR_IS_NAN (X) && MPFR_IS_NAN (Y)) ||                              \
51    (mpfr_equal_p ((X), (Y)) && MPFR_INT_SIGN (X) == MPFR_INT_SIGN (Y)))
52 
53 /* The MAX, MIN and ABS macros may already be defined if gmp-impl.h has
54    been included. They have the same semantics as in gmp-impl.h, but the
55    expressions may be slightly different. So, it's better to undefine
56    them first, as required by the ISO C standard. */
57 #undef MAX
58 #undef MIN
59 #undef ABS
60 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
61 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
62 #define ABS(x) (((x)>0) ? (x) : -(x))
63 
64 #define FLIST mpfr_ptr, mpfr_srcptr, mpfr_rnd_t
65 
66 #if defined (__cplusplus)
67 extern "C" {
68 #endif
69 
70 int test_version _MPFR_PROTO ((void));
71 
72 void tests_memory_start _MPFR_PROTO ((void));
73 void tests_memory_end _MPFR_PROTO ((void));
74 
75 void tests_start_mpfr _MPFR_PROTO ((void));
76 void tests_end_mpfr _MPFR_PROTO ((void));
77 
78 int mpfr_set_machine_rnd_mode _MPFR_PROTO ((mpfr_rnd_t));
79 void mpfr_test_init _MPFR_PROTO ((void));
80 mp_limb_t randlimb _MPFR_PROTO ((void));
81 void randseed _MPFR_PROTO ((unsigned int));
82 void mpfr_random2 _MPFR_PROTO ((mpfr_ptr, mp_size_t, mpfr_exp_t, gmp_randstate_t));
83 int ulp _MPFR_PROTO ((double, double));
84 double dbl _MPFR_PROTO ((double, int));
85 double Ulp _MPFR_PROTO ((double));
86 int Isnan _MPFR_PROTO ((double));
87 void d_trace _MPFR_PROTO ((const char *, double));
88 void ld_trace _MPFR_PROTO ((const char *, long double));
89 
90 FILE *src_fopen _MPFR_PROTO ((const char *, const char *));
91 void set_emin _MPFR_PROTO ((mpfr_exp_t));
92 void set_emax _MPFR_PROTO ((mpfr_exp_t));
93 void tests_default_random _MPFR_PROTO ((mpfr_ptr, int, mpfr_exp_t, mpfr_exp_t,
94                                         int));
95 void data_check _MPFR_PROTO ((const char *, int (*) (FLIST), const char *));
96 void bad_cases _MPFR_PROTO ((int (*)(FLIST), int (*)(FLIST),
97                              const char *, int, mpfr_exp_t, mpfr_exp_t,
98                              mpfr_prec_t, mpfr_prec_t, mpfr_prec_t, int));
99 void flags_out _MPFR_PROTO ((unsigned int));
100 
101 int mpfr_cmp_str _MPFR_PROTO ((mpfr_srcptr x, const char *, int, mpfr_rnd_t));
102 #define mpfr_cmp_str1(x,s) mpfr_cmp_str(x,s,10,MPFR_RNDN)
103 #define mpfr_set_str1(x,s) mpfr_set_str(x,s,10,MPFR_RNDN)
104 
105 #define mpfr_cmp0(x,y) (MPFR_ASSERTN (!MPFR_IS_NAN (x) && !MPFR_IS_NAN (y)), mpfr_cmp (x,y))
106 #define mpfr_cmp_ui0(x,i) (MPFR_ASSERTN (!MPFR_IS_NAN (x)), mpfr_cmp_ui (x,i))
107 
108 /* Allocation */
109 void *tests_allocate _MPFR_PROTO ((size_t));
110 void *tests_reallocate _MPFR_PROTO ((void *, size_t, size_t));
111 void tests_free _MPFR_PROTO ((void *, size_t));
112 
113 #if defined (__cplusplus)
114 }
115 #endif
116 
117 /* define CHECK_EXTERNAL if you want to check mpfr against another library
118    with correct rounding. You'll probably have to modify mpfr_print_raw()
119    and/or test_add() below:
120    * mpfr_print_raw() prints each number as "p m e" where p is the precision,
121      m the mantissa (as a binary integer with sign), and e the exponent.
122      The corresponding number is m*2^e. Example: "2 10 -6" represents
123      2*2^(-6) with a precision of 2 bits.
124    * test_add() outputs "b c a" on one line, for each addition a <- b + c.
125      Currently it only prints such a line for rounding to nearest, when
126      the inputs b and c are not NaN and/or Inf.
127 */
128 #ifdef CHECK_EXTERNAL
129 static void
130 mpfr_print_raw (mpfr_srcptr x)
131 {
132   printf ("%lu ", MPFR_PREC (x));
133   if (MPFR_IS_NAN (x))
134     {
135       printf ("@NaN@");
136       return;
137     }
138 
139   if (MPFR_SIGN (x) < 0)
140     printf ("-");
141 
142   if (MPFR_IS_INF (x))
143     printf ("@Inf@");
144   else if (MPFR_IS_ZERO (x))
145     printf ("0 0");
146   else
147     {
148       mp_limb_t *mx;
149       mpfr_prec_t px;
150       mp_size_t n;
151 
152       mx = MPFR_MANT (x);
153       px = MPFR_PREC (x);
154 
155       for (n = (px - 1) / GMP_NUMB_BITS; ; n--)
156         {
157           mp_limb_t wd, t;
158 
159           MPFR_ASSERTN (n >= 0);
160           wd = mx[n];
161           for (t = MPFR_LIMB_HIGHBIT; t != 0; t >>= 1)
162             {
163               printf ((wd & t) == 0 ? "0" : "1");
164               if (--px == 0)
165                 {
166                   mpfr_exp_t ex;
167 
168                   ex = MPFR_GET_EXP (x);
169                   MPFR_ASSERTN (ex >= LONG_MIN && ex <= LONG_MAX);
170                   printf (" %ld", (long) ex - (long) MPFR_PREC (x));
171                   return;
172                 }
173             }
174         }
175     }
176 }
177 #endif
178 
179 #endif
180