xref: /llvm-project/libclc/generic/lib/math/atanh.cl (revision 78b5bb702fe97fe85f66d72598d0dfa7c49fe001)
1/*
2 * Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include <clc/clc.h>
24#include <clc/clcmacro.h>
25#include <clc/math/math.h>
26
27_CLC_OVERLOAD _CLC_DEF float atanh(float x) {
28    uint ux = as_uint(x);
29    uint ax = ux & EXSIGNBIT_SP32;
30    uint xs = ux ^ ax;
31
32    // |x| > 1 or NaN
33    float z = as_float(QNANBITPATT_SP32);
34
35    // |x| == 1
36    float t = as_float(xs | PINFBITPATT_SP32);
37    z = ax == 0x3f800000U ? t : z;
38
39    // 1/2 <= |x| < 1
40    t = as_float(ax);
41    t = MATH_DIVIDE(2.0f*t, 1.0f - t);
42    t = 0.5f * log1p(t);
43    t = as_float(xs | as_uint(t));
44    z = ax < 0x3f800000U ? t : z;
45
46    // |x| < 1/2
47    t = x * x;
48    float a = mad(mad(0.92834212715e-2f, t, -0.28120347286e0f), t, 0.39453629046e0f);
49    float b = mad(mad(0.45281890445e0f, t, -0.15537744551e1f), t, 0.11836088638e1f);
50    float p = MATH_DIVIDE(a, b);
51    t = mad(x*t, p, x);
52    z = ax < 0x3f000000 ? t : z;
53
54    // |x| < 2^-13
55    z = ax < 0x39000000U ? x : z;
56
57    return z;
58}
59
60_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, atanh, float)
61
62#ifdef cl_khr_fp64
63#pragma OPENCL EXTENSION cl_khr_fp64 : enable
64
65_CLC_OVERLOAD _CLC_DEF double atanh(double x) {
66    double absx = fabs(x);
67
68    double ret = absx == 1.0 ? as_double(PINFBITPATT_DP64) : as_double(QNANBITPATT_DP64);
69
70    // |x| >= 0.5
71    // Note that atanh(x) = 0.5 * ln((1+x)/(1-x))
72    // For greater accuracy we use
73    // ln((1+x)/(1-x)) = ln(1 + 2x/(1-x)) = log1p(2x/(1-x)).
74    double r = 0.5 * log1p(2.0 * absx / (1.0 - absx));
75    ret = absx < 1.0 ? r : ret;
76
77    r = -ret;
78    ret = x < 0.0 ? r : ret;
79
80    // Arguments up to 0.5 in magnitude are
81    // approximated by a [5,5] minimax polynomial
82    double t = x * x;
83
84    double pn = fma(t,
85                    fma(t,
86                        fma(t,
87                            fma(t,
88                                fma(t, -0.10468158892753136958e-3, 0.28728638600548514553e-1),
89                                -0.28180210961780814148e0),
90                            0.88468142536501647470e0),
91                        -0.11028356797846341457e1),
92                    0.47482573589747356373e0);
93
94    double pd = fma(t,
95                    fma(t,
96                        fma(t,
97                            fma(t,
98                                fma(t, -0.35861554370169537512e-1, 0.49561196555503101989e0),
99                                -0.22608883748988489342e1),
100                            0.45414700626084508355e1),
101                        -0.41631933639693546274e1),
102                    0.14244772076924206909e1);
103
104    r = fma(x*t, pn/pd, x);
105    ret = absx < 0.5 ? r : ret;
106
107    return ret;
108}
109
110_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atanh, double)
111
112#endif
113
114#ifdef cl_khr_fp16
115
116#pragma OPENCL EXTENSION cl_khr_fp16 : enable
117
118_CLC_DEFINE_UNARY_BUILTIN_FP16(atanh)
119
120#endif
121