master
1/*
2 This Software is provided under the Zope Public License (ZPL) Version 2.1.
3
4 Copyright (c) 2009, 2010 by the mingw-w64 project
5
6 See the AUTHORS file for the list of contributors to the mingw-w64 project.
7
8 This license has been certified as open source. It has also been designated
9 as GPL compatible by the Free Software Foundation (FSF).
10
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13
14 1. Redistributions in source code must retain the accompanying copyright
15 notice, this list of conditions, and the following disclaimer.
16 2. Redistributions in binary form must reproduce the accompanying
17 copyright notice, this list of conditions, and the following disclaimer
18 in the documentation and/or other materials provided with the
19 distribution.
20 3. Names of the copyright holders must not be used to endorse or promote
21 products derived from this software without prior written permission
22 from the copyright holders.
23 4. The right to distribute this software or to use it for any purpose does
24 not give you the right to use Servicemarks (sm) or Trademarks (tm) of
25 the copyright holders. Use of them is covered by separate agreement
26 with the copyright holders.
27 5. If any files are modified, you must cause the modified files to carry
28 prominent notices stating that you changed the files and the date of
29 any change.
30
31 Disclaimer
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESSED
34 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
36 EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
37 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43*/
44
45__FLT_TYPE __complex__ __cdecl
46__FLT_ABI(casinh) (__FLT_TYPE __complex__ z)
47{
48 __complex__ __FLT_TYPE ret;
49 __complex__ __FLT_TYPE x;
50 __FLT_TYPE arz, aiz;
51 int r_class = fpclassify (__real__ z);
52 int i_class = fpclassify (__imag__ z);
53
54 if (i_class == FP_INFINITE)
55 {
56 __real__ ret = __FLT_ABI(copysign) (__FLT_HUGE_VAL, __real__ z);
57 __imag__ ret = (r_class == FP_NAN
58 ? __FLT_NAN
59 : (__FLT_ABI(copysign) ((r_class != FP_NAN && r_class != FP_INFINITE) ? __FLT_PI_2 : __FLT_PI_4, __imag__ z)));
60 return ret;
61 }
62
63 if (r_class == FP_INFINITE)
64 {
65 __real__ ret = __real__ z;
66 __imag__ ret = (i_class != FP_NAN
67 ? __FLT_ABI(copysign) (__FLT_CST(0.0), __imag__ z)
68 : __FLT_NAN);
69 return ret;
70 }
71
72 if (r_class == FP_NAN)
73 {
74 __real__ ret = __real__ z;
75 __imag__ ret = (i_class == FP_ZERO
76 ? __FLT_ABI(copysign) (__FLT_CST(0.0), __imag__ z)
77 : __FLT_NAN);
78 return ret;
79 }
80
81 if (i_class == FP_NAN)
82 {
83 __real__ ret = __FLT_NAN;
84 __imag__ ret = __FLT_NAN;
85 return ret;
86 }
87
88 if (r_class == FP_ZERO && i_class == FP_ZERO)
89 return z;
90
91 /* casinh(z) = log(z + sqrt(z*z + 1)) */
92
93 /* Use symmetries to perform the calculation in the first quadrant. */
94 arz = __FLT_ABI(fabs) (__real__ z);
95 aiz = __FLT_ABI(fabs) (__imag__ z);
96
97 if (arz >= __FLT_CST(1.0)/__FLT_EPSILON
98 || aiz >= __FLT_CST(1.0)/__FLT_EPSILON)
99 {
100 /* For large z, z + sqrt(z*z + 1) is approximately 2*z.
101 Use that approximation to avoid overflow when squaring. */
102 __real__ x = arz;
103 __imag__ x = aiz;
104 ret = __FLT_ABI(clog) (x);
105 __real__ ret += M_LN2;
106 }
107 else if (aiz < __FLT_CST(1.0) && arz <= __FLT_EPSILON)
108 {
109 /* Taylor series expansion around arz=0 for z + sqrt(z*z + 1):
110 c = arz + sqrt(1-aiz^2) + i*(aiz + arz*aiz / sqrt(1-aiz^2)) + O(arz^2)
111 Identity: clog(c) = log(|c|) + i*arg(c)
112 For real part of result:
113 |c| = 1 + arz / sqrt(1-aiz^2) + O(arz^2) (Taylor series expansion)
114 For imaginary part of result:
115 c = (arz + sqrt(1-aiz^2))/sqrt(1-aiz^2) * (sqrt(1-aiz^2) + i*aiz) + O(arz^6)
116 */
117 __FLT_TYPE s1maiz2 = __FLT_ABI(sqrt) ((__FLT_CST(1.0)+aiz)*(__FLT_CST(1.0)-aiz));
118 __real__ ret = __FLT_ABI(log1p) (arz / s1maiz2);
119 __imag__ ret = __FLT_ABI(atan2) (aiz, s1maiz2);
120 }
121 else if (aiz < __FLT_CST(1.0) && arz*arz <= __FLT_EPSILON)
122 {
123 /* Taylor series expansion around arz=0 for z + sqrt(z*z + 1):
124 c = arz + sqrt(1-aiz^2) + arz^2 / (2*(1-aiz^2)^(3/2)) + i*(aiz + arz*aiz / sqrt(1-aiz^2)) + O(arz^4)
125 Identity: clog(c) = log(|c|) + i*arg(c)
126 For real part of result:
127 |c| = 1 + arz / sqrt(1-aiz^2) + arz^2/(2*(1-aiz^2)) + O(arz^3) (Taylor series expansion)
128 For imaginary part of result:
129 c = 1/sqrt(1-aiz^2) * ((1-aiz^2) + arz*sqrt(1-aiz^2) + arz^2/(2*(1-aiz^2)) + i*aiz*(sqrt(1-aiz^2)+arz)) + O(arz^3)
130 */
131 __FLT_TYPE onemaiz2 = (__FLT_CST(1.0)+aiz)*(__FLT_CST(1.0)-aiz);
132 __FLT_TYPE s1maiz2 = __FLT_ABI(sqrt) (onemaiz2);
133 __FLT_TYPE arz2red = arz * arz / __FLT_CST(2.0) / s1maiz2;
134 __real__ ret = __FLT_ABI(log1p) ((arz + arz2red) / s1maiz2);
135 __imag__ ret = __FLT_ABI(atan2) (aiz * (s1maiz2 + arz),
136 onemaiz2 + arz*s1maiz2 + arz2red);
137 }
138 else
139 {
140 __real__ x = (arz - aiz) * (arz + aiz) + __FLT_CST(1.0);
141 __imag__ x = __FLT_CST(2.0) * arz * aiz;
142
143 x = __FLT_ABI(csqrt) (x);
144
145 __real__ x += arz;
146 __imag__ x += aiz;
147
148 ret = __FLT_ABI(clog) (x);
149 }
150
151 /* adjust signs for input quadrant */
152 __real__ ret = __FLT_ABI(copysign) (__real__ ret, __real__ z);
153 __imag__ ret = __FLT_ABI(copysign) (__imag__ ret, __imag__ z);
154
155 return ret;
156}