blob: 0fd9206da349664c986683c4699357f0f111aaea [file] [log] [blame]
Elliott Hughesa0ee0782013-01-30 19:06:37 -08001/*-
Elliott Hughesc1e46b62023-07-19 14:06:31 -07002 * SPDX-License-Identifier: BSD-2-Clause
Elliott Hughes8da8ca42018-05-08 13:35:33 -07003 *
Elliott Hughesa0ee0782013-01-30 19:06:37 -08004 * 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.
Elliott Hughes8cff2f92015-08-28 20:21:43 -070037 * 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).
Elliott Hughesa0ee0782013-01-30 19:06:37 -080039 */
40
41#include <sys/cdefs.h>
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080042__FBSDID("$FreeBSD$");
Elliott Hughesa0ee0782013-01-30 19:06:37 -080043
44#include <complex.h>
45#include <math.h>
46
47#include "math_private.h"
48
49static const double huge = 0x1p1023;
50
51double complex
52ccosh(double complex z)
53{
54 double x, y, h;
55 int32_t hx, hy, ix, iy, lx, ly;
56
57 x = creal(z);
58 y = cimag(z);
59
60 EXTRACT_WORDS(hx, lx, x);
61 EXTRACT_WORDS(hy, ly, y);
62
63 ix = 0x7fffffff & hx;
64 iy = 0x7fffffff & hy;
65
66 /* Handle the nearly-non-exceptional cases where x and y are finite. */
67 if (ix < 0x7ff00000 && iy < 0x7ff00000) {
68 if ((iy | ly) == 0)
Elliott Hughes8cff2f92015-08-28 20:21:43 -070069 return (CMPLX(cosh(x), x * y));
70 if (ix < 0x40360000) /* |x| < 22: normal case */
71 return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080072
73 /* |x| >= 22, so cosh(x) ~= exp(|x|) */
74 if (ix < 0x40862e42) {
75 /* x < 710: exp(|x|) won't overflow */
76 h = exp(fabs(x)) * 0.5;
Elliott Hughes8cff2f92015-08-28 20:21:43 -070077 return (CMPLX(h * cos(y), copysign(h, x) * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080078 } else if (ix < 0x4096bbaa) {
79 /* x < 1455: scale to avoid overflow */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070080 z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
81 return (CMPLX(creal(z), cimag(z) * copysign(1, x)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080082 } else {
83 /* x >= 1455: the result always overflows */
84 h = huge * x;
Elliott Hughes8cff2f92015-08-28 20:21:43 -070085 return (CMPLX(h * h * cos(y), h * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080086 }
87 }
88
89 /*
Elliott Hughes8cff2f92015-08-28 20:21:43 -070090 * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0.
91 * The sign of 0 in the result is unspecified. Choice = product
92 * of the signs of the argument. Raise the invalid floating-point
93 * exception.
Elliott Hughesa0ee0782013-01-30 19:06:37 -080094 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070095 * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0.
96 * The sign of 0 in the result is unspecified. Choice = product
97 * of the signs of the argument.
Elliott Hughesa0ee0782013-01-30 19:06:37 -080098 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070099 if ((ix | lx) == 0) /* && iy >= 0x7ff00000 */
100 return (CMPLX(y - y, x * copysign(0, y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800101
102 /*
103 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
104 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700105 * cosh(NaN +- I 0) = d(NaN) + I (+-)(+-)0.
106 * The sign of 0 in the result is unspecified. Choice = product
107 * of the signs of the argument.
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800108 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700109 if ((iy | ly) == 0) /* && ix >= 0x7ff00000 */
110 return (CMPLX(x * x, copysign(0, x) * y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800111
112 /*
113 * cosh(x +- I Inf) = dNaN + I dNaN.
114 * Raise the invalid floating-point exception for finite nonzero x.
115 *
116 * cosh(x + I NaN) = d(NaN) + I d(NaN).
117 * Optionally raises the invalid floating-point exception for finite
118 * nonzero x. Choice = don't raise (except for signaling NaNs).
119 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700120 if (ix < 0x7ff00000) /* && iy >= 0x7ff00000 */
121 return (CMPLX(y - y, x * (y - y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800122
123 /*
124 * cosh(+-Inf + I NaN) = +Inf + I d(NaN).
125 *
126 * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
127 * The sign of Inf in the result is unspecified. Choice = always +.
128 * Raise the invalid floating-point exception.
129 *
130 * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y)
131 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700132 if (ix == 0x7ff00000 && lx == 0) {
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800133 if (iy >= 0x7ff00000)
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700134 return (CMPLX(INFINITY, x * (y - y)));
135 return (CMPLX(INFINITY * cos(y), x * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800136 }
137
138 /*
139 * cosh(NaN + I NaN) = d(NaN) + I d(NaN).
140 *
141 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
142 * Optionally raises the invalid floating-point exception.
143 * Choice = raise.
144 *
145 * cosh(NaN + I y) = d(NaN) + I d(NaN).
146 * Optionally raises the invalid floating-point exception for finite
147 * nonzero y. Choice = don't raise (except for signaling NaNs).
148 */
Elliott Hughesab528072018-07-24 00:01:52 +0000149 return (CMPLX(((long double)x * x) * (y - y),
150 ((long double)x + x) * (y - y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800151}
152
153double complex
154ccos(double complex z)
155{
156
157 /* ccos(z) = ccosh(I * z) */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700158 return (ccosh(CMPLX(-cimag(z), creal(z))));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800159}