From b384cdb6d3fa7dda4e7d79b506f6c5d7e8d42de1 Mon Sep 17 00:00:00 2001 From: thevenyp Date: Wed, 4 Dec 2013 12:20:21 +0000 Subject: [tests/] Add templated tests for mpc_sqr and mpc_sqrt. git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/branches/benchs_tests@1380 211d60ee-9f03-0410-a15a-8952a2c7a4e4 --- tests/Makefile.am | 6 +- tests/sqr.dsc | 29 ++++++++ tests/sqrt.dsc | 29 ++++++++ tests/tsqr_tmpl.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tsqrt_tmpl.c | 43 ++++++++++++ 5 files changed, 303 insertions(+), 3 deletions(-) create mode 100644 tests/sqr.dsc create mode 100644 tests/sqrt.dsc create mode 100644 tests/tsqr_tmpl.c create mode 100644 tests/tsqrt_tmpl.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 70bcdf3..e851dbb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -36,8 +36,8 @@ TEMPLATED = tabs_tmpl tacos_tmpl tacosh_tmpl tadd_fr_tmpl tadd_tmpl \ tlog10_tmpl tmul_tmpl tmul_2si_tmpl tmul_2ui_tmpl tmul_fr_tmpl \ tmul_i_tmpl tmul_si_tmpl tmul_ui_tmpl tneg_tmpl tnorm_tmpl tpow_tmpl \ tpow_d_tmpl tpow_fr_tmpl tpow_si_tmpl tpow_ui_tmpl tpow_z_tmpl \ - tproj_tmpl treal_tmpl tsin_tmpl tsinh_tmpl tsin_cos_tmpl tsub_fr_tmpl \ - tsub_tmpl ttan_tmpl ttanh_tmpl + tproj_tmpl treal_tmpl tsin_tmpl tsinh_tmpl tsin_cos_tmpl tsqr_tmpl \ + tsqrt_tmpl tsub_fr_tmpl tsub_tmpl ttan_tmpl ttanh_tmpl check_PROGRAMS = tabs tacos tacosh tadd tadd_fr tadd_si tadd_ui targ \ tasin tasinh tatan tatanh tconj tcos tcosh tdiv tdiv_2si tdiv_2ui \ tdiv_fr tdiv_ui texp tfma tfr_div tfr_sub timag tio_str tlog tlog10 \ @@ -61,7 +61,7 @@ DESCRIPTIONS = abs.dsc acos.dsc acosh.dsc add.dsc add_fr.dsc add_si.dsc \ log10.dsc mul.dsc mul_2si.dsc mul_2ui.dsc mul_fr.dsc mul_i.dsc \ mul_si.dsc mul_ui.dsc neg.dsc norm.dsc pow.dsc pow_d.dsc pow_fr.dsc \ pow_si.dsc pow_ui.dsc pow_z.dsc proj.dsc real.dsc sin.dsc sinh.dsc \ - sin_cos.dsc sub.dsc sub_fr.dsc tan.dsc tanh.dsc + sin_cos.dsc sqr.dsc sqrt.dsc sub.dsc sub_fr.dsc tan.dsc tanh.dsc DATA_SETS = abs.dat acos.dat acosh.dat add.dat add_fr.dat arg.dat \ asin.dat asinh.dat atan.dat atanh.dat conj.dat cos.dat cosh.dat \ div.dat div_fr.dat exp.dat fma.dat fr_div.dat fr_sub.dat inp_str.dat \ diff --git a/tests/sqr.dsc b/tests/sqr.dsc new file mode 100644 index 0000000..56f098a --- /dev/null +++ b/tests/sqr.dsc @@ -0,0 +1,29 @@ +# Description file for mpc_sqr +# +# Copyright (C) 2013 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 +#o ption) 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/ . +# +# Description file of the function +NAME: + mpc_sqr +RETURN: + mpc_inex +OUTPUT: + mpc_ptr +INPUT: + mpc_srcptr + mpc_rnd_t diff --git a/tests/sqrt.dsc b/tests/sqrt.dsc new file mode 100644 index 0000000..b564f00 --- /dev/null +++ b/tests/sqrt.dsc @@ -0,0 +1,29 @@ +# Description file for mpc_sqrt +# +# Copyright (C) 2013 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 +#o ption) 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/ . +# +# Description file of the function +NAME: + mpc_sqrt +RETURN: + mpc_inex +OUTPUT: + mpc_ptr +INPUT: + mpc_srcptr + mpc_rnd_t diff --git a/tests/tsqr_tmpl.c b/tests/tsqr_tmpl.c new file mode 100644 index 0000000..b39e89f --- /dev/null +++ b/tests/tsqr_tmpl.c @@ -0,0 +1,199 @@ +/* tsqr_tmpl.c -- test file for mpc_sqr. + +Copyright (C) 2002, 2005, 2008, 2010, 2011, 2012, 2013 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/ . +*/ + +#include +#include "templates.h" + +static void +cmpsqr (mpc_srcptr x, mpc_rnd_t rnd) + /* computes the square of x with the specific function or by simple */ + /* multiplication using the rounding mode rnd and compares the results */ + /* and return values. */ + /* In our current test suite, the real and imaginary parts of x have */ + /* the same precision, and we use this precision also for the result. */ + /* Furthermore, we check whether computing the square in the same */ + /* place yields the same result. */ + /* We also compute the result with four times the precision and check */ + /* whether the rounding is correct. Error reports in this part of the */ + /* algorithm might still be wrong, though, since there are two */ + /* consecutive roundings. */ +{ + mpc_t z, t, u; + int inexact_z, inexact_t; + + mpc_init2 (z, MPC_MAX_PREC (x)); + mpc_init2 (t, MPC_MAX_PREC (x)); + mpc_init2 (u, 4 * MPC_MAX_PREC (x)); + + inexact_z = mpc_sqr (z, x, rnd); + inexact_t = mpc_mul (t, x, x, rnd); + + if (mpc_cmp (z, t)) + { + fprintf (stderr, "sqr and mul differ for rnd=(%s,%s) \nx=", + mpfr_print_rnd_mode(MPC_RND_RE(rnd)), + mpfr_print_rnd_mode(MPC_RND_IM(rnd))); + mpc_out_str (stderr, 2, 0, x, MPC_RNDNN); + fprintf (stderr, "\nmpc_sqr gives "); + mpc_out_str (stderr, 2, 0, z, MPC_RNDNN); + fprintf (stderr, "\nmpc_mul gives "); + mpc_out_str (stderr, 2, 0, t, MPC_RNDNN); + fprintf (stderr, "\n"); + exit (1); + } + if (inexact_z != inexact_t) + { + fprintf (stderr, "The return values of sqr and mul differ for rnd=(%s,%s) \nx= ", + mpfr_print_rnd_mode(MPC_RND_RE(rnd)), + mpfr_print_rnd_mode(MPC_RND_IM(rnd))); + mpc_out_str (stderr, 2, 0, x, MPC_RNDNN); + fprintf (stderr, "\nx^2="); + mpc_out_str (stderr, 2, 0, z, MPC_RNDNN); + fprintf (stderr, "\nmpc_sqr gives %i", inexact_z); + fprintf (stderr, "\nmpc_mul gives %i", inexact_t); + fprintf (stderr, "\n"); + exit (1); + } + + mpc_set (t, x, MPC_RNDNN); + inexact_t = mpc_sqr (t, t, rnd); + if (mpc_cmp (z, t)) + { + fprintf (stderr, "sqr and sqr in place differ for rnd=(%s,%s) \nx=", + mpfr_print_rnd_mode(MPC_RND_RE(rnd)), + mpfr_print_rnd_mode(MPC_RND_IM(rnd))); + mpc_out_str (stderr, 2, 0, x, MPC_RNDNN); + fprintf (stderr, "\nmpc_sqr gives "); + mpc_out_str (stderr, 2, 0, z, MPC_RNDNN); + fprintf (stderr, "\nmpc_sqr in place gives "); + mpc_out_str (stderr, 2, 0, t, MPC_RNDNN); + fprintf (stderr, "\n"); + exit (1); + } + if (inexact_z != inexact_t) + { + fprintf (stderr, "The return values of sqr and sqr in place differ for rnd=(%s,%s) \nx= ", + mpfr_print_rnd_mode(MPC_RND_RE(rnd)), + mpfr_print_rnd_mode(MPC_RND_IM(rnd))); + mpc_out_str (stderr, 2, 0, x, MPC_RNDNN); + fprintf (stderr, "\nx^2="); + mpc_out_str (stderr, 2, 0, z, MPC_RNDNN); + fprintf (stderr, "\nmpc_sqr gives %i", inexact_z); + fprintf (stderr, "\nmpc_sqr in place gives %i", inexact_t); + fprintf (stderr, "\n"); + exit (1); + } + + mpc_sqr (u, x, rnd); + mpc_set (t, u, rnd); + if (mpc_cmp (z, t)) + { + fprintf (stderr, "rounding in sqr might be incorrect for rnd=(%s,%s) \nx=", + mpfr_print_rnd_mode(MPC_RND_RE(rnd)), + mpfr_print_rnd_mode(MPC_RND_IM(rnd))); + mpc_out_str (stderr, 2, 0, x, MPC_RNDNN); + fprintf (stderr, "\nmpc_sqr gives "); + mpc_out_str (stderr, 2, 0, z, MPC_RNDNN); + fprintf (stderr, "\nmpc_sqr quadruple precision gives "); + mpc_out_str (stderr, 2, 0, u, MPC_RNDNN); + fprintf (stderr, "\nand is rounded to "); + mpc_out_str (stderr, 2, 0, t, MPC_RNDNN); + fprintf (stderr, "\n"); + exit (1); + } + + mpc_clear (z); + mpc_clear (t); + mpc_clear (u); +} + + +static void +testsqr (long a, long b, mpfr_prec_t prec, mpc_rnd_t rnd) +{ + mpc_t x; + + mpc_init2 (x, prec); + + mpc_set_si_si (x, a, b, rnd); + + cmpsqr (x, rnd); + + mpc_clear (x); +} + + +static void +reuse_bug (void) +{ + mpc_t z1; + + /* reuse bug found by Paul Zimmermann 20081021 */ + mpc_init2 (z1, 2); + /* RE (z1^2) overflows, IM(z^2) = -0 */ + mpfr_set_str (mpc_realref (z1), "0.11", 2, MPFR_RNDN); + mpfr_mul_2si (mpc_realref (z1), mpc_realref (z1), mpfr_get_emax (), MPFR_RNDN); + mpfr_set_ui (mpc_imagref (z1), 0, MPFR_RNDN); + mpc_conj (z1, z1, MPC_RNDNN); + mpc_sqr (z1, z1, MPC_RNDNN); + if (!mpfr_inf_p (mpc_realref (z1)) || mpfr_signbit (mpc_realref (z1)) + ||!mpfr_zero_p (mpc_imagref (z1)) || !mpfr_signbit (mpc_imagref (z1))) + { + printf ("Error: Regression, bug 20081021 reproduced\n"); + MPC_OUT (z1); + exit (1); + } + + mpc_clear (z1); +} + +#define MPC_FUNCTION_CALL \ + P[0].mpc_inex = mpc_sqr (P[1].mpc, P[2].mpc, P[3].mpc_rnd) +#define MPC_FUNCTION_CALL_REUSE_OP1 \ + P[0].mpc_inex = mpc_sqr (P[1].mpc, P[1].mpc, P[3].mpc_rnd) + +#include "data_check.tpl" +#include "tgeneric.tpl" + +int +main (void) +{ + test_start (); + + testsqr (247, -65, 8, 24); + testsqr (5, -896, 3, 2); + testsqr (-3, -512, 2, 16); + testsqr (266013312, 121990769, 27, 0); + testsqr (170, 9, 8, 0); + testsqr (768, 85, 8, 16); + testsqr (145, 1816, 8, 24); + testsqr (0, 1816, 8, 24); + testsqr (145, 0, 8, 24); + + data_check_template ("sqr.dsc", "sqr.dat"); + + tgeneric_template ("sqr.dsc", 2, 1024, 1, 1024); + + reuse_bug (); + + test_end (); + + return 0; +} diff --git a/tests/tsqrt_tmpl.c b/tests/tsqrt_tmpl.c new file mode 100644 index 0000000..7674d54 --- /dev/null +++ b/tests/tsqrt_tmpl.c @@ -0,0 +1,43 @@ +/* tsqrt_tmpl.c -- templated test file for mpc_sqrt. + +Copyright (C) 2008, 2013 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/ . +*/ + +#include "templates.h" + +#define MPC_FUNCTION_CALL \ + P[0].mpc_inex = mpc_sqrt (P[1].mpc, P[2].mpc, P[3].mpc_rnd) +#define MPC_FUNCTION_CALL_REUSE_OP1 \ + P[0].mpc_inex = mpc_sqrt (P[1].mpc, P[1].mpc, P[3].mpc_rnd) + +#include "data_check.tpl" +#include "tgeneric.tpl" + +int +main (void) +{ + test_start (); + + data_check_template ("sqrt.dsc", "sqrt.dat"); + + tgeneric_template ("sqrt.dsc", 2, 1024, 7, 256); + + test_end (); + + return 0; +} -- cgit v1.2.1