xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_atan_2u5.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*
2  * Double-precision vector atan(x) function.
3  *
4  * Copyright (c) 2021-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11 
12 #if SV_SUPPORTED
13 
14 #include "sv_atan_common.h"
15 
16 /* Useful constants.  */
17 #define PiOver2 sv_f64 (0x1.921fb54442d18p+0)
18 #define AbsMask (0x7fffffffffffffff)
19 
20 /* Fast implementation of SVE atan.
21    Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
22    z=1/x and shift = pi/2. Largest errors are close to 1. The maximum observed
23    error is 2.27 ulps:
24    __sv_atan(0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1
25 				  want 0x1.9225645bdd7c3p-1.  */
26 sv_f64_t
27 __sv_atan_x (sv_f64_t x, const svbool_t pg)
28 {
29   /* No need to trigger special case. Small cases, infs and nans
30      are supported by our approximation technique.  */
31   sv_u64_t ix = sv_as_u64_f64 (x);
32   sv_u64_t sign = svand_n_u64_x (pg, ix, ~AbsMask);
33 
34   /* Argument reduction:
35      y := arctan(x) for x < 1
36      y := pi/2 + arctan(-1/x) for x > 1
37      Hence, use z=-1/a if x>=1, otherwise z=a.  */
38   svbool_t red = svacgt_n_f64 (pg, x, 1.0);
39   /* Avoid dependency in abs(x) in division (and comparison).  */
40   sv_f64_t z = svsel_f64 (red, svdiv_f64_x (pg, sv_f64 (-1.0), x), x);
41   /* Use absolute value only when needed (odd powers of z).  */
42   sv_f64_t az = svabs_f64_x (pg, z);
43   az = svneg_f64_m (az, red, az);
44 
45   sv_f64_t y = __sv_atan_common (pg, red, z, az, PiOver2);
46 
47   /* y = atan(x) if x>0, -atan(-x) otherwise.  */
48   y = sv_as_f64_u64 (sveor_u64_x (pg, sv_as_u64_f64 (y), sign));
49 
50   return y;
51 }
52 
53 PL_ALIAS (__sv_atan_x, _ZGVsMxv_atan)
54 
55 PL_SIG (SV, D, 1, atan, -3.1, 3.1)
56 PL_TEST_ULP (__sv_atan, 1.78)
57 PL_TEST_INTERVAL (__sv_atan, -10.0, 10.0, 50000)
58 PL_TEST_INTERVAL (__sv_atan, -1.0, 1.0, 40000)
59 PL_TEST_INTERVAL (__sv_atan, 0.0, 1.0, 40000)
60 PL_TEST_INTERVAL (__sv_atan, 1.0, 100.0, 40000)
61 PL_TEST_INTERVAL (__sv_atan, 1e6, 1e32, 40000)
62 #endif
63