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