]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_catanh_template.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / math / s_catanh_template.c
CommitLineData
d5602ceb 1/* Return arc hyperbolic tangent for a complex float type.
dff8da6b 2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
f6d3a72e 3 This file is part of the GNU C Library.
f6d3a72e
PM
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
f6d3a72e
PM
18
19#include <complex.h>
20#include <math.h>
21#include <math_private.h>
8f5b00d3 22#include <math-underflow.h>
f6d3a72e
PM
23#include <float.h>
24
d5602ceb
PM
25CFLOAT
26M_DECL_FUNC (__catanh) (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 (icls == FP_INFINITE)
35 {
d5602ceb
PM
36 __real__ res = M_COPYSIGN (0, __real__ x);
37 __imag__ res = M_COPYSIGN (M_MLIT (M_PI_2), __imag__ x);
f6d3a72e
PM
38 }
39 else if (rcls == FP_INFINITE || rcls == FP_ZERO)
40 {
d5602ceb 41 __real__ res = M_COPYSIGN (0, __real__ x);
f6d3a72e 42 if (icls >= FP_ZERO)
d5602ceb 43 __imag__ res = M_COPYSIGN (M_MLIT (M_PI_2), __imag__ x);
f6d3a72e 44 else
d5602ceb 45 __imag__ res = M_NAN;
f6d3a72e
PM
46 }
47 else
48 {
d5602ceb
PM
49 __real__ res = M_NAN;
50 __imag__ res = M_NAN;
f6d3a72e
PM
51 }
52 }
53 else if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
54 {
55 res = x;
56 }
57 else
58 {
d5602ceb
PM
59 if (M_FABS (__real__ x) >= 16 / M_EPSILON
60 || M_FABS (__imag__ x) >= 16 / M_EPSILON)
f6d3a72e 61 {
d5602ceb
PM
62 __imag__ res = M_COPYSIGN (M_MLIT (M_PI_2), __imag__ x);
63 if (M_FABS (__imag__ x) <= 1)
64 __real__ res = 1 / __real__ x;
65 else if (M_FABS (__real__ x) <= 1)
f6d3a72e
PM
66 __real__ res = __real__ x / __imag__ x / __imag__ x;
67 else
68 {
d5602ceb
PM
69 FLOAT h = M_HYPOT (__real__ x / 2, __imag__ x / 2);
70 __real__ res = __real__ x / h / h / 4;
f6d3a72e
PM
71 }
72 }
73 else
74 {
d5602ceb
PM
75 if (M_FABS (__real__ x) == 1
76 && M_FABS (__imag__ x) < M_EPSILON * M_EPSILON)
77 __real__ res = (M_COPYSIGN (M_LIT (0.5), __real__ x)
347a5b59 78 * (M_MLIT (M_LN2)
d5602ceb 79 - M_LOG (M_FABS (__imag__ x))));
f6d3a72e
PM
80 else
81 {
d5602ceb
PM
82 FLOAT i2 = 0;
83 if (M_FABS (__imag__ x) >= M_EPSILON * M_EPSILON)
f6d3a72e
PM
84 i2 = __imag__ x * __imag__ x;
85
d5602ceb 86 FLOAT num = 1 + __real__ x;
f6d3a72e
PM
87 num = i2 + num * num;
88
d5602ceb 89 FLOAT den = 1 - __real__ x;
f6d3a72e
PM
90 den = i2 + den * den;
91
d5602ceb
PM
92 FLOAT f = num / den;
93 if (f < M_LIT (0.5))
94 __real__ res = M_LIT (0.25) * M_LOG (f);
f6d3a72e
PM
95 else
96 {
d5602ceb
PM
97 num = 4 * __real__ x;
98 __real__ res = M_LIT (0.25) * M_LOG1P (num / den);
f6d3a72e
PM
99 }
100 }
101
d5602ceb 102 FLOAT absx, absy, den;
f6d3a72e 103
d5602ceb
PM
104 absx = M_FABS (__real__ x);
105 absy = M_FABS (__imag__ x);
f6d3a72e
PM
106 if (absx < absy)
107 {
d5602ceb 108 FLOAT t = absx;
f6d3a72e
PM
109 absx = absy;
110 absy = t;
111 }
112
d5602ceb 113 if (absy < M_EPSILON / 2)
f6d3a72e 114 {
d5602ceb
PM
115 den = (1 - absx) * (1 + absx);
116 if (den == 0)
117 den = 0;
f6d3a72e 118 }
d5602ceb
PM
119 else if (absx >= 1)
120 den = (1 - absx) * (1 + absx) - absy * absy;
121 else if (absx >= M_LIT (0.75) || absy >= M_LIT (0.5))
122 den = -M_SUF (__x2y2m1) (absx, absy);
f6d3a72e 123 else
d5602ceb 124 den = (1 - absx) * (1 + absx) - absy * absy;
f6d3a72e 125
d5602ceb 126 __imag__ res = M_LIT (0.5) * M_ATAN2 (2 * __imag__ x, den);
f6d3a72e
PM
127 }
128
129 math_check_force_underflow_complex (res);
130 }
131
132 return res;
133}
d5602ceb
PM
134
135declare_mgen_alias (__catanh, catanh)