1226458Sdas/*-
2226458Sdas * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
3226458Sdas * All rights reserved.
4226458Sdas *
5226458Sdas * Redistribution and use in source and binary forms, with or without
6226458Sdas * modification, are permitted provided that the following conditions
7226458Sdas * are met:
8226458Sdas * 1. Redistributions of source code must retain the above copyright
9226458Sdas *    notice unmodified, this list of conditions, and the following
10226458Sdas *    disclaimer.
11226458Sdas * 2. Redistributions in binary form must reproduce the above copyright
12226458Sdas *    notice, this list of conditions and the following disclaimer in the
13226458Sdas *    documentation and/or other materials provided with the distribution.
14226458Sdas *
15226458Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16226458Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17226458Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18226458Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19226458Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20226458Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21226458Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22226458Sdas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23226458Sdas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24226458Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25226458Sdas */
26226458Sdas
27226458Sdas/*
28226458Sdas * Hyperbolic cosine of a complex argument z = x + i y.
29226458Sdas *
30226458Sdas * cosh(z) = cosh(x+iy)
31226458Sdas *         = cosh(x) cos(y) + i sinh(x) sin(y).
32226458Sdas *
33226458Sdas * Exceptional values are noted in the comments within the source code.
34226458Sdas * These values and the return value were taken from n1124.pdf.
35226458Sdas */
36226458Sdas
37226458Sdas#include <sys/cdefs.h>
38226458Sdas__FBSDID("$FreeBSD$");
39226458Sdas
40226458Sdas#include <complex.h>
41226458Sdas#include <math.h>
42226458Sdas
43226458Sdas#include "math_private.h"
44226458Sdas
45226599Sdasstatic const double huge = 0x1p1023;
46226599Sdas
47226458Sdasdouble complex
48226458Sdasccosh(double complex z)
49226458Sdas{
50226599Sdas	double x, y, h;
51226458Sdas	int32_t hx, hy, ix, iy, lx, ly;
52226458Sdas
53226458Sdas	x = creal(z);
54226458Sdas	y = cimag(z);
55226458Sdas
56226458Sdas	EXTRACT_WORDS(hx, lx, x);
57226458Sdas	EXTRACT_WORDS(hy, ly, y);
58226458Sdas
59226458Sdas	ix = 0x7fffffff & hx;
60226458Sdas	iy = 0x7fffffff & hy;
61226458Sdas
62226458Sdas	/* Handle the nearly-non-exceptional cases where x and y are finite. */
63226458Sdas	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
64226458Sdas		if ((iy | ly) == 0)
65226458Sdas			return (cpack(cosh(x), x * y));
66226599Sdas		if (ix < 0x40360000)	/* small x: normal case */
67226599Sdas			return (cpack(cosh(x) * cos(y), sinh(x) * sin(y)));
68226599Sdas
69226599Sdas		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
70226599Sdas		if (ix < 0x40862e42) {
71226599Sdas			/* x < 710: exp(|x|) won't overflow */
72226599Sdas			h = exp(fabs(x)) * 0.5;
73226599Sdas			return (cpack(h * cos(y), copysign(h, x) * sin(y)));
74226599Sdas		} else if (ix < 0x4096bbaa) {
75226599Sdas			/* x < 1455: scale to avoid overflow */
76226599Sdas			z = __ldexp_cexp(cpack(fabs(x), y), -1);
77226599Sdas			return (cpack(creal(z), cimag(z) * copysign(1, x)));
78226599Sdas		} else {
79226599Sdas			/* x >= 1455: the result always overflows */
80226599Sdas			h = huge * x;
81226599Sdas			return (cpack(h * h * cos(y), h * sin(y)));
82226599Sdas		}
83226458Sdas	}
84226458Sdas
85226458Sdas	/*
86226458Sdas	 * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0.
87226458Sdas	 * The sign of 0 in the result is unspecified.  Choice = normally
88226458Sdas	 * the same as dNaN.  Raise the invalid floating-point exception.
89226458Sdas	 *
90226458Sdas	 * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0.
91226458Sdas	 * The sign of 0 in the result is unspecified.  Choice = normally
92226458Sdas	 * the same as d(NaN).
93226458Sdas	 */
94226458Sdas	if ((ix | lx) == 0 && iy >= 0x7ff00000)
95226458Sdas		return (cpack(y - y, copysign(0, x * (y - y))));
96226458Sdas
97226458Sdas	/*
98226458Sdas	 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
99226458Sdas	 *
100226458Sdas	 * cosh(NaN +- I 0)   = d(NaN) + I sign(d(NaN, +-0))0.
101226458Sdas	 * The sign of 0 in the result is unspecified.
102226458Sdas	 */
103226458Sdas	if ((iy | ly) == 0 && ix >= 0x7ff00000) {
104226458Sdas		if (((hx & 0xfffff) | lx) == 0)
105226458Sdas			return (cpack(x * x, copysign(0, x) * y));
106226458Sdas		return (cpack(x * x, copysign(0, (x + x) * y)));
107226458Sdas	}
108226458Sdas
109226458Sdas	/*
110226458Sdas	 * cosh(x +- I Inf) = dNaN + I dNaN.
111226458Sdas	 * Raise the invalid floating-point exception for finite nonzero x.
112226458Sdas	 *
113226458Sdas	 * cosh(x + I NaN) = d(NaN) + I d(NaN).
114226458Sdas	 * Optionally raises the invalid floating-point exception for finite
115226458Sdas	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
116226458Sdas	 */
117226458Sdas	if (ix < 0x7ff00000 && iy >= 0x7ff00000)
118226458Sdas		return (cpack(y - y, x * (y - y)));
119226458Sdas
120226458Sdas	/*
121226458Sdas	 * cosh(+-Inf + I NaN)  = +Inf + I d(NaN).
122226458Sdas	 *
123226458Sdas	 * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
124226458Sdas	 * The sign of Inf in the result is unspecified.  Choice = always +.
125226458Sdas	 * Raise the invalid floating-point exception.
126226458Sdas	 *
127226458Sdas	 * cosh(+-Inf + I y)   = +Inf cos(y) +- I Inf sin(y)
128226458Sdas	 */
129226458Sdas	if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
130226458Sdas		if (iy >= 0x7ff00000)
131226458Sdas			return (cpack(x * x, x * (y - y)));
132226458Sdas		return (cpack((x * x) * cos(y), x * sin(y)));
133226458Sdas	}
134226458Sdas
135226458Sdas	/*
136226458Sdas	 * cosh(NaN + I NaN)  = d(NaN) + I d(NaN).
137226458Sdas	 *
138226458Sdas	 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
139226458Sdas	 * Optionally raises the invalid floating-point exception.
140226458Sdas	 * Choice = raise.
141226458Sdas	 *
142226458Sdas	 * cosh(NaN + I y)    = d(NaN) + I d(NaN).
143226458Sdas	 * Optionally raises the invalid floating-point exception for finite
144226458Sdas	 * nonzero y.  Choice = don't raise (except for signaling NaNs).
145226458Sdas	 */
146226458Sdas	return (cpack((x * x) * (y - y), (x + x) * (y - y)));
147226458Sdas}
148226458Sdas
149226458Sdasdouble complex
150226458Sdasccos(double complex z)
151226458Sdas{
152226458Sdas
153226458Sdas	/* ccos(z) = ccosh(I * z) */
154226458Sdas	return (ccosh(cpack(-cimag(z), creal(z))));
155226458Sdas}
156