1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/s_csinh.c */
2570af302Sopenharmony_ci/*-
3570af302Sopenharmony_ci * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
4570af302Sopenharmony_ci * All rights reserved.
5570af302Sopenharmony_ci *
6570af302Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
7570af302Sopenharmony_ci * modification, are permitted provided that the following conditions
8570af302Sopenharmony_ci * are met:
9570af302Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
10570af302Sopenharmony_ci *    notice unmodified, this list of conditions, and the following
11570af302Sopenharmony_ci *    disclaimer.
12570af302Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
13570af302Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
14570af302Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
15570af302Sopenharmony_ci *
16570af302Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17570af302Sopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18570af302Sopenharmony_ci * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19570af302Sopenharmony_ci * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20570af302Sopenharmony_ci * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21570af302Sopenharmony_ci * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22570af302Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23570af302Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24570af302Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25570af302Sopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26570af302Sopenharmony_ci */
27570af302Sopenharmony_ci/*
28570af302Sopenharmony_ci * Hyperbolic sine of a complex argument z = x + i y.
29570af302Sopenharmony_ci *
30570af302Sopenharmony_ci * sinh(z) = sinh(x+iy)
31570af302Sopenharmony_ci *         = sinh(x) cos(y) + i cosh(x) sin(y).
32570af302Sopenharmony_ci *
33570af302Sopenharmony_ci * Exceptional values are noted in the comments within the source code.
34570af302Sopenharmony_ci * These values and the return value were taken from n1124.pdf.
35570af302Sopenharmony_ci */
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci#include "complex_impl.h"
38570af302Sopenharmony_ci
39570af302Sopenharmony_cistatic const double huge = 0x1p1023;
40570af302Sopenharmony_ci
41570af302Sopenharmony_cidouble complex csinh(double complex z)
42570af302Sopenharmony_ci{
43570af302Sopenharmony_ci	double x, y, h;
44570af302Sopenharmony_ci	int32_t hx, hy, ix, iy, lx, ly;
45570af302Sopenharmony_ci
46570af302Sopenharmony_ci	x = creal(z);
47570af302Sopenharmony_ci	y = cimag(z);
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci	EXTRACT_WORDS(hx, lx, x);
50570af302Sopenharmony_ci	EXTRACT_WORDS(hy, ly, y);
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci	ix = 0x7fffffff & hx;
53570af302Sopenharmony_ci	iy = 0x7fffffff & hy;
54570af302Sopenharmony_ci
55570af302Sopenharmony_ci	/* Handle the nearly-non-exceptional cases where x and y are finite. */
56570af302Sopenharmony_ci	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
57570af302Sopenharmony_ci		if ((iy | ly) == 0)
58570af302Sopenharmony_ci			return CMPLX(sinh(x), y);
59570af302Sopenharmony_ci		if (ix < 0x40360000)    /* small x: normal case */
60570af302Sopenharmony_ci			return CMPLX(sinh(x) * cos(y), cosh(x) * sin(y));
61570af302Sopenharmony_ci
62570af302Sopenharmony_ci		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
63570af302Sopenharmony_ci		if (ix < 0x40862e42) {
64570af302Sopenharmony_ci			/* x < 710: exp(|x|) won't overflow */
65570af302Sopenharmony_ci			h = exp(fabs(x)) * 0.5;
66570af302Sopenharmony_ci			return CMPLX(copysign(h, x) * cos(y), h * sin(y));
67570af302Sopenharmony_ci		} else if (ix < 0x4096bbaa) {
68570af302Sopenharmony_ci			/* x < 1455: scale to avoid overflow */
69570af302Sopenharmony_ci			z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
70570af302Sopenharmony_ci			return CMPLX(creal(z) * copysign(1, x), cimag(z));
71570af302Sopenharmony_ci		} else {
72570af302Sopenharmony_ci			/* x >= 1455: the result always overflows */
73570af302Sopenharmony_ci			h = huge * x;
74570af302Sopenharmony_ci			return CMPLX(h * cos(y), h * h * sin(y));
75570af302Sopenharmony_ci		}
76570af302Sopenharmony_ci	}
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci	/*
79570af302Sopenharmony_ci	 * sinh(+-0 +- I Inf) = sign(d(+-0, dNaN))0 + I dNaN.
80570af302Sopenharmony_ci	 * The sign of 0 in the result is unspecified.  Choice = normally
81570af302Sopenharmony_ci	 * the same as dNaN.  Raise the invalid floating-point exception.
82570af302Sopenharmony_ci	 *
83570af302Sopenharmony_ci	 * sinh(+-0 +- I NaN) = sign(d(+-0, NaN))0 + I d(NaN).
84570af302Sopenharmony_ci	 * The sign of 0 in the result is unspecified.  Choice = normally
85570af302Sopenharmony_ci	 * the same as d(NaN).
86570af302Sopenharmony_ci	 */
87570af302Sopenharmony_ci	if ((ix | lx) == 0 && iy >= 0x7ff00000)
88570af302Sopenharmony_ci		return CMPLX(copysign(0, x * (y - y)), y - y);
89570af302Sopenharmony_ci
90570af302Sopenharmony_ci	/*
91570af302Sopenharmony_ci	 * sinh(+-Inf +- I 0) = +-Inf + I +-0.
92570af302Sopenharmony_ci	 *
93570af302Sopenharmony_ci	 * sinh(NaN +- I 0)   = d(NaN) + I +-0.
94570af302Sopenharmony_ci	 */
95570af302Sopenharmony_ci	if ((iy | ly) == 0 && ix >= 0x7ff00000) {
96570af302Sopenharmony_ci		if (((hx & 0xfffff) | lx) == 0)
97570af302Sopenharmony_ci			return CMPLX(x, y);
98570af302Sopenharmony_ci		return CMPLX(x, copysign(0, y));
99570af302Sopenharmony_ci	}
100570af302Sopenharmony_ci
101570af302Sopenharmony_ci	/*
102570af302Sopenharmony_ci	 * sinh(x +- I Inf) = dNaN + I dNaN.
103570af302Sopenharmony_ci	 * Raise the invalid floating-point exception for finite nonzero x.
104570af302Sopenharmony_ci	 *
105570af302Sopenharmony_ci	 * sinh(x + I NaN) = d(NaN) + I d(NaN).
106570af302Sopenharmony_ci	 * Optionally raises the invalid floating-point exception for finite
107570af302Sopenharmony_ci	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
108570af302Sopenharmony_ci	 */
109570af302Sopenharmony_ci	if (ix < 0x7ff00000 && iy >= 0x7ff00000)
110570af302Sopenharmony_ci		return CMPLX(y - y, x * (y - y));
111570af302Sopenharmony_ci
112570af302Sopenharmony_ci	/*
113570af302Sopenharmony_ci	 * sinh(+-Inf + I NaN)  = +-Inf + I d(NaN).
114570af302Sopenharmony_ci	 * The sign of Inf in the result is unspecified.  Choice = normally
115570af302Sopenharmony_ci	 * the same as d(NaN).
116570af302Sopenharmony_ci	 *
117570af302Sopenharmony_ci	 * sinh(+-Inf +- I Inf) = +Inf + I dNaN.
118570af302Sopenharmony_ci	 * The sign of Inf in the result is unspecified.  Choice = always +.
119570af302Sopenharmony_ci	 * Raise the invalid floating-point exception.
120570af302Sopenharmony_ci	 *
121570af302Sopenharmony_ci	 * sinh(+-Inf + I y)   = +-Inf cos(y) + I Inf sin(y)
122570af302Sopenharmony_ci	 */
123570af302Sopenharmony_ci	if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
124570af302Sopenharmony_ci		if (iy >= 0x7ff00000)
125570af302Sopenharmony_ci			return CMPLX(x * x, x * (y - y));
126570af302Sopenharmony_ci		return CMPLX(x * cos(y), INFINITY * sin(y));
127570af302Sopenharmony_ci	}
128570af302Sopenharmony_ci
129570af302Sopenharmony_ci	/*
130570af302Sopenharmony_ci	 * sinh(NaN + I NaN)  = d(NaN) + I d(NaN).
131570af302Sopenharmony_ci	 *
132570af302Sopenharmony_ci	 * sinh(NaN +- I Inf) = d(NaN) + I d(NaN).
133570af302Sopenharmony_ci	 * Optionally raises the invalid floating-point exception.
134570af302Sopenharmony_ci	 * Choice = raise.
135570af302Sopenharmony_ci	 *
136570af302Sopenharmony_ci	 * sinh(NaN + I y)    = d(NaN) + I d(NaN).
137570af302Sopenharmony_ci	 * Optionally raises the invalid floating-point exception for finite
138570af302Sopenharmony_ci	 * nonzero y.  Choice = don't raise (except for signaling NaNs).
139570af302Sopenharmony_ci	 */
140570af302Sopenharmony_ci	return CMPLX((x * x) * (y - y), (x + x) * (y - y));
141570af302Sopenharmony_ci}
142