NPTL is no longer an add-on!
[platform/upstream/glibc.git] / math / s_catanhl.c
1 /* Return arc hyperbole tangent for long double value.
2    Copyright (C) 1997-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <complex.h>
21 #include <math.h>
22 #include <math_private.h>
23 #include <float.h>
24
25 /* To avoid spurious overflows, use this definition to treat IBM long
26    double as approximating an IEEE-style format.  */
27 #if LDBL_MANT_DIG == 106
28 # undef LDBL_EPSILON
29 # define LDBL_EPSILON 0x1p-106L
30 #endif
31
32 __complex__ long double
33 __catanhl (__complex__ long double x)
34 {
35   __complex__ long double res;
36   int rcls = fpclassify (__real__ x);
37   int icls = fpclassify (__imag__ x);
38
39   if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE))
40     {
41       if (icls == FP_INFINITE)
42         {
43           __real__ res = __copysignl (0.0, __real__ x);
44           __imag__ res = __copysignl (M_PI_2l, __imag__ x);
45         }
46       else if (rcls == FP_INFINITE || rcls == FP_ZERO)
47         {
48           __real__ res = __copysignl (0.0, __real__ x);
49           if (icls >= FP_ZERO)
50             __imag__ res = __copysignl (M_PI_2l, __imag__ x);
51           else
52             __imag__ res = __nanl ("");
53         }
54       else
55         {
56           __real__ res = __nanl ("");
57           __imag__ res = __nanl ("");
58         }
59     }
60   else if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
61     {
62       res = x;
63     }
64   else
65     {
66       if (fabsl (__real__ x) >= 16.0L / LDBL_EPSILON
67           || fabsl (__imag__ x) >= 16.0L / LDBL_EPSILON)
68         {
69           __imag__ res = __copysignl (M_PI_2l, __imag__ x);
70           if (fabsl (__imag__ x) <= 1.0L)
71             __real__ res = 1.0L / __real__ x;
72           else if (fabsl (__real__ x) <= 1.0L)
73             __real__ res = __real__ x / __imag__ x / __imag__ x;
74           else
75             {
76               long double h = __ieee754_hypotl (__real__ x / 2.0L,
77                                                 __imag__ x / 2.0L);
78               __real__ res = __real__ x / h / h / 4.0L;
79             }
80         }
81       else
82         {
83           if (fabsl (__real__ x) == 1.0L
84               && fabsl (__imag__ x) < LDBL_EPSILON * LDBL_EPSILON)
85             __real__ res = (__copysignl (0.5L, __real__ x)
86                             * (M_LN2l - __ieee754_logl (fabsl (__imag__ x))));
87           else
88             {
89               long double i2 = 0.0;
90               if (fabsl (__imag__ x) >= LDBL_EPSILON * LDBL_EPSILON)
91                 i2 = __imag__ x * __imag__ x;
92
93               long double num = 1.0L + __real__ x;
94               num = i2 + num * num;
95
96               long double den = 1.0L - __real__ x;
97               den = i2 + den * den;
98
99               long double f = num / den;
100               if (f < 0.5L)
101                 __real__ res = 0.25L * __ieee754_logl (f);
102               else
103                 {
104                   num = 4.0L * __real__ x;
105                   __real__ res = 0.25L * __log1pl (num / den);
106                 }
107             }
108
109           long double absx, absy, den;
110
111           absx = fabsl (__real__ x);
112           absy = fabsl (__imag__ x);
113           if (absx < absy)
114             {
115               long double t = absx;
116               absx = absy;
117               absy = t;
118             }
119
120           if (absy < LDBL_EPSILON / 2.0L)
121             {
122               den = (1.0L - absx) * (1.0L + absx);
123               if (den == -0.0L)
124                 den = 0.0L;
125             }
126           else if (absx >= 1.0L)
127             den = (1.0L - absx) * (1.0L + absx) - absy * absy;
128           else if (absx >= 0.75L || absy >= 0.5L)
129             den = -__x2y2m1l (absx, absy);
130           else
131             den = (1.0L - absx) * (1.0L + absx) - absy * absy;
132
133           __imag__ res = 0.5L * __ieee754_atan2l (2.0L * __imag__ x, den);
134         }
135
136       if (fabsl (__real__ res) < LDBL_MIN)
137         {
138           volatile long double force_underflow = __real__ res * __real__ res;
139           (void) force_underflow;
140         }
141       if (fabsl (__imag__ res) < LDBL_MIN)
142         {
143           volatile long double force_underflow = __imag__ res * __imag__ res;
144           (void) force_underflow;
145         }
146     }
147
148   return res;
149 }
150 weak_alias (__catanhl, catanhl)