xref: /freebsd/lib/msun/src/s_ccosh.c (revision 2cec876a59a7c5396e3df7e21e82091cd461a94a)
13daee1d6SDavid Schultz /*-
23daee1d6SDavid Schultz  * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
33daee1d6SDavid Schultz  * All rights reserved.
43daee1d6SDavid Schultz  *
53daee1d6SDavid Schultz  * Redistribution and use in source and binary forms, with or without
63daee1d6SDavid Schultz  * modification, are permitted provided that the following conditions
73daee1d6SDavid Schultz  * are met:
83daee1d6SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
93daee1d6SDavid Schultz  *    notice unmodified, this list of conditions, and the following
103daee1d6SDavid Schultz  *    disclaimer.
113daee1d6SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
123daee1d6SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
133daee1d6SDavid Schultz  *    documentation and/or other materials provided with the distribution.
143daee1d6SDavid Schultz  *
153daee1d6SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
163daee1d6SDavid Schultz  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
173daee1d6SDavid Schultz  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
183daee1d6SDavid Schultz  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
193daee1d6SDavid Schultz  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
203daee1d6SDavid Schultz  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
213daee1d6SDavid Schultz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223daee1d6SDavid Schultz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
233daee1d6SDavid Schultz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
243daee1d6SDavid Schultz  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
253daee1d6SDavid Schultz  */
263daee1d6SDavid Schultz 
273daee1d6SDavid Schultz /*
283daee1d6SDavid Schultz  * Hyperbolic cosine of a complex argument z = x + i y.
293daee1d6SDavid Schultz  *
303daee1d6SDavid Schultz  * cosh(z) = cosh(x+iy)
313daee1d6SDavid Schultz  *         = cosh(x) cos(y) + i sinh(x) sin(y).
323daee1d6SDavid Schultz  *
333daee1d6SDavid Schultz  * Exceptional values are noted in the comments within the source code.
343daee1d6SDavid Schultz  * These values and the return value were taken from n1124.pdf.
353daee1d6SDavid Schultz  */
363daee1d6SDavid Schultz 
373daee1d6SDavid Schultz #include <sys/cdefs.h>
383daee1d6SDavid Schultz __FBSDID("$FreeBSD$");
393daee1d6SDavid Schultz 
403daee1d6SDavid Schultz #include <complex.h>
413daee1d6SDavid Schultz #include <math.h>
423daee1d6SDavid Schultz 
433daee1d6SDavid Schultz #include "math_private.h"
443daee1d6SDavid Schultz 
45c6df46baSDavid Schultz static const double huge = 0x1p1023;
46c6df46baSDavid Schultz 
473daee1d6SDavid Schultz double complex
483daee1d6SDavid Schultz ccosh(double complex z)
493daee1d6SDavid Schultz {
50c6df46baSDavid Schultz 	double x, y, h;
513daee1d6SDavid Schultz 	int32_t hx, hy, ix, iy, lx, ly;
523daee1d6SDavid Schultz 
533daee1d6SDavid Schultz 	x = creal(z);
543daee1d6SDavid Schultz 	y = cimag(z);
553daee1d6SDavid Schultz 
563daee1d6SDavid Schultz 	EXTRACT_WORDS(hx, lx, x);
573daee1d6SDavid Schultz 	EXTRACT_WORDS(hy, ly, y);
583daee1d6SDavid Schultz 
593daee1d6SDavid Schultz 	ix = 0x7fffffff & hx;
603daee1d6SDavid Schultz 	iy = 0x7fffffff & hy;
613daee1d6SDavid Schultz 
623daee1d6SDavid Schultz 	/* Handle the nearly-non-exceptional cases where x and y are finite. */
633daee1d6SDavid Schultz 	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
643daee1d6SDavid Schultz 		if ((iy | ly) == 0)
65*2cec876aSEd Schouten 			return (CMPLX(cosh(x), x * y));
66c6df46baSDavid Schultz 		if (ix < 0x40360000)	/* small x: normal case */
67*2cec876aSEd Schouten 			return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)));
68c6df46baSDavid Schultz 
69c6df46baSDavid Schultz 		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
70c6df46baSDavid Schultz 		if (ix < 0x40862e42) {
71c6df46baSDavid Schultz 			/* x < 710: exp(|x|) won't overflow */
72c6df46baSDavid Schultz 			h = exp(fabs(x)) * 0.5;
73*2cec876aSEd Schouten 			return (CMPLX(h * cos(y), copysign(h, x) * sin(y)));
74c6df46baSDavid Schultz 		} else if (ix < 0x4096bbaa) {
75c6df46baSDavid Schultz 			/* x < 1455: scale to avoid overflow */
76*2cec876aSEd Schouten 			z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
77*2cec876aSEd Schouten 			return (CMPLX(creal(z), cimag(z) * copysign(1, x)));
78c6df46baSDavid Schultz 		} else {
79c6df46baSDavid Schultz 			/* x >= 1455: the result always overflows */
80c6df46baSDavid Schultz 			h = huge * x;
81*2cec876aSEd Schouten 			return (CMPLX(h * h * cos(y), h * sin(y)));
82c6df46baSDavid Schultz 		}
833daee1d6SDavid Schultz 	}
843daee1d6SDavid Schultz 
853daee1d6SDavid Schultz 	/*
863daee1d6SDavid Schultz 	 * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0.
873daee1d6SDavid Schultz 	 * The sign of 0 in the result is unspecified.  Choice = normally
883daee1d6SDavid Schultz 	 * the same as dNaN.  Raise the invalid floating-point exception.
893daee1d6SDavid Schultz 	 *
903daee1d6SDavid Schultz 	 * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0.
913daee1d6SDavid Schultz 	 * The sign of 0 in the result is unspecified.  Choice = normally
923daee1d6SDavid Schultz 	 * the same as d(NaN).
933daee1d6SDavid Schultz 	 */
943daee1d6SDavid Schultz 	if ((ix | lx) == 0 && iy >= 0x7ff00000)
95*2cec876aSEd Schouten 		return (CMPLX(y - y, copysign(0, x * (y - y))));
963daee1d6SDavid Schultz 
973daee1d6SDavid Schultz 	/*
983daee1d6SDavid Schultz 	 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
993daee1d6SDavid Schultz 	 *
1003daee1d6SDavid Schultz 	 * cosh(NaN +- I 0)   = d(NaN) + I sign(d(NaN, +-0))0.
1013daee1d6SDavid Schultz 	 * The sign of 0 in the result is unspecified.
1023daee1d6SDavid Schultz 	 */
1033daee1d6SDavid Schultz 	if ((iy | ly) == 0 && ix >= 0x7ff00000) {
1043daee1d6SDavid Schultz 		if (((hx & 0xfffff) | lx) == 0)
105*2cec876aSEd Schouten 			return (CMPLX(x * x, copysign(0, x) * y));
106*2cec876aSEd Schouten 		return (CMPLX(x * x, copysign(0, (x + x) * y)));
1073daee1d6SDavid Schultz 	}
1083daee1d6SDavid Schultz 
1093daee1d6SDavid Schultz 	/*
1103daee1d6SDavid Schultz 	 * cosh(x +- I Inf) = dNaN + I dNaN.
1113daee1d6SDavid Schultz 	 * Raise the invalid floating-point exception for finite nonzero x.
1123daee1d6SDavid Schultz 	 *
1133daee1d6SDavid Schultz 	 * cosh(x + I NaN) = d(NaN) + I d(NaN).
1143daee1d6SDavid Schultz 	 * Optionally raises the invalid floating-point exception for finite
1153daee1d6SDavid Schultz 	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
1163daee1d6SDavid Schultz 	 */
1173daee1d6SDavid Schultz 	if (ix < 0x7ff00000 && iy >= 0x7ff00000)
118*2cec876aSEd Schouten 		return (CMPLX(y - y, x * (y - y)));
1193daee1d6SDavid Schultz 
1203daee1d6SDavid Schultz 	/*
1213daee1d6SDavid Schultz 	 * cosh(+-Inf + I NaN)  = +Inf + I d(NaN).
1223daee1d6SDavid Schultz 	 *
1233daee1d6SDavid Schultz 	 * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
1243daee1d6SDavid Schultz 	 * The sign of Inf in the result is unspecified.  Choice = always +.
1253daee1d6SDavid Schultz 	 * Raise the invalid floating-point exception.
1263daee1d6SDavid Schultz 	 *
1273daee1d6SDavid Schultz 	 * cosh(+-Inf + I y)   = +Inf cos(y) +- I Inf sin(y)
1283daee1d6SDavid Schultz 	 */
1293daee1d6SDavid Schultz 	if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
1303daee1d6SDavid Schultz 		if (iy >= 0x7ff00000)
131*2cec876aSEd Schouten 			return (CMPLX(x * x, x * (y - y)));
132*2cec876aSEd Schouten 		return (CMPLX((x * x) * cos(y), x * sin(y)));
1333daee1d6SDavid Schultz 	}
1343daee1d6SDavid Schultz 
1353daee1d6SDavid Schultz 	/*
1363daee1d6SDavid Schultz 	 * cosh(NaN + I NaN)  = d(NaN) + I d(NaN).
1373daee1d6SDavid Schultz 	 *
1383daee1d6SDavid Schultz 	 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
1393daee1d6SDavid Schultz 	 * Optionally raises the invalid floating-point exception.
1403daee1d6SDavid Schultz 	 * Choice = raise.
1413daee1d6SDavid Schultz 	 *
1423daee1d6SDavid Schultz 	 * cosh(NaN + I y)    = d(NaN) + I d(NaN).
1433daee1d6SDavid Schultz 	 * Optionally raises the invalid floating-point exception for finite
1443daee1d6SDavid Schultz 	 * nonzero y.  Choice = don't raise (except for signaling NaNs).
1453daee1d6SDavid Schultz 	 */
146*2cec876aSEd Schouten 	return (CMPLX((x * x) * (y - y), (x + x) * (y - y)));
1473daee1d6SDavid Schultz }
1483daee1d6SDavid Schultz 
1493daee1d6SDavid Schultz double complex
1503daee1d6SDavid Schultz ccos(double complex z)
1513daee1d6SDavid Schultz {
1523daee1d6SDavid Schultz 
1533daee1d6SDavid Schultz 	/* ccos(z) = ccosh(I * z) */
154*2cec876aSEd Schouten 	return (ccosh(CMPLX(-cimag(z), creal(z))));
1553daee1d6SDavid Schultz }
156