xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/atan.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision vector atan(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   float64x2_t c0, c2, c4, c6, c8, c10, c12, c14, c16, c18;
15*f3087befSAndrew Turner   float64x2_t pi_over_2;
16*f3087befSAndrew Turner   double c1, c3, c5, c7, c9, c11, c13, c15, c17, c19;
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**-1022, 1.0].  */
20*f3087befSAndrew Turner   .c0 = V2 (-0x1.5555555555555p-2),	  .c1 = 0x1.99999999996c1p-3,
21*f3087befSAndrew Turner   .c2 = V2 (-0x1.2492492478f88p-3),	  .c3 = 0x1.c71c71bc3951cp-4,
22*f3087befSAndrew Turner   .c4 = V2 (-0x1.745d160a7e368p-4),	  .c5 = 0x1.3b139b6a88ba1p-4,
23*f3087befSAndrew Turner   .c6 = V2 (-0x1.11100ee084227p-4),	  .c7 = 0x1.e1d0f9696f63bp-5,
24*f3087befSAndrew Turner   .c8 = V2 (-0x1.aebfe7b418581p-5),	  .c9 = 0x1.842dbe9b0d916p-5,
25*f3087befSAndrew Turner   .c10 = V2 (-0x1.5d30140ae5e99p-5),	  .c11 = 0x1.338e31eb2fbbcp-5,
26*f3087befSAndrew Turner   .c12 = V2 (-0x1.00e6eece7de8p-5),	  .c13 = 0x1.860897b29e5efp-6,
27*f3087befSAndrew Turner   .c14 = V2 (-0x1.0051381722a59p-6),	  .c15 = 0x1.14e9dc19a4a4ep-7,
28*f3087befSAndrew Turner   .c16 = V2 (-0x1.d0062b42fe3bfp-9),	  .c17 = 0x1.17739e210171ap-10,
29*f3087befSAndrew Turner   .c18 = V2 (-0x1.ab24da7be7402p-13),	  .c19 = 0x1.358851160a528p-16,
30*f3087befSAndrew Turner   .pi_over_2 = V2 (0x1.921fb54442d18p+0),
31*f3087befSAndrew Turner };
32*f3087befSAndrew Turner 
33*f3087befSAndrew Turner #define SignMask v_u64 (0x8000000000000000)
34*f3087befSAndrew Turner #define TinyBound 0x3e10000000000000 /* asuint64(0x1p-30).  */
35*f3087befSAndrew Turner #define BigBound 0x4340000000000000  /* asuint64(0x1p53).  */
36*f3087befSAndrew Turner 
37*f3087befSAndrew Turner /* Fast implementation of vector atan.
38*f3087befSAndrew Turner    Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
39*f3087befSAndrew Turner    z=1/x and shift = pi/2. Maximum observed error is 2.27 ulps:
40*f3087befSAndrew Turner    _ZGVnN2v_atan (0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1
41*f3087befSAndrew Turner 				       want 0x1.9225645bdd7c3p-1.  */
V_NAME_D1(atan)42*f3087befSAndrew Turner float64x2_t VPCS_ATTR V_NAME_D1 (atan) (float64x2_t x)
43*f3087befSAndrew Turner {
44*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
45*f3087befSAndrew Turner   float64x2_t c13 = vld1q_f64 (&d->c1);
46*f3087befSAndrew Turner   float64x2_t c57 = vld1q_f64 (&d->c5);
47*f3087befSAndrew Turner   float64x2_t c911 = vld1q_f64 (&d->c9);
48*f3087befSAndrew Turner   float64x2_t c1315 = vld1q_f64 (&d->c13);
49*f3087befSAndrew Turner   float64x2_t c1719 = vld1q_f64 (&d->c17);
50*f3087befSAndrew Turner 
51*f3087befSAndrew Turner   /* Small cases, infs and nans are supported by our approximation technique,
52*f3087befSAndrew Turner      but do not set fenv flags correctly. Only trigger special case if we need
53*f3087befSAndrew Turner      fenv.  */
54*f3087befSAndrew Turner   uint64x2_t ix = vreinterpretq_u64_f64 (x);
55*f3087befSAndrew Turner   uint64x2_t sign = vandq_u64 (ix, SignMask);
56*f3087befSAndrew Turner 
57*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
58*f3087befSAndrew Turner   uint64x2_t ia12 = vandq_u64 (ix, v_u64 (0x7ff0000000000000));
59*f3087befSAndrew Turner   uint64x2_t special = vcgtq_u64 (vsubq_u64 (ia12, v_u64 (TinyBound)),
60*f3087befSAndrew Turner 				  v_u64 (BigBound - TinyBound));
61*f3087befSAndrew Turner   /* If any lane is special, fall back to the scalar routine for all lanes.  */
62*f3087befSAndrew Turner   if (unlikely (v_any_u64 (special)))
63*f3087befSAndrew Turner     return v_call_f64 (atan, x, v_f64 (0), v_u64 (-1));
64*f3087befSAndrew Turner #endif
65*f3087befSAndrew Turner 
66*f3087befSAndrew Turner   /* Argument reduction:
67*f3087befSAndrew Turner      y := arctan(x) for x < 1
68*f3087befSAndrew Turner      y := pi/2 + arctan(-1/x) for x > 1
69*f3087befSAndrew Turner      Hence, use z=-1/a if x>=1, otherwise z=a.  */
70*f3087befSAndrew Turner   uint64x2_t red = vcagtq_f64 (x, v_f64 (1.0));
71*f3087befSAndrew Turner   /* Avoid dependency in abs(x) in division (and comparison).  */
72*f3087befSAndrew Turner   float64x2_t z = vbslq_f64 (red, vdivq_f64 (v_f64 (1.0), x), x);
73*f3087befSAndrew Turner   float64x2_t shift = vreinterpretq_f64_u64 (
74*f3087befSAndrew Turner       vandq_u64 (red, vreinterpretq_u64_f64 (d->pi_over_2)));
75*f3087befSAndrew Turner   /* Use absolute value only when needed (odd powers of z).  */
76*f3087befSAndrew Turner   float64x2_t az = vbslq_f64 (
77*f3087befSAndrew Turner       SignMask, vreinterpretq_f64_u64 (vandq_u64 (SignMask, red)), z);
78*f3087befSAndrew Turner 
79*f3087befSAndrew Turner   /* Calculate the polynomial approximation.
80*f3087befSAndrew Turner      Use split Estrin scheme for P(z^2) with deg(P)=19. Use split instead of
81*f3087befSAndrew Turner      full scheme to avoid underflow in x^16.
82*f3087befSAndrew Turner      The order 19 polynomial P approximates
83*f3087befSAndrew Turner      (atan(sqrt(x))-sqrt(x))/x^(3/2).  */
84*f3087befSAndrew Turner   float64x2_t z2 = vmulq_f64 (z, z);
85*f3087befSAndrew Turner   float64x2_t x2 = vmulq_f64 (z2, z2);
86*f3087befSAndrew Turner   float64x2_t x4 = vmulq_f64 (x2, x2);
87*f3087befSAndrew Turner   float64x2_t x8 = vmulq_f64 (x4, x4);
88*f3087befSAndrew Turner 
89*f3087befSAndrew Turner   /* estrin_7.  */
90*f3087befSAndrew Turner   float64x2_t p01 = vfmaq_laneq_f64 (d->c0, z2, c13, 0);
91*f3087befSAndrew Turner   float64x2_t p23 = vfmaq_laneq_f64 (d->c2, z2, c13, 1);
92*f3087befSAndrew Turner   float64x2_t p03 = vfmaq_f64 (p01, x2, p23);
93*f3087befSAndrew Turner 
94*f3087befSAndrew Turner   float64x2_t p45 = vfmaq_laneq_f64 (d->c4, z2, c57, 0);
95*f3087befSAndrew Turner   float64x2_t p67 = vfmaq_laneq_f64 (d->c6, z2, c57, 1);
96*f3087befSAndrew Turner   float64x2_t p47 = vfmaq_f64 (p45, x2, p67);
97*f3087befSAndrew Turner 
98*f3087befSAndrew Turner   float64x2_t p07 = vfmaq_f64 (p03, x4, p47);
99*f3087befSAndrew Turner 
100*f3087befSAndrew Turner   /* estrin_11.  */
101*f3087befSAndrew Turner   float64x2_t p89 = vfmaq_laneq_f64 (d->c8, z2, c911, 0);
102*f3087befSAndrew Turner   float64x2_t p1011 = vfmaq_laneq_f64 (d->c10, z2, c911, 1);
103*f3087befSAndrew Turner   float64x2_t p811 = vfmaq_f64 (p89, x2, p1011);
104*f3087befSAndrew Turner 
105*f3087befSAndrew Turner   float64x2_t p1213 = vfmaq_laneq_f64 (d->c12, z2, c1315, 0);
106*f3087befSAndrew Turner   float64x2_t p1415 = vfmaq_laneq_f64 (d->c14, z2, c1315, 1);
107*f3087befSAndrew Turner   float64x2_t p1215 = vfmaq_f64 (p1213, x2, p1415);
108*f3087befSAndrew Turner 
109*f3087befSAndrew Turner   float64x2_t p1617 = vfmaq_laneq_f64 (d->c16, z2, c1719, 0);
110*f3087befSAndrew Turner   float64x2_t p1819 = vfmaq_laneq_f64 (d->c18, z2, c1719, 1);
111*f3087befSAndrew Turner   float64x2_t p1619 = vfmaq_f64 (p1617, x2, p1819);
112*f3087befSAndrew Turner 
113*f3087befSAndrew Turner   float64x2_t p815 = vfmaq_f64 (p811, x4, p1215);
114*f3087befSAndrew Turner   float64x2_t p819 = vfmaq_f64 (p815, x8, p1619);
115*f3087befSAndrew Turner 
116*f3087befSAndrew Turner   float64x2_t y = vfmaq_f64 (p07, p819, x8);
117*f3087befSAndrew Turner 
118*f3087befSAndrew Turner   /* Finalize. y = shift + z + z^3 * P(z^2).  */
119*f3087befSAndrew Turner   y = vfmaq_f64 (az, y, vmulq_f64 (z2, az));
120*f3087befSAndrew Turner   y = vaddq_f64 (y, shift);
121*f3087befSAndrew Turner 
122*f3087befSAndrew Turner   /* y = atan(x) if x>0, -atan(-x) otherwise.  */
123*f3087befSAndrew Turner   y = vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), sign));
124*f3087befSAndrew Turner   return y;
125*f3087befSAndrew Turner }
126*f3087befSAndrew Turner 
127*f3087befSAndrew Turner TEST_SIG (V, D, 1, atan, -10.0, 10.0)
128*f3087befSAndrew Turner TEST_ULP (V_NAME_D1 (atan), 1.78)
129*f3087befSAndrew Turner TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (atan), WANT_SIMD_EXCEPT)
130*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (atan), 0, 0x1p-30, 10000)
131*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (atan), -0, -0x1p-30, 1000)
132*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (atan), 0x1p-30, 0x1p53, 900000)
133*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (atan), -0x1p-30, -0x1p53, 90000)
134*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (atan), 0x1p53, inf, 10000)
135*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_D1 (atan), -0x1p53, -inf, 1000)
136