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 <clc/clc.h>
24
25#include "math.h"
26#include "tables.h"
27#include "../clcmacro.h"
28
29_CLC_OVERLOAD _CLC_DEF  float atan2pi(float y, float x) {
30    const float pi = 0x1.921fb6p+1f;
31
32    float ax = fabs(x);
33    float ay = fabs(y);
34    float v = min(ax, ay);
35    float u = max(ax, ay);
36
37    // Scale since u could be large, as in "regular" divide
38    float s = u > 0x1.0p+96f ? 0x1.0p-32f : 1.0f;
39    float vbyu = s * MATH_DIVIDE(v, s*u);
40
41    float vbyu2 = vbyu * vbyu;
42
43    float p = mad(vbyu2, mad(vbyu2, -0x1.7e1f78p-9f, -0x1.7d1b98p-3f), -0x1.5554d0p-2f) * vbyu2 * vbyu;
44    float q = mad(vbyu2, mad(vbyu2, 0x1.1a714cp-2f, 0x1.287c56p+0f), 1.0f);
45
46    // Octant 0 result
47    float a = MATH_DIVIDE(mad(p, MATH_RECIP(q), vbyu), pi);
48
49    // Fix up 3 other octants
50    float at = 0.5f - a;
51    a = ay > ax ? at : a;
52    at = 1.0f - a;
53    a = x < 0.0F ? at : a;
54
55    // y == 0 => 0 for x >= 0, pi for x < 0
56    at = as_int(x) < 0 ? 1.0f : 0.0f;
57    a = y == 0.0f ? at : a;
58
59    // if (!FINITE_ONLY()) {
60        // x and y are +- Inf
61        at = x > 0.0f ? 0.25f : 0.75f;
62        a = ax == INFINITY & ay == INFINITY ? at : a;
63
64	// x or y is NaN
65	a = isnan(x) | isnan(y) ? as_float(QNANBITPATT_SP32) : a;
66    // }
67
68    // Fixup sign and return
69    return copysign(a, y);
70}
71
72_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, atan2pi, float, float)
73
74#ifdef cl_khr_fp64
75#pragma OPENCL EXTENSION cl_khr_fp64 : enable
76
77_CLC_OVERLOAD _CLC_DEF double atan2pi(double y, double x) {
78    const double pi = 3.1415926535897932e+00;          /* 0x400921fb54442d18 */
79    const double pi_head = 3.1415926218032836e+00;     /* 0x400921fb50000000 */
80    const double pi_tail = 3.1786509547056392e-08;     /* 0x3e6110b4611a6263 */
81    const double piby2_head = 1.5707963267948965e+00;  /* 0x3ff921fb54442d18 */
82    const double piby2_tail = 6.1232339957367660e-17;  /* 0x3c91a62633145c07 */
83
84    double x2 = x;
85    int xneg = as_int2(x).hi < 0;
86    int xexp = (as_int2(x).hi >> 20) & 0x7ff;
87
88    double y2 = y;
89    int yneg = as_int2(y).hi < 0;
90    int yexp = (as_int2(y).hi >> 20) & 0x7ff;
91
92    int cond2 = (xexp < 1021) & (yexp < 1021);
93    int diffexp = yexp - xexp;
94
95    // Scale up both x and y if they are both below 1/4
96    double x1 = ldexp(x, 1024);
97    int xexp1 = (as_int2(x1).hi >> 20) & 0x7ff;
98    double y1 = ldexp(y, 1024);
99    int yexp1 = (as_int2(y1).hi >> 20) & 0x7ff;
100    int diffexp1 = yexp1 - xexp1;
101
102    diffexp = cond2 ? diffexp1 : diffexp;
103    x = cond2 ? x1 : x;
104    y = cond2 ? y1 : y;
105
106    // General case: take absolute values of arguments
107    double u = fabs(x);
108    double v = fabs(y);
109
110    // Swap u and v if necessary to obtain 0 < v < u. Compute v/u.
111    int swap_vu = u < v;
112    double uu = u;
113    u = swap_vu ? v : u;
114    v = swap_vu ? uu : v;
115
116    double vbyu = v / u;
117    double q1, q2;
118
119    // General values of v/u. Use a look-up table and series expansion.
120
121    {
122        double val = vbyu > 0.0625 ? vbyu : 0.063;
123        int index = convert_int(fma(256.0, val, 0.5));
124	double2 tv = USE_TABLE(atan_jby256_tbl, (index - 16));
125	q1 = tv.s0;
126	q2 = tv.s1;
127        double c = (double)index * 0x1.0p-8;
128
129        // We're going to scale u and v by 2^(-u_exponent) to bring them close to 1
130        // u_exponent could be EMAX so we have to do it in 2 steps
131        int m = -((int)(as_ulong(u) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64);
132	    double um = ldexp(u, m);
133	    double vm = ldexp(v, m);
134
135        // 26 leading bits of u
136        double u1 = as_double(as_ulong(um) & 0xfffffffff8000000UL);
137        double u2 = um - u1;
138
139        double r = MATH_DIVIDE(fma(-c, u2, fma(-c, u1, vm)), fma(c, vm, um));
140
141        // Polynomial approximation to atan(r)
142        double s = r * r;
143        q2 = q2 + fma((s * fma(-s, 0.19999918038989143496, 0.33333333333224095522)), -r, r);
144    }
145
146
147    double q3, q4;
148    {
149        q3 = 0.0;
150        q4 = vbyu;
151    }
152
153    double q5, q6;
154    {
155        double u1 = as_double(as_ulong(u) & 0xffffffff00000000UL);
156        double u2 = u - u1;
157        double vu1 = as_double(as_ulong(vbyu) & 0xffffffff00000000UL);
158        double vu2 = vbyu - vu1;
159
160        q5 = 0.0;
161        double s = vbyu * vbyu;
162        q6 = vbyu + fma(-vbyu * s,
163                        fma(-s,
164                            fma(-s,
165                                fma(-s,
166                                    fma(-s, 0.90029810285449784439E-01,
167                                        0.11110736283514525407),
168                                    0.14285713561807169030),
169                                0.19999999999393223405),
170                            0.33333333333333170500),
171			 MATH_DIVIDE(fma(-u, vu2, fma(-u2, vu1, fma(-u1, vu1, v))), u));
172    }
173
174
175    q3 = vbyu < 0x1.d12ed0af1a27fp-27 ? q3 : q5;
176    q4 = vbyu < 0x1.d12ed0af1a27fp-27 ? q4 : q6;
177
178    q1 = vbyu > 0.0625 ? q1 : q3;
179    q2 = vbyu > 0.0625 ? q2 : q4;
180
181    // Tidy-up according to which quadrant the arguments lie in
182    double res1, res2, res3, res4;
183    q1 = swap_vu ? piby2_head - q1 : q1;
184    q2 = swap_vu ? piby2_tail - q2 : q2;
185    q1 = xneg ? pi_head - q1 : q1;
186    q2 = xneg ? pi_tail - q2 : q2;
187    q1 = MATH_DIVIDE(q1 + q2, pi);
188    res4 = yneg ? -q1 : q1;
189
190    res1 = yneg ? -0.75 : 0.75;
191    res2 = yneg ? -0.25 : 0.25;
192    res3 = xneg ? res1 : res2;
193
194    res3 = isinf(y2) & isinf(x2) ? res3 : res4;
195    res1 = yneg ? -1.0 : 1.0;
196
197    // abs(x)/abs(y) > 2^56 and x < 0
198    res3 = (diffexp < -56 && xneg) ? res1 : res3;
199
200    res4 = MATH_DIVIDE(MATH_DIVIDE(y, x), pi);
201    // x positive and dominant over y by a factor of 2^28
202    res3 = diffexp < -28 & xneg == 0 ? res4 : res3;
203
204    // abs(y)/abs(x) > 2^56
205    res4 = yneg ? -0.5 : 0.5;        // atan(y/x) is insignificant compared to piby2
206    res3 = diffexp > 56 ? res4 : res3;
207
208    res3 = x2 == 0.0 ? res4 : res3;  // Zero x gives +- pi/2 depending on sign of y
209    res4 = xneg ? res1 : y2;
210
211    res3 = y2 == 0.0 ? res4 : res3;  // Zero y gives +-0 for positive x and +-pi for negative x
212    res3 = isnan(y2) ? y2 : res3;
213    res3 = isnan(x2) ? x2 : res3;
214
215    return res3;
216}
217
218
219_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atan2pi, double, double)
220
221#endif
222