glibc/math/basic-test.c

222 lines
8.9 KiB
C
Raw Permalink Normal View History

/* Copyright (C) 1999-2022 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library 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 2.1 of the License, or (at your option) any later version.
The GNU C Library 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 the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <math-tests.h>
static int errors = 0;
static void
check (const char *testname, int result)
{
if (!result) {
printf ("Failure: %s\n", testname);
errors++;
}
}
#define TEST_FUNC(NAME, FLOAT, SUFFIX, EPSILON, HUGEVAL) \
static void \
NAME (void) \
{ \
/* Variables are declared volatile to forbid some compiler \
optimizations. */ \
volatile FLOAT Inf_var, qNaN_var, zero_var, one_var; \
/* A sNaN is only guaranteed to be representable in variables with */ \
/* static (or thread-local) storage duration. */ \
static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX (""); \
static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \
FLOAT x1, x2; \
\
zero_var = 0.0; \
one_var = 1.0; \
qNaN_var = __builtin_nan ## SUFFIX (""); \
Inf_var = one_var / zero_var; \
\
(void) &zero_var; \
(void) &one_var; \
(void) &qNaN_var; \
(void) &sNaN_var; \
(void) &minus_sNaN_var; \
(void) &Inf_var; \
\
\
check (#FLOAT " isinf (inf) == 1", isinf (Inf_var) == 1); \
check (#FLOAT " isinf (-inf) == -1", isinf (-Inf_var) == -1); \
check (#FLOAT " !isinf (1)", !(isinf (one_var))); \
check (#FLOAT " !isinf (qNaN)", !(isinf (qNaN_var))); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " !isinf (sNaN)", !(isinf (sNaN_var))); \
\
check (#FLOAT " isnan (qNaN)", isnan (qNaN_var)); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " isnan (sNaN)", isnan (sNaN_var)); \
check (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var)); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " isnan (-sNaN)", isnan (minus_sNaN_var)); \
check (#FLOAT " !isnan (1)", !(isnan (one_var))); \
check (#FLOAT " !isnan (inf)", !(isnan (Inf_var))); \
\
check (#FLOAT " !issignaling (qNaN)", !(issignaling (qNaN_var))); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " issignaling (sNaN)", issignaling (sNaN_var)); \
check (#FLOAT " !issignaling (-qNaN)", !(issignaling (-qNaN_var))); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " issignaling (-sNaN)", issignaling (minus_sNaN_var)); \
check (#FLOAT " !issignaling (1)", !(issignaling (one_var))); \
check (#FLOAT " !issignaling (inf)", !(issignaling (Inf_var))); \
\
check (#FLOAT " inf == inf", Inf_var == Inf_var); \
check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var); \
check (#FLOAT " inf != -inf", Inf_var != -Inf_var); \
check (#FLOAT " qNaN != qNaN", qNaN_var != qNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " sNaN != sNaN", sNaN_var != sNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " qNaN != sNaN", qNaN_var != sNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " -sNaN != -sNaN", minus_sNaN_var != minus_sNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " sNaN != -sNaN", sNaN_var != minus_sNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " qNaN != -sNaN", qNaN_var != minus_sNaN_var); \
\
/* \
the same tests but this time with NAN from <bits/nan.h> \
NAN is a double const \
*/ \
check (#FLOAT " isnan (NAN)", isnan (NAN)); \
check (#FLOAT " isnan (-NAN)", isnan (-NAN)); \
check (#FLOAT " !isinf (NAN)", !(isinf (NAN))); \
check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN))); \
check (#FLOAT " NAN != NAN", NAN != NAN); \
check (#FLOAT " NAN != qNaN", NAN != qNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " NAN != sNaN", NAN != sNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " NAN != -sNaN", NAN != minus_sNaN_var); \
\
/* \
And again with the value returned by the `nan' function. \
*/ \
check (#FLOAT " isnan (nan (\"\"))", isnan (nan ## SUFFIX (""))); \
check (#FLOAT " isnan (-nan (\"\"))", isnan (-nan ## SUFFIX (""))); \
check (#FLOAT " !isinf (nan (\"\"))", !(isinf (nan ## SUFFIX ("")))); \
check (#FLOAT " !isinf (-nan (\"\"))", !(isinf (-nan ## SUFFIX ("")))); \
check (#FLOAT " nan (\"\") != nan (\"\")", \
nan ## SUFFIX ("") != nan ## SUFFIX ("")); \
check (#FLOAT " nan (\"\") != qNaN", nan ## SUFFIX ("") != qNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " nan (\"\") != sNaN", nan ## SUFFIX ("") != sNaN_var); \
if (SNAN_TESTS (FLOAT)) \
check (#FLOAT " nan (\"\") != -sNaN", \
nan ## SUFFIX ("") != minus_sNaN_var); \
\
/* test if EPSILON is ok */ \
x1 = 1.0; \
x2 = x1 + EPSILON; \
check (#FLOAT " 1 != 1+EPSILON", x1 != x2); \
\
x1 = 1.0; \
x2 = x1 - EPSILON; \
check (#FLOAT " 1 != 1-EPSILON", x1 != x2); \
\
/* test if HUGE_VALx is ok */ \
x1 = HUGEVAL; \
check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1); \
x1 = - HUGEVAL; \
check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1); \
}
#define TEST_CONVERT(NAME, FLOAT, DOUBLE, SUFFIX) \
void \
NAME (void) \
{ \
volatile DOUBLE Inf_var, qNaN_var, zero_var, one_var; \
/* A sNaN is only guaranteed to be representable in variables with */ \
/* static (or thread-local) storage duration. */ \
static volatile DOUBLE sNaN_var = __builtin_nans ## SUFFIX (""); \
FLOAT x1, x2; \
\
zero_var = 0.0; \
one_var = 1.0; \
qNaN_var = __builtin_nan ## SUFFIX (""); \
Inf_var = one_var / zero_var; \
\
(void) &qNaN_var; \
(void) &sNaN_var; \
(void) &Inf_var; \
\
x1 = (FLOAT) qNaN_var; \
check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, isnan", isnan (x1)); \
check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, !issignaling", \
!issignaling (x1)); \
if (SNAN_TESTS (FLOAT)) \
{ \
x1 = (FLOAT) sNaN_var; \
check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, isnan", isnan (x1)); \
if (SNAN_TESTS_TYPE_CAST) \
{ \
/* Upon type conversion, a sNaN is converted into a qNaN plus an */ \
/* INVALID exception (not checked here). */ \
check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, !issignaling", \
!issignaling (x1)); \
} \
} \
x2 = (FLOAT) Inf_var; \
check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") Inf", isinf (x2) != 0); \
}
TEST_FUNC (float_test, float, f, FLT_EPSILON, HUGE_VALF)
TEST_FUNC (double_test, double, , DBL_EPSILON, HUGE_VAL)
TEST_CONVERT (convert_dfsf_test, float, double, )
TEST_CONVERT (convert_sfdf_test, double, float, f)
Remove NO_LONG_DOUBLE conditionals in libm tests (bug 21607). As noted in bug 21607, NO_LONG_DOUBLE conditionals in libm tests are no longer effective. For most this is harmless - they were only present because of long double functions not being declared with _LIBC defined, and _LIBC is no longer defined for building most tests. For the few where this is actually relevant to the test, testing LDBL_MANT_DIG > DBL_MANT_DIG is more appropriate as that limits the test to public APIs. This patch fixes the tests accordingly. Tested for x86_64 and arm. [BZ #21607] * math/basic-test.c [!NO_LONG_DOUBLE]: Change conditionals to [LDBL_MANT_DIG > DBL_MANT_DIG]. * math/bug-nextafter.c [!NO_LONG_DOUBLE]: Remove conditionals. * math/bug-nexttoward.c [!NO_LONG_DOUBLE]: Likewise. * math/test-math-isinff.cc [!NO_LONG_DOUBLE]: Likewise. * math/test-math-iszero.cc [!NO_LONG_DOUBLE]: Likewise. * math/test-nan-overflow.c [!NO_LONG_DOUBLE]: Likewise. * math/test-nan-payload.c [!NO_LONG_DOUBLE]: Likewise. * math/test-nearbyint-except-2.c [!NO_LONG_DOUBLE]: Likewise. * math/test-nearbyint-except.c [!NO_LONG_DOUBLE]: Likewise. * math/test-powl.c [!NO_LONG_DOUBLE]: Likewise. * math/test-signgam-finite-c99.c [!NO_LONG_DOUBLE]: Likewise. * math/test-signgam-finite.c [!NO_LONG_DOUBLE]: Likewise. * math/test-signgam-main.c [!NO_LONG_DOUBLE]: Likewise. * math/test-snan.c [!NO_LONG_DOUBLE]: Likewise. * math/test-tgmath-ret.c [!NO_LONG_DOUBLE]: Likewise. * math/test-tgmath.c: Include <float.h>. [!NO_LONG_DOUBLE]: Change conditionals to [LDBL_MANT_DIG > DBL_MANT_DIG]. * math/test-tgmath2.c: Include <float.h>. [!NO_LONG_DOUBLE]: Change conditionals to [LDBL_MANT_DIG > DBL_MANT_DIG].
2017-06-28 16:01:20 +00:00
#if LDBL_MANT_DIG > DBL_MANT_DIG
TEST_FUNC (ldouble_test, long double, l, LDBL_EPSILON, HUGE_VALL)
TEST_CONVERT (convert_tfsf_test, float, long double, l)
TEST_CONVERT (convert_sftf_test, long double, float, f)
TEST_CONVERT (convert_tfdf_test, double, long double, l)
TEST_CONVERT (convert_dftf_test, long double, double, )
#endif
int
do_test (void)
{
float_test ();
double_test ();
convert_dfsf_test();
convert_sfdf_test();
Remove NO_LONG_DOUBLE conditionals in libm tests (bug 21607). As noted in bug 21607, NO_LONG_DOUBLE conditionals in libm tests are no longer effective. For most this is harmless - they were only present because of long double functions not being declared with _LIBC defined, and _LIBC is no longer defined for building most tests. For the few where this is actually relevant to the test, testing LDBL_MANT_DIG > DBL_MANT_DIG is more appropriate as that limits the test to public APIs. This patch fixes the tests accordingly. Tested for x86_64 and arm. [BZ #21607] * math/basic-test.c [!NO_LONG_DOUBLE]: Change conditionals to [LDBL_MANT_DIG > DBL_MANT_DIG]. * math/bug-nextafter.c [!NO_LONG_DOUBLE]: Remove conditionals. * math/bug-nexttoward.c [!NO_LONG_DOUBLE]: Likewise. * math/test-math-isinff.cc [!NO_LONG_DOUBLE]: Likewise. * math/test-math-iszero.cc [!NO_LONG_DOUBLE]: Likewise. * math/test-nan-overflow.c [!NO_LONG_DOUBLE]: Likewise. * math/test-nan-payload.c [!NO_LONG_DOUBLE]: Likewise. * math/test-nearbyint-except-2.c [!NO_LONG_DOUBLE]: Likewise. * math/test-nearbyint-except.c [!NO_LONG_DOUBLE]: Likewise. * math/test-powl.c [!NO_LONG_DOUBLE]: Likewise. * math/test-signgam-finite-c99.c [!NO_LONG_DOUBLE]: Likewise. * math/test-signgam-finite.c [!NO_LONG_DOUBLE]: Likewise. * math/test-signgam-main.c [!NO_LONG_DOUBLE]: Likewise. * math/test-snan.c [!NO_LONG_DOUBLE]: Likewise. * math/test-tgmath-ret.c [!NO_LONG_DOUBLE]: Likewise. * math/test-tgmath.c: Include <float.h>. [!NO_LONG_DOUBLE]: Change conditionals to [LDBL_MANT_DIG > DBL_MANT_DIG]. * math/test-tgmath2.c: Include <float.h>. [!NO_LONG_DOUBLE]: Change conditionals to [LDBL_MANT_DIG > DBL_MANT_DIG].
2017-06-28 16:01:20 +00:00
#if LDBL_MANT_DIG > DBL_MANT_DIG
ldouble_test ();
convert_tfsf_test();
convert_sftf_test();
convert_tfdf_test();
convert_dftf_test();
#endif
return errors != 0;
}
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"