From 2d91a71b4af88ffea2bf5cf9a18e2f803ecba537 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 6 Mar 2012 03:05:14 +0100 Subject: [PATCH] exp* tests: More tests. * tests/test-exp.h: New file. * tests/test-exp.c: Include and test-exp.h. (main): Invoke test_function. * tests/test-expf.c: Include and test-exp.h. (main): Invoke test_function. * tests/test-expl.c: Include and test-exp.h. (main): Invoke test_function. * modules/exp-tests (Files): Add tests/test-exp.h, tests/randomd.c. (Makefile.am): Add randomd.c to test_exp_SOURCES. * modules/expf-tests (Files): Add tests/test-exp.h, tests/randomf.c. (Makefile.am): Add randomf.c to test_expf_SOURCES. * modules/expl-tests (Files): Add tests/test-exp.h, tests/randoml.c. (Depends-on): Add 'float'. (Makefile.am): Add randoml.c to test_expl_SOURCES. --- ChangeLog | 16 ++++++++++ modules/exp-tests | 3 ++ modules/expf-tests | 3 ++ modules/expl-tests | 4 +++ tests/test-exp.c | 12 ++++++-- tests/test-exp.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-expf.c | 12 ++++++-- tests/test-expl.c | 12 ++++++-- 8 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 tests/test-exp.h diff --git a/ChangeLog b/ChangeLog index a18d03370..411846fef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2012-03-05 Bruno Haible + exp* tests: More tests. + * tests/test-exp.h: New file. + * tests/test-exp.c: Include and test-exp.h. + (main): Invoke test_function. + * tests/test-expf.c: Include and test-exp.h. + (main): Invoke test_function. + * tests/test-expl.c: Include and test-exp.h. + (main): Invoke test_function. + * modules/exp-tests (Files): Add tests/test-exp.h, tests/randomd.c. + (Makefile.am): Add randomd.c to test_exp_SOURCES. + * modules/expf-tests (Files): Add tests/test-exp.h, tests/randomf.c. + (Makefile.am): Add randomf.c to test_expf_SOURCES. + * modules/expl-tests (Files): Add tests/test-exp.h, tests/randoml.c. + (Depends-on): Add 'float'. + (Makefile.am): Add randoml.c to test_expl_SOURCES. + expl: Fix precision of computed result. * lib/expl.c: Completely rewritten. * modules/expl (Depends-on): Add isnanl, roundl, ldexpl. Remove floorl. diff --git a/modules/exp-tests b/modules/exp-tests index 133886117..0b5ad6917 100644 --- a/modules/exp-tests +++ b/modules/exp-tests @@ -1,7 +1,9 @@ Files: tests/test-exp.c +tests/test-exp.h tests/signature.h tests/macros.h +tests/randomd.c Depends-on: @@ -10,4 +12,5 @@ configure.ac: Makefile.am: TESTS += test-exp check_PROGRAMS += test-exp +test_exp_SOURCES = test-exp.c randomd.c test_exp_LDADD = $(LDADD) @EXP_LIBM@ diff --git a/modules/expf-tests b/modules/expf-tests index ec6d58238..b1d1c4ac1 100644 --- a/modules/expf-tests +++ b/modules/expf-tests @@ -1,7 +1,9 @@ Files: tests/test-expf.c +tests/test-exp.h tests/signature.h tests/macros.h +tests/randomf.c Depends-on: @@ -10,4 +12,5 @@ configure.ac: Makefile.am: TESTS += test-expf check_PROGRAMS += test-expf +test_expf_SOURCES = test-expf.c randomf.c test_expf_LDADD = $(LDADD) @EXPF_LIBM@ diff --git a/modules/expl-tests b/modules/expl-tests index bc7b28ae3..5296c2b3a 100644 --- a/modules/expl-tests +++ b/modules/expl-tests @@ -1,14 +1,18 @@ Files: tests/test-expl.c +tests/test-exp.h tests/signature.h tests/macros.h +tests/randoml.c Depends-on: fpucw +float configure.ac: Makefile.am: TESTS += test-expl check_PROGRAMS += test-expl +test_expl_SOURCES = test-expl.c randoml.c test_expl_LDADD = $(LDADD) @EXPL_LIBM@ diff --git a/tests/test-exp.c b/tests/test-exp.c index 8eb0aa52d..0459739d1 100644 --- a/tests/test-exp.c +++ b/tests/test-exp.c @@ -23,10 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (exp, double, (double)); +#include + #include "macros.h" -volatile double x; -double y; +#define DOUBLE double +#define L_(literal) literal +#define MANT_DIG DBL_MANT_DIG +#define EXP exp +#define RANDOM randomd +#include "test-exp.h" int main () @@ -36,5 +42,7 @@ main () y = exp (x); ASSERT (y >= 1.822118800 && y <= 1.822118801); + test_function (); + return 0; } diff --git a/tests/test-exp.h b/tests/test-exp.h new file mode 100644 index 000000000..c2609b72d --- /dev/null +++ b/tests/test-exp.h @@ -0,0 +1,88 @@ +/* Test of exp*() function family. + Copyright (C) 2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +static void +test_function (void) +{ + int i; + int j; + const DOUBLE TWO_MANT_DIG = + /* Assume MANT_DIG <= 5 * 31. + Use the identity + n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */ + (DOUBLE) (1U << ((MANT_DIG - 1) / 5)) + * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5)) + * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5)) + * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5)) + * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5)); + + /* Randomized tests. */ + { + /* Error bound, in ulps. */ + const DOUBLE err_bound = + (sizeof (DOUBLE) == sizeof (long double) ? +#if defined __i386__ && defined __FreeBSD__ + /* On FreeBSD/x86 6.4, the 'long double' type really has only 53 bits of + precision in the compiler but 64 bits of precision at runtime. See + . + The compiler has truncated all 'long double' literals in expl.c to + 53 bits of precision. */ + L_(1350.0) +#else + L_(4.0) +#endif + : L_(2.0)); + + for (i = 0; i < SIZEOF (RANDOM); i++) + { + DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */ + DOUBLE y = EXP (x); + DOUBLE z = EXP (- x); + DOUBLE err = y * z - L_(1.0); + ASSERT (y > L_(0.0)); + ASSERT (z > L_(0.0)); + ASSERT (err > - err_bound / TWO_MANT_DIG + && err < err_bound / TWO_MANT_DIG); + } + } + + { + /* Error bound, in ulps. */ + const DOUBLE err_bound = + (sizeof (DOUBLE) == sizeof (long double) ? +#if defined __i386__ && defined __FreeBSD__ + L_(2400.0) +#else + L_(24.0) +#endif + : L_(24.0)); + + for (i = 0; i < SIZEOF (RANDOM) / 5; i++) + for (j = 0; j < SIZEOF (RANDOM) / 5; j++) + { + DOUBLE x = L_(32.0) * RANDOM[i] - L_(16.0); /* -16.0 <= x <= 16.0 */ + DOUBLE y = L_(32.0) * RANDOM[j] - L_(16.0); /* -16.0 <= y <= 16.0 */ + DOUBLE z = - x - y; + /* Approximately x + y + z = 0. */ + DOUBLE err = EXP (x) * EXP (y) * EXP (z) - L_(1.0); + ASSERT (err > - err_bound / TWO_MANT_DIG + && err < err_bound / TWO_MANT_DIG); + } + } +} + +volatile DOUBLE x; +DOUBLE y; diff --git a/tests/test-expf.c b/tests/test-expf.c index f16026e78..d7ae2858e 100644 --- a/tests/test-expf.c +++ b/tests/test-expf.c @@ -23,10 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (expf, float, (float)); +#include + #include "macros.h" -volatile float x; -float y; +#define DOUBLE float +#define L_(literal) literal##f +#define MANT_DIG FLT_MANT_DIG +#define EXP expf +#define RANDOM randomf +#include "test-exp.h" int main () @@ -36,5 +42,7 @@ main () y = expf (x); ASSERT (y >= 1.8221188f && y <= 1.8221189f); + test_function (); + return 0; } diff --git a/tests/test-expl.c b/tests/test-expl.c index 21dc65e64..730910653 100644 --- a/tests/test-expl.c +++ b/tests/test-expl.c @@ -23,11 +23,17 @@ #include "signature.h" SIGNATURE_CHECK (expl, long double, (long double)); +#include + #include "fpucw.h" #include "macros.h" -volatile long double x; -long double y; +#define DOUBLE long double +#define L_(literal) literal##L +#define MANT_DIG LDBL_MANT_DIG +#define EXP expl +#define RANDOM randoml +#include "test-exp.h" int main () @@ -41,5 +47,7 @@ main () y = expl (x); ASSERT (y >= 1.822118800L && y <= 1.822118801L); + test_function (); + return 0; } -- 2.11.0