xref: /freebsd/lib/msun/src/s_ccosh.c (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Hyperbolic cosine of a complex argument z = x + i y.
31  *
32  * cosh(z) = cosh(x+iy)
33  *         = cosh(x) cos(y) + i sinh(x) sin(y).
34  *
35  * Exceptional values are noted in the comments within the source code.
36  * These values and the return value were taken from n1124.pdf.
37  * The sign of the result for some exceptional values is unspecified but
38  * must satisfy both cosh(conj(z)) == conj(cosh(z)) and cosh(-z) == cosh(z).
39  */
40 
41 #include <sys/cdefs.h>
42 #include <complex.h>
43 #include <math.h>
44 
45 #include "math_private.h"
46 
47 static const double huge = 0x1p1023;
48 
49 double complex
50 ccosh(double complex z)
51 {
52 	double x, y, h;
53 	int32_t hx, hy, ix, iy, lx, ly;
54 
55 	x = creal(z);
56 	y = cimag(z);
57 
58 	EXTRACT_WORDS(hx, lx, x);
59 	EXTRACT_WORDS(hy, ly, y);
60 
61 	ix = 0x7fffffff & hx;
62 	iy = 0x7fffffff & hy;
63 
64 	/* Handle the nearly-non-exceptional cases where x and y are finite. */
65 	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
66 		if ((iy | ly) == 0)
67 			return (CMPLX(cosh(x), x * y));
68 		if (ix < 0x40360000)	/* |x| < 22: normal case */
69 			return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)));
70 
71 		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
72 		if (ix < 0x40862e42) {
73 			/* x < 710: exp(|x|) won't overflow */
74 			h = exp(fabs(x)) * 0.5;
75 			return (CMPLX(h * cos(y), copysign(h, x) * sin(y)));
76 		} else if (ix < 0x4096bbaa) {
77 			/* x < 1455: scale to avoid overflow */
78 			z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
79 			return (CMPLX(creal(z), cimag(z) * copysign(1, x)));
80 		} else {
81 			/* x >= 1455: the result always overflows */
82 			h = huge * x;
83 			return (CMPLX(h * h * cos(y), h * sin(y)));
84 		}
85 	}
86 
87 	/*
88 	 * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0.
89 	 * The sign of 0 in the result is unspecified.  Choice = product
90 	 * of the signs of the argument.  Raise the invalid floating-point
91 	 * exception.
92 	 *
93 	 * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0.
94 	 * The sign of 0 in the result is unspecified.  Choice = product
95 	 * of the signs of the argument.
96 	 */
97 	if ((ix | lx) == 0)		/* && iy >= 0x7ff00000 */
98 		return (CMPLX(y - y, x * copysign(0, y)));
99 
100 	/*
101 	 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
102 	 *
103 	 * cosh(NaN +- I 0)   = d(NaN) + I (+-)(+-)0.
104 	 * The sign of 0 in the result is unspecified.  Choice = product
105 	 * of the signs of the argument.
106 	 */
107 	if ((iy | ly) == 0)		/* && ix >= 0x7ff00000 */
108 		return (CMPLX(x * x, copysign(0, x) * y));
109 
110 	/*
111 	 * cosh(x +- I Inf) = dNaN + I dNaN.
112 	 * Raise the invalid floating-point exception for finite nonzero x.
113 	 *
114 	 * cosh(x + I NaN) = d(NaN) + I d(NaN).
115 	 * Optionally raises the invalid floating-point exception for finite
116 	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
117 	 */
118 	if (ix < 0x7ff00000)		/* && iy >= 0x7ff00000 */
119 		return (CMPLX(y - y, x * (y - y)));
120 
121 	/*
122 	 * cosh(+-Inf + I NaN)  = +Inf + I d(NaN).
123 	 *
124 	 * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
125 	 * The sign of Inf in the result is unspecified.  Choice = always +.
126 	 * Raise the invalid floating-point exception.
127 	 *
128 	 * cosh(+-Inf + I y)   = +Inf cos(y) +- I Inf sin(y)
129 	 */
130 	if (ix == 0x7ff00000 && lx == 0) {
131 		if (iy >= 0x7ff00000)
132 			return (CMPLX(INFINITY, x * (y - y)));
133 		return (CMPLX(INFINITY * cos(y), x * sin(y)));
134 	}
135 
136 	/*
137 	 * cosh(NaN + I NaN)  = d(NaN) + I d(NaN).
138 	 *
139 	 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
140 	 * Optionally raises the invalid floating-point exception.
141 	 * Choice = raise.
142 	 *
143 	 * cosh(NaN + I y)    = d(NaN) + I d(NaN).
144 	 * Optionally raises the invalid floating-point exception for finite
145 	 * nonzero y.  Choice = don't raise (except for signaling NaNs).
146 	 */
147 	return (CMPLX(((long double)x * x) * (y - y),
148 	    ((long double)x + x) * (y - y)));
149 }
150 
151 double complex
152 ccos(double complex z)
153 {
154 
155 	/* ccos(z) = ccosh(I * z) */
156 	return (ccosh(CMPLX(-cimag(z), creal(z))));
157 }
158