xref: /freebsd/lib/msun/src/s_ccosh.c (revision 6f1b8a07926a4a00060628d9ed70412b4842ed69)
13daee1d6SDavid Schultz /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
35e53a4f9SPedro F. Giffuni  *
43daee1d6SDavid Schultz  * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
53daee1d6SDavid Schultz  * All rights reserved.
63daee1d6SDavid Schultz  *
73daee1d6SDavid Schultz  * Redistribution and use in source and binary forms, with or without
83daee1d6SDavid Schultz  * modification, are permitted provided that the following conditions
93daee1d6SDavid Schultz  * are met:
103daee1d6SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
113daee1d6SDavid Schultz  *    notice unmodified, this list of conditions, and the following
123daee1d6SDavid Schultz  *    disclaimer.
133daee1d6SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
143daee1d6SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
153daee1d6SDavid Schultz  *    documentation and/or other materials provided with the distribution.
163daee1d6SDavid Schultz  *
173daee1d6SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183daee1d6SDavid Schultz  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193daee1d6SDavid Schultz  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203daee1d6SDavid Schultz  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213daee1d6SDavid Schultz  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223daee1d6SDavid Schultz  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233daee1d6SDavid Schultz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243daee1d6SDavid Schultz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253daee1d6SDavid Schultz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263daee1d6SDavid Schultz  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273daee1d6SDavid Schultz  */
283daee1d6SDavid Schultz 
293daee1d6SDavid Schultz /*
303daee1d6SDavid Schultz  * Hyperbolic cosine of a complex argument z = x + i y.
313daee1d6SDavid Schultz  *
323daee1d6SDavid Schultz  * cosh(z) = cosh(x+iy)
333daee1d6SDavid Schultz  *         = cosh(x) cos(y) + i sinh(x) sin(y).
343daee1d6SDavid Schultz  *
353daee1d6SDavid Schultz  * Exceptional values are noted in the comments within the source code.
363daee1d6SDavid Schultz  * These values and the return value were taken from n1124.pdf.
37db7548d0STijl Coosemans  * The sign of the result for some exceptional values is unspecified but
38db7548d0STijl Coosemans  * must satisfy both cosh(conj(z)) == conj(cosh(z)) and cosh(-z) == cosh(z).
393daee1d6SDavid Schultz  */
403daee1d6SDavid Schultz 
413daee1d6SDavid Schultz #include <sys/cdefs.h>
423daee1d6SDavid Schultz __FBSDID("$FreeBSD$");
433daee1d6SDavid Schultz 
443daee1d6SDavid Schultz #include <complex.h>
453daee1d6SDavid Schultz #include <math.h>
463daee1d6SDavid Schultz 
473daee1d6SDavid Schultz #include "math_private.h"
483daee1d6SDavid Schultz 
49c6df46baSDavid Schultz static const double huge = 0x1p1023;
50c6df46baSDavid Schultz 
513daee1d6SDavid Schultz double complex
523daee1d6SDavid Schultz ccosh(double complex z)
533daee1d6SDavid Schultz {
54c6df46baSDavid Schultz 	double x, y, h;
553daee1d6SDavid Schultz 	int32_t hx, hy, ix, iy, lx, ly;
563daee1d6SDavid Schultz 
573daee1d6SDavid Schultz 	x = creal(z);
583daee1d6SDavid Schultz 	y = cimag(z);
593daee1d6SDavid Schultz 
603daee1d6SDavid Schultz 	EXTRACT_WORDS(hx, lx, x);
613daee1d6SDavid Schultz 	EXTRACT_WORDS(hy, ly, y);
623daee1d6SDavid Schultz 
633daee1d6SDavid Schultz 	ix = 0x7fffffff & hx;
643daee1d6SDavid Schultz 	iy = 0x7fffffff & hy;
653daee1d6SDavid Schultz 
663daee1d6SDavid Schultz 	/* Handle the nearly-non-exceptional cases where x and y are finite. */
673daee1d6SDavid Schultz 	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
683daee1d6SDavid Schultz 		if ((iy | ly) == 0)
692cec876aSEd Schouten 			return (CMPLX(cosh(x), x * y));
70db7548d0STijl Coosemans 		if (ix < 0x40360000)	/* |x| < 22: normal case */
712cec876aSEd Schouten 			return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)));
72c6df46baSDavid Schultz 
73c6df46baSDavid Schultz 		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
74c6df46baSDavid Schultz 		if (ix < 0x40862e42) {
75c6df46baSDavid Schultz 			/* x < 710: exp(|x|) won't overflow */
76c6df46baSDavid Schultz 			h = exp(fabs(x)) * 0.5;
772cec876aSEd Schouten 			return (CMPLX(h * cos(y), copysign(h, x) * sin(y)));
78c6df46baSDavid Schultz 		} else if (ix < 0x4096bbaa) {
79c6df46baSDavid Schultz 			/* x < 1455: scale to avoid overflow */
802cec876aSEd Schouten 			z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
812cec876aSEd Schouten 			return (CMPLX(creal(z), cimag(z) * copysign(1, x)));
82c6df46baSDavid Schultz 		} else {
83c6df46baSDavid Schultz 			/* x >= 1455: the result always overflows */
84c6df46baSDavid Schultz 			h = huge * x;
852cec876aSEd Schouten 			return (CMPLX(h * h * cos(y), h * sin(y)));
86c6df46baSDavid Schultz 		}
873daee1d6SDavid Schultz 	}
883daee1d6SDavid Schultz 
893daee1d6SDavid Schultz 	/*
90db7548d0STijl Coosemans 	 * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0.
91db7548d0STijl Coosemans 	 * The sign of 0 in the result is unspecified.  Choice = product
92db7548d0STijl Coosemans 	 * of the signs of the argument.  Raise the invalid floating-point
93db7548d0STijl Coosemans 	 * exception.
943daee1d6SDavid Schultz 	 *
95db7548d0STijl Coosemans 	 * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0.
96db7548d0STijl Coosemans 	 * The sign of 0 in the result is unspecified.  Choice = product
97db7548d0STijl Coosemans 	 * of the signs of the argument.
983daee1d6SDavid Schultz 	 */
99db7548d0STijl Coosemans 	if ((ix | lx) == 0)		/* && iy >= 0x7ff00000 */
100db7548d0STijl Coosemans 		return (CMPLX(y - y, x * copysign(0, y)));
1013daee1d6SDavid Schultz 
1023daee1d6SDavid Schultz 	/*
1033daee1d6SDavid Schultz 	 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
1043daee1d6SDavid Schultz 	 *
105db7548d0STijl Coosemans 	 * cosh(NaN +- I 0)   = d(NaN) + I (+-)(+-)0.
106db7548d0STijl Coosemans 	 * The sign of 0 in the result is unspecified.  Choice = product
107db7548d0STijl Coosemans 	 * of the signs of the argument.
1083daee1d6SDavid Schultz 	 */
109db7548d0STijl Coosemans 	if ((iy | ly) == 0)		/* && ix >= 0x7ff00000 */
1102cec876aSEd Schouten 		return (CMPLX(x * x, copysign(0, x) * y));
1113daee1d6SDavid Schultz 
1123daee1d6SDavid Schultz 	/*
1133daee1d6SDavid Schultz 	 * cosh(x +- I Inf) = dNaN + I dNaN.
1143daee1d6SDavid Schultz 	 * Raise the invalid floating-point exception for finite nonzero x.
1153daee1d6SDavid Schultz 	 *
1163daee1d6SDavid Schultz 	 * cosh(x + I NaN) = d(NaN) + I d(NaN).
1173daee1d6SDavid Schultz 	 * Optionally raises the invalid floating-point exception for finite
1183daee1d6SDavid Schultz 	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
1193daee1d6SDavid Schultz 	 */
120db7548d0STijl Coosemans 	if (ix < 0x7ff00000)		/* && iy >= 0x7ff00000 */
1212cec876aSEd Schouten 		return (CMPLX(y - y, x * (y - y)));
1223daee1d6SDavid Schultz 
1233daee1d6SDavid Schultz 	/*
1243daee1d6SDavid Schultz 	 * cosh(+-Inf + I NaN)  = +Inf + I d(NaN).
1253daee1d6SDavid Schultz 	 *
1263daee1d6SDavid Schultz 	 * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
1273daee1d6SDavid Schultz 	 * The sign of Inf in the result is unspecified.  Choice = always +.
1283daee1d6SDavid Schultz 	 * Raise the invalid floating-point exception.
1293daee1d6SDavid Schultz 	 *
1303daee1d6SDavid Schultz 	 * cosh(+-Inf + I y)   = +Inf cos(y) +- I Inf sin(y)
1313daee1d6SDavid Schultz 	 */
132db7548d0STijl Coosemans 	if (ix == 0x7ff00000 && lx == 0) {
1333daee1d6SDavid Schultz 		if (iy >= 0x7ff00000)
134db7548d0STijl Coosemans 			return (CMPLX(INFINITY, x * (y - y)));
135db7548d0STijl Coosemans 		return (CMPLX(INFINITY * cos(y), x * sin(y)));
1363daee1d6SDavid Schultz 	}
1373daee1d6SDavid Schultz 
1383daee1d6SDavid Schultz 	/*
1393daee1d6SDavid Schultz 	 * cosh(NaN + I NaN)  = d(NaN) + I d(NaN).
1403daee1d6SDavid Schultz 	 *
1413daee1d6SDavid Schultz 	 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
1423daee1d6SDavid Schultz 	 * Optionally raises the invalid floating-point exception.
1433daee1d6SDavid Schultz 	 * Choice = raise.
1443daee1d6SDavid Schultz 	 *
1453daee1d6SDavid Schultz 	 * cosh(NaN + I y)    = d(NaN) + I d(NaN).
1463daee1d6SDavid Schultz 	 * Optionally raises the invalid floating-point exception for finite
1473daee1d6SDavid Schultz 	 * nonzero y.  Choice = don't raise (except for signaling NaNs).
1483daee1d6SDavid Schultz 	 */
149*6f1b8a07SBruce Evans 	return (CMPLX(((long double)x * x) * (y - y),
150*6f1b8a07SBruce Evans 	    ((long double)x + x) * (y - y)));
1513daee1d6SDavid Schultz }
1523daee1d6SDavid Schultz 
1533daee1d6SDavid Schultz double complex
1543daee1d6SDavid Schultz ccos(double complex z)
1553daee1d6SDavid Schultz {
1563daee1d6SDavid Schultz 
1573daee1d6SDavid Schultz 	/* ccos(z) = ccosh(I * z) */
1582cec876aSEd Schouten 	return (ccosh(CMPLX(-cimag(z), creal(z))));
1593daee1d6SDavid Schultz }
160