From c599f4f4d705ba215a2672b92baa1c29d9dd0735 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Thu, 15 Aug 2013 10:14:46 +0000 Subject: math: fix asin, atan, log1p, tanh to raise underflow on subnormal for these functions f(x)=x for small inputs, because f(0)=0 and f'(0)=1, but for subnormal values they should raise the underflow flag (required by annex F), if they are approximated by a polynomial around 0 then spurious underflow should be avoided (not required by annex F) all these functions should raise inexact flag for small x if x!=0, but it's not required by the standard and it does not seem a worthy goal, so support for it is removed in some cases. raising underflow: - x*x may not raise underflow for subnormal x if FLT_EVAL_METHOD!=0 - x*x may raise spurious underflow for normal x if FLT_EVAL_METHOD==0 - in case of double subnormal x, store x as float - in case of float subnormal x, store x*x as float --- src/math/asin.c | 6 ++---- src/math/asinf.c | 7 +++---- src/math/atan.c | 5 +++-- src/math/atanf.c | 5 +++-- src/math/log1p.c | 7 +++++-- src/math/log1pf.c | 7 +++++-- src/math/sinh.c | 4 ++-- src/math/tanh.c | 14 +++++++++----- src/math/tanhf.c | 10 +++++++--- 9 files changed, 39 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/math/asin.c b/src/math/asin.c index 3e8f99ed..c926b188 100644 --- a/src/math/asin.c +++ b/src/math/asin.c @@ -82,11 +82,9 @@ double asin(double x) } /* |x| < 0.5 */ if (ix < 0x3fe00000) { - if (ix < 0x3e500000) { - /* |x|<0x1p-26, return x with inexact if x!=0*/ - FORCE_EVAL(x + 0x1p120f); + /* if 0x1p-1022 <= |x| < 0x1p-26, avoid raising underflow */ + if (ix < 0x3e500000 && ix >= 0x00100000) return x; - } return x + x*R(x*x); } /* 1 > |x| >= 0.5 */ diff --git a/src/math/asinf.c b/src/math/asinf.c index 51fe6c61..bcd304a3 100644 --- a/src/math/asinf.c +++ b/src/math/asinf.c @@ -46,10 +46,9 @@ float asinf(float x) return 0/(x-x); /* asin(|x|>1) is NaN */ } if (ix < 0x3f000000) { /* |x| < 0.5 */ - if (ix < 0x39800000) { /* |x| < 2**-12 */ - FORCE_EVAL(x + 0x1p120f); - return x; /* return x with inexact if x!=0 */ - } + /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */ + if (ix < 0x39800000 && ix >= 0x00800000) + return x; return x + x*R(x*x); } /* 1 > |x| >= 0.5 */ diff --git a/src/math/atan.c b/src/math/atan.c index 5a1d33e6..63b0ab25 100644 --- a/src/math/atan.c +++ b/src/math/atan.c @@ -77,8 +77,9 @@ double atan(double x) } if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ if (ix < 0x3e400000) { /* |x| < 2^-27 */ - /* raise inexact if x!=0 */ - FORCE_EVAL(x + 0x1p120f); + if (ix < 0x00100000) + /* raise underflow for subnormal x */ + FORCE_EVAL((float)x); return x; } id = -1; diff --git a/src/math/atanf.c b/src/math/atanf.c index ac8bfd06..178341b6 100644 --- a/src/math/atanf.c +++ b/src/math/atanf.c @@ -55,8 +55,9 @@ float atanf(float x) } if (ix < 0x3ee00000) { /* |x| < 0.4375 */ if (ix < 0x39800000) { /* |x| < 2**-12 */ - /* raise inexact if x!=0 */ - FORCE_EVAL(x + 0x1p120f); + if (ix < 0x00800000) + /* raise underflow for subnormal x */ + FORCE_EVAL(x*x); return x; } id = -1; diff --git a/src/math/log1p.c b/src/math/log1p.c index 6c67249c..9bed63c2 100644 --- a/src/math/log1p.c +++ b/src/math/log1p.c @@ -104,9 +104,12 @@ double log1p(double x) return (x-x)/(x-x); /* log1p(x<-1)=NaN */ } if (ax < 0x3e200000) { /* |x| < 2**-29 */ - /* raise inexact */ - if (two54 + x > 0.0 && ax < 0x3c900000) /* |x| < 2**-54 */ + /* if 0x1p-1022 <= |x| < 0x1p-54, avoid raising underflow */ + if (ax < 0x3c900000 && ax >= 0x00100000) return x; +#if FLT_EVAL_METHOD != 0 + FORCE_EVAL((float)x); +#endif return x - x*x*0.5; } if (hx > 0 || hx <= (int32_t)0xbfd2bec4) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ diff --git a/src/math/log1pf.c b/src/math/log1pf.c index 39832d28..c38e0bcb 100644 --- a/src/math/log1pf.c +++ b/src/math/log1pf.c @@ -43,9 +43,12 @@ float log1pf(float x) return (x-x)/(x-x); /* log1p(x<-1)=NaN */ } if (ax < 0x38000000) { /* |x| < 2**-15 */ - /* raise inexact */ - if (two25 + x > 0.0f && ax < 0x33800000) /* |x| < 2**-24 */ + /* if 0x1p-126 <= |x| < 0x1p-24, avoid raising underflow */ + if (ax < 0x33800000 && ax >= 0x00800000) return x; +#if FLT_EVAL_METHOD != 0 + FORCE_EVAL(x*x); +#endif return x - x*x*0.5f; } if (hx > 0 || hx <= (int32_t)0xbe95f619) { /* sqrt(2)/2- <= 1+x < sqrt(2)+ */ diff --git a/src/math/sinh.c b/src/math/sinh.c index 47e36bfa..00022c4e 100644 --- a/src/math/sinh.c +++ b/src/math/sinh.c @@ -23,8 +23,8 @@ double sinh(double x) t = expm1(absx); if (w < 0x3ff00000) { if (w < 0x3ff00000 - (26<<20)) - /* note: inexact is raised by expm1 */ - /* note: this branch avoids underflow */ + /* note: inexact and underflow are raised by expm1 */ + /* note: this branch avoids spurious underflow */ return x; return h*(2*t - t*t/(t+1)); } diff --git a/src/math/tanh.c b/src/math/tanh.c index 0e766c5c..20d6dbcf 100644 --- a/src/math/tanh.c +++ b/src/math/tanh.c @@ -9,7 +9,7 @@ double tanh(double x) union {double f; uint64_t i;} u = {.f = x}; uint32_t w; int sign; - double t; + double_t t; /* x = |x| */ sign = u.i >> 63; @@ -22,8 +22,7 @@ double tanh(double x) if (w > 0x40340000) { /* |x| > 20 or nan */ /* note: this branch avoids raising overflow */ - /* raise inexact if x!=+-inf and handle nan */ - t = 1 + 0/(x + 0x1p-120f); + t = 1 - 0/x; } else { t = expm1(2*x); t = 1 - 2/(t+2); @@ -32,10 +31,15 @@ double tanh(double x) /* |x| > log(5/3)/2 ~= 0.2554 */ t = expm1(2*x); t = t/(t+2); - } else { - /* |x| is small, up to 2ulp error in [0.1,0.2554] */ + } else if (w >= 0x00100000) { + /* |x| >= 0x1p-1022, up to 2ulp error in [0.1,0.2554] */ t = expm1(-2*x); t = -t/(t+2); + } else { + /* |x| is subnormal */ + /* note: the branch above would not raise underflow in [0x1p-1023,0x1p-1022) */ + FORCE_EVAL((float)x); + t = x; } return sign ? -t : t; } diff --git a/src/math/tanhf.c b/src/math/tanhf.c index 8099ec30..10636fbd 100644 --- a/src/math/tanhf.c +++ b/src/math/tanhf.c @@ -17,7 +17,7 @@ float tanhf(float x) /* |x| > log(3)/2 ~= 0.5493 or nan */ if (w > 0x41200000) { /* |x| > 10 */ - t = 1 + 0/(x + 0x1p-120f); + t = 1 + 0/x; } else { t = expm1f(2*x); t = 1 - 2/(t+2); @@ -26,10 +26,14 @@ float tanhf(float x) /* |x| > log(5/3)/2 ~= 0.2554 */ t = expm1f(2*x); t = t/(t+2); - } else { - /* |x| is small */ + } else if (w >= 0x00800000) { + /* |x| >= 0x1p-126 */ t = expm1f(-2*x); t = -t/(t+2); + } else { + /* |x| is subnormal */ + FORCE_EVAL(x*x); + t = x; } return sign ? -t : t; } -- cgit v1.2.1