xref: /freebsd/lib/msun/src/s_ccosh.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
13daee1d6SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 <complex.h>
423daee1d6SDavid Schultz #include <math.h>
433daee1d6SDavid Schultz 
443daee1d6SDavid Schultz #include "math_private.h"
453daee1d6SDavid Schultz 
46c6df46baSDavid Schultz static const double huge = 0x1p1023;
47c6df46baSDavid Schultz 
483daee1d6SDavid Schultz double complex
ccosh(double complex z)493daee1d6SDavid Schultz ccosh(double complex z)
503daee1d6SDavid Schultz {
51c6df46baSDavid Schultz 	double x, y, h;
523daee1d6SDavid Schultz 	int32_t hx, hy, ix, iy, lx, ly;
533daee1d6SDavid Schultz 
543daee1d6SDavid Schultz 	x = creal(z);
553daee1d6SDavid Schultz 	y = cimag(z);
563daee1d6SDavid Schultz 
573daee1d6SDavid Schultz 	EXTRACT_WORDS(hx, lx, x);
583daee1d6SDavid Schultz 	EXTRACT_WORDS(hy, ly, y);
593daee1d6SDavid Schultz 
603daee1d6SDavid Schultz 	ix = 0x7fffffff & hx;
613daee1d6SDavid Schultz 	iy = 0x7fffffff & hy;
623daee1d6SDavid Schultz 
633daee1d6SDavid Schultz 	/* Handle the nearly-non-exceptional cases where x and y are finite. */
643daee1d6SDavid Schultz 	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
653daee1d6SDavid Schultz 		if ((iy | ly) == 0)
662cec876aSEd Schouten 			return (CMPLX(cosh(x), x * y));
67db7548d0STijl Coosemans 		if (ix < 0x40360000)	/* |x| < 22: normal case */
682cec876aSEd Schouten 			return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)));
69c6df46baSDavid Schultz 
70c6df46baSDavid Schultz 		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
71c6df46baSDavid Schultz 		if (ix < 0x40862e42) {
72c6df46baSDavid Schultz 			/* x < 710: exp(|x|) won't overflow */
73c6df46baSDavid Schultz 			h = exp(fabs(x)) * 0.5;
742cec876aSEd Schouten 			return (CMPLX(h * cos(y), copysign(h, x) * sin(y)));
75c6df46baSDavid Schultz 		} else if (ix < 0x4096bbaa) {
76c6df46baSDavid Schultz 			/* x < 1455: scale to avoid overflow */
772cec876aSEd Schouten 			z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
782cec876aSEd Schouten 			return (CMPLX(creal(z), cimag(z) * copysign(1, x)));
79c6df46baSDavid Schultz 		} else {
80c6df46baSDavid Schultz 			/* x >= 1455: the result always overflows */
81c6df46baSDavid Schultz 			h = huge * x;
822cec876aSEd Schouten 			return (CMPLX(h * h * cos(y), h * sin(y)));
83c6df46baSDavid Schultz 		}
843daee1d6SDavid Schultz 	}
853daee1d6SDavid Schultz 
863daee1d6SDavid Schultz 	/*
87db7548d0STijl Coosemans 	 * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0.
88db7548d0STijl Coosemans 	 * The sign of 0 in the result is unspecified.  Choice = product
89db7548d0STijl Coosemans 	 * of the signs of the argument.  Raise the invalid floating-point
90db7548d0STijl Coosemans 	 * exception.
913daee1d6SDavid Schultz 	 *
92db7548d0STijl Coosemans 	 * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0.
93db7548d0STijl Coosemans 	 * The sign of 0 in the result is unspecified.  Choice = product
94db7548d0STijl Coosemans 	 * of the signs of the argument.
953daee1d6SDavid Schultz 	 */
96db7548d0STijl Coosemans 	if ((ix | lx) == 0)		/* && iy >= 0x7ff00000 */
97db7548d0STijl Coosemans 		return (CMPLX(y - y, x * copysign(0, y)));
983daee1d6SDavid Schultz 
993daee1d6SDavid Schultz 	/*
1003daee1d6SDavid Schultz 	 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
1013daee1d6SDavid Schultz 	 *
102db7548d0STijl Coosemans 	 * cosh(NaN +- I 0)   = d(NaN) + I (+-)(+-)0.
103db7548d0STijl Coosemans 	 * The sign of 0 in the result is unspecified.  Choice = product
104db7548d0STijl Coosemans 	 * of the signs of the argument.
1053daee1d6SDavid Schultz 	 */
106db7548d0STijl Coosemans 	if ((iy | ly) == 0)		/* && ix >= 0x7ff00000 */
1072cec876aSEd Schouten 		return (CMPLX(x * x, copysign(0, x) * y));
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 	 */
117db7548d0STijl Coosemans 	if (ix < 0x7ff00000)		/* && iy >= 0x7ff00000 */
1182cec876aSEd 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 	 */
129db7548d0STijl Coosemans 	if (ix == 0x7ff00000 && lx == 0) {
1303daee1d6SDavid Schultz 		if (iy >= 0x7ff00000)
131db7548d0STijl Coosemans 			return (CMPLX(INFINITY, x * (y - y)));
132db7548d0STijl Coosemans 		return (CMPLX(INFINITY * 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 	 */
1466f1b8a07SBruce Evans 	return (CMPLX(((long double)x * x) * (y - y),
1476f1b8a07SBruce Evans 	    ((long double)x + x) * (y - y)));
1483daee1d6SDavid Schultz }
1493daee1d6SDavid Schultz 
1503daee1d6SDavid Schultz double complex
ccos(double complex z)1513daee1d6SDavid Schultz ccos(double complex z)
1523daee1d6SDavid Schultz {
1533daee1d6SDavid Schultz 
1543daee1d6SDavid Schultz 	/* ccos(z) = ccosh(I * z) */
1552cec876aSEd Schouten 	return (ccosh(CMPLX(-cimag(z), creal(z))));
1563daee1d6SDavid Schultz }
157