From 939fdd1c36dde8884543bc9c3b8506e76c9ff0e4 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Thu, 5 Nov 2015 21:23:07 +0000 Subject: [PATCH] Fix finite-math-only lgamma functions signgam setting (bug 19211). The lgamma inline functions in bits/math-finite.h do not set signgam if __USE_ISOC99, even when other feature test macros mean a standard such as XSI POSIX is selected for which it should be set. (This is essentially the opposite issue to bug 15421, the out-of-line versions setting signgam even when they shouldn't.) This patch fixes those functions to use __USE_MISC || __USE_XOPEN as the condition for when to set signgam, since it's the condition for when math.h declares signgam. The legacy gamma* names are only declared at all if __USE_MISC || __USE_XOPEN, so they just set signgam unconditionally. Tests for certain standards or not using _GNU_SOURCE cannot use test-skeleton.c (this is a known issue noted on the wiki todo list). Thus, the new tests that signgam remains not set in ISO C modes do not use test-skeleton.c. They also define _ISOMAC to avoid running into declarations in the internal include/ headers that only work in _GNU_SOURCE mode. Tested for x86_64 and x86. [BZ #19211] * math/bits/math-finite.h (lgamma): Set signgam if [__USE_MISC || __USE_XOPEN], not if [!__USE_ISOC99]. (lgammaf): Likewise. (lgammal): Likewise. (gamma): Set signgam unconditionally, not if [!__USE_ISOC99]. (gammaf): Likewise. (gammal): Likewise. * math/test-signgam-finite-c11.c: New file. * math/test-signgam-finite-c99.c: Likewise. * math/test-signgam-finite.c: Likewise. * math/Makefile (tests): Add test-signgam-finite, test-signgam-finite-c99 and test-signgam-finite-c11. (CFLAGS-test-signgam-finite.c): New variable. (CFLAGS-test-signgam-finite-c99.c): Likewise. (CFLAGS-test-signgam-finite-c11.c): Likewise. --- ChangeLog | 17 +++++++++ NEWS | 2 +- math/Makefile | 7 +++- math/bits/math-finite.h | 33 +++++------------ math/test-signgam-finite-c11.c | 1 + math/test-signgam-finite-c99.c | 65 ++++++++++++++++++++++++++++++++++ math/test-signgam-finite.c | 65 ++++++++++++++++++++++++++++++++++ 7 files changed, 164 insertions(+), 26 deletions(-) create mode 100644 math/test-signgam-finite-c11.c create mode 100644 math/test-signgam-finite-c99.c create mode 100644 math/test-signgam-finite.c diff --git a/ChangeLog b/ChangeLog index 3ee9f19cf8..9c78a11722 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,22 @@ 2015-11-05 Joseph Myers + [BZ #19211] + * math/bits/math-finite.h (lgamma): Set signgam if [__USE_MISC || + __USE_XOPEN], not if [!__USE_ISOC99]. + (lgammaf): Likewise. + (lgammal): Likewise. + (gamma): Set signgam unconditionally, not if [!__USE_ISOC99]. + (gammaf): Likewise. + (gammal): Likewise. + * math/test-signgam-finite-c11.c: New file. + * math/test-signgam-finite-c99.c: Likewise. + * math/test-signgam-finite.c: Likewise. + * math/Makefile (tests): Add test-signgam-finite, + test-signgam-finite-c99 and test-signgam-finite-c11. + (CFLAGS-test-signgam-finite.c): New variable. + (CFLAGS-test-signgam-finite-c99.c): Likewise. + (CFLAGS-test-signgam-finite-c11.c): Likewise. + [BZ #19212] * include/features.h [(_XOPEN_SOURCE - 0) >= 500]: Change conditional to [defined _XOPEN_SOURCE && (_XOPEN_SOURCE - 0) >= diff --git a/NEWS b/NEWS index 901f92b4cc..b462e85ddc 100644 --- a/NEWS +++ b/NEWS @@ -22,7 +22,7 @@ Version 2.23 19003, 19007, 19012, 19016, 19018, 19032, 19046, 19048, 19049, 19050, 19059, 19071, 19074, 19076, 19077, 19078, 19079, 19085, 19086, 19088, 19094, 19095, 19124, 19125, 19129, 19134, 19137, 19156, 19174, 19181, - 19189, 19201, 19205, 19209, 19212. + 19189, 19201, 19205, 19209, 19211, 19212. * A defect in the malloc implementation, present since glibc 2.15 (2012) or glibc 2.10 via --enable-experimental-malloc (2009), could result in the diff --git a/math/Makefile b/math/Makefile index 3efbc58d5c..adeb806e3f 100644 --- a/math/Makefile +++ b/math/Makefile @@ -108,7 +108,8 @@ tests = test-matherr test-fenv atest-exp atest-sincos atest-exp2 basic-test \ test-tgmath-ret bug-nextafter bug-nexttoward bug-tgmath1 \ test-tgmath-int test-tgmath2 test-powl tst-CMPLX tst-CMPLX2 test-snan \ test-fenv-tls test-fenv-preserve test-fenv-return test-fenvinline \ - test-nearbyint-except test-fenv-clear $(tests-static) + test-nearbyint-except test-fenv-clear test-signgam-finite \ + test-signgam-finite-c99 test-signgam-finite-c11 $(tests-static) tests-static = test-fpucw-static test-fpucw-ieee-static # We do the `long double' tests only if this data type is available and # distinct from `double'. @@ -185,6 +186,10 @@ CPPFLAGS-test-idouble.c = -U__LIBC_INTERNAL_MATH_INLINES \ CPPFLAGS-test-ildoubl.c = -U__LIBC_INTERNAL_MATH_INLINES \ $(libm-test-fast-math-cflags) +CFLAGS-test-signgam-finite.c = -ffinite-math-only +CFLAGS-test-signgam-finite-c99.c = -ffinite-math-only -std=c99 +CFLAGS-test-signgam-finite-c11.c = -ffinite-math-only -std=c11 + # The -lieee module sets the _LIB_VERSION_ switch to IEEE mode # for error handling in the -lm functions. install-lib += libieee.a diff --git a/math/bits/math-finite.h b/math/bits/math-finite.h index 5dd8406588..e3f93bd239 100644 --- a/math/bits/math-finite.h +++ b/math/bits/math-finite.h @@ -307,32 +307,32 @@ extern long double __lgammal_r_finite (long double, int *); /* lgamma. */ __extern_always_inline double __NTH (lgamma (double __d)) { -# ifdef __USE_ISOC99 +# if defined __USE_MISC || defined __USE_XOPEN + return __lgamma_r_finite (__d, &signgam); +# else int __local_signgam = 0; return __lgamma_r_finite (__d, &__local_signgam); -# else - return __lgamma_r_finite (__d, &signgam); # endif } #endif #if defined __USE_ISOC99 && defined __extern_always_inline __extern_always_inline float __NTH (lgammaf (float __d)) { -# ifdef __USE_ISOC99 +# if defined __USE_MISC || defined __USE_XOPEN + return __lgammaf_r_finite (__d, &signgam); +# else int __local_signgam = 0; return __lgammaf_r_finite (__d, &__local_signgam); -# else - return __lgammaf_r_finite (__d, &signgam); # endif } # ifdef __MATH_DECLARE_LDOUBLE __extern_always_inline long double __NTH (lgammal (long double __d)) { -# ifdef __USE_ISOC99 +# if defined __USE_MISC || defined __USE_XOPEN + return __lgammal_r_finite (__d, &signgam); +# else int __local_signgam = 0; return __lgammal_r_finite (__d, &__local_signgam); -# else - return __lgammal_r_finite (__d, &signgam); # endif } # endif @@ -343,32 +343,17 @@ __extern_always_inline long double __NTH (lgammal (long double __d)) /* gamma. */ __extern_always_inline double __NTH (gamma (double __d)) { -# ifdef __USE_ISOC99 - int __local_signgam = 0; - return __lgamma_r_finite (__d, &__local_signgam); -# else return __lgamma_r_finite (__d, &signgam); -# endif } # ifdef __USE_ISOC99 __extern_always_inline float __NTH (gammaf (float __d)) { -# ifdef __USE_ISOC99 - int __local_signgam = 0; - return __lgammaf_r_finite (__d, &__local_signgam); -# else return __lgammaf_r_finite (__d, &signgam); -# endif } # ifdef __MATH_DECLARE_LDOUBLE __extern_always_inline long double __NTH (gammal (long double __d)) { -# ifdef __USE_ISOC99 - int __local_signgam = 0; - return __lgammal_r_finite (__d, &__local_signgam); -# else return __lgammal_r_finite (__d, &signgam); -# endif } # endif # endif diff --git a/math/test-signgam-finite-c11.c b/math/test-signgam-finite-c11.c new file mode 100644 index 0000000000..dff132df93 --- /dev/null +++ b/math/test-signgam-finite-c11.c @@ -0,0 +1 @@ +#include "test-signgam-finite-c99.c" diff --git a/math/test-signgam-finite-c99.c b/math/test-signgam-finite-c99.c new file mode 100644 index 0000000000..bdbaf5a13c --- /dev/null +++ b/math/test-signgam-finite-c99.c @@ -0,0 +1,65 @@ +/* Test lgamma functions do not set signgam for -ffinite-math-only for ISO C. + Copyright (C) 2015 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 + . */ + +#undef _LIBC +#undef _GNU_SOURCE +#define _ISOMAC + +#include +#include + +int signgam; + +#define RUN_TESTS(FUNC, TYPE) \ + do \ + { \ + volatile TYPE a, b, c __attribute__ ((unused)); \ + a = 0.5; \ + b = -0.5; \ + signgam = 123; \ + c = FUNC (a); \ + if (signgam == 123) \ + puts ("PASS: " #FUNC " (0.5) setting signgam"); \ + else \ + { \ + puts ("FAIL: " #FUNC " (0.5) setting signgam"); \ + result = 1; \ + } \ + signgam = 123; \ + c = FUNC (b); \ + if (signgam == 123) \ + puts ("PASS: " #FUNC " (0.5) setting signgam"); \ + else \ + { \ + puts ("FAIL: " #FUNC " (0.5) setting signgam"); \ + result = 1; \ + } \ + } \ + while (0) + +int +main (void) +{ + int result = 0; + RUN_TESTS (lgammaf, float); + RUN_TESTS (lgamma, double); +#ifndef NO_LONG_DOUBLE + RUN_TESTS (lgammal, long double); +#endif + return result; +} diff --git a/math/test-signgam-finite.c b/math/test-signgam-finite.c new file mode 100644 index 0000000000..5d444c8068 --- /dev/null +++ b/math/test-signgam-finite.c @@ -0,0 +1,65 @@ +/* Test lgamma functions set signgam for -ffinite-math-only (bug 19211). + Copyright (C) 2015 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 + . */ + +#include +#include + +#define RUN_TESTS(FUNC, TYPE) \ + do \ + { \ + volatile TYPE a, b, c __attribute__ ((unused)); \ + a = 0.5; \ + b = -0.5; \ + signgam = 123; \ + c = FUNC (a); \ + if (signgam == 1) \ + puts ("PASS: " #FUNC " (0.5) setting signgam"); \ + else \ + { \ + puts ("FAIL: " #FUNC " (0.5) setting signgam"); \ + result = 1; \ + } \ + signgam = 123; \ + c = FUNC (b); \ + if (signgam == -1) \ + puts ("PASS: " #FUNC " (0.5) setting signgam"); \ + else \ + { \ + puts ("FAIL: " #FUNC " (0.5) setting signgam"); \ + result = 1; \ + } \ + } \ + while (0) + +static int +do_test (void) +{ + int result = 0; + RUN_TESTS (lgammaf, float); + RUN_TESTS (gammaf, float); + RUN_TESTS (lgamma, double); + RUN_TESTS (gamma, double); +#ifndef NO_LONG_DOUBLE + RUN_TESTS (lgammal, long double); + RUN_TESTS (gammal, long double); +#endif + return result; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c"