xref: /llvm-project/libclc/generic/lib/math/cosh.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#include <clc/math/tables.h>
27
28_CLC_OVERLOAD _CLC_DEF float cosh(float x) {
29
30    // After dealing with special cases the computation is split into regions as follows.
31    // abs(x) >= max_cosh_arg:
32    // cosh(x) = sign(x)*Inf
33    // abs(x) >= small_threshold:
34    // cosh(x) = sign(x)*exp(abs(x))/2 computed using the
35    // splitexp and scaleDouble functions as for exp_amd().
36    // abs(x) < small_threshold:
37    // compute p = exp(y) - 1 and then z = 0.5*(p+(p/(p+1.0)))
38    // cosh(x) is then z.
39
40    const float max_cosh_arg = 0x1.65a9fap+6f;
41    const float small_threshold = 0x1.0a2b24p+3f;
42
43    uint ux = as_uint(x);
44    uint aux = ux & EXSIGNBIT_SP32;
45    float y = as_float(aux);
46
47    // Find the integer part y0 of y and the increment dy = y - y0. We then compute
48    // z = sinh(y) = sinh(y0)cosh(dy) + cosh(y0)sinh(dy)
49    // z = cosh(y) = cosh(y0)cosh(dy) + sinh(y0)sinh(dy)
50    // where sinh(y0) and cosh(y0) are tabulated above.
51
52    int ind = (int)y;
53    ind = (uint)ind > 36U ? 0 : ind;
54
55    float dy = y - ind;
56    float dy2 = dy * dy;
57
58    float sdy = mad(dy2,
59                    mad(dy2,
60                        mad(dy2,
61                            mad(dy2,
62                                mad(dy2,
63                                    mad(dy2, 0.7746188980094184251527126e-12f, 0.160576793121939886190847e-9f),
64                                    0.250521176994133472333666e-7f),
65                                0.275573191913636406057211e-5f),
66                            0.198412698413242405162014e-3f),
67                        0.833333333333329931873097e-2f),
68                    0.166666666666666667013899e0f);
69    sdy = mad(sdy, dy*dy2, dy);
70
71    float cdy = mad(dy2,
72                    mad(dy2,
73                        mad(dy2,
74                            mad(dy2,
75                                mad(dy2,
76                                    mad(dy2, 0.1163921388172173692062032e-10f, 0.208744349831471353536305e-8f),
77                                    0.275573350756016588011357e-6f),
78                                0.248015872460622433115785e-4f),
79                            0.138888888889814854814536e-2f),
80                        0.416666666666660876512776e-1f),
81                    0.500000000000000005911074e0f);
82    cdy = mad(cdy, dy2, 1.0f);
83
84    float2 tv = USE_TABLE(sinhcosh_tbl, ind);
85    float z = mad(tv.s0, sdy, tv.s1 * cdy);
86
87    // When exp(-x) is insignificant compared to exp(x), return exp(x)/2
88    float t = exp(y - 0x1.62e500p-1f);
89    float zsmall = mad(0x1.a0210ep-18f, t, t);
90    z = y >= small_threshold ? zsmall : z;
91
92    // Corner cases
93    z = y >= max_cosh_arg ? as_float(PINFBITPATT_SP32) : z;
94    z = aux > PINFBITPATT_SP32 ? as_float(QNANBITPATT_SP32) : z;
95    z = aux < 0x38800000 ? 1.0f : z;
96
97    return z;
98}
99
100_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, cosh, float);
101
102#ifdef cl_khr_fp64
103#pragma OPENCL EXTENSION cl_khr_fp64 : enable
104
105_CLC_OVERLOAD _CLC_DEF double cosh(double x) {
106
107    // After dealing with special cases the computation is split into
108    // regions as follows:
109    //
110    // abs(x) >= max_cosh_arg:
111    // cosh(x) = sign(x)*Inf
112    //
113    // abs(x) >= small_threshold:
114    // cosh(x) = sign(x)*exp(abs(x))/2 computed using the
115    // splitexp and scaleDouble functions as for exp_amd().
116    //
117    // abs(x) < small_threshold:
118    // compute p = exp(y) - 1 and then z = 0.5*(p+(p/(p+1.0)))
119    // cosh(x) is then sign(x)*z.
120
121    // This is ln(2^1025)
122    const double max_cosh_arg = 7.10475860073943977113e+02;      // 0x408633ce8fb9f87e
123
124    // This is where exp(-x) is insignificant compared to exp(x) = ln(2^27)
125    const double small_threshold = 0x1.2b708872320e2p+4;
126
127    double y = fabs(x);
128
129    // In this range we find the integer part y0 of y
130    // and the increment dy = y - y0. We then compute
131    // z = cosh(y) = cosh(y0)cosh(dy) + sinh(y0)sinh(dy)
132    // where sinh(y0) and cosh(y0) are tabulated above.
133
134    int ind = min((int)y, 36);
135    double dy = y - ind;
136    double dy2 = dy * dy;
137
138    double sdy = dy * dy2 *
139	         fma(dy2,
140		     fma(dy2,
141			 fma(dy2,
142			     fma(dy2,
143				 fma(dy2,
144				     fma(dy2, 0.7746188980094184251527126e-12, 0.160576793121939886190847e-9),
145				     0.250521176994133472333666e-7),
146				 0.275573191913636406057211e-5),
147			     0.198412698413242405162014e-3),
148			 0.833333333333329931873097e-2),
149		     0.166666666666666667013899e0);
150
151    double cdy = dy2 * fma(dy2,
152	                   fma(dy2,
153			       fma(dy2,
154				   fma(dy2,
155				       fma(dy2,
156					   fma(dy2, 0.1163921388172173692062032e-10, 0.208744349831471353536305e-8),
157					   0.275573350756016588011357e-6),
158				       0.248015872460622433115785e-4),
159				   0.138888888889814854814536e-2),
160			       0.416666666666660876512776e-1),
161			   0.500000000000000005911074e0);
162
163    // At this point sinh(dy) is approximated by dy + sdy,
164    // and cosh(dy) is approximated by 1 + cdy.
165    double2 tv = USE_TABLE(cosh_tbl, ind);
166    double cl = tv.s0;
167    double ct = tv.s1;
168    tv = USE_TABLE(sinh_tbl, ind);
169    double sl = tv.s0;
170    double st = tv.s1;
171
172    double z = fma(sl, dy, fma(sl, sdy, fma(cl, cdy, fma(st, dy, fma(st, sdy, ct*cdy)) + ct))) + cl;
173
174    // Other cases
175    z = y < 0x1.0p-28 ? 1.0 : z;
176
177    double t = exp(y - 0x1.62e42fefa3800p-1);
178    t =  fma(t, -0x1.ef35793c76641p-45, t);
179    z = y >= small_threshold ? t : z;
180
181    z = y >= max_cosh_arg ? as_double(PINFBITPATT_DP64) : z;
182
183    z = isinf(x) | isnan(x) ? y : z;
184
185    return z;
186
187}
188
189_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, cosh, double)
190
191#endif
192
193#ifdef cl_khr_fp16
194
195#pragma OPENCL EXTENSION cl_khr_fp16 : enable
196
197_CLC_DEFINE_UNARY_BUILTIN_FP16(cosh)
198
199#endif
200