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
25#include "config.h"
26#include "math.h"
27#include "tables.h"
28#include "../clcmacro.h"
29
30/*
31 compute pow using log and exp
32 x^y = exp(y * log(x))
33
34 we take care not to lose precision in the intermediate steps
35
36 When computing log, calculate it in splits,
37
38 r = f * (p_invead + p_inv_tail)
39 r = rh + rt
40
41 calculate log polynomial using r, in end addition, do
42 poly = poly + ((rh-r) + rt)
43
44 lth = -r
45 ltt = ((xexp * log2_t) - poly) + logT
46 lt = lth + ltt
47
48 lh = (xexp * log2_h) + logH
49 l = lh + lt
50
51 Calculate final log answer as gh and gt,
52 gh = l & higher-half bits
53 gt = (((ltt - (lt - lth)) + ((lh - l) + lt)) + (l - gh))
54
55 yh = y & higher-half bits
56 yt = y - yh
57
58 Before entering computation of exp,
59 vs = ((yt*gt + yt*gh) + yh*gt)
60 v = vs + yh*gh
61 vt = ((yh*gh - v) + vs)
62
63 In calculation of exp, add vt to r that is used for poly
64 At the end of exp, do
65 ((((expT * poly) + expT) + expH*poly) + expH)
66*/
67
68_CLC_DEF _CLC_OVERLOAD float __clc_pow(float x, float y)
69{
70
71    int ix = as_int(x);
72    int ax = ix & EXSIGNBIT_SP32;
73    int xpos = ix == ax;
74
75    int iy = as_int(y);
76    int ay = iy & EXSIGNBIT_SP32;
77    int ypos = iy == ay;
78
79    /* Extra precise log calculation
80     *  First handle case that x is close to 1
81     */
82    float r = 1.0f - as_float(ax);
83    int near1 = fabs(r) < 0x1.0p-4f;
84    float r2 = r*r;
85
86    /* Coefficients are just 1/3, 1/4, 1/5 and 1/6 */
87    float poly = mad(r,
88                     mad(r,
89                         mad(r,
90                             mad(r, 0x1.24924ap-3f, 0x1.555556p-3f),
91                             0x1.99999ap-3f),
92                         0x1.000000p-2f),
93                     0x1.555556p-2f);
94
95    poly *= r2*r;
96
97    float lth_near1 = -r2 * 0.5f;
98    float ltt_near1 = -poly;
99    float lt_near1 = lth_near1 + ltt_near1;
100    float lh_near1 = -r;
101    float l_near1 = lh_near1 + lt_near1;
102
103    /* Computations for x not near 1 */
104    int m = (int)(ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
105    float mf = (float)m;
106    int ixs = as_int(as_float(ax | 0x3f800000) - 1.0f);
107    float mfs = (float)((ixs >> EXPSHIFTBITS_SP32) - 253);
108    int c = m == -127;
109    int ixn = c ? ixs : ax;
110    float mfn = c ? mfs : mf;
111
112    int indx = (ixn & 0x007f0000) + ((ixn & 0x00008000) << 1);
113
114    /* F - Y */
115    float f = as_float(0x3f000000 | indx) - as_float(0x3f000000 | (ixn & MANTBITS_SP32));
116
117    indx = indx >> 16;
118    float2 tv = USE_TABLE(log_inv_tbl_ep, indx);
119    float rh = f * tv.s0;
120    float rt = f * tv.s1;
121    r = rh + rt;
122
123    poly = mad(r, mad(r, 0x1.0p-2f, 0x1.555556p-2f), 0x1.0p-1f) * (r*r);
124    poly += (rh - r) + rt;
125
126    const float LOG2_HEAD = 0x1.62e000p-1f;  /* 0.693115234 */
127    const float LOG2_TAIL = 0x1.0bfbe8p-15f; /* 0.0000319461833 */
128    tv = USE_TABLE(loge_tbl, indx);
129    float lth = -r;
130    float ltt = mad(mfn, LOG2_TAIL, -poly) + tv.s1;
131    float lt = lth + ltt;
132    float lh = mad(mfn, LOG2_HEAD, tv.s0);
133    float l = lh + lt;
134
135    /* Select near 1 or not */
136    lth = near1 ? lth_near1 : lth;
137    ltt = near1 ? ltt_near1 : ltt;
138    lt = near1 ? lt_near1 : lt;
139    lh = near1 ? lh_near1 : lh;
140    l = near1 ? l_near1 : l;
141
142    float gh = as_float(as_int(l) & 0xfffff000);
143    float gt = ((ltt - (lt - lth)) + ((lh - l) + lt)) + (l - gh);
144
145    float yh = as_float(iy & 0xfffff000);
146
147    float yt = y - yh;
148
149    float ylogx_s = mad(gt, yh, mad(gh, yt, yt*gt));
150    float ylogx = mad(yh, gh, ylogx_s);
151    float ylogx_t = mad(yh, gh, -ylogx) + ylogx_s;
152
153    /* Extra precise exp of ylogx */
154    const float R_64_BY_LOG2 = 0x1.715476p+6f; /* 64/log2 : 92.332482616893657 */
155    int n = convert_int(ylogx * R_64_BY_LOG2);
156    float nf = (float) n;
157
158    int j = n & 0x3f;
159    m = n >> 6;
160    int m2 = m << EXPSHIFTBITS_SP32;
161
162    const float R_LOG2_BY_64_LD = 0x1.620000p-7f;  /* log2/64 lead: 0.0108032227 */
163    const float R_LOG2_BY_64_TL = 0x1.c85fdep-16f; /* log2/64 tail: 0.0000272020388 */
164    r = mad(nf, -R_LOG2_BY_64_TL, mad(nf, -R_LOG2_BY_64_LD, ylogx)) + ylogx_t;
165
166    /* Truncated Taylor series for e^r */
167    poly = mad(mad(mad(r, 0x1.555556p-5f, 0x1.555556p-3f), r, 0x1.000000p-1f), r*r, r);
168
169    tv = USE_TABLE(exp_tbl_ep, j);
170
171    float expylogx = mad(tv.s0, poly, mad(tv.s1, poly, tv.s1)) + tv.s0;
172    float sexpylogx = expylogx * as_float(0x1 << (m + 149));
173    float texpylogx = as_float(as_int(expylogx) + m2);
174    expylogx = m < -125 ? sexpylogx : texpylogx;
175
176    /* Result is +-Inf if (ylogx + ylogx_t) > 128*log2 */
177    expylogx = (ylogx > 0x1.62e430p+6f) | (ylogx == 0x1.62e430p+6f & ylogx_t > -0x1.05c610p-22f) ? as_float(PINFBITPATT_SP32) : expylogx;
178
179    /* Result is 0 if ylogx < -149*log2 */
180    expylogx = ylogx <  -0x1.9d1da0p+6f ? 0.0f : expylogx;
181
182    /* Classify y:
183     *   inty = 0 means not an integer.
184     *   inty = 1 means odd integer.
185     *   inty = 2 means even integer.
186     */
187
188    int yexp = (int)(ay >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32 + 1;
189    int mask = (1 << (24 - yexp)) - 1;
190    int yodd = ((iy >> (24 - yexp)) & 0x1) != 0;
191    int inty = yodd ? 1 : 2;
192    inty = (iy & mask) != 0 ? 0 : inty;
193    inty = yexp < 1 ? 0 : inty;
194    inty = yexp > 24 ? 2 : inty;
195
196    float signval = as_float((as_uint(expylogx) ^ SIGNBIT_SP32));
197    expylogx = ((inty == 1) & !xpos) ? signval : expylogx;
198    int ret = as_int(expylogx);
199
200    /* Corner case handling */
201    ret = (!xpos & (inty == 0)) ? QNANBITPATT_SP32 : ret;
202    ret = ax < 0x3f800000 & iy == NINFBITPATT_SP32 ? PINFBITPATT_SP32 : ret;
203    ret = ax > 0x3f800000 & iy == NINFBITPATT_SP32 ? 0 : ret;
204    ret = ax < 0x3f800000 & iy == PINFBITPATT_SP32 ? 0 : ret;
205    ret = ax > 0x3f800000 & iy == PINFBITPATT_SP32 ? PINFBITPATT_SP32 : ret;
206    int xinf = xpos ? PINFBITPATT_SP32 : NINFBITPATT_SP32;
207    ret = ((ax == 0) & !ypos & (inty == 1)) ? xinf : ret;
208    ret = ((ax == 0) & !ypos & (inty != 1)) ? PINFBITPATT_SP32 : ret;
209    int xzero = xpos ? 0 : 0x80000000;
210    ret = ((ax == 0) & ypos & (inty == 1)) ? xzero : ret;
211    ret = ((ax == 0) & ypos & (inty != 1)) ? 0 : ret;
212    ret = ((ax == 0) & (iy == NINFBITPATT_SP32)) ? PINFBITPATT_SP32 : ret;
213    ret = ((ix == 0xbf800000) & (ay == PINFBITPATT_SP32)) ? 0x3f800000 : ret;
214    ret = ((ix == NINFBITPATT_SP32) & !ypos & (inty == 1)) ? 0x80000000 : ret;
215    ret = ((ix == NINFBITPATT_SP32) & !ypos & (inty != 1)) ? 0 : ret;
216    ret = ((ix == NINFBITPATT_SP32) & ypos & (inty == 1)) ? NINFBITPATT_SP32 : ret;
217    ret = ((ix == NINFBITPATT_SP32) & ypos & (inty != 1)) ? PINFBITPATT_SP32 : ret;
218    ret = ((ix == PINFBITPATT_SP32) & !ypos) ? 0 : ret;
219    ret = ((ix == PINFBITPATT_SP32) & ypos) ? PINFBITPATT_SP32 : ret;
220    ret = (ax > PINFBITPATT_SP32) ? ix : ret;
221    ret = (ay > PINFBITPATT_SP32) ? iy : ret;
222    ret = ay == 0 ? 0x3f800000 : ret;
223    ret = ix == 0x3f800000 ? 0x3f800000 : ret;
224
225    return as_float(ret);
226}
227_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_pow, float, float)
228
229#ifdef cl_khr_fp64
230_CLC_DEF _CLC_OVERLOAD double __clc_pow(double x, double y)
231{
232    const double real_log2_tail = 5.76999904754328540596e-08;
233    const double real_log2_lead = 6.93147122859954833984e-01;
234
235    long ux = as_long(x);
236    long ax = ux & (~SIGNBIT_DP64);
237    int xpos = ax == ux;
238
239    long uy = as_long(y);
240    long ay = uy & (~SIGNBIT_DP64);
241    int ypos = ay == uy;
242
243    // Extended precision log
244    double v, vt;
245    {
246        int exp = (int)(ax >> 52) - 1023;
247        int mask_exp_1023 = exp == -1023;
248        double xexp = (double) exp;
249        long mantissa = ax & 0x000FFFFFFFFFFFFFL;
250
251        long temp_ux = as_long(as_double(0x3ff0000000000000L | mantissa) - 1.0);
252        exp = ((temp_ux & 0x7FF0000000000000L) >> 52) - 2045;
253        double xexp1 = (double) exp;
254        long mantissa1 = temp_ux & 0x000FFFFFFFFFFFFFL;
255
256        xexp = mask_exp_1023 ? xexp1 : xexp;
257        mantissa = mask_exp_1023 ? mantissa1 : mantissa;
258
259        long rax = (mantissa & 0x000ff00000000000) + ((mantissa & 0x0000080000000000) << 1);
260        int index = rax >> 44;
261
262        double F = as_double(rax | 0x3FE0000000000000L);
263        double Y = as_double(mantissa | 0x3FE0000000000000L);
264        double f = F - Y;
265        double2 tv = USE_TABLE(log_f_inv_tbl, index);
266        double log_h = tv.s0;
267        double log_t = tv.s1;
268        double f_inv = (log_h + log_t) * f;
269        double r1 = as_double(as_long(f_inv) & 0xfffffffff8000000L);
270        double r2 = fma(-F, r1, f) * (log_h + log_t);
271        double r = r1 + r2;
272
273        double poly = fma(r,
274                          fma(r,
275                              fma(r,
276                                  fma(r, 1.0/7.0, 1.0/6.0),
277                                  1.0/5.0),
278                              1.0/4.0),
279                          1.0/3.0);
280        poly = poly * r * r * r;
281
282        double hr1r1 = 0.5*r1*r1;
283        double poly0h = r1 + hr1r1;
284        double poly0t = r1 - poly0h + hr1r1;
285        poly = fma(r1, r2, fma(0.5*r2, r2, poly)) + r2 + poly0t;
286
287        tv = USE_TABLE(powlog_tbl, index);
288        log_h = tv.s0;
289        log_t = tv.s1;
290
291        double resT_t = fma(xexp, real_log2_tail, + log_t) - poly;
292        double resT = resT_t - poly0h;
293        double resH = fma(xexp, real_log2_lead, log_h);
294        double resT_h = poly0h;
295
296        double H = resT + resH;
297        double H_h = as_double(as_long(H) & 0xfffffffff8000000L);
298        double T = (resH - H + resT) + (resT_t - (resT + resT_h)) + (H - H_h);
299        H = H_h;
300
301        double y_head = as_double(uy & 0xfffffffff8000000L);
302        double y_tail = y - y_head;
303
304        double temp = fma(y_tail, H, fma(y_head, T, y_tail*T));
305        v = fma(y_head, H, temp);
306        vt = fma(y_head, H, -v) + temp;
307    }
308
309    // Now calculate exp of (v,vt)
310
311    double expv;
312    {
313        const double max_exp_arg = 709.782712893384;
314        const double min_exp_arg = -745.1332191019411;
315        const double sixtyfour_by_lnof2 = 92.33248261689366;
316        const double lnof2_by_64_head = 0.010830424260348081;
317        const double lnof2_by_64_tail = -4.359010638708991e-10;
318
319        double temp = v * sixtyfour_by_lnof2;
320        int n = (int)temp;
321        double dn = (double)n;
322        int j = n & 0x0000003f;
323        int m = n >> 6;
324
325        double2 tv = USE_TABLE(two_to_jby64_ep_tbl, j);
326        double f1 = tv.s0;
327        double f2 = tv.s1;
328        double f = f1 + f2;
329
330        double r1 = fma(dn, -lnof2_by_64_head, v);
331        double r2 = dn * lnof2_by_64_tail;
332        double r = (r1 + r2) + vt;
333
334        double q = fma(r,
335                       fma(r,
336                           fma(r,
337                               fma(r, 1.38889490863777199667e-03, 8.33336798434219616221e-03),
338                               4.16666666662260795726e-02),
339                           1.66666666665260878863e-01),
340                       5.00000000000000008883e-01);
341        q = fma(r*r, q, r);
342
343        expv = fma(f, q, f2) + f1;
344	      expv = ldexp(expv, m);
345
346        expv = v > max_exp_arg ? as_double(0x7FF0000000000000L) : expv;
347        expv = v < min_exp_arg ? 0.0 : expv;
348    }
349
350    // See whether y is an integer.
351    // inty = 0 means not an integer.
352    // inty = 1 means odd integer.
353    // inty = 2 means even integer.
354
355    int inty;
356    {
357        int yexp = (int)(ay >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64 + 1;
358        inty = yexp < 1 ? 0 : 2;
359        inty = yexp > 53 ? 2 : inty;
360        long mask = (1L << (53 - yexp)) - 1L;
361        int inty1 = (((ay & ~mask) >> (53 - yexp)) & 1L) == 1L ? 1 : 2;
362        inty1 = (ay & mask) != 0 ? 0 : inty1;
363        inty = !(yexp < 1) & !(yexp > 53) ? inty1 : inty;
364    }
365
366    expv *= (inty == 1) & !xpos ? -1.0 : 1.0;
367
368    long ret = as_long(expv);
369
370    // Now all the edge cases
371    ret = !xpos & (inty == 0) ? QNANBITPATT_DP64 : ret;
372    ret = ax < 0x3ff0000000000000L & uy == NINFBITPATT_DP64 ? PINFBITPATT_DP64 : ret;
373    ret = ax > 0x3ff0000000000000L & uy == NINFBITPATT_DP64 ? 0L : ret;
374    ret = ax < 0x3ff0000000000000L & uy == PINFBITPATT_DP64 ? 0L : ret;
375    ret = ax > 0x3ff0000000000000L & uy == PINFBITPATT_DP64 ? PINFBITPATT_DP64 : ret;
376    long xinf = xpos ? PINFBITPATT_DP64 : NINFBITPATT_DP64;
377    ret = ((ax == 0L) & !ypos & (inty == 1)) ? xinf : ret;
378    ret = ((ax == 0L) & !ypos & (inty != 1)) ? PINFBITPATT_DP64 : ret;
379    long xzero = xpos ? 0L : 0x8000000000000000L;
380    ret = ((ax == 0L) & ypos & (inty == 1)) ? xzero : ret;
381    ret = ((ax == 0L) & ypos & (inty != 1)) ? 0L : ret;
382    ret = ((ax == 0L) & (uy == NINFBITPATT_DP64)) ? PINFBITPATT_DP64 : ret;
383    ret = ((ux == 0xbff0000000000000L) & (ay == PINFBITPATT_DP64)) ? 0x3ff0000000000000L : ret;
384    ret = ((ux == NINFBITPATT_DP64) & !ypos & (inty == 1)) ? 0x8000000000000000L : ret;
385    ret = ((ux == NINFBITPATT_DP64) & !ypos & (inty != 1)) ? 0L : ret;
386    ret = ((ux == NINFBITPATT_DP64) & ypos & (inty == 1)) ? NINFBITPATT_DP64 : ret;
387    ret = ((ux == NINFBITPATT_DP64) & ypos & (inty != 1)) ? PINFBITPATT_DP64 : ret;
388    ret = (ux == PINFBITPATT_DP64) & !ypos ? 0L : ret;
389    ret = (ux == PINFBITPATT_DP64) & ypos ? PINFBITPATT_DP64 : ret;
390    ret = ax > PINFBITPATT_DP64 ? ux : ret;
391    ret = ay > PINFBITPATT_DP64 ? uy : ret;
392    ret = ay == 0L ? 0x3ff0000000000000L : ret;
393    ret = ux == 0x3ff0000000000000L ? 0x3ff0000000000000L : ret;
394
395    return as_double(ret);
396}
397_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_pow, double, double)
398#endif
399