Warning: This file is not a C or C++ file. It does not have highlighting.

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

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libclc/clc/lib/generic/math/clc_log_base.h