From a9708fed779ff4e61e07ec2eae78e5c0753e1222 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Mon, 7 Jan 2013 14:59:53 +0000 Subject: Fix casinh, casin overflow (bug 14996). --- math/libm-test.inc | 16 ++++++++++++++++ math/s_casinh.c | 28 +++++++++++++++++++++------- math/s_casinhf.c | 28 +++++++++++++++++++++------- math/s_casinhl.c | 33 +++++++++++++++++++++++++++------ 4 files changed, 85 insertions(+), 20 deletions(-) (limited to 'math') diff --git a/math/libm-test.inc b/math/libm-test.inc index a586f7e..56e3217 100644 --- a/math/libm-test.inc +++ b/math/libm-test.inc @@ -1727,6 +1727,14 @@ casin_test (void) TEST_c_c (casin, -1.0L, -0x1p5000L, -7.079811261048172892385615158694057552948e-1506L, -3.466429049980286492395577839412341016946e3L); #endif + TEST_c_c (casin, 0x1.fp127L, 0x1.fp127L, 7.853981633974483096156608458198757210493e-1L, 8.973081118419833726837456344608533993585e1L); +#ifndef TEST_FLOAT + TEST_c_c (casin, 0x1.fp1023L, 0x1.fp1023L, 7.853981633974483096156608458198757210493e-1L, 7.107906849659093345062145442726115449315e2L); +#endif +#if defined TEST_LDOUBLE && LDBL_MAX_EXP >= 16384 + TEST_c_c (casin, 0x1.fp16383L, 0x1.fp16383L, 7.853981633974483096156608458198757210493e-1L, 1.135753137836666928715489992987020363057e4L); +#endif + TEST_c_c (casin, 0.75L, 1.25L, 0.453276177638793913448921196101971749L, 1.13239363160530819522266333696834467L); TEST_c_c (casin, -2, -3, -0.57065278432109940071028387968566963L, -1.9833870299165354323470769028940395L); @@ -1846,6 +1854,14 @@ casinh_test (void) TEST_c_c (casinh, -1.0L, -0x1p5000L, -3.466429049980286492395577839412341016946e3L, -1.570796326794896619231321691639751442099L); #endif + TEST_c_c (casinh, 0x1.fp127L, 0x1.fp127L, 8.973081118419833726837456344608533993585e1L, 7.853981633974483096156608458198757210493e-1L); +#ifndef TEST_FLOAT + TEST_c_c (casinh, 0x1.fp1023L, 0x1.fp1023L, 7.107906849659093345062145442726115449315e2L, 7.853981633974483096156608458198757210493e-1L); +#endif +#if defined TEST_LDOUBLE && LDBL_MAX_EXP >= 16384 + TEST_c_c (casinh, 0x1.fp16383L, 0x1.fp16383L, 1.135753137836666928715489992987020363057e4L, 7.853981633974483096156608458198757210493e-1L); +#endif + TEST_c_c (casinh, 0.75L, 1.25L, 1.03171853444778027336364058631006594L, 0.911738290968487636358489564316731207L); TEST_c_c (casinh, -2, -3, -1.9686379257930962917886650952454982L, -0.96465850440760279204541105949953237L); diff --git a/math/s_casinh.c b/math/s_casinh.c index acdf1a1..b493982 100644 --- a/math/s_casinh.c +++ b/math/s_casinh.c @@ -20,7 +20,7 @@ #include #include #include - +#include __complex__ double __casinh (__complex__ double x) @@ -69,15 +69,29 @@ __casinh (__complex__ double x) rx = fabs (__real__ x); ix = fabs (__imag__ x); - __real__ y = (rx - ix) * (rx + ix) + 1.0; - __imag__ y = 2.0 * rx * ix; + if (rx >= 1.0 / DBL_EPSILON || ix >= 1.0 / DBL_EPSILON) + { + /* For large x in the first quadrant, x + csqrt (1 + x * x) + is sufficiently close to 2 * x to make no significant + difference to the result; avoid possible overflow from + the squaring and addition. */ + __real__ y = rx; + __imag__ y = ix; + res = __clog (y); + __real__ res += M_LN2; + } + else + { + __real__ y = (rx - ix) * (rx + ix) + 1.0; + __imag__ y = 2.0 * rx * ix; - y = __csqrt (y); + y = __csqrt (y); - __real__ y += rx; - __imag__ y += ix; + __real__ y += rx; + __imag__ y += ix; - res = __clog (y); + res = __clog (y); + } /* Give results the correct sign for the original argument. */ __real__ res = __copysign (__real__ res, __real__ x); diff --git a/math/s_casinhf.c b/math/s_casinhf.c index 8f4315c..f865e14 100644 --- a/math/s_casinhf.c +++ b/math/s_casinhf.c @@ -20,7 +20,7 @@ #include #include #include - +#include __complex__ float __casinhf (__complex__ float x) @@ -69,15 +69,29 @@ __casinhf (__complex__ float x) rx = fabsf (__real__ x); ix = fabsf (__imag__ x); - __real__ y = (rx - ix) * (rx + ix) + 1.0; - __imag__ y = 2.0 * rx * ix; + if (rx >= 1.0f / FLT_EPSILON || ix >= 1.0f / FLT_EPSILON) + { + /* For large x in the first quadrant, x + csqrt (1 + x * x) + is sufficiently close to 2 * x to make no significant + difference to the result; avoid possible overflow from + the squaring and addition. */ + __real__ y = rx; + __imag__ y = ix; + res = __clogf (y); + __real__ res += (float) M_LN2; + } + else + { + __real__ y = (rx - ix) * (rx + ix) + 1.0; + __imag__ y = 2.0 * rx * ix; - y = __csqrtf (y); + y = __csqrtf (y); - __real__ y += rx; - __imag__ y += ix; + __real__ y += rx; + __imag__ y += ix; - res = __clogf (y); + res = __clogf (y); + } /* Give results the correct sign for the original argument. */ __real__ res = __copysignf (__real__ res, __real__ x); diff --git a/math/s_casinhl.c b/math/s_casinhl.c index 83f8d18..d7c7459 100644 --- a/math/s_casinhl.c +++ b/math/s_casinhl.c @@ -20,7 +20,14 @@ #include #include #include +#include +/* To avoid spurious overflows, use this definition to treat IBM long + double as approximating an IEEE-style format. */ +#if LDBL_MANT_DIG == 106 +# undef LDBL_EPSILON +# define LDBL_EPSILON 0x1p-106L +#endif __complex__ long double __casinhl (__complex__ long double x) @@ -69,15 +76,29 @@ __casinhl (__complex__ long double x) rx = fabsl (__real__ x); ix = fabsl (__imag__ x); - __real__ y = (rx - ix) * (rx + ix) + 1.0; - __imag__ y = 2.0 * rx * ix; + if (rx >= 1.0L / LDBL_EPSILON || ix >= 1.0L / LDBL_EPSILON) + { + /* For large x in the first quadrant, x + csqrt (1 + x * x) + is sufficiently close to 2 * x to make no significant + difference to the result; avoid possible overflow from + the squaring and addition. */ + __real__ y = rx; + __imag__ y = ix; + res = __clogl (y); + __real__ res += M_LN2l; + } + else + { + __real__ y = (rx - ix) * (rx + ix) + 1.0; + __imag__ y = 2.0 * rx * ix; - y = __csqrtl (y); + y = __csqrtl (y); - __real__ y += rx; - __imag__ y += ix; + __real__ y += rx; + __imag__ y += ix; - res = __clogl (y); + res = __clogl (y); + } /* Give results the correct sign for the original argument. */ __real__ res = __copysignl (__real__ res, __real__ x); -- cgit v1.1