Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Oct 2011 06:29:32 +0000 (UTC)
From:      David Schultz <das@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r226599 - head/lib/msun/src
Message-ID:  <201110210629.p9L6TWQb009912@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: das
Date: Fri Oct 21 06:29:32 2011
New Revision: 226599
URL: http://svn.freebsd.org/changeset/base/226599

Log:
  Improved handling of large x in ccosh{,f}():
  - Handle cases where exp(x) would overflow, but ccosh(x) ~= exp(x) / 2
    shouldn't.
  - Use the ccosh(x) ~= exp(x) / 2 approximation to simplify the calculation
    when x is large.
  
  Similarly for csinh().  Also fixed the return value of csinh(-Inf +- 0i).

Modified:
  head/lib/msun/src/s_ccosh.c
  head/lib/msun/src/s_ccoshf.c
  head/lib/msun/src/s_csinh.c
  head/lib/msun/src/s_csinhf.c

Modified: head/lib/msun/src/s_ccosh.c
==============================================================================
--- head/lib/msun/src/s_ccosh.c	Fri Oct 21 06:28:47 2011	(r226598)
+++ head/lib/msun/src/s_ccosh.c	Fri Oct 21 06:29:32 2011	(r226599)
@@ -42,10 +42,12 @@ __FBSDID("$FreeBSD$");
 
 #include "math_private.h"
 
+static const double huge = 0x1p1023;
+
 double complex
 ccosh(double complex z)
 {
-	double x, y;
+	double x, y, h;
 	int32_t hx, hy, ix, iy, lx, ly;
 
 	x = creal(z);
@@ -61,8 +63,23 @@ ccosh(double complex z)
 	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
 		if ((iy | ly) == 0)
 			return (cpack(cosh(x), x * y));
-		/* XXX We don't handle |x| > DBL_MAX ln(2) yet. */
-		return (cpack(cosh(x) * cos(y), sinh(x) * sin(y)));
+		if (ix < 0x40360000)	/* small x: normal case */
+			return (cpack(cosh(x) * cos(y), sinh(x) * sin(y)));
+
+		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
+		if (ix < 0x40862e42) {
+			/* x < 710: exp(|x|) won't overflow */
+			h = exp(fabs(x)) * 0.5;
+			return (cpack(h * cos(y), copysign(h, x) * sin(y)));
+		} else if (ix < 0x4096bbaa) {
+			/* x < 1455: scale to avoid overflow */
+			z = __ldexp_cexp(cpack(fabs(x), y), -1);
+			return (cpack(creal(z), cimag(z) * copysign(1, x)));
+		} else {
+			/* x >= 1455: the result always overflows */
+			h = huge * x;
+			return (cpack(h * h * cos(y), h * sin(y)));
+		}
 	}
 
 	/*

Modified: head/lib/msun/src/s_ccoshf.c
==============================================================================
--- head/lib/msun/src/s_ccoshf.c	Fri Oct 21 06:28:47 2011	(r226598)
+++ head/lib/msun/src/s_ccoshf.c	Fri Oct 21 06:29:32 2011	(r226599)
@@ -36,10 +36,12 @@ __FBSDID("$FreeBSD$");
 
 #include "math_private.h"
 
+static const float huge = 0x1p127;
+
 float complex
 ccoshf(float complex z)
 {
-	float x, y;
+	float x, y, h;
 	int32_t hx, hy, ix, iy;
 
 	x = crealf(z);
@@ -54,8 +56,23 @@ ccoshf(float complex z)
 	if (ix < 0x7f800000 && iy < 0x7f800000) {
 		if (iy == 0)
 			return (cpackf(coshf(x), x * y));
-		/* XXX We don't handle |x| > FLT_MAX ln(2) yet. */
-		return (cpackf(coshf(x) * cosf(y), sinhf(x) * sinf(y)));
+		if (ix < 0x41100000)	/* small x: normal case */
+			return (cpackf(coshf(x) * cosf(y), sinhf(x) * sinf(y)));
+
+		/* |x| >= 9, so cosh(x) ~= exp(|x|) */
+		if (ix < 0x42b17218) {
+			/* x < 88.7: expf(|x|) won't overflow */
+			h = expf(fabsf(x)) * 0.5f;
+			return (cpackf(h * cosf(y), copysignf(h, x) * sinf(y)));
+		} else if (ix < 0x4340b1e7) {
+			/* x < 192.7: scale to avoid overflow */
+			z = __ldexp_cexpf(cpackf(fabsf(x), y), -1);
+			return (cpackf(crealf(z), cimagf(z) * copysignf(1, x)));
+		} else {
+			/* x >= 192.7: the result always overflows */
+			h = huge * x;
+			return (cpackf(h * h * cosf(y), h * sinf(y)));
+		}
 	}
 
 	if (ix == 0 && iy >= 0x7f800000)

Modified: head/lib/msun/src/s_csinh.c
==============================================================================
--- head/lib/msun/src/s_csinh.c	Fri Oct 21 06:28:47 2011	(r226598)
+++ head/lib/msun/src/s_csinh.c	Fri Oct 21 06:29:32 2011	(r226599)
@@ -42,10 +42,12 @@ __FBSDID("$FreeBSD$");
 
 #include "math_private.h"
 
+static const double huge = 0x1p1023;
+
 double complex
 csinh(double complex z)
 {
-	double x, y;
+	double x, y, h;
 	int32_t hx, hy, ix, iy, lx, ly;
 
 	x = creal(z);
@@ -61,8 +63,23 @@ csinh(double complex z)
 	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
 		if ((iy | ly) == 0)
 			return (cpack(sinh(x), y));
-		/* XXX We don't handle |x| > DBL_MAX ln(2) yet. */
-		return (cpack(sinh(x) * cos(y), cosh(x) * sin(y)));
+		if (ix < 0x40360000)	/* small x: normal case */
+			return (cpack(sinh(x) * cos(y), cosh(x) * sin(y)));
+
+		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
+		if (ix < 0x40862e42) {
+			/* x < 710: exp(|x|) won't overflow */
+			h = exp(fabs(x)) * 0.5;
+			return (cpack(copysign(h, x) * cos(y), h * sin(y)));
+		} else if (ix < 0x4096bbaa) {
+			/* x < 1455: scale to avoid overflow */
+			z = __ldexp_cexp(cpack(fabs(x), y), -1);
+			return (cpack(creal(z) * copysign(1, x), cimag(z)));
+		} else {
+			/* x >= 1455: the result always overflows */
+			h = huge * x;
+			return (cpack(h * cos(y), h * h * sin(y)));
+		}
 	}
 
 	/*
@@ -78,13 +95,13 @@ csinh(double complex z)
 		return (cpack(copysign(0, x * (y - y)), y - y));
 
 	/*
-	 * sinh(+-Inf +- I 0) = +-Inf + I (+-)(+-)0.
+	 * sinh(+-Inf +- I 0) = +-Inf + I +-0.
 	 *
 	 * sinh(NaN +- I 0)   = d(NaN) + I +-0.
 	 */
 	if ((iy | ly) == 0 && ix >= 0x7ff00000) {
 		if (((hx & 0xfffff) | lx) == 0)
-			return (cpack(x, copysign(0, x) * y));
+			return (cpack(x, y));
 		return (cpack(x, copysign(0, y)));
 	}
 

Modified: head/lib/msun/src/s_csinhf.c
==============================================================================
--- head/lib/msun/src/s_csinhf.c	Fri Oct 21 06:28:47 2011	(r226598)
+++ head/lib/msun/src/s_csinhf.c	Fri Oct 21 06:29:32 2011	(r226599)
@@ -36,10 +36,12 @@ __FBSDID("$FreeBSD$");
 
 #include "math_private.h"
 
+static const float huge = 0x1p127;
+
 float complex
 csinhf(float complex z)
 {
-	float x, y;
+	float x, y, h;
 	int32_t hx, hy, ix, iy;
 
 	x = crealf(z);
@@ -54,8 +56,23 @@ csinhf(float complex z)
 	if (ix < 0x7f800000 && iy < 0x7f800000) {
 		if (iy == 0)
 			return (cpackf(sinhf(x), y));
-		/* XXX We don't handle |x| > FLT_MAX ln(2) yet. */
-		return (cpackf(sinhf(x) * cosf(y), coshf(x) * sinf(y)));
+		if (ix < 0x41100000)	/* small x: normal case */
+			return (cpackf(sinhf(x) * cosf(y), coshf(x) * sinf(y)));
+
+		/* |x| >= 9, so cosh(x) ~= exp(|x|) */
+		if (ix < 0x42b17218) {
+			/* x < 88.7: expf(|x|) won't overflow */
+			h = expf(fabsf(x)) * 0.5f;
+			return (cpackf(copysignf(h, x) * cosf(y), h * sinf(y)));
+		} else if (ix < 0x4340b1e7) {
+			/* x < 192.7: scale to avoid overflow */
+			z = __ldexp_cexpf(cpackf(fabsf(x), y), -1);
+			return (cpackf(crealf(z) * copysignf(1, x), cimagf(z)));
+		} else {
+			/* x >= 192.7: the result always overflows */
+			h = huge * x;
+			return (cpackf(h * cosf(y), h * h * sinf(y)));
+		}
 	}
 
 	if (ix == 0 && iy >= 0x7f800000)
@@ -63,7 +80,7 @@ csinhf(float complex z)
 
 	if (iy == 0 && ix >= 0x7f800000) {
 		if ((hx & 0x7fffff) == 0)
-			return (cpackf(x, copysignf(0, x) * y));
+			return (cpackf(x, y));
 		return (cpackf(x, copysignf(0, y)));
 	}
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201110210629.p9L6TWQb009912>