From a7ecbd9ce61979eea0180ed7f7d3805c45eac460 Mon Sep 17 00:00:00 2001 From: Paul Zimmermann Date: Wed, 5 Feb 2020 20:43:21 +0100 Subject: added mpcheck-float --- tests/Makefile.am | 2 +- tests/mpcheck-double.c | 14 ++- tests/mpcheck-float.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++ tests/mpcheck-template1.c | 6 +- tests/mpcheck-template2.c | 8 +- tests/mpcheck-template3.c | 6 +- 6 files changed, 265 insertions(+), 14 deletions(-) create mode 100644 tests/mpcheck-float.c diff --git a/tests/Makefile.am b/tests/Makefile.am index f5cab22..e8b08df 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -66,7 +66,7 @@ DATA_SETS = abs.dat acos.dat acosh.dat add.dat add_fr.dat arg.dat \ sin.dat sinh.dat \ sqr.dat sqrt.dat strtoc.dat sub.dat sub_fr.dat tan.dat tanh.dat EXTRA_DIST = data_check.tpl tgeneric.tpl $(DATA_SETS) $(DESCRIPTIONS) \ - mpcheck-double.c \ + mpcheck-double.c mpcheck-float.c \ mpcheck-template1.c mpcheck-template2.c mpcheck-template3.c LOG_COMPILER = $(VALGRIND) diff --git a/tests/mpcheck-double.c b/tests/mpcheck-double.c index b1ef696..3631252 100644 --- a/tests/mpcheck-double.c +++ b/tests/mpcheck-double.c @@ -40,6 +40,14 @@ along with this program. If not, see http://www.gnu.org/licenses/ . #include #endif +#define PRECISION 53 +#define EMAX 1024 +#define TYPE double + +#define mpc_get_type mpc_get_dc +#define mpc_set_type mpc_set_dc +#define mpfr_set_type mpfr_set_d + gmp_randstate_t state; unsigned long seed = 1; int verbose = 0; @@ -140,7 +148,7 @@ ulp_error (mpfr_t x, mpfr_t y) int main (int argc, char *argv[]) { - mpfr_prec_t p = 53; /* precision of 'double' */ + mpfr_prec_t p = PRECISION; /* precision of 'double' */ unsigned long n = 1000000; /* default number of random tests per function */ while (argc >= 2 && argv[1][0] == '-') @@ -177,8 +185,8 @@ main (int argc, char *argv[]) } /* set exponent range for 'double' */ - mpfr_set_emin (-1073); - mpfr_set_emax (1024); + mpfr_set_emin (-EMAX - PRECISION + 4); + mpfr_set_emax (EMAX); gmp_randinit_default (state); diff --git a/tests/mpcheck-float.c b/tests/mpcheck-float.c new file mode 100644 index 0000000..10be8d1 --- /dev/null +++ b/tests/mpcheck-float.c @@ -0,0 +1,243 @@ +/* mpcheck-double -- compare mpc functions against "float complex" + from the GNU libc implementation + +Copyright (C) 2020 INRIA + +This file is part of GNU MPC. + +GNU MPC is free software; you can redistribute it and/or modify it under +the terms of the GNU Lesser General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for +more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see http://www.gnu.org/licenses/ . +*/ + +/* the GNU libc provides the following functions (as of 2.31), + with 'f' suffix for the float/binary32 version, with no suffix + for the double/binary64 version, with 'l' suffix for the long double + version, and with 'f128' suffix for the __float128 version: + + cabs casinh cexp csinh + cacos catan clog csqrt + cacosh catanh clog10 ctan + carg ccos cpow ctanh + casin ccosh csin +*/ + +#define _GNU_SOURCE /* for clog10 */ +#include +#include +#include +#include "mpc-tests.h" +#ifdef __GNUC__ +#include +#endif + +#define PRECISION 24 +#define EMAX 128 +#define TYPE float + +#define mpfr_set_type mpfr_set_flt + +static TYPE complex +mpc_get_type (mpc_t x, mpc_rnd_t rnd) +{ + /* there is no mpc_get_fltc function */ + return (TYPE complex) mpc_get_dc (x, rnd); +} + +static int +mpc_set_type (mpc_t x, TYPE complex y, mpc_rnd_t rnd) +{ + /* there is no mpc_set_fltc function */ + return mpc_set_dc (x, (double complex) y, rnd); +} + +gmp_randstate_t state; +unsigned long seed = 1; +int verbose = 0; + +static unsigned long +ulp_error (mpfr_t x, mpfr_t y) +{ + mpfr_t z; + mpfr_prec_t p = mpfr_get_prec (y); + unsigned long n; + + if (mpfr_cmp (x, y) == 0) + return 0; + + mpfr_init2 (z, p); + mpfr_sub (z, x, y, MPFR_RNDN); + mpfr_abs (z, z, MPFR_RNDN); + /* divide by ulp(y) = 2^(EXP(y) - p) */ + mpfr_div_2si (z, z, mpfr_get_exp (y) - p, MPFR_RNDN); + n = mpfr_get_ui (z, MPFR_RNDZ); + mpfr_clear (z); + return n; +} + +#define FOO add +#define CFOO(x,y) (x+y) +#include "mpcheck-template3.c" + +#define FOO sub +#define CFOO(x,y) (x-y) +#include "mpcheck-template3.c" + +#define FOO mul +#define CFOO(x,y) (x*y) +#include "mpcheck-template3.c" + +#define FOO div +#define CFOO(x,y) (x/y) +#include "mpcheck-template3.c" + +#define FOO pow +#include "mpcheck-template3.c" + +#define FOO abs +#include "mpcheck-template2.c" + +#define FOO arg +#include "mpcheck-template2.c" + +#define FOO sqrt +#include "mpcheck-template1.c" + +#define FOO acos +#include "mpcheck-template1.c" + +#define FOO acosh +#include "mpcheck-template1.c" + +#define FOO asin +#include "mpcheck-template1.c" + +#define FOO asinh +#include "mpcheck-template1.c" + +#define FOO atan +#include "mpcheck-template1.c" + +#define FOO atanh +#include "mpcheck-template1.c" + +#define FOO cos +#include "mpcheck-template1.c" + +#define FOO cosh +#include "mpcheck-template1.c" + +#define FOO exp +#include "mpcheck-template1.c" + +#define FOO log +#include "mpcheck-template1.c" + +#define FOO log10 +#include "mpcheck-template1.c" + +#define FOO sin +#include "mpcheck-template1.c" + +#define FOO sinh +#include "mpcheck-template1.c" + +#define FOO tan +#include "mpcheck-template1.c" + +#define FOO tanh +#include "mpcheck-template1.c" + +int +main (int argc, char *argv[]) +{ + mpfr_prec_t p = PRECISION; /* precision of 'float' */ + unsigned long n = 1000000; /* default number of random tests per function */ + + while (argc >= 2 && argv[1][0] == '-') + { + if (argc >= 3 && strcmp (argv[1], "-p") == 0) + { + p = atoi (argv[2]); + argc -= 2; + argv += 2; + } + else if (argc >= 3 && strcmp (argv[1], "-seed") == 0) + { + seed = atoi (argv[2]); + argc -= 2; + argv += 2; + } + else if (argc >= 3 && strcmp (argv[1], "-num") == 0) + { + n = atoi (argv[2]); + argc -= 2; + argv += 2; + } + else if (strcmp (argv[1], "-v") == 0) + { + verbose ++; + argc --; + argv ++; + } + else + { + fprintf (stderr, "Unknown option %s\n", argv[1]); + exit (1); + } + } + + /* set exponent range for 'float' */ + mpfr_set_emin (-EMAX - PRECISION + 4); + mpfr_set_emax (EMAX); + + gmp_randinit_default (state); + +#ifdef __GNUC__ + printf ("GNU libc version: %s\n", gnu_get_libc_version ()); + printf ("GNU libc release: %s\n", gnu_get_libc_release ()); +#endif + printf ("Using random seed %lu\n", seed); + + /* (complex,complex) -> complex */ + test_add (p, n); + test_sub (p, n); + test_mul (p, n); + test_div (p, n); + test_pow (p, n); + + /* complex -> real */ + test_abs (p, n); + test_arg (p, n); + + /* complex -> complex */ + test_sqrt (p, n); + test_acos (p, n); + test_acosh (p, n); + test_asin (p, n); + test_asinh (p, n); + test_atan (p, n); + test_atanh (p, n); + test_cos (p, n); + test_cosh (p, n); + test_exp (p, n); + test_log (p, n); + test_log10 (p, n); + test_sin (p, n); + test_sinh (p, n); + test_tan (p, n); + test_tanh (p, n); + + gmp_randclear (state); + + return 0; +} diff --git a/tests/mpcheck-template1.c b/tests/mpcheck-template1.c index 771054b..712f8c3 100644 --- a/tests/mpcheck-template1.c +++ b/tests/mpcheck-template1.c @@ -33,7 +33,7 @@ FUN (mpfr_prec_t p, unsigned long n) { unsigned long i = 0; mpc_t x, z, t; - double complex xx, zz; + TYPE complex xx, zz; int inex; unsigned long errors = 0, max_err_re = 0, max_err_im = 0; @@ -48,9 +48,9 @@ FUN (mpfr_prec_t p, unsigned long n) inex = MPC_FOO (z, x, MPC_RNDNN); mpfr_subnormalize (mpc_realref (z), MPC_INEX_RE(inex), MPFR_RNDN); mpfr_subnormalize (mpc_imagref (z), MPC_INEX_IM(inex), MPFR_RNDN); - xx = mpc_get_dc (x, MPC_RNDNN); + xx = mpc_get_type (x, MPC_RNDNN); zz = CFOO (xx); - mpc_set_dc (t, zz, MPFR_RNDN); + mpc_set_type (t, zz, MPFR_RNDN); if (mpc_cmp (z, t) != 0) { unsigned long err_re = ulp_error (mpc_realref (t), mpc_realref (z)); diff --git a/tests/mpcheck-template2.c b/tests/mpcheck-template2.c index fdb26c3..e75f25f 100644 --- a/tests/mpcheck-template2.c +++ b/tests/mpcheck-template2.c @@ -34,8 +34,8 @@ FUN (mpfr_prec_t p, unsigned long n) unsigned long i = 0; mpc_t x; mpfr_t z, t; - double complex xx; - double zz; + TYPE complex xx; + TYPE zz; int inex; unsigned long errors = 0, max_err = 0; @@ -49,9 +49,9 @@ FUN (mpfr_prec_t p, unsigned long n) mpc_urandom (x, state); inex = MPC_FOO (z, x, MPC_RNDNN); mpfr_subnormalize (z, inex, MPFR_RNDN); - xx = mpc_get_dc (x, MPC_RNDNN); + xx = mpc_get_type (x, MPC_RNDNN); zz = CFOO (xx); - mpfr_set_d (t, zz, MPFR_RNDN); + mpfr_set_type (t, zz, MPFR_RNDN); if (mpfr_cmp (z, t) != 0) { unsigned long err = ulp_error (t, z); diff --git a/tests/mpcheck-template3.c b/tests/mpcheck-template3.c index 506e7ac..821e570 100644 --- a/tests/mpcheck-template3.c +++ b/tests/mpcheck-template3.c @@ -35,7 +35,7 @@ FUN (mpfr_prec_t p, unsigned long n) { unsigned long i = 0; mpc_t x, y, z, t; - double complex xx, yy, zz; + TYPE complex xx, yy, zz; int inex; unsigned long errors = 0, max_err_re = 0, max_err_im = 0; @@ -52,10 +52,10 @@ FUN (mpfr_prec_t p, unsigned long n) inex = MPC_FOO (z, x, y, MPC_RNDNN); mpfr_subnormalize (mpc_realref (z), MPC_INEX_RE(inex), MPFR_RNDN); mpfr_subnormalize (mpc_imagref (z), MPC_INEX_IM(inex), MPFR_RNDN); - xx = mpc_get_dc (x, MPC_RNDNN); + xx = mpc_get_type (x, MPC_RNDNN); yy = mpc_get_dc (y, MPC_RNDNN); zz = CFOO(xx, yy); - mpc_set_dc (t, zz, MPFR_RNDN); + mpc_set_type (t, zz, MPFR_RNDN); if (mpc_cmp (z, t) != 0) { unsigned long err_re = ulp_error (mpc_realref (t), mpc_realref (z)); -- cgit v1.2.1