xref: /freebsd/lib/msun/src/s_csinh.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /*-
2  * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * Hyperbolic sine of a complex argument z = x + i y.
29  *
30  * sinh(z) = sinh(x+iy)
31  *         = sinh(x) cos(y) + i cosh(x) sin(y).
32  *
33  * Exceptional values are noted in the comments within the source code.
34  * These values and the return value were taken from n1124.pdf.
35  * The sign of the result for some exceptional values is unspecified but
36  * must satisfy both sinh(conj(z)) == conj(sinh(z)) and sinh(-z) == -sinh(z).
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <complex.h>
43 #include <math.h>
44 
45 #include "math_private.h"
46 
47 static const double huge = 0x1p1023;
48 
49 double complex
50 csinh(double complex z)
51 {
52 	double x, y, h;
53 	int32_t hx, hy, ix, iy, lx, ly;
54 
55 	x = creal(z);
56 	y = cimag(z);
57 
58 	EXTRACT_WORDS(hx, lx, x);
59 	EXTRACT_WORDS(hy, ly, y);
60 
61 	ix = 0x7fffffff & hx;
62 	iy = 0x7fffffff & hy;
63 
64 	/* Handle the nearly-non-exceptional cases where x and y are finite. */
65 	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
66 		if ((iy | ly) == 0)
67 			return (CMPLX(sinh(x), y));
68 		if (ix < 0x40360000)	/* |x| < 22: normal case */
69 			return (CMPLX(sinh(x) * cos(y), cosh(x) * sin(y)));
70 
71 		/* |x| >= 22, so cosh(x) ~= exp(|x|) */
72 		if (ix < 0x40862e42) {
73 			/* x < 710: exp(|x|) won't overflow */
74 			h = exp(fabs(x)) * 0.5;
75 			return (CMPLX(copysign(h, x) * cos(y), h * sin(y)));
76 		} else if (ix < 0x4096bbaa) {
77 			/* x < 1455: scale to avoid overflow */
78 			z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
79 			return (CMPLX(creal(z) * copysign(1, x), cimag(z)));
80 		} else {
81 			/* x >= 1455: the result always overflows */
82 			h = huge * x;
83 			return (CMPLX(h * cos(y), h * h * sin(y)));
84 		}
85 	}
86 
87 	/*
88 	 * sinh(+-0 +- I Inf) = +-0 + I dNaN.
89 	 * The sign of 0 in the result is unspecified.  Choice = same sign
90 	 * as the argument.  Raise the invalid floating-point exception.
91 	 *
92 	 * sinh(+-0 +- I NaN) = +-0 + I d(NaN).
93 	 * The sign of 0 in the result is unspecified.  Choice = same sign
94 	 * as the argument.
95 	 */
96 	if ((ix | lx) == 0)		/* && iy >= 0x7ff00000 */
97 		return (CMPLX(x, y - y));
98 
99 	/*
100 	 * sinh(+-Inf +- I 0) = +-Inf + I +-0.
101 	 *
102 	 * sinh(NaN +- I 0)   = d(NaN) + I +-0.
103 	 */
104 	if ((iy | ly) == 0)		/* && ix >= 0x7ff00000 */
105 		return (CMPLX(x + x, y));
106 
107 	/*
108 	 * sinh(x +- I Inf) = dNaN + I dNaN.
109 	 * Raise the invalid floating-point exception for finite nonzero x.
110 	 *
111 	 * sinh(x + I NaN) = d(NaN) + I d(NaN).
112 	 * Optionally raises the invalid floating-point exception for finite
113 	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
114 	 */
115 	if (ix < 0x7ff00000)		/* && iy >= 0x7ff00000 */
116 		return (CMPLX(y - y, y - y));
117 
118 	/*
119 	 * sinh(+-Inf + I NaN)  = +-Inf + I d(NaN).
120 	 * The sign of Inf in the result is unspecified.  Choice = same sign
121 	 * as the argument.
122 	 *
123 	 * sinh(+-Inf +- I Inf) = +-Inf + I dNaN.
124 	 * The sign of Inf in the result is unspecified.  Choice = same sign
125 	 * as the argument.  Raise the invalid floating-point exception.
126 	 *
127 	 * sinh(+-Inf + I y)   = +-Inf cos(y) + I Inf sin(y)
128 	 */
129 	if (ix == 0x7ff00000 && lx == 0) {
130 		if (iy >= 0x7ff00000)
131 			return (CMPLX(x, y - y));
132 		return (CMPLX(x * cos(y), INFINITY * sin(y)));
133 	}
134 
135 	/*
136 	 * sinh(NaN1 + I NaN2) = d(NaN1, NaN2) + I d(NaN1, NaN2).
137 	 *
138 	 * sinh(NaN +- I Inf)  = d(NaN, dNaN) + I d(NaN, dNaN).
139 	 * Optionally raises the invalid floating-point exception.
140 	 * Choice = raise.
141 	 *
142 	 * sinh(NaN + I y)     = d(NaN) + I d(NaN).
143 	 * Optionally raises the invalid floating-point exception for finite
144 	 * nonzero y.  Choice = don't raise (except for signaling NaNs).
145 	 */
146 	return (CMPLX((x + x) * (y - y), (x * x) * (y - y)));
147 }
148 
149 double complex
150 csin(double complex z)
151 {
152 
153 	/* csin(z) = -I * csinh(I * z) = I * conj(csinh(I * conj(z))). */
154 	z = csinh(CMPLX(cimag(z), creal(z)));
155 	return (CMPLX(cimag(z), creal(z)));
156 }
157