]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_catan_template.c
Move math_opt_barrier, math_force_eval to separate math-barriers.h.
[thirdparty/glibc.git] / math / s_catan_template.c
CommitLineData
d5602ceb 1/* Return arc tangent of complex float type.
688903eb 2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
f6d3a72e
PM
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
d5602ceb
PM
25CFLOAT
26M_DECL_FUNC (__catan) (CFLOAT x)
f6d3a72e 27{
d5602ceb 28 CFLOAT res;
f6d3a72e
PM
29 int rcls = fpclassify (__real__ x);
30 int icls = fpclassify (__imag__ x);
31
32 if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE))
33 {
34 if (rcls == FP_INFINITE)
35 {
d5602ceb
PM
36 __real__ res = M_COPYSIGN (M_MLIT (M_PI_2), __real__ x);
37 __imag__ res = M_COPYSIGN (0, __imag__ x);
f6d3a72e
PM
38 }
39 else if (icls == FP_INFINITE)
40 {
41 if (rcls >= FP_ZERO)
d5602ceb 42 __real__ res = M_COPYSIGN (M_MLIT (M_PI_2), __real__ x);
f6d3a72e 43 else
d5602ceb
PM
44 __real__ res = M_NAN;
45 __imag__ res = M_COPYSIGN (0, __imag__ x);
f6d3a72e
PM
46 }
47 else if (icls == FP_ZERO || icls == FP_INFINITE)
48 {
d5602ceb
PM
49 __real__ res = M_NAN;
50 __imag__ res = M_COPYSIGN (0, __imag__ x);
f6d3a72e
PM
51 }
52 else
53 {
d5602ceb
PM
54 __real__ res = M_NAN;
55 __imag__ res = M_NAN;
f6d3a72e
PM
56 }
57 }
58 else if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
59 {
60 res = x;
61 }
62 else
63 {
d5602ceb
PM
64 if (M_FABS (__real__ x) >= 16 / M_EPSILON
65 || M_FABS (__imag__ x) >= 16 / M_EPSILON)
f6d3a72e 66 {
d5602ceb
PM
67 __real__ res = M_COPYSIGN (M_MLIT (M_PI_2), __real__ x);
68 if (M_FABS (__real__ x) <= 1)
69 __imag__ res = 1 / __imag__ x;
70 else if (M_FABS (__imag__ x) <= 1)
f6d3a72e
PM
71 __imag__ res = __imag__ x / __real__ x / __real__ x;
72 else
73 {
d5602ceb
PM
74 FLOAT h = M_HYPOT (__real__ x / 2, __imag__ x / 2);
75 __imag__ res = __imag__ x / h / h / 4;
f6d3a72e
PM
76 }
77 }
78 else
79 {
d5602ceb 80 FLOAT den, absx, absy;
f6d3a72e 81
d5602ceb
PM
82 absx = M_FABS (__real__ x);
83 absy = M_FABS (__imag__ x);
f6d3a72e
PM
84 if (absx < absy)
85 {
d5602ceb 86 FLOAT t = absx;
f6d3a72e
PM
87 absx = absy;
88 absy = t;
89 }
90
d5602ceb 91 if (absy < M_EPSILON / 2)
f6d3a72e 92 {
d5602ceb
PM
93 den = (1 - absx) * (1 + absx);
94 if (den == 0)
95 den = 0;
f6d3a72e 96 }
d5602ceb
PM
97 else if (absx >= 1)
98 den = (1 - absx) * (1 + absx) - absy * absy;
99 else if (absx >= M_LIT (0.75) || absy >= M_LIT (0.5))
100 den = -M_SUF (__x2y2m1) (absx, absy);
f6d3a72e 101 else
d5602ceb 102 den = (1 - absx) * (1 + absx) - absy * absy;
f6d3a72e 103
d5602ceb 104 __real__ res = M_LIT (0.5) * M_ATAN2 (2 * __real__ x, den);
f6d3a72e 105
d5602ceb
PM
106 if (M_FABS (__imag__ x) == 1
107 && M_FABS (__real__ x) < M_EPSILON * M_EPSILON)
108 __imag__ res = (M_COPYSIGN (M_LIT (0.5), __imag__ x)
109 * ((FLOAT) M_MLIT (M_LN2)
110 - M_LOG (M_FABS (__real__ x))));
f6d3a72e
PM
111 else
112 {
d5602ceb 113 FLOAT r2 = 0, num, f;
f6d3a72e 114
d5602ceb 115 if (M_FABS (__real__ x) >= M_EPSILON * M_EPSILON)
f6d3a72e
PM
116 r2 = __real__ x * __real__ x;
117
d5602ceb 118 num = __imag__ x + 1;
f6d3a72e
PM
119 num = r2 + num * num;
120
d5602ceb 121 den = __imag__ x - 1;
f6d3a72e
PM
122 den = r2 + den * den;
123
124 f = num / den;
d5602ceb
PM
125 if (f < M_LIT (0.5))
126 __imag__ res = M_LIT (0.25) * M_LOG (f);
f6d3a72e
PM
127 else
128 {
d5602ceb
PM
129 num = 4 * __imag__ x;
130 __imag__ res = M_LIT (0.25) * M_LOG1P (num / den);
f6d3a72e
PM
131 }
132 }
133 }
134
135 math_check_force_underflow_complex (res);
136 }
137
138 return res;
139}
d5602ceb
PM
140
141declare_mgen_alias (__catan, catan)