Tom Stellard | 44b6117 | 2015-07-24 18:07:12 +0000 | [diff] [blame] | 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 "math.h" |
| 24 | |
| 25 | /* |
| 26 | Algorithm: |
| 27 | |
| 28 | Based on: |
| 29 | Ping-Tak Peter Tang |
| 30 | "Table-driven implementation of the logarithm function in IEEE |
| 31 | floating-point arithmetic" |
| 32 | ACM Transactions on Mathematical Software (TOMS) |
| 33 | Volume 16, Issue 4 (December 1990) |
| 34 | |
| 35 | |
| 36 | x very close to 1.0 is handled differently, for x everywhere else |
| 37 | a brief explanation is given below |
| 38 | |
| 39 | x = (2^m)*A |
| 40 | x = (2^m)*(G+g) with (1 <= G < 2) and (g <= 2^(-8)) |
| 41 | x = (2^m)*2*(G/2+g/2) |
| 42 | x = (2^m)*2*(F+f) with (0.5 <= F < 1) and (f <= 2^(-9)) |
| 43 | |
| 44 | Y = (2^(-1))*(2^(-m))*(2^m)*A |
| 45 | Now, range of Y is: 0.5 <= Y < 1 |
| 46 | |
| 47 | F = 0x80 + (first 7 mantissa bits) + (8th mantissa bit) |
| 48 | Now, range of F is: 128 <= F <= 256 |
| 49 | F = F / 256 |
| 50 | Now, range of F is: 0.5 <= F <= 1 |
| 51 | |
| 52 | f = -(Y-F), with (f <= 2^(-9)) |
| 53 | |
| 54 | log(x) = m*log(2) + log(2) + log(F-f) |
| 55 | log(x) = m*log(2) + log(2) + log(F) + log(1-(f/F)) |
| 56 | log(x) = m*log(2) + log(2*F) + log(1-r) |
| 57 | |
| 58 | r = (f/F), with (r <= 2^(-8)) |
| 59 | r = f*(1/F) with (1/F) precomputed to avoid division |
| 60 | |
| 61 | log(x) = m*log(2) + log(G) - poly |
| 62 | |
| 63 | log(G) is precomputed |
| 64 | poly = (r + (r^2)/2 + (r^3)/3 + (r^4)/4) + (r^5)/5)) |
| 65 | |
| 66 | log(2) and log(G) need to be maintained in extra precision |
| 67 | to avoid losing precision in the calculations |
| 68 | |
| 69 | |
| 70 | For x close to 1.0, we employ the following technique to |
| 71 | ensure faster convergence. |
| 72 | |
| 73 | log(x) = log((1+s)/(1-s)) = 2*s + (2/3)*s^3 + (2/5)*s^5 + (2/7)*s^7 |
| 74 | x = ((1+s)/(1-s)) |
| 75 | x = 1 + r |
| 76 | s = r/(2+r) |
| 77 | |
| 78 | */ |
| 79 | |
| 80 | _CLC_OVERLOAD _CLC_DEF float |
| 81 | #if defined(COMPILING_LOG2) |
| 82 | log2(float x) |
| 83 | #elif defined(COMPILING_LOG10) |
| 84 | log10(float x) |
| 85 | #else |
| 86 | log(float x) |
| 87 | #endif |
| 88 | { |
| 89 | |
| 90 | #if defined(COMPILING_LOG2) |
| 91 | const float LOG2E = 0x1.715476p+0f; // 1.4426950408889634 |
| 92 | const float LOG2E_HEAD = 0x1.700000p+0f; // 1.4375 |
| 93 | const float LOG2E_TAIL = 0x1.547652p-8f; // 0.00519504072 |
| 94 | #elif defined(COMPILING_LOG10) |
| 95 | USE_TABLE(float2, p_log, LOG10_TBL); |
| 96 | const float LOG10E = 0x1.bcb7b2p-2f; // 0.43429448190325182 |
| 97 | const float LOG10E_HEAD = 0x1.bc0000p-2f; // 0.43359375 |
| 98 | const float LOG10E_TAIL = 0x1.6f62a4p-11f; // 0.0007007319 |
| 99 | const float LOG10_2_HEAD = 0x1.340000p-2f; // 0.30078125 |
| 100 | const float LOG10_2_TAIL = 0x1.04d426p-12f; // 0.000248745637 |
| 101 | #else |
| 102 | USE_TABLE(float2, p_log, LOGE_TBL); |
| 103 | const float LOG2_HEAD = 0x1.62e000p-1f; // 0.693115234 |
| 104 | const float LOG2_TAIL = 0x1.0bfbe8p-15f; // 0.0000319461833 |
| 105 | #endif |
| 106 | |
| 107 | uint xi = as_uint(x); |
| 108 | uint ax = xi & EXSIGNBIT_SP32; |
| 109 | |
| 110 | // Calculations for |x-1| < 2^-4 |
| 111 | float r = x - 1.0f; |
| 112 | int near1 = fabs(r) < 0x1.0p-4f; |
| 113 | float u2 = MATH_DIVIDE(r, 2.0f + r); |
| 114 | float corr = u2 * r; |
| 115 | float u = u2 + u2; |
| 116 | float v = u * u; |
| 117 | float znear1, z1, z2; |
| 118 | |
| 119 | // 2/(5 * 2^5), 2/(3 * 2^3) |
| 120 | z2 = mad(u, mad(v, 0x1.99999ap-7f, 0x1.555556p-4f)*v, -corr); |
| 121 | |
| 122 | #if defined(COMPILING_LOG2) |
| 123 | z1 = as_float(as_int(r) & 0xffff0000); |
| 124 | z2 = z2 + (r - z1); |
| 125 | znear1 = mad(z1, LOG2E_HEAD, mad(z2, LOG2E_HEAD, mad(z1, LOG2E_TAIL, z2*LOG2E_TAIL))); |
| 126 | #elif defined(COMPILING_LOG10) |
| 127 | z1 = as_float(as_int(r) & 0xffff0000); |
| 128 | z2 = z2 + (r - z1); |
| 129 | znear1 = mad(z1, LOG10E_HEAD, mad(z2, LOG10E_HEAD, mad(z1, LOG10E_TAIL, z2*LOG10E_TAIL))); |
| 130 | #else |
| 131 | znear1 = z2 + r; |
| 132 | #endif |
| 133 | |
| 134 | // Calculations for x not near 1 |
| 135 | int m = (int)(xi >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32; |
| 136 | |
| 137 | // Normalize subnormal |
| 138 | uint xis = as_uint(as_float(xi | 0x3f800000) - 1.0f); |
| 139 | int ms = (int)(xis >> EXPSHIFTBITS_SP32) - 253; |
| 140 | int c = m == -127; |
| 141 | m = c ? ms : m; |
| 142 | uint xin = c ? xis : xi; |
| 143 | |
| 144 | float mf = (float)m; |
| 145 | uint indx = (xin & 0x007f0000) + ((xin & 0x00008000) << 1); |
| 146 | |
| 147 | // F - Y |
| 148 | float f = as_float(0x3f000000 | indx) - as_float(0x3f000000 | (xin & MANTBITS_SP32)); |
| 149 | |
| 150 | indx = indx >> 16; |
| 151 | r = f * USE_TABLE(log_inv_tbl, indx); |
| 152 | |
| 153 | // 1/3, 1/2 |
| 154 | float poly = mad(mad(r, 0x1.555556p-2f, 0.5f), r*r, r); |
| 155 | |
| 156 | #if defined(COMPILING_LOG2) |
| 157 | float2 tv = USE_TABLE(log2_tbl, indx); |
| 158 | z1 = tv.s0 + mf; |
| 159 | z2 = mad(poly, -LOG2E, tv.s1); |
| 160 | #elif defined(COMPILING_LOG10) |
| 161 | float2 tv = p_log[indx]; |
| 162 | z1 = mad(mf, LOG10_2_HEAD, tv.s0); |
| 163 | z2 = mad(poly, -LOG10E, mf*LOG10_2_TAIL) + tv.s1; |
| 164 | #else |
| 165 | float2 tv = p_log[indx]; |
| 166 | z1 = mad(mf, LOG2_HEAD, tv.s0); |
| 167 | z2 = mad(mf, LOG2_TAIL, -poly) + tv.s1; |
| 168 | #endif |
| 169 | |
| 170 | float z = z1 + z2; |
| 171 | z = near1 ? znear1 : z; |
| 172 | |
| 173 | // Corner cases |
| 174 | z = ax >= PINFBITPATT_SP32 ? x : z; |
| 175 | z = xi != ax ? as_float(QNANBITPATT_SP32) : z; |
| 176 | z = ax == 0 ? as_float(NINFBITPATT_SP32) : z; |
| 177 | |
| 178 | return z; |
| 179 | } |
| 180 | |
| 181 | #ifdef cl_khr_fp64 |
| 182 | |
| 183 | _CLC_OVERLOAD _CLC_DEF double |
| 184 | #if defined(COMPILING_LOG2) |
| 185 | log2(double x) |
| 186 | #elif defined(COMPILING_LOG10) |
| 187 | log10(double x) |
| 188 | #else |
| 189 | log(double x) |
| 190 | #endif |
| 191 | { |
| 192 | |
| 193 | #ifndef COMPILING_LOG2 |
| 194 | // log2_lead and log2_tail sum to an extra-precise version of ln(2) |
| 195 | const double log2_lead = 6.93147122859954833984e-01; /* 0x3fe62e42e0000000 */ |
| 196 | const double log2_tail = 5.76999904754328540596e-08; /* 0x3e6efa39ef35793c */ |
| 197 | #endif |
| 198 | |
| 199 | #if defined(COMPILING_LOG10) |
| 200 | // log10e_lead and log10e_tail sum to an extra-precision version of log10(e) (19 bits in lead) |
| 201 | const double log10e_lead = 4.34293746948242187500e-01; /* 0x3fdbcb7800000000 */ |
| 202 | const double log10e_tail = 7.3495500964015109100644e-7; /* 0x3ea8a93728719535 */ |
| 203 | #elif defined(COMPILING_LOG2) |
| 204 | // log2e_lead and log2e_tail sum to an extra-precision version of log2(e) (19 bits in lead) |
| 205 | const double log2e_lead = 1.44269180297851562500E+00; /* 0x3FF7154400000000 */ |
| 206 | const double log2e_tail = 3.23791044778235969970E-06; /* 0x3ECB295C17F0BBBE */ |
| 207 | #endif |
| 208 | |
| 209 | // log_thresh1 = 9.39412117004394531250e-1 = 0x3fee0faa00000000 |
| 210 | // log_thresh2 = 1.06449508666992187500 = 0x3ff1082c00000000 |
| 211 | const double log_thresh1 = 0x1.e0faap-1; |
| 212 | const double log_thresh2 = 0x1.1082cp+0; |
| 213 | |
| 214 | int is_near = x >= log_thresh1 & x <= log_thresh2; |
| 215 | |
| 216 | // Near 1 code |
| 217 | double r = x - 1.0; |
| 218 | double u = r / (2.0 + r); |
| 219 | double correction = r * u; |
| 220 | u = u + u; |
| 221 | double v = u * u; |
| 222 | double r1 = r; |
| 223 | |
| 224 | const double ca_1 = 8.33333333333317923934e-02; /* 0x3fb55555555554e6 */ |
| 225 | const double ca_2 = 1.25000000037717509602e-02; /* 0x3f89999999bac6d4 */ |
| 226 | const double ca_3 = 2.23213998791944806202e-03; /* 0x3f62492307f1519f */ |
| 227 | const double ca_4 = 4.34887777707614552256e-04; /* 0x3f3c8034c85dfff0 */ |
| 228 | |
| 229 | double r2 = fma(u*v, fma(v, fma(v, fma(v, ca_4, ca_3), ca_2), ca_1), -correction); |
| 230 | |
| 231 | #if defined(COMPILING_LOG10) |
| 232 | r = r1; |
| 233 | r1 = as_double(as_ulong(r1) & 0xffffffff00000000); |
| 234 | r2 = r2 + (r - r1); |
| 235 | double ret_near = fma(log10e_lead, r1, fma(log10e_lead, r2, fma(log10e_tail, r1, log10e_tail * r2))); |
| 236 | #elif defined(COMPILING_LOG2) |
| 237 | r = r1; |
| 238 | r1 = as_double(as_ulong(r1) & 0xffffffff00000000); |
| 239 | r2 = r2 + (r - r1); |
| 240 | double ret_near = fma(log2e_lead, r1, fma(log2e_lead, r2, fma(log2e_tail, r1, log2e_tail*r2))); |
| 241 | #else |
| 242 | double ret_near = r1 + r2; |
| 243 | #endif |
| 244 | |
| 245 | // This is the far from 1 code |
| 246 | |
| 247 | // Deal with subnormal |
| 248 | ulong ux = as_ulong(x); |
| 249 | ulong uxs = as_ulong(as_double(0x03d0000000000000UL | ux) - 0x1.0p-962); |
| 250 | int c = ux < IMPBIT_DP64; |
| 251 | ux = c ? uxs : ux; |
| 252 | int expadjust = c ? 60 : 0; |
| 253 | |
| 254 | int xexp = ((as_int2(ux).hi >> 20) & 0x7ff) - EXPBIAS_DP64 - expadjust; |
| 255 | double f = as_double(HALFEXPBITS_DP64 | (ux & MANTBITS_DP64)); |
| 256 | int index = as_int2(ux).hi >> 13; |
| 257 | index = ((0x80 | (index & 0x7e)) >> 1) + (index & 0x1); |
| 258 | |
| 259 | double2 tv = USE_TABLE(ln_tbl, index - 64); |
| 260 | double z1 = tv.s0; |
| 261 | double q = tv.s1; |
| 262 | |
| 263 | double f1 = index * 0x1.0p-7; |
| 264 | double f2 = f - f1; |
| 265 | u = f2 / fma(f2, 0.5, f1); |
| 266 | v = u * u; |
| 267 | |
| 268 | const double cb_1 = 8.33333333333333593622e-02; /* 0x3fb5555555555557 */ |
| 269 | const double cb_2 = 1.24999999978138668903e-02; /* 0x3f89999999865ede */ |
| 270 | const double cb_3 = 2.23219810758559851206e-03; /* 0x3f6249423bd94741 */ |
| 271 | |
| 272 | double poly = v * fma(v, fma(v, cb_3, cb_2), cb_1); |
| 273 | double z2 = q + fma(u, poly, u); |
| 274 | |
| 275 | double dxexp = (double)xexp; |
| 276 | #if defined (COMPILING_LOG10) |
| 277 | // Add xexp * log(2) to z1,z2 to get log(x) |
| 278 | r1 = fma(dxexp, log2_lead, z1); |
| 279 | r2 = fma(dxexp, log2_tail, z2); |
| 280 | double ret_far = fma(log10e_lead, r1, fma(log10e_lead, r2, fma(log10e_tail, r1, log10e_tail*r2))); |
| 281 | #elif defined(COMPILING_LOG2) |
| 282 | r1 = fma(log2e_lead, z1, dxexp); |
| 283 | r2 = fma(log2e_lead, z2, fma(log2e_tail, z1, log2e_tail*z2)); |
| 284 | double ret_far = r1 + r2; |
| 285 | #else |
| 286 | r1 = fma(dxexp, log2_lead, z1); |
| 287 | r2 = fma(dxexp, log2_tail, z2); |
| 288 | double ret_far = r1 + r2; |
| 289 | #endif |
| 290 | |
| 291 | double ret = is_near ? ret_near : ret_far; |
| 292 | |
| 293 | ret = isinf(x) ? as_double(PINFBITPATT_DP64) : ret; |
| 294 | ret = isnan(x) | (x < 0.0) ? as_double(QNANBITPATT_DP64) : ret; |
| 295 | ret = x == 0.0 ? as_double(NINFBITPATT_DP64) : ret; |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | #endif // cl_khr_fp64 |