1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005-2025 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 <complex.h>
42 #include <math.h>
43
44 #include "math_private.h"
45
46 static const double huge = 0x1p1023;
47
48 double complex
ccosh(double complex z)49 ccosh(double complex z)
50 {
51 double c, h, s, x, y;
52 int32_t hx, hy, ix, iy, lx, ly;
53
54 x = creal(z);
55 y = cimag(z);
56
57 EXTRACT_WORDS(hx, lx, x);
58 EXTRACT_WORDS(hy, ly, y);
59
60 ix = 0x7fffffff & hx;
61 iy = 0x7fffffff & hy;
62
63 /* Handle the nearly-non-exceptional cases where x and y are finite. */
64 if (ix < 0x7ff00000 && iy < 0x7ff00000) {
65 if ((iy | ly) == 0)
66 return (CMPLX(cosh(x), x * y));
67
68 sincos(y, &s, &c);
69 if (ix < 0x40360000) /* |x| < 22: normal case */
70 return (CMPLX(cosh(x) * c, sinh(x) * s));
71
72 /* |x| >= 22, so cosh(x) ~= exp(|x|) */
73 if (ix < 0x40862e42) {
74 /* x < 710: exp(|x|) won't overflow */
75 h = exp(fabs(x)) / 2;
76 return (CMPLX(h * c, copysign(h, x) * s));
77 } else if (ix < 0x4096bbaa) {
78 /* x < 1455: scale to avoid overflow */
79 z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
80 return (CMPLX(creal(z), cimag(z) * copysign(1, x)));
81 } else {
82 /* x >= 1455: the result always overflows */
83 h = huge * x;
84 return (CMPLX(h * h * c, h * s));
85 }
86 }
87
88 /*
89 * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0.
90 * The sign of 0 in the result is unspecified. Choice = product
91 * of the signs of the argument. Raise the invalid floating-point
92 * exception.
93 *
94 * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0.
95 * The sign of 0 in the result is unspecified. Choice = product
96 * of the signs of the argument.
97 */
98 if ((ix | lx) == 0) /* && iy >= 0x7ff00000 */
99 return (CMPLX(y - y, x * copysign(0, y)));
100
101 /*
102 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
103 *
104 * cosh(NaN +- I 0) = d(NaN) + I (+-)(+-)0.
105 * The sign of 0 in the result is unspecified. Choice = product
106 * of the signs of the argument.
107 */
108 if ((iy | ly) == 0) /* && ix >= 0x7ff00000 */
109 return (CMPLX(x * x, copysign(0, x) * y));
110
111 /*
112 * cosh(x +- I Inf) = dNaN + I dNaN.
113 * Raise the invalid floating-point exception for finite nonzero x.
114 *
115 * cosh(x + I NaN) = d(NaN) + I d(NaN).
116 * Optionally raises the invalid floating-point exception for finite
117 * nonzero x. Choice = don't raise (except for signaling NaNs).
118 */
119 if (ix < 0x7ff00000) /* && iy >= 0x7ff00000 */
120 return (CMPLX(y - y, x * (y - y)));
121
122 /*
123 * cosh(+-Inf + I NaN) = +Inf + I d(NaN).
124 *
125 * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
126 * The sign of Inf in the result is unspecified. Choice = always +.
127 * Raise the invalid floating-point exception.
128 *
129 * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y)
130 */
131 if (ix == 0x7ff00000 && lx == 0) {
132 if (iy >= 0x7ff00000)
133 return (CMPLX(INFINITY, x * (y - y)));
134
135 sincos(y, &s, &c);
136 return (CMPLX(INFINITY * c, x * s));
137 }
138
139 /*
140 * cosh(NaN + I NaN) = d(NaN) + I d(NaN).
141 *
142 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
143 * Optionally raises the invalid floating-point exception.
144 * Choice = raise.
145 *
146 * cosh(NaN + I y) = d(NaN) + I d(NaN).
147 * Optionally raises the invalid floating-point exception for finite
148 * nonzero y. Choice = don't raise (except for signaling NaNs).
149 */
150 return (CMPLX(((long double)x * x) * (y - y),
151 ((long double)x + x) * (y - y)));
152 }
153
154 double complex
ccos(double complex z)155 ccos(double complex z)
156 {
157
158 /* ccos(z) = ccosh(I * z) */
159 return (ccosh(CMPLX(-cimag(z), creal(z))));
160 }
161
162 #if (LDBL_MANT_DIG == 53)
163 __weak_reference(ccosh, ccoshl);
164 __weak_reference(ccos, ccosl);
165 #endif
166