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 sine of a complex argument z = x + i y.
29226458Sdas *
30226458Sdas * sinh(z) = sinh(x+iy)
31226458Sdas *         = sinh(x) cos(y) + i cosh(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.
35284810Stijl * The sign of the result for some exceptional values is unspecified but
36284810Stijl * must satisfy both sinh(conj(z)) == conj(sinh(z)) and sinh(-z) == -sinh(z).
37226458Sdas */
38226458Sdas
39226458Sdas#include <sys/cdefs.h>
40226458Sdas__FBSDID("$FreeBSD$");
41226458Sdas
42226458Sdas#include <complex.h>
43226458Sdas#include <math.h>
44226458Sdas
45226458Sdas#include "math_private.h"
46226458Sdas
47226599Sdasstatic const double huge = 0x1p1023;
48226599Sdas
49226458Sdasdouble complex
50226458Sdascsinh(double complex z)
51226458Sdas{
52226599Sdas	double x, y, h;
53226458Sdas	int32_t hx, hy, ix, iy, lx, ly;
54226458Sdas
55226458Sdas	x = creal(z);
56226458Sdas	y = cimag(z);
57226458Sdas
58226458Sdas	EXTRACT_WORDS(hx, lx, x);
59226458Sdas	EXTRACT_WORDS(hy, ly, y);
60226458Sdas
61226458Sdas	ix = 0x7fffffff & hx;
62226458Sdas	iy = 0x7fffffff & hy;
63226458Sdas
64226458Sdas	/* Handle the nearly-non-exceptional cases where x and y are finite. */
65226458Sdas	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
66226458Sdas		if ((iy | ly) == 0)
67284810Stijl			return (CMPLX(sinh(x), y));
68284810Stijl		if (ix < 0x40360000)	/* |x| < 22: normal case */
69284810Stijl			return (CMPLX(sinh(x) * cos(y), cosh(x) * sin(y)));
70226599Sdas
71226599Sdas		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
72226599Sdas		if (ix < 0x40862e42) {
73226599Sdas			/* x < 710: exp(|x|) won't overflow */
74226599Sdas			h = exp(fabs(x)) * 0.5;
75284810Stijl			return (CMPLX(copysign(h, x) * cos(y), h * sin(y)));
76226599Sdas		} else if (ix < 0x4096bbaa) {
77226599Sdas			/* x < 1455: scale to avoid overflow */
78284810Stijl			z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
79284810Stijl			return (CMPLX(creal(z) * copysign(1, x), cimag(z)));
80226599Sdas		} else {
81226599Sdas			/* x >= 1455: the result always overflows */
82226599Sdas			h = huge * x;
83284810Stijl			return (CMPLX(h * cos(y), h * h * sin(y)));
84226599Sdas		}
85226458Sdas	}
86226458Sdas
87226458Sdas	/*
88284810Stijl	 * sinh(+-0 +- I Inf) = +-0 + I dNaN.
89284810Stijl	 * The sign of 0 in the result is unspecified.  Choice = same sign
90284810Stijl	 * as the argument.  Raise the invalid floating-point exception.
91226458Sdas	 *
92284810Stijl	 * sinh(+-0 +- I NaN) = +-0 + I d(NaN).
93284810Stijl	 * The sign of 0 in the result is unspecified.  Choice = same sign
94284810Stijl	 * as the argument.
95226458Sdas	 */
96284810Stijl	if ((ix | lx) == 0)		/* && iy >= 0x7ff00000 */
97284810Stijl		return (CMPLX(x, y - y));
98226458Sdas
99226458Sdas	/*
100226599Sdas	 * sinh(+-Inf +- I 0) = +-Inf + I +-0.
101226458Sdas	 *
102226458Sdas	 * sinh(NaN +- I 0)   = d(NaN) + I +-0.
103226458Sdas	 */
104284810Stijl	if ((iy | ly) == 0)		/* && ix >= 0x7ff00000 */
105284810Stijl		return (CMPLX(x + x, y));
106226458Sdas
107226458Sdas	/*
108226458Sdas	 * sinh(x +- I Inf) = dNaN + I dNaN.
109226458Sdas	 * Raise the invalid floating-point exception for finite nonzero x.
110226458Sdas	 *
111226458Sdas	 * sinh(x + I NaN) = d(NaN) + I d(NaN).
112226458Sdas	 * Optionally raises the invalid floating-point exception for finite
113226458Sdas	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
114226458Sdas	 */
115284810Stijl	if (ix < 0x7ff00000)		/* && iy >= 0x7ff00000 */
116284810Stijl		return (CMPLX(y - y, y - y));
117226458Sdas
118226458Sdas	/*
119226458Sdas	 * sinh(+-Inf + I NaN)  = +-Inf + I d(NaN).
120284810Stijl	 * The sign of Inf in the result is unspecified.  Choice = same sign
121284810Stijl	 * as the argument.
122226458Sdas	 *
123284810Stijl	 * sinh(+-Inf +- I Inf) = +-Inf + I dNaN.
124284810Stijl	 * The sign of Inf in the result is unspecified.  Choice = same sign
125284810Stijl	 * as the argument.  Raise the invalid floating-point exception.
126226458Sdas	 *
127226458Sdas	 * sinh(+-Inf + I y)   = +-Inf cos(y) + I Inf sin(y)
128226458Sdas	 */
129284810Stijl	if (ix == 0x7ff00000 && lx == 0) {
130226458Sdas		if (iy >= 0x7ff00000)
131284810Stijl			return (CMPLX(x, y - y));
132284810Stijl		return (CMPLX(x * cos(y), INFINITY * sin(y)));
133226458Sdas	}
134226458Sdas
135226458Sdas	/*
136284810Stijl	 * sinh(NaN1 + I NaN2) = d(NaN1, NaN2) + I d(NaN1, NaN2).
137226458Sdas	 *
138284810Stijl	 * sinh(NaN +- I Inf)  = d(NaN, dNaN) + I d(NaN, dNaN).
139226458Sdas	 * Optionally raises the invalid floating-point exception.
140226458Sdas	 * Choice = raise.
141226458Sdas	 *
142284810Stijl	 * sinh(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	 */
146284810Stijl	return (CMPLX((x + x) * (y - y), (x * x) * (y - y)));
147226458Sdas}
148226458Sdas
149226458Sdasdouble complex
150226458Sdascsin(double complex z)
151226458Sdas{
152226458Sdas
153284810Stijl	/* csin(z) = -I * csinh(I * z) = I * conj(csinh(I * conj(z))). */
154284810Stijl	z = csinh(CMPLX(cimag(z), creal(z)));
155284810Stijl	return (CMPLX(cimag(z), creal(z)));
156226458Sdas}
157