1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-precision vector atan2(x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2021-2024, Arm Limited.
5*f3087befSAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner */
7*f3087befSAndrew Turner
8*f3087befSAndrew Turner #include "v_math.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner
12*f3087befSAndrew Turner static const struct data
13*f3087befSAndrew Turner {
14*f3087befSAndrew Turner float32x4_t c0, pi_over_2, c4, c6, c2;
15*f3087befSAndrew Turner float c1, c3, c5, c7;
16*f3087befSAndrew Turner uint32x4_t comp_const;
17*f3087befSAndrew Turner } data = {
18*f3087befSAndrew Turner /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
19*f3087befSAndrew Turner [2**-128, 1.0].
20*f3087befSAndrew Turner Generated using fpminimax between FLT_MIN and 1. */
21*f3087befSAndrew Turner .c0 = V4 (-0x1.55555p-2f), .c1 = 0x1.99935ep-3f,
22*f3087befSAndrew Turner .c2 = V4 (-0x1.24051ep-3f), .c3 = 0x1.bd7368p-4f,
23*f3087befSAndrew Turner .c4 = V4 (-0x1.491f0ep-4f), .c5 = 0x1.93a2c0p-5f,
24*f3087befSAndrew Turner .c6 = V4 (-0x1.4c3c60p-6f), .c7 = 0x1.01fd88p-8f,
25*f3087befSAndrew Turner .pi_over_2 = V4 (0x1.921fb6p+0f), .comp_const = V4 (2 * 0x7f800000lu - 1),
26*f3087befSAndrew Turner };
27*f3087befSAndrew Turner
28*f3087befSAndrew Turner #define SignMask v_u32 (0x80000000)
29*f3087befSAndrew Turner
30*f3087befSAndrew Turner /* Special cases i.e. 0, infinity and nan (fall back to scalar calls). */
31*f3087befSAndrew Turner static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t y,float32x4_t x,float32x4_t ret,uint32x4_t sign_xy,uint32x4_t cmp)32*f3087befSAndrew Turner special_case (float32x4_t y, float32x4_t x, float32x4_t ret,
33*f3087befSAndrew Turner uint32x4_t sign_xy, uint32x4_t cmp)
34*f3087befSAndrew Turner {
35*f3087befSAndrew Turner /* Account for the sign of y. */
36*f3087befSAndrew Turner ret = vreinterpretq_f32_u32 (
37*f3087befSAndrew Turner veorq_u32 (vreinterpretq_u32_f32 (ret), sign_xy));
38*f3087befSAndrew Turner return v_call2_f32 (atan2f, y, x, ret, cmp);
39*f3087befSAndrew Turner }
40*f3087befSAndrew Turner
41*f3087befSAndrew Turner /* Returns 1 if input is the bit representation of 0, infinity or nan. */
42*f3087befSAndrew Turner static inline uint32x4_t
zeroinfnan(uint32x4_t i,const struct data * d)43*f3087befSAndrew Turner zeroinfnan (uint32x4_t i, const struct data *d)
44*f3087befSAndrew Turner {
45*f3087befSAndrew Turner /* 2 * i - 1 >= 2 * 0x7f800000lu - 1. */
46*f3087befSAndrew Turner return vcgeq_u32 (vsubq_u32 (vmulq_n_u32 (i, 2), v_u32 (1)), d->comp_const);
47*f3087befSAndrew Turner }
48*f3087befSAndrew Turner
49*f3087befSAndrew Turner /* Fast implementation of vector atan2f. Maximum observed error is
50*f3087befSAndrew Turner 2.95 ULP in [0x1.9300d6p+6 0x1.93c0c6p+6] x [0x1.8c2dbp+6 0x1.8cea6p+6]:
51*f3087befSAndrew Turner _ZGVnN4vv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1
52*f3087befSAndrew Turner want 0x1.967f00p-1. */
V_NAME_F2(atan2)53*f3087befSAndrew Turner float32x4_t VPCS_ATTR NOINLINE V_NAME_F2 (atan2) (float32x4_t y, float32x4_t x)
54*f3087befSAndrew Turner {
55*f3087befSAndrew Turner const struct data *d = ptr_barrier (&data);
56*f3087befSAndrew Turner
57*f3087befSAndrew Turner uint32x4_t ix = vreinterpretq_u32_f32 (x);
58*f3087befSAndrew Turner uint32x4_t iy = vreinterpretq_u32_f32 (y);
59*f3087befSAndrew Turner
60*f3087befSAndrew Turner uint32x4_t special_cases
61*f3087befSAndrew Turner = vorrq_u32 (zeroinfnan (ix, d), zeroinfnan (iy, d));
62*f3087befSAndrew Turner
63*f3087befSAndrew Turner uint32x4_t sign_x = vandq_u32 (ix, SignMask);
64*f3087befSAndrew Turner uint32x4_t sign_y = vandq_u32 (iy, SignMask);
65*f3087befSAndrew Turner uint32x4_t sign_xy = veorq_u32 (sign_x, sign_y);
66*f3087befSAndrew Turner
67*f3087befSAndrew Turner float32x4_t ax = vabsq_f32 (x);
68*f3087befSAndrew Turner float32x4_t ay = vabsq_f32 (y);
69*f3087befSAndrew Turner
70*f3087befSAndrew Turner uint32x4_t pred_xlt0 = vcltzq_f32 (x);
71*f3087befSAndrew Turner uint32x4_t pred_aygtax = vcgtq_f32 (ay, ax);
72*f3087befSAndrew Turner
73*f3087befSAndrew Turner /* Set up z for call to atanf. */
74*f3087befSAndrew Turner float32x4_t n = vbslq_f32 (pred_aygtax, vnegq_f32 (ax), ay);
75*f3087befSAndrew Turner float32x4_t q = vbslq_f32 (pred_aygtax, ay, ax);
76*f3087befSAndrew Turner float32x4_t z = vdivq_f32 (n, q);
77*f3087befSAndrew Turner
78*f3087befSAndrew Turner /* Work out the correct shift. */
79*f3087befSAndrew Turner float32x4_t shift = vreinterpretq_f32_u32 (
80*f3087befSAndrew Turner vandq_u32 (pred_xlt0, vreinterpretq_u32_f32 (v_f32 (-2.0f))));
81*f3087befSAndrew Turner shift = vbslq_f32 (pred_aygtax, vaddq_f32 (shift, v_f32 (1.0f)), shift);
82*f3087befSAndrew Turner shift = vmulq_f32 (shift, d->pi_over_2);
83*f3087befSAndrew Turner
84*f3087befSAndrew Turner /* Calculate the polynomial approximation.
85*f3087befSAndrew Turner Use 2-level Estrin scheme for P(z^2) with deg(P)=7. However,
86*f3087befSAndrew Turner a standard implementation using z8 creates spurious underflow
87*f3087befSAndrew Turner in the very last fma (when z^8 is small enough).
88*f3087befSAndrew Turner Therefore, we split the last fma into a mul and an fma.
89*f3087befSAndrew Turner Horner and single-level Estrin have higher errors that exceed
90*f3087befSAndrew Turner threshold. */
91*f3087befSAndrew Turner float32x4_t z2 = vmulq_f32 (z, z);
92*f3087befSAndrew Turner float32x4_t z4 = vmulq_f32 (z2, z2);
93*f3087befSAndrew Turner
94*f3087befSAndrew Turner float32x4_t c1357 = vld1q_f32 (&d->c1);
95*f3087befSAndrew Turner float32x4_t p01 = vfmaq_laneq_f32 (d->c0, z2, c1357, 0);
96*f3087befSAndrew Turner float32x4_t p23 = vfmaq_laneq_f32 (d->c2, z2, c1357, 1);
97*f3087befSAndrew Turner float32x4_t p45 = vfmaq_laneq_f32 (d->c4, z2, c1357, 2);
98*f3087befSAndrew Turner float32x4_t p67 = vfmaq_laneq_f32 (d->c6, z2, c1357, 3);
99*f3087befSAndrew Turner float32x4_t p03 = vfmaq_f32 (p01, z4, p23);
100*f3087befSAndrew Turner float32x4_t p47 = vfmaq_f32 (p45, z4, p67);
101*f3087befSAndrew Turner
102*f3087befSAndrew Turner float32x4_t ret = vfmaq_f32 (p03, z4, vmulq_f32 (z4, p47));
103*f3087befSAndrew Turner
104*f3087befSAndrew Turner /* y = shift + z * P(z^2). */
105*f3087befSAndrew Turner ret = vaddq_f32 (vfmaq_f32 (z, ret, vmulq_f32 (z2, z)), shift);
106*f3087befSAndrew Turner
107*f3087befSAndrew Turner if (unlikely (v_any_u32 (special_cases)))
108*f3087befSAndrew Turner {
109*f3087befSAndrew Turner return special_case (y, x, ret, sign_xy, special_cases);
110*f3087befSAndrew Turner }
111*f3087befSAndrew Turner
112*f3087befSAndrew Turner /* Account for the sign of y. */
113*f3087befSAndrew Turner return vreinterpretq_f32_u32 (
114*f3087befSAndrew Turner veorq_u32 (vreinterpretq_u32_f32 (ret), sign_xy));
115*f3087befSAndrew Turner }
116*f3087befSAndrew Turner
117*f3087befSAndrew Turner HALF_WIDTH_ALIAS_F2 (atan2)
118*f3087befSAndrew Turner
119*f3087befSAndrew Turner /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */
120*f3087befSAndrew Turner TEST_SIG (V, F, 2, atan2)
121*f3087befSAndrew Turner TEST_DISABLE_FENV (V_NAME_F2 (atan2))
122*f3087befSAndrew Turner TEST_ULP (V_NAME_F2 (atan2), 2.46)
123*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_F2 (atan2), -10.0, 10.0, 50000)
124*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_F2 (atan2), -1.0, 1.0, 40000)
125*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_F2 (atan2), 0.0, 1.0, 40000)
126*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_F2 (atan2), 1.0, 100.0, 40000)
127*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_F2 (atan2), 1e6, 1e32, 40000)
128