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#include <clc/math/tables.h> 27 28_CLC_OVERLOAD _CLC_DEF float sinh(float x) 29{ 30 // After dealing with special cases the computation is split into regions as follows. 31 // abs(x) >= max_sinh_arg: 32 // sinh(x) = sign(x)*Inf 33 // abs(x) >= small_threshold: 34 // sinh(x) = sign(x)*exp(abs(x))/2 computed using the splitexp and scaleDouble functions as for exp_amd(). 35 // abs(x) < small_threshold: 36 // compute p = exp(y) - 1 and then z = 0.5*(p+(p/(p+1.0))) 37 // sinh(x) is then sign(x)*z. 38 39 const float max_sinh_arg = 0x1.65a9fap+6f; 40 const float small_threshold = 0x1.0a2b24p+3f; 41 42 uint ux = as_uint(x); 43 uint aux = ux & EXSIGNBIT_SP32; 44 uint xs = ux ^ aux; 45 float y = as_float(aux); 46 47 // We 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 // where sinh(y0) and cosh(y0) are tabulated above. 50 int ind = (int) y; 51 ind = (uint)ind > 36U ? 0 : ind; 52 53 float dy = y - ind; 54 float dy2 = dy * dy; 55 56 float sdy = mad(dy2, 57 mad(dy2, 58 mad(dy2, 59 mad(dy2, 60 mad(dy2, 61 mad(dy2, 0.7746188980094184251527126e-12f, 0.160576793121939886190847e-9f), 62 0.250521176994133472333666e-7f), 63 0.275573191913636406057211e-5f), 64 0.198412698413242405162014e-3f), 65 0.833333333333329931873097e-2f), 66 0.166666666666666667013899e0f); 67 sdy = mad(sdy, dy*dy2, dy); 68 69 float cdy = mad(dy2, 70 mad(dy2, 71 mad(dy2, 72 mad(dy2, 73 mad(dy2, 74 mad(dy2, 0.1163921388172173692062032e-10f, 0.208744349831471353536305e-8f), 75 0.275573350756016588011357e-6f), 76 0.248015872460622433115785e-4f), 77 0.138888888889814854814536e-2f), 78 0.416666666666660876512776e-1f), 79 0.500000000000000005911074e0f); 80 cdy = mad(cdy, dy2, 1.0f); 81 82 float2 tv = USE_TABLE(sinhcosh_tbl, ind); 83 float z = mad(tv.s1, sdy, tv.s0 * cdy); 84 z = as_float(xs | as_uint(z)); 85 86 // When y is large enough so that the negative exponential is negligible, 87 // so sinh(y) is approximated by sign(x)*exp(y)/2. 88 float t = exp(y - 0x1.62e500p-1f); 89 float zsmall = mad(0x1.a0210ep-18f, t, t); 90 zsmall = as_float(xs | as_uint(zsmall)); 91 z = y >= small_threshold ? zsmall : z; 92 93 // Corner cases 94 float zinf = as_float(PINFBITPATT_SP32 | xs); 95 z = y >= max_sinh_arg ? zinf : z; 96 z = aux > PINFBITPATT_SP32 | aux < 0x38800000U ? x : z; 97 98 return z; 99} 100 101_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, sinh, float); 102 103#ifdef cl_khr_fp64 104#pragma OPENCL EXTENSION cl_khr_fp64 : enable 105 106_CLC_OVERLOAD _CLC_DEF double sinh(double x) 107{ 108 // After dealing with special cases the computation is split into 109 // regions as follows: 110 // 111 // abs(x) >= max_sinh_arg: 112 // sinh(x) = sign(x)*Inf 113 // 114 // abs(x) >= small_threshold: 115 // sinh(x) = sign(x)*exp(abs(x))/2 computed using the 116 // splitexp and scaleDouble functions as for exp_amd(). 117 // 118 // abs(x) < small_threshold: 119 // compute p = exp(y) - 1 and then z = 0.5*(p+(p/(p+1.0))) 120 // sinh(x) is then sign(x)*z. 121 122 const double max_sinh_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 = sinh(y) = sinh(y0)cosh(dy) + cosh(y0)sinh(dy) 132 // where sinh(y0) and cosh(y0) are obtained from tables 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 // Shift some significant bits from dy to sdy. 165 double sdy1 = as_double(as_ulong(dy) & 0xfffffffff8000000UL); 166 double sdy2 = sdy + (dy - sdy1); 167 168 double2 tv = USE_TABLE(cosh_tbl, ind); 169 double cl = tv.s0; 170 double ct = tv.s1; 171 tv = USE_TABLE(sinh_tbl, ind); 172 double sl = tv.s0; 173 double st = tv.s1; 174 175 double z = fma(cl, sdy1, fma(sl, cdy, fma(cl, sdy2, fma(ct, sdy1, fma(st, cdy, ct*sdy2)) + st))) + sl; 176 177 // Other cases 178 z = (y < 0x1.0p-28) | isnan(x) | isinf(x) ? y : z; 179 180 double t = exp(y - 0x1.62e42fefa3800p-1); 181 t = fma(t, -0x1.ef35793c76641p-45, t); 182 z = y >= small_threshold ? t : z; 183 z = y >= max_sinh_arg ? as_double(PINFBITPATT_DP64) : z; 184 185 return copysign(z, x); 186} 187 188_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sinh, double) 189 190#endif 191 192#ifdef cl_khr_fp16 193 194#pragma OPENCL EXTENSION cl_khr_fp16 : enable 195 196_CLC_DEFINE_UNARY_BUILTIN_FP16(sinh) 197 198#endif 199