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 log1p(float x) 29{ 30 float w = x; 31 uint ux = as_uint(x); 32 uint ax = ux & EXSIGNBIT_SP32; 33 34 // |x| < 2^-4 35 float u2 = MATH_DIVIDE(x, 2.0f + x); 36 float u = u2 + u2; 37 float v = u * u; 38 // 2/(5 * 2^5), 2/(3 * 2^3) 39 float zsmall = mad(-u2, x, mad(v, 0x1.99999ap-7f, 0x1.555556p-4f) * v * u) + x; 40 41 // |x| >= 2^-4 42 ux = as_uint(x + 1.0f); 43 44 int m = (int)((ux >> EXPSHIFTBITS_SP32) & 0xff) - EXPBIAS_SP32; 45 float mf = (float)m; 46 uint indx = (ux & 0x007f0000) + ((ux & 0x00008000) << 1); 47 float F = as_float(indx | 0x3f000000); 48 49 // x > 2^24 50 float fg24 = F - as_float(0x3f000000 | (ux & MANTBITS_SP32)); 51 52 // x <= 2^24 53 uint xhi = ux & 0xffff8000; 54 float xh = as_float(xhi); 55 float xt = (1.0f - xh) + w; 56 uint xnm = ((~(xhi & 0x7f800000)) - 0x00800000) & 0x7f800000; 57 xt = xt * as_float(xnm) * 0.5f; 58 float fl24 = F - as_float(0x3f000000 | (xhi & MANTBITS_SP32)) - xt; 59 60 float f = mf > 24.0f ? fg24 : fl24; 61 62 indx = indx >> 16; 63 float r = f * USE_TABLE(log_inv_tbl, indx); 64 65 // 1/3, 1/2 66 float poly = mad(mad(r, 0x1.555556p-2f, 0x1.0p-1f), r*r, r); 67 68 const float LOG2_HEAD = 0x1.62e000p-1f; // 0.693115234 69 const float LOG2_TAIL = 0x1.0bfbe8p-15f; // 0.0000319461833 70 71 float2 tv = USE_TABLE(loge_tbl, indx); 72 float z1 = mad(mf, LOG2_HEAD, tv.s0); 73 float z2 = mad(mf, LOG2_TAIL, -poly) + tv.s1; 74 float z = z1 + z2; 75 76 z = ax < 0x3d800000U ? zsmall : z; 77 78 79 80 // Edge cases 81 z = ax >= PINFBITPATT_SP32 ? w : z; 82 z = w < -1.0f ? as_float(QNANBITPATT_SP32) : z; 83 z = w == -1.0f ? as_float(NINFBITPATT_SP32) : z; 84 //fix subnormals 85 z = ax < 0x33800000 ? x : z; 86 87 return z; 88} 89 90_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, log1p, float); 91 92#ifdef cl_khr_fp64 93 94#pragma OPENCL EXTENSION cl_khr_fp64 : enable 95 96_CLC_OVERLOAD _CLC_DEF double log1p(double x) 97{ 98 // Computes natural log(1+x). Algorithm based on: 99 // Ping-Tak Peter Tang 100 // "Table-driven implementation of the logarithm function in IEEE 101 // floating-point arithmetic" 102 // ACM Transactions on Mathematical Software (TOMS) 103 // Volume 16, Issue 4 (December 1990) 104 // Note that we use a lookup table of size 64 rather than 128, 105 // and compensate by having extra terms in the minimax polynomial 106 // for the kernel approximation. 107 108 // Process Inside the threshold now 109 ulong ux = as_ulong(1.0 + x); 110 int xexp = ((as_int2(ux).hi >> 20) & 0x7ff) - EXPBIAS_DP64; 111 double f = as_double(ONEEXPBITS_DP64 | (ux & MANTBITS_DP64)); 112 113 int j = as_int2(ux).hi >> 13; 114 j = ((0x80 | (j & 0x7e)) >> 1) + (j & 0x1); 115 double f1 = (double)j * 0x1.0p-6; 116 j -= 64; 117 118 double f2temp = f - f1; 119 double m2 = as_double(convert_ulong(0x3ff - xexp) << EXPSHIFTBITS_DP64); 120 double f2l = fma(m2, x, m2 - f1); 121 double f2g = fma(m2, x, -f1) + m2; 122 double f2 = xexp <= MANTLENGTH_DP64-1 ? f2l : f2g; 123 f2 = (xexp <= -2) | (xexp >= MANTLENGTH_DP64+8) ? f2temp : f2; 124 125 double2 tv = USE_TABLE(ln_tbl, j); 126 double z1 = tv.s0; 127 double q = tv.s1; 128 129 double u = MATH_DIVIDE(f2, fma(0.5, f2, f1)); 130 double v = u * u; 131 132 double poly = v * fma(v, 133 fma(v, 2.23219810758559851206e-03, 1.24999999978138668903e-02), 134 8.33333333333333593622e-02); 135 136 // log2_lead and log2_tail sum to an extra-precise version of log(2) 137 const double log2_lead = 6.93147122859954833984e-01; /* 0x3fe62e42e0000000 */ 138 const double log2_tail = 5.76999904754328540596e-08; /* 0x3e6efa39ef35793c */ 139 140 double z2 = q + fma(u, poly, u); 141 double dxexp = (double)xexp; 142 double r1 = fma(dxexp, log2_lead, z1); 143 double r2 = fma(dxexp, log2_tail, z2); 144 double result1 = r1 + r2; 145 146 // Process Outside the threshold now 147 double r = x; 148 u = r / (2.0 + r); 149 double correction = r * u; 150 u = u + u; 151 v = u * u; 152 r1 = r; 153 154 poly = fma(v, 155 fma(v, 156 fma(v, 4.34887777707614552256e-04, 2.23213998791944806202e-03), 157 1.25000000037717509602e-02), 158 8.33333333333317923934e-02); 159 160 r2 = fma(u*v, poly, -correction); 161 162 // The values exp(-1/16)-1 and exp(1/16)-1 163 const double log1p_thresh1 = -0x1.f0540438fd5c3p-5; 164 const double log1p_thresh2 = 0x1.082b577d34ed8p-4; 165 double result2 = r1 + r2; 166 result2 = x < log1p_thresh1 | x > log1p_thresh2 ? result1 : result2; 167 168 result2 = isinf(x) ? x : result2; 169 result2 = x < -1.0 ? as_double(QNANBITPATT_DP64) : result2; 170 result2 = x == -1.0 ? as_double(NINFBITPATT_DP64) : result2; 171 return result2; 172} 173 174_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, log1p, double); 175 176#endif // cl_khr_fp64 177 178#ifdef cl_khr_fp16 179 180#pragma OPENCL EXTENSION cl_khr_fp16 : enable 181 182_CLC_DEFINE_UNARY_BUILTIN_FP16(log1p) 183 184#endif 185