xref: /llvm-project/libclc/generic/lib/math/tanh.cl (revision 78b5bb702fe97fe85f66d72598d0dfa7c49fe001)
1/*
2 * Copyright (c) 2014 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 tanh(float x)
28{
29    // The definition of tanh(x) is sinh(x)/cosh(x), which is also equivalent
30    // to the following three formulae:
31    // 1.  (exp(x) - exp(-x))/(exp(x) + exp(-x))
32    // 2.  (1 - (2/(exp(2*x) + 1 )))
33    // 3.  (exp(2*x) - 1)/(exp(2*x) + 1)
34    // but computationally, some formulae are better on some ranges.
35
36    const float large_threshold = 0x1.0a2b24p+3f;
37
38    uint ux = as_uint(x);
39    uint aux = ux & EXSIGNBIT_SP32;
40    uint xs = ux ^ aux;
41
42    float y = as_float(aux);
43    float y2 = y*y;
44
45    float a1 = mad(y2,
46                   mad(y2, 0.4891631088530669873e-4F, -0.14628356048797849e-2F),
47                   -0.28192806108402678e0F);
48    float b1 = mad(y2, 0.3427017942262751343e0F, 0.845784192581041099e0F);
49
50    float a2 = mad(y2,
51                   mad(y2, 0.3827534993599483396e-4F, -0.12325644183611929e-2F),
52                   -0.24069858695196524e0F);
53    float b2 = mad(y2, 0.292529068698052819e0F, 0.72209738473684982e0F);
54
55    int c = y < 0.9f;
56    float a = c ? a1 : a2;
57    float b = c ? b1 : b2;
58    float zlo = mad(MATH_DIVIDE(a, b), y*y2, y);
59
60    float p = exp(2.0f * y) + 1.0f;
61    float zhi = 1.0F - MATH_DIVIDE(2.0F, p);
62
63    float z = y <= 1.0f ? zlo : zhi;
64    z = as_float(xs | as_uint(z));
65
66    // Edge cases
67    float sone = as_float(0x3f800000U | xs);
68    z = y > large_threshold ? sone : z;
69    z = aux < 0x39000000 | aux > 0x7f800000 ? x : z;
70
71    return z;
72}
73
74_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, tanh, float);
75
76#ifdef cl_khr_fp64
77
78#pragma OPENCL EXTENSION cl_khr_fp64 : enable
79
80_CLC_OVERLOAD _CLC_DEF double tanh(double x)
81{
82    // The definition of tanh(x) is sinh(x)/cosh(x), which is also equivalent
83    // to the following three formulae:
84    // 1.  (exp(x) - exp(-x))/(exp(x) + exp(-x))
85    // 2.  (1 - (2/(exp(2*x) + 1 )))
86    // 3.  (exp(2*x) - 1)/(exp(2*x) + 1)
87    // but computationally, some formulae are better on some ranges.
88
89    // The point at which e^-x is insignificant compared to e^x = ln(2^27)
90    const double large_threshold = 0x1.2b708872320e2p+4;
91
92    ulong ux = as_ulong(x);
93    ulong ax = ux & ~SIGNBIT_DP64;
94    ulong sx = ux ^ ax;
95    double y = as_double(ax);
96    double y2 = y * y;
97
98    // y < 0.9
99    double znl = fma(y2,
100                     fma(y2,
101                         fma(y2, -0.142077926378834722618091e-7, -0.200047621071909498730453e-3),
102                         -0.176016349003044679402273e-1),
103                     -0.274030424656179760118928e0);
104
105    double zdl = fma(y2,
106                     fma(y2,
107                         fma(y2, 0.2091140262529164482568557e-3, 0.201562166026937652780575e-1),
108                         0.381641414288328849317962e0),
109                     0.822091273968539282568011e0);
110
111    // 0.9 <= y <= 1
112    double znm = fma(y2,
113                     fma(y2,
114                         fma(y2, -0.115475878996143396378318e-7, -0.165597043903549960486816e-3),
115                         -0.146173047288731678404066e-1),
116                     -0.227793870659088295252442e0);
117
118    double zdm = fma(y2,
119                     fma(y2,
120                         fma(y2, 0.173076050126225961768710e-3, 0.167358775461896562588695e-1),
121                         0.317204558977294374244770e0),
122                     0.683381611977295894959554e0);
123
124    int c = y < 0.9;
125    double zn = c ? znl : znm;
126    double zd = c ? zdl : zdm;
127    double z = y + y*y2 * MATH_DIVIDE(zn, zd);
128
129    // y > 1
130    double p = exp(2.0 * y) + 1.0;
131    double zg = 1.0 - 2.0 / p;
132
133    z = y > 1.0 ? zg : z;
134
135    // Other cases
136    z = y < 0x1.0p-28 | ax > PINFBITPATT_DP64 ? x : z;
137
138    z = y > large_threshold ? 1.0 : z;
139
140    return as_double(sx | as_ulong(z));
141}
142
143_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, tanh, double);
144
145#endif // cl_khr_fp64
146
147#ifdef cl_khr_fp16
148
149#pragma OPENCL EXTENSION cl_khr_fp16 : enable
150
151_CLC_DEFINE_UNARY_BUILTIN_FP16(tanh)
152
153#endif
154