Branch data Line data Source code
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 : : */
36 : :
37 : : #include "cdefs-compat.h"
38 : : //__FBSDID("$FreeBSD: src/lib/msun/src/s_csinh.c,v 1.2 2011/10/21 06:29:32 das Exp $");
39 : :
40 : : #include <openlibm_complex.h>
41 : : #include <openlibm_math.h>
42 : :
43 : : #include "math_private.h"
44 : :
45 : : static const double huge = 0x1p1023;
46 : :
47 : : OLM_DLLEXPORT double complex
48 : 0 : csinh(double complex z)
49 : : {
50 : : double x, y, h;
51 : : int32_t hx, hy, ix, iy, lx, ly;
52 : :
53 : 0 : x = creal(z);
54 : 0 : y = cimag(z);
55 : :
56 : 0 : EXTRACT_WORDS(hx, lx, x);
57 : 0 : EXTRACT_WORDS(hy, ly, y);
58 : :
59 : 0 : ix = 0x7fffffff & hx;
60 : 0 : iy = 0x7fffffff & hy;
61 : :
62 : : /* Handle the nearly-non-exceptional cases where x and y are finite. */
63 [ # # # # ]: 0 : if (ix < 0x7ff00000 && iy < 0x7ff00000) {
64 [ # # ]: 0 : if ((iy | ly) == 0)
65 : 0 : return (CMPLX(sinh(x), y));
66 [ # # ]: 0 : if (ix < 0x40360000) /* small x: normal case */
67 : 0 : return (CMPLX(sinh(x) * cos(y), cosh(x) * sin(y)));
68 : :
69 : : /* |x| >= 22, so cosh(x) ~= exp(|x|) */
70 [ # # ]: 0 : if (ix < 0x40862e42) {
71 : : /* x < 710: exp(|x|) won't overflow */
72 : 0 : h = exp(fabs(x)) * 0.5;
73 : 0 : return (CMPLX(copysign(h, x) * cos(y), h * sin(y)));
74 [ # # ]: 0 : } else if (ix < 0x4096bbaa) {
75 : : /* x < 1455: scale to avoid overflow */
76 : 0 : z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
77 : 0 : return (CMPLX(creal(z) * copysign(1, x), cimag(z)));
78 : : } else {
79 : : /* x >= 1455: the result always overflows */
80 : 0 : h = huge * x;
81 : 0 : return (CMPLX(h * cos(y), h * h * sin(y)));
82 : : }
83 : : }
84 : :
85 : : /*
86 : : * sinh(+-0 +- I Inf) = sign(d(+-0, dNaN))0 + I dNaN.
87 : : * The sign of 0 in the result is unspecified. Choice = normally
88 : : * the same as dNaN. Raise the invalid floating-point exception.
89 : : *
90 : : * sinh(+-0 +- I NaN) = sign(d(+-0, NaN))0 + I d(NaN).
91 : : * The sign of 0 in the result is unspecified. Choice = normally
92 : : * the same as d(NaN).
93 : : */
94 [ # # # # ]: 0 : if ((ix | lx) == 0 && iy >= 0x7ff00000)
95 : 0 : return (CMPLX(copysign(0, x * (y - y)), y - y));
96 : :
97 : : /*
98 : : * sinh(+-Inf +- I 0) = +-Inf + I +-0.
99 : : *
100 : : * sinh(NaN +- I 0) = d(NaN) + I +-0.
101 : : */
102 [ # # # # ]: 0 : if ((iy | ly) == 0 && ix >= 0x7ff00000) {
103 [ # # ]: 0 : if (((hx & 0xfffff) | lx) == 0)
104 : 0 : return (CMPLX(x, y));
105 : 0 : return (CMPLX(x, copysign(0, y)));
106 : : }
107 : :
108 : : /*
109 : : * sinh(x +- I Inf) = dNaN + I dNaN.
110 : : * Raise the invalid floating-point exception for finite nonzero x.
111 : : *
112 : : * sinh(x + I NaN) = d(NaN) + I d(NaN).
113 : : * Optionally raises the invalid floating-point exception for finite
114 : : * nonzero x. Choice = don't raise (except for signaling NaNs).
115 : : */
116 [ # # # # ]: 0 : if (ix < 0x7ff00000 && iy >= 0x7ff00000)
117 : 0 : return (CMPLX(y - y, x * (y - y)));
118 : :
119 : : /*
120 : : * sinh(+-Inf + I NaN) = +-Inf + I d(NaN).
121 : : * The sign of Inf in the result is unspecified. Choice = normally
122 : : * the same as d(NaN).
123 : : *
124 : : * sinh(+-Inf +- I Inf) = +Inf + I dNaN.
125 : : * The sign of Inf in the result is unspecified. Choice = always +.
126 : : * Raise the invalid floating-point exception.
127 : : *
128 : : * sinh(+-Inf + I y) = +-Inf cos(y) + I Inf sin(y)
129 : : */
130 [ # # # # ]: 0 : if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
131 [ # # ]: 0 : if (iy >= 0x7ff00000)
132 : 0 : return (CMPLX(x * x, x * (y - y)));
133 : 0 : return (CMPLX(x * cos(y), INFINITY * sin(y)));
134 : : }
135 : :
136 : : /*
137 : : * sinh(NaN + I NaN) = d(NaN) + I d(NaN).
138 : : *
139 : : * sinh(NaN +- I Inf) = d(NaN) + I d(NaN).
140 : : * Optionally raises the invalid floating-point exception.
141 : : * Choice = raise.
142 : : *
143 : : * sinh(NaN + I y) = d(NaN) + I d(NaN).
144 : : * Optionally raises the invalid floating-point exception for finite
145 : : * nonzero y. Choice = don't raise (except for signaling NaNs).
146 : : */
147 : 0 : return (CMPLX((x * x) * (y - y), (x + x) * (y - y)));
148 : : }
149 : :
150 : : OLM_DLLEXPORT double complex
151 : 0 : csin(double complex z)
152 : : {
153 : :
154 : : /* csin(z) = -I * csinh(I * z) */
155 : 0 : z = csinh(CMPLX(-cimag(z), creal(z)));
156 : 0 : return (CMPLX(cimag(z), -creal(z)));
157 : : }
|