blob: e7ed10e7d88534036886323e143be5af5611dbef [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 sine of a complex argument z = x + i y.
31 *
32 * sinh(z) = sinh(x+iy)
33 * = sinh(x) cos(y) + i cosh(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 sinh(conj(z)) == conj(sinh(z)) and sinh(-z) == -sinh(z).
Elliott Hughesa0ee0782013-01-30 19:06:37 -080039 */
40
Elliott Hughesa0ee0782013-01-30 19:06:37 -080041#include <complex.h>
42#include <math.h>
43
44#include "math_private.h"
45
46static const double huge = 0x1p1023;
47
48double complex
49csinh(double complex z)
50{
51 double x, y, h;
52 int32_t hx, hy, ix, iy, lx, ly;
53
54 x = creal(z);
55 y = cimag(z);
56
57 EXTRACT_WORDS(hx, lx, x);
58 EXTRACT_WORDS(hy, ly, y);
59
60 ix = 0x7fffffff & hx;
61 iy = 0x7fffffff & hy;
62
63 /* Handle the nearly-non-exceptional cases where x and y are finite. */
64 if (ix < 0x7ff00000 && iy < 0x7ff00000) {
65 if ((iy | ly) == 0)
Elliott Hughes8cff2f92015-08-28 20:21:43 -070066 return (CMPLX(sinh(x), y));
67 if (ix < 0x40360000) /* |x| < 22: normal case */
68 return (CMPLX(sinh(x) * cos(y), cosh(x) * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080069
70 /* |x| >= 22, so cosh(x) ~= exp(|x|) */
71 if (ix < 0x40862e42) {
72 /* x < 710: exp(|x|) won't overflow */
73 h = exp(fabs(x)) * 0.5;
Elliott Hughes8cff2f92015-08-28 20:21:43 -070074 return (CMPLX(copysign(h, x) * cos(y), h * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080075 } else if (ix < 0x4096bbaa) {
76 /* x < 1455: scale to avoid overflow */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070077 z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
78 return (CMPLX(creal(z) * copysign(1, x), cimag(z)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080079 } else {
80 /* x >= 1455: the result always overflows */
81 h = huge * x;
Elliott Hughes8cff2f92015-08-28 20:21:43 -070082 return (CMPLX(h * cos(y), h * h * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080083 }
84 }
85
86 /*
Elliott Hughes8cff2f92015-08-28 20:21:43 -070087 * sinh(+-0 +- I Inf) = +-0 + I dNaN.
88 * The sign of 0 in the result is unspecified. Choice = same sign
89 * as the argument. Raise the invalid floating-point exception.
Elliott Hughesa0ee0782013-01-30 19:06:37 -080090 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -070091 * sinh(+-0 +- I NaN) = +-0 + I d(NaN).
92 * The sign of 0 in the result is unspecified. Choice = same sign
93 * as the argument.
Elliott Hughesa0ee0782013-01-30 19:06:37 -080094 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070095 if ((ix | lx) == 0) /* && iy >= 0x7ff00000 */
96 return (CMPLX(x, y - y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080097
98 /*
99 * sinh(+-Inf +- I 0) = +-Inf + I +-0.
100 *
101 * sinh(NaN +- I 0) = d(NaN) + I +-0.
102 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700103 if ((iy | ly) == 0) /* && ix >= 0x7ff00000 */
104 return (CMPLX(x + x, y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800105
106 /*
107 * sinh(x +- I Inf) = dNaN + I dNaN.
108 * Raise the invalid floating-point exception for finite nonzero x.
109 *
110 * sinh(x + I NaN) = d(NaN) + I d(NaN).
111 * Optionally raises the invalid floating-point exception for finite
112 * nonzero x. Choice = don't raise (except for signaling NaNs).
113 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700114 if (ix < 0x7ff00000) /* && iy >= 0x7ff00000 */
115 return (CMPLX(y - y, y - y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800116
117 /*
118 * sinh(+-Inf + I NaN) = +-Inf + I d(NaN).
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700119 * The sign of Inf in the result is unspecified. Choice = same sign
120 * as the argument.
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800121 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700122 * sinh(+-Inf +- I Inf) = +-Inf + I dNaN.
123 * The sign of Inf in the result is unspecified. Choice = same sign
124 * as the argument. Raise the invalid floating-point exception.
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800125 *
126 * sinh(+-Inf + I y) = +-Inf cos(y) + I Inf sin(y)
127 */
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700128 if (ix == 0x7ff00000 && lx == 0) {
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800129 if (iy >= 0x7ff00000)
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700130 return (CMPLX(x, y - y));
131 return (CMPLX(x * cos(y), INFINITY * sin(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800132 }
133
134 /*
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700135 * sinh(NaN1 + I NaN2) = d(NaN1, NaN2) + I d(NaN1, NaN2).
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800136 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700137 * sinh(NaN +- I Inf) = d(NaN, dNaN) + I d(NaN, dNaN).
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800138 * Optionally raises the invalid floating-point exception.
139 * Choice = raise.
140 *
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700141 * sinh(NaN + I y) = d(NaN) + I d(NaN).
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800142 * Optionally raises the invalid floating-point exception for finite
143 * nonzero y. Choice = don't raise (except for signaling NaNs).
144 */
Elliott Hughesab528072018-07-24 00:01:52 +0000145 return (CMPLX(((long double)x + x) * (y - y),
146 ((long double)x * x) * (y - y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800147}
148
149double complex
150csin(double complex z)
151{
152
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700153 /* 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)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800156}