diff --git a/src/math/asin.c b/src/math/asin.c index 3e8f99ed558fb8df591c662648e0ab38335fd182..c926b188552b20b1842a26e4c5500b2f0686a95f 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 51fe6c617cc03569c759cd56acb1a0ecc2e55b49..bcd304a3425991689cbf14dd1cd575a43a8b1117 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 5a1d33e6a05c644610933b1bea8ea8e39f3f99f1..63b0ab25e3cf02ea81bab5a9ee4d99d6c40bb582 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 ac8bfd0665f08547e444939c540dd6eafe461fd0..178341b670fa249fa50157d878ac2a66bd7f1843 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 6c67249cedeced4cf02ecdc37fc945c6f8806af1..9bed63c298356c6c692a452892cc1c36b8072a74 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 39832d2857ab453af5626b9d9d99e68b49b58de3..c38e0bcbd331a366d9559d030f0b554c6fb91d85 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 47e36bfa293a1dd41a3a4b1055aae1fcf8029868..00022c4e6ff6d8250ea238207a12199a1e349dac 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 0e766c5cbeb195b9e8d27df8f4c008d965025075..20d6dbcf4175c7aad93fe70a53056f0d3e1c4f13 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 8099ec3019a9ea3de0f88bfa98be977ac55d2e23..10636fbd7be6c21d1c58326eba2bf7179b0dea8f 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; }