1 /* Miscellaneous support for test programs.
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
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.
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.
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. */
25 # include "config.h" /* for a build within gmp */
37 #ifdef MPFR_TEST_DIVBYZERO
41 #ifdef TIME_WITH_SYS_TIME
42 # include <sys/time.h> /* for struct timeval */
44 #elif defined HAVE_SYS_TIME_H
45 # include <sys/time.h>
50 /* <sys/fpu.h> is needed to have union fpc_csr defined under IRIX64
51 (see below). Let's include it only if need be. */
52 #if defined HAVE_SYS_FPU_H && defined HAVE_FPC_CSR
56 #ifdef MPFR_TESTS_TIMEOUT
57 #include <sys/resource.h>
60 #include "mpfr-test.h"
63 /* This option allows to test MPFR on x86 processors when the FPU
64 * rounding precision has been changed. As MPFR is a library, this can
65 * occur in practice, either by the calling software or by some other
66 * library or plug-in used by the calling software. This option is
67 * mainly for developers. If it is used, then the <fpu_control.h>
68 * header is assumed to exist and work like under Linux/x86. MPFR does
69 * not need to be recompiled. So, a possible usage is the following:
73 * make check CFLAGS="-g -O2 -ffloat-store -DMPFR_FPU_PREC=_FPU_SINGLE"
75 * i.e. just add -DMPFR_FPU_PREC=... to the CFLAGS found in Makefile.
78 * + SSE2 (used to implement double's on x86_64, and possibly on x86
79 * too, depending on the compiler configuration and flags) is not
80 * affected by the dynamic precision.
81 * + When the FPU is set to single precision, the behavior of MPFR
82 * functions that have a native floating-point type (float, double,
83 * long double) as argument or return value is not guaranteed.
86 #include <fpu_control.h>
94 cw &= ~(_FPU_EXTENDED|_FPU_DOUBLE|_FPU_SINGLE);
95 cw |= (MPFR_FPU_PREC);
101 static mpfr_exp_t default_emin, default_emax;
103 static void tests_rand_start (void);
104 static void tests_rand_end (void);
105 static void tests_limit_start (void);
107 /* We want to always import the function mpfr_dump inside the test
108 suite, so that we can use it in GDB. But it doesn't work if we build
109 a Windows DLL (initializer element is not a constant) */
110 #if !__GMP_LIBGMP_DLL
111 extern void (*dummy_func) (mpfr_srcptr);
112 void (*dummy_func)(mpfr_srcptr) = mpfr_dump;
120 /* VL: I get the following error on an OpenSUSE machine, and changing
121 the value of shlibpath_overrides_runpath in the libtool file from
122 'no' to 'yes' fixes the problem. */
124 version = mpfr_get_version ();
125 if (strcmp (MPFR_VERSION_STRING, version) == 0)
130 sprintf (buffer, "%d.%d.%d", MPFR_VERSION_MAJOR, MPFR_VERSION_MINOR,
131 MPFR_VERSION_PATCHLEVEL);
132 for (i = 0; buffer[i] == version[i]; i++)
133 if (buffer[i] == '\0')
135 if (buffer[i] == '\0' && version[i] == '-')
137 printf ("MPFR_VERSION_MAJOR.MPFR_VERSION_MINOR.MPFR_VERSION_PATCHLEVEL"
138 " (%s)\nand MPFR_VERSION_STRING (%s) do not match!\nIt seems "
139 "that the mpfr.h file has been corrupted.\n", buffer, version);
143 printf ("Incorrect MPFR version! (%s header vs %s library)\n"
144 "Nothing else has been tested since for this reason,\n"
145 "any other test may fail. Please fix this one first.\n\n"
146 "You can try to avoid this problem by changing the value of\n"
147 "shlibpath_overrides_runpath in the libtool file and rebuild\n"
148 "MPFR (make clean && make && make check).\n"
149 "Otherwise this error may be due to a corrupted mpfr.h, an\n"
150 "incomplete build (try to rebuild MPFR from scratch and/or\n"
151 "use 'make clean'), or something wrong in the system.\n",
152 MPFR_VERSION_STRING, version);
157 tests_start_mpfr (void)
161 /* don't buffer, so output is not lost if a test causes a segv etc */
162 setbuf (stdout, NULL);
164 #if defined HAVE_LOCALE_H && defined HAVE_SETLOCALE
165 /* Added on 2005-07-09. This allows to test MPFR under various
166 locales. New bugs will probably be found, in particular with
167 LC_ALL="tr_TR.ISO8859-9" because of the i/I character... */
168 setlocale (LC_ALL, "");
175 #ifdef MPFR_TEST_DIVBYZERO
176 /* Define to test the use of MPFR_ERRDIVZERO */
177 feclearexcept (FE_ALL_EXCEPT);
180 tests_memory_start ();
182 tests_limit_start ();
184 default_emin = mpfr_get_emin ();
185 default_emax = mpfr_get_emax ();
189 tests_end_mpfr (void)
193 if (mpfr_get_emin () != default_emin)
195 printf ("Default emin value has not been restored!\n");
199 if (mpfr_get_emax () != default_emax)
201 printf ("Default emax value has not been restored!\n");
209 #ifdef MPFR_TEST_DIVBYZERO
210 /* Define to test the use of MPFR_ERRDIVZERO */
211 if (fetestexcept (FE_DIVBYZERO|FE_INVALID))
213 printf ("A floating-point division by 0 or an invalid operation"
215 #ifdef MPFR_ERRDIVZERO
216 /* This should never occur because the purpose of defining
217 MPFR_ERRDIVZERO is to avoid all the FP divisions by 0. */
228 tests_limit_start (void)
230 #ifdef MPFR_TESTS_TIMEOUT
231 struct rlimit rlim[1];
235 timeoutp = getenv ("MPFR_TESTS_TIMEOUT");
236 timeout = timeoutp != NULL ? atoi (timeoutp) : MPFR_TESTS_TIMEOUT;
239 /* We need to call getrlimit first to initialize rlim_max to
240 an acceptable value for setrlimit. When enabled, timeouts
241 are regarded as important: we don't want to take too much
242 CPU time on machines shared with other users. So, if we
243 can't set the timeout, we exit immediately. */
244 if (getrlimit (RLIMIT_CPU, rlim))
246 printf ("Error: getrlimit failed\n");
249 rlim->rlim_cur = timeout;
250 if (setrlimit (RLIMIT_CPU, rlim))
252 printf ("Error: setrlimit failed\n");
260 tests_rand_start (void)
262 gmp_randstate_ptr rands;
266 if (__gmp_rands_initialized)
269 "Please let tests_start() initialize the global __gmp_rands, i.e.\n"
270 "ensure that function is called before the first use of RANDS.\n");
274 gmp_randinit_default (__gmp_rands);
275 __gmp_rands_initialized = 1;
278 perform_seed = getenv ("GMP_CHECK_RANDOMIZE");
279 if (perform_seed != NULL)
281 seed = strtoul (perform_seed, NULL, 10);
282 if (! (seed == 0 || seed == 1))
284 printf ("Re-seeding with GMP_CHECK_RANDOMIZE=%lu\n", seed);
285 gmp_randseed_ui (rands, seed);
289 #ifdef HAVE_GETTIMEOFDAY
291 gettimeofday (&tv, NULL);
292 seed = tv.tv_sec + tv.tv_usec;
298 gmp_randseed_ui (rands, seed);
299 printf ("Seed GMP_CHECK_RANDOMIZE=%lu "
300 "(include this in bug reports)\n", seed);
304 gmp_randseed_ui (rands, 0x2143FEDC);
308 tests_rand_end (void)
313 /* initialization function for tests using the hardware floats
314 Not very useful now. */
316 mpfr_test_init (void)
320 /* to get denormalized numbers on IRIX64 */
323 exp.fc_word = get_fpc_csr();
324 exp.fc_struct.flush = 0;
325 set_fpc_csr(exp.fc_word);
329 if (2.0 * (d / 2.0) != d)
331 printf ("Error: HAVE_DENORMS defined, but no subnormals.\n");
336 /* generate DBL_EPSILON with a loop to avoid that the compiler
337 optimizes the code below in non-IEEE 754 mode, deciding that
338 c = d is always false. */
340 for (eps = 1.0; eps != DBL_EPSILON; eps /= 2.0);
342 d = eps * (1.0 - eps) / 2.0;
346 printf ("Warning: IEEE 754 standard not fully supported\n"
347 " (maybe extended precision not disabled)\n"
348 " Some tests may fail\n");
354 /* generate a random limb */
360 mpfr_rand_raw (&limb, RANDS, GMP_NUMB_BITS);
364 /* returns ulp(x) for x a 'normal' double-precision number */
372 y = x * 2.220446049250313080847263336181640625e-16 ; /* x / 2^52 */
374 /* as ulp(x) <= y = x/2^52 < 2*ulp(x),
375 we have x + ulp(x) <= x + y <= x + 2*ulp(x),
376 therefore o(x + y) = x + ulp(x) or x + 2*ulp(x) */
379 eps = eps - x; /* ulp(x) or 2*ulp(x) */
381 return (eps > y) ? 0.5 * eps : eps;
384 /* returns the number of ulp's between a and b,
385 where a and b can be any floating-point number, except NaN
388 ulp (double a, double b)
392 if (a == b) return 0; /* also deals with a=b=inf or -inf */
395 if (twoa == a) /* a is +/-0.0 or +/-Inf */
396 return ((b < a) ? INT_MAX : -INT_MAX);
398 return (int) ((a - b) / Ulp (a));
401 /* return double m*2^e */
403 dbl (double m, int e)
414 /* Warning: NaN values cannot be distinguished if MPFR_NANISNAN is defined. */
422 d_trace (const char *name, double d)
426 unsigned char b[sizeof(double)];
430 if (name != NULL && name[0] != '\0')
431 printf ("%s=", name);
435 for (i = 0; i < (int) sizeof (u.b); i++)
439 printf ("%02X", (int) u.b[i]);
441 printf ("] %.20g\n", d);
445 ld_trace (const char *name, long double ld)
449 unsigned char b[sizeof(long double)];
453 if (name != NULL && name[0] != '\0')
454 printf ("%s=", name);
458 for (i = 0; i < (int) sizeof (u.b); i++)
462 printf ("%02X", (int) u.b[i]);
464 printf ("] %.20Lg\n", ld);
467 /* Open a file in the src directory - can't use fopen directly */
469 src_fopen (const char *filename, const char *mode)
472 return fopen (filename, mode);
474 const char *srcdir = SRCDIR;
479 buffsize = strlen (filename) + strlen (srcdir) + 2;
480 buffer = (char *) (*__gmp_allocate_func) (buffsize);
483 printf ("src_fopen: failed to alloc memory)\n");
486 sprintf (buffer, "%s/%s", srcdir, filename);
487 f = fopen (buffer, mode);
488 (*__gmp_free_func) (buffer, buffsize);
494 set_emin (mpfr_exp_t exponent)
496 if (mpfr_set_emin (exponent))
498 printf ("set_emin: setting emin to %ld failed\n", (long int) exponent);
504 set_emax (mpfr_exp_t exponent)
506 if (mpfr_set_emax (exponent))
508 printf ("set_emax: setting emax to %ld failed\n", (long int) exponent);
513 /* pos is 512 times the proportion of negative numbers.
514 If pos=256, half of the numbers are negative.
515 If pos=0, all generated numbers are positive.
518 tests_default_random (mpfr_ptr x, int pos, mpfr_exp_t emin, mpfr_exp_t emax)
520 MPFR_ASSERTN (emin <= emax);
521 MPFR_ASSERTN (emin >= MPFR_EMIN_MIN);
522 MPFR_ASSERTN (emax <= MPFR_EMAX_MAX);
523 /* but it isn't required that emin and emax are in the current
524 exponent range (see below), so that underflow/overflow checks
525 can be done on 64-bit machines. */
527 mpfr_urandomb (x, RANDS);
528 if (MPFR_IS_PURE_FP (x) && (emin >= 1 || (randlimb () & 1)))
531 e = MPFR_GET_EXP (x) +
532 (emin + (long) (randlimb () % (emax - emin + 1)));
533 /* Note: There should be no overflow here because both terms are
534 between MPFR_EMIN_MIN and MPFR_EMAX_MAX, but the sum e isn't
535 necessarily between MPFR_EMIN_MIN and MPFR_EMAX_MAX. */
536 if (mpfr_set_exp (x, e))
538 /* The random number doesn't fit in the current exponent range.
539 In this case, test the function in the extended exponent range,
540 which should be restored by the caller. */
541 mpfr_set_emin (MPFR_EMIN_MIN);
542 mpfr_set_emax (MPFR_EMAX_MAX);
546 if (randlimb () % 512 < pos)
547 mpfr_neg (x, x, MPFR_RNDN);
550 /* The test_one argument is seen a boolean. If it is true and rnd is
551 a rounding mode toward infinity, then the function is tested in
552 only one rounding mode (the one provided in rnd) and the variable
553 rndnext is not used (due to the break). If it is true and rnd is a
554 rounding mode toward or away from zero, then the function is tested
555 twice, first with the provided rounding mode and second with the
556 rounding mode toward the corresponding infinity (determined by the
557 sign of the result). If it is false, then the function is tested
558 in the 5 rounding modes, and rnd must initially be MPFR_RNDZ; thus
559 rndnext will be initialized in the first iteration.
560 If the test_one argument is 2, then this means that y is exact, and
561 the ternary value is checked.
562 As examples of use, see the calls to test5rm from the data_check and
563 bad_cases functions. */
565 test5rm (int (*fct) (FLIST), mpfr_srcptr x, mpfr_ptr y, mpfr_ptr z,
566 mpfr_rnd_t rnd, int test_one, const char *name)
568 mpfr_prec_t yprec = MPFR_PREC (y);
569 mpfr_rnd_t rndnext = MPFR_RND_MAX; /* means uninitialized */
571 MPFR_ASSERTN (test_one || rnd == MPFR_RNDZ);
572 mpfr_set_prec (z, yprec);
577 MPFR_ASSERTN (rnd != MPFR_RND_MAX);
578 inex = fct (z, x, rnd);
579 if (! (mpfr_equal_p (y, z) || (mpfr_nan_p (y) && mpfr_nan_p (z))))
581 printf ("Error for %s with xprec=%lu, yprec=%lu, rnd=%s\nx = ",
582 name, (unsigned long) MPFR_PREC (x), (unsigned long) yprec,
583 mpfr_print_rnd_mode (rnd));
584 mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN);
585 printf ("\nexpected ");
586 mpfr_out_str (stdout, 16, 0, y, MPFR_RNDN);
588 mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN);
592 if (test_one == 2 && inex != 0)
594 printf ("Error for %s with xprec=%lu, yprec=%lu, rnd=%s\nx = ",
595 name, (unsigned long) MPFR_PREC (x), (unsigned long) yprec,
596 mpfr_print_rnd_mode (rnd));
597 mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN);
598 printf ("\nexact case, but non-zero ternary value (%d)\n", inex);
601 if (rnd == MPFR_RNDN)
606 if (rnd == MPFR_RNDU || rnd == MPFR_RNDD)
610 rnd = (rnd == MPFR_RNDA) ? MPFR_RNDD : MPFR_RNDU;
612 rnd = (rnd == MPFR_RNDA) ? MPFR_RNDU : MPFR_RNDD;
614 else if (rnd == MPFR_RNDZ)
616 rnd = MPFR_IS_NEG (y) ? MPFR_RNDU : MPFR_RNDD;
622 if (rnd == MPFR_RNDA)
625 rndnext = (MPFR_IS_NEG (y)) ? MPFR_RNDD : MPFR_RNDU;
627 else if (rndnext != MPFR_RNDN)
631 if (yprec == MPFR_PREC_MIN)
633 mpfr_prec_round (y, --yprec, MPFR_RNDZ);
634 mpfr_set_prec (z, yprec);
640 /* Check data in file f for function foo, with name 'name'.
641 Each line consists of the file f one:
647 xprec is the input precision
648 yprec is the output precision
649 rnd is the rounding mode (n, z, u, d, a, Z, *)
650 x is the input (hexadecimal format)
651 y is the expected output (hexadecimal format) for foo(x) with rounding rnd
653 If rnd is Z, y is the expected output in round-toward-zero, and the
654 four directed rounding modes are tested, then the round-to-nearest
655 mode is tested in precision yprec-1. This is useful for worst cases,
656 where yprec is the minimum value such that one has a worst case in a
657 directed rounding mode.
659 If rnd is *, y must be an exact case. All the rounding modes are tested
660 and the ternary value is checked (it must be 0).
663 data_check (const char *f, int (*foo) (FLIST), const char *name)
666 int xprec, yprec; /* not mpfr_prec_t because of the fscanf */
674 fp = src_fopen (f, "r");
677 char *v = (char *) MPFR_VERSION_STRING;
679 /* In the '-dev' versions, assume that the data file exists and
680 return an error if the file cannot be opened to make sure
681 that such failures are detected. */
684 if (v[-4] == '-' && v[-3] == 'd' && v[-2] == 'e' && v[-1] == 'v')
686 printf ("Error: unable to open file '%s'\n", f);
699 /* skip whitespace, for consistency */
700 if (fscanf (fp, " ") == EOF)
704 perror ("data_check");
708 break; /* end of file */
711 if ((c = getc (fp)) == EOF)
715 perror ("data_check");
719 break; /* end of file */
722 if (c == '#') /* comment: read entire line */
728 while (!feof (fp) && c != '\n');
734 c = fscanf (fp, "%d %d %c", &xprec, &yprec, &r);
735 MPFR_ASSERTN (xprec >= MPFR_PREC_MIN && xprec <= MPFR_PREC_MAX);
736 MPFR_ASSERTN (yprec >= MPFR_PREC_MIN && yprec <= MPFR_PREC_MAX);
739 perror ("data_check");
744 printf ("Error: corrupted line in file '%s'\n", f);
763 rnd = MPFR_RND_MAX; /* non-existing rounding mode */
766 printf ("Error: unexpected rounding mode"
767 " in file '%s': %c\n", f, (int) r);
770 mpfr_set_prec (x, xprec);
771 mpfr_set_prec (y, yprec);
772 if (mpfr_inp_str (x, fp, 0, MPFR_RNDN) == 0)
774 printf ("Error: corrupted argument in file '%s'\n", f);
777 if (mpfr_inp_str (y, fp, 0, MPFR_RNDN) == 0)
779 printf ("Error: corrupted result in file '%s'\n", f);
782 if (getc (fp) != '\n')
784 printf ("Error: result not followed by \\n in file '%s'\n", f);
787 /* Skip whitespace, in particular at the end of the file. */
788 if (fscanf (fp, " ") == EOF && ferror (fp))
790 perror ("data_check");
797 test5rm (foo, x, y, z, (mpfr_rnd_t) rndint, 2, name);
800 test5rm (foo, x, y, z, rnd, r != 'Z', name);
811 /* Test n random bad cases. A precision py in [pymin,pymax] and
812 * a number y of precision py are chosen randomly. One computes
813 * x = inv(y) in precision px = py + psup (rounded to nearest).
814 * Then (in general), y is a bad case for fct in precision py (in
815 * the directed rounding modes, but also in the rounding-to-nearest
816 * mode for some lower precision: see data_check).
817 * fct, inv, name: data related to the function.
818 * pos, emin, emax: arguments for tests_default_random.
821 bad_cases (int (*fct)(FLIST), int (*inv)(FLIST), const char *name,
822 int pos, mpfr_exp_t emin, mpfr_exp_t emax,
823 mpfr_prec_t pymin, mpfr_prec_t pymax, mpfr_prec_t psup,
829 mpfr_exp_t old_emin, old_emax;
831 old_emin = mpfr_get_emin ();
832 old_emax = mpfr_get_emax ();
834 dbgenv = getenv ("MPFR_DEBUG_BADCASES");
835 dbg = dbgenv != 0 ? atoi (dbgenv) : 0; /* debug level */
836 mpfr_inits (x, y, z, (mpfr_ptr) 0);
837 for (i = 0; i < n; i++)
839 mpfr_prec_t px, py, pz;
843 printf ("bad_cases: i = %d\n", i);
844 py = pymin + (randlimb () % (pymax - pymin + 1));
845 mpfr_set_prec (y, py);
846 tests_default_random (y, pos, emin, emax);
849 printf ("bad_cases: yprec =%4ld, y = ", (long) py);
850 mpfr_out_str (stdout, 16, 0, y, MPFR_RNDN);
854 mpfr_set_prec (x, px);
856 inv (x, y, MPFR_RNDN);
857 if (mpfr_nanflag_p () || mpfr_overflow_p () || mpfr_underflow_p ())
860 printf ("bad_cases: no normal inverse\n");
865 printf ("bad_cases: x = ");
866 mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN);
873 mpfr_set_prec (z, pz);
874 if (fct (z, x, MPFR_RNDN) == 0)
877 printf ("bad_cases: exact case\n");
884 printf ("bad_cases: %s(x) ~= ", name);
885 mpfr_out_str (stdout, 16, 0, z, MPFR_RNDN);
889 printf ("bad_cases: [MPFR_RNDZ] ~= ");
890 mpfr_out_str (stdout, 16, 40, z, MPFR_RNDZ);
894 inex = mpfr_prec_round (z, py, MPFR_RNDN);
895 if (mpfr_nanflag_p () || mpfr_overflow_p () || mpfr_underflow_p ()
896 || ! mpfr_equal_p (z, y))
899 printf ("bad_cases: inverse doesn't match\n");
904 /* We really have a bad case. */
907 while (py >= MPFR_PREC_MIN && mpfr_prec_round (z, py, MPFR_RNDZ) == 0);
909 /* py is now the smallest output precision such that we have
910 a bad case in the directed rounding modes. */
911 if (mpfr_prec_round (y, py, MPFR_RNDZ) != 0)
913 printf ("Internal error for i = %d\n", i);
916 if ((inex > 0 && MPFR_IS_POS (z)) ||
917 (inex < 0 && MPFR_IS_NEG (z)))
925 printf ("bad_cases: yprec =%4ld, y = ", (long) py);
926 mpfr_out_str (stdout, 16, 0, y, MPFR_RNDN);
929 /* Note: y is now the expected result rounded toward zero. */
930 test5rm (fct, x, y, z, MPFR_RNDZ, 0, name);
932 /* In case the exponent range has been changed by
933 tests_default_random()... */
934 mpfr_set_emin (old_emin);
935 mpfr_set_emax (old_emax);
937 mpfr_clears (x, y, z, (mpfr_ptr) 0);
941 flags_out (unsigned int flags)
945 if (flags & MPFR_FLAGS_UNDERFLOW)
946 none = 0, printf (" underflow");
947 if (flags & MPFR_FLAGS_OVERFLOW)
948 none = 0, printf (" overflow");
949 if (flags & MPFR_FLAGS_NAN)
950 none = 0, printf (" nan");
951 if (flags & MPFR_FLAGS_INEXACT)
952 none = 0, printf (" inexact");
953 if (flags & MPFR_FLAGS_ERANGE)
954 none = 0, printf (" erange");
957 printf (" (%u)\n", flags);