From 4629c866ad79167d60ca9bf263d871eabb59d3d9 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Wed, 18 Feb 2015 21:10:49 +0000 Subject: Fix atan / atan2 missing underflows (bug 15319). This patch fixes bug 15319, missing underflows from atan / atan2 when the result of atan is very close to its small argument (or that of atan2 is very close to the ratio of its arguments, which may be an exact division). The usual approach of doing an underflowing computation if the computed result is subnormal is followed. For 32-bit x86, there are extra complications: the inline __ieee754_atan2 in bits/mathinline.h needs to be disabled for float and double because other libm functions using it generally rely on getting proper underflow exceptions from it, while the out-of-line functions have to remove excess range and precision from the underflowing result so as to return an exact 0 in the case where errno should be set for underflow to 0. (The failures I saw without that are similar to those Carlos reported for other functions, where I haven't seen a response to confirming if my diagnosis is correct. Arguably all libm functions with float and double returns should remove excess range and precision, but that's a separate matter.) The x86_64 long double case reported in a comment in bug 15319 is not a bug (it's an argument of LDBL_MIN, and x86_64 is an after-rounding architecture so the correct IEEE result is not to raise underflow in the given rounding mode, in addition to treating the result as an exact LDBL_MIN being within the newly clarified documentation of accuracy goals). I'm presuming that the fpatan instruction can be trusted to raise appropriate exceptions when the (long double) result underflows (after rounding) and so no changes are needed for x86 / x86_64 long double functions here; empirically this is the case for the cases covered in the testsuite, on my system. Tested for x86_64, x86, powerpc and mips64. Only 32-bit x86 needs ulps updates (for the changes to inlines meaning some functions no longer get excess precision from their __ieee754_atan2* calls). [BZ #15319] * sysdeps/i386/fpu/e_atan2.S (dbl_min): New object. (MO): New macro. (__ieee754_atan2): For results with small absolute value, force underflow exception and remove excess range and precision from return value. * sysdeps/i386/fpu/e_atan2f.S (flt_min): New object. (MO): New macro. (__ieee754_atan2f): For results with small absolute value, force underflow exception and remove excess range and precision from return value. * sysdeps/i386/fpu/s_atan.S (dbl_min): New object. (MO): New macro. (__atan): For results with small absolute value, force underflow exception and remove excess range and precision from return value. * sysdeps/i386/fpu/s_atanf.S (flt_min): New object. (MO): New macro. (__atanf): For results with small absolute value, force underflow exception and remove excess range and precision from return value. * sysdeps/ieee754/dbl-64/e_atan2.c: Include and . (__ieee754_atan2): Force underflow exception for results with small absolute value. * sysdeps/ieee754/dbl-64/s_atan.c: Include and . (atan): Force underflow exception for results with small absolute value. * sysdeps/ieee754/flt-32/s_atanf.c: Include . (__atanf): Force underflow exception for results with small absolute value. * sysdeps/ieee754/ldbl-128/s_atanl.c: Include and . (__atanl): Force underflow exception for results with small absolute value. * sysdeps/ieee754/ldbl-128ibm/s_atanl.c: Include . (__atanl): Force underflow exception for results with small absolute value. * sysdeps/x86/fpu/bits/mathinline.h [!__SSE2_MATH__ && !__x86_64__ && __LIBC_INTERNAL_MATH_INLINES] (__ieee754_atan2): Only define inline for long double. * sysdeps/x86_64/fpu/multiarch/e_atan2.c [HAVE_FMA4_SUPPORT || HAVE_AVX_SUPPORT]: Include . * math/auto-libm-test-in: Do not mark underflow exceptions as possibly missing for bug 15319. Add more tests of atan2. * math/auto-libm-test-out: Regenerated. * math/libm-test.inc (casin_test_data): Do not mark underflow exceptions as possibly missing for bug 15319. (casinh_test_data): Likewise. * sysdeps/i386/fpu/libm-test-ulps: Update. --- sysdeps/ieee754/dbl-64/e_atan2.c | 14 ++++++++++++-- sysdeps/ieee754/dbl-64/s_atan.c | 11 ++++++++++- sysdeps/ieee754/flt-32/s_atanf.c | 6 ++++++ sysdeps/ieee754/ldbl-128/s_atanl.c | 7 +++++++ sysdeps/ieee754/ldbl-128ibm/s_atanl.c | 6 ++++++ 5 files changed, 41 insertions(+), 3 deletions(-) (limited to 'sysdeps/ieee754') diff --git a/sysdeps/ieee754/dbl-64/e_atan2.c b/sysdeps/ieee754/dbl-64/e_atan2.c index 425da22719..a03ce3e4a6 100644 --- a/sysdeps/ieee754/dbl-64/e_atan2.c +++ b/sysdeps/ieee754/dbl-64/e_atan2.c @@ -41,6 +41,8 @@ #include "MathLib.h" #include "uatan.tbl" #include "atnat2.h" +#include +#include #include #include @@ -202,10 +204,18 @@ __ieee754_atan2 (double y, double x) { if (x > 0) { + double ret; if ((z = ay / ax) < TWOM1022) - return normalized (ax, ay, y, z); + ret = normalized (ax, ay, y, z); else - return signArctan2 (y, z); + ret = signArctan2 (y, z); + if (fabs (ret) < DBL_MIN) + { + double vret = ret ? ret : DBL_MIN; + double force_underflow = vret * vret; + math_force_eval (force_underflow); + } + return ret; } else { diff --git a/sysdeps/ieee754/dbl-64/s_atan.c b/sysdeps/ieee754/dbl-64/s_atan.c index a482badc56..7b598f14a9 100644 --- a/sysdeps/ieee754/dbl-64/s_atan.c +++ b/sysdeps/ieee754/dbl-64/s_atan.c @@ -41,7 +41,9 @@ #include "MathLib.h" #include "uatan.tbl" #include "atnat.h" +#include #include +#include #include void __mpatan (mp_no *, mp_no *, int); /* see definition in mpatan.c */ @@ -85,7 +87,14 @@ atan (double x) if (u < B) { if (u < A) - return x; + { + if (u < DBL_MIN) + { + double force_underflow = x * x; + math_force_eval (force_underflow); + } + return x; + } else { /* A <= u < B */ v = x * x; diff --git a/sysdeps/ieee754/flt-32/s_atanf.c b/sysdeps/ieee754/flt-32/s_atanf.c index 02c5e46287..159391894f 100644 --- a/sysdeps/ieee754/flt-32/s_atanf.c +++ b/sysdeps/ieee754/flt-32/s_atanf.c @@ -17,6 +17,7 @@ static char rcsid[] = "$NetBSD: s_atanf.c,v 1.4 1995/05/10 20:46:47 jtc Exp $"; #endif +#include #include #include @@ -66,6 +67,11 @@ float __atanf(float x) else return -atanhi[3]-atanlo[3]; } if (ix < 0x3ee00000) { /* |x| < 0.4375 */ if (ix < 0x31000000) { /* |x| < 2^-29 */ + if (fabsf (x) < FLT_MIN) + { + float force_underflow = x * x; + math_force_eval (force_underflow); + } if(huge+x>one) return x; /* raise inexact */ } id = -1; diff --git a/sysdeps/ieee754/ldbl-128/s_atanl.c b/sysdeps/ieee754/ldbl-128/s_atanl.c index dc5e7ef462..1367b6b15d 100644 --- a/sysdeps/ieee754/ldbl-128/s_atanl.c +++ b/sysdeps/ieee754/ldbl-128/s_atanl.c @@ -59,6 +59,8 @@ . */ +#include +#include #include /* arctan(k/8), k = 0, ..., 82 */ @@ -200,6 +202,11 @@ __atanl (long double x) if (k <= 0x3fc50000) /* |x| < 2**-58 */ { + if (fabsl (x) < LDBL_MIN) + { + long double force_underflow = x * x; + math_force_eval (force_underflow); + } /* Raise inexact. */ if (huge + x > 0.0) return x; diff --git a/sysdeps/ieee754/ldbl-128ibm/s_atanl.c b/sysdeps/ieee754/ldbl-128ibm/s_atanl.c index 41dde23998..6ddf4b1c5e 100644 --- a/sysdeps/ieee754/ldbl-128ibm/s_atanl.c +++ b/sysdeps/ieee754/ldbl-128ibm/s_atanl.c @@ -59,6 +59,7 @@ . */ +#include #include #include #include @@ -198,6 +199,11 @@ __atanl (long double x) if (k <= 0x3c800000) /* |x| <= 2**-55. */ { + if (fabsl (x) < LDBL_MIN) + { + long double force_underflow = x * x; + math_force_eval (force_underflow); + } /* Raise inexact. */ if (1e300L + x > 0.0) return x; -- cgit 1.4.1