1 /*
2  * Single-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_atanf_common.h"
15 
16 #define PiOver2 sv_f32 (0x1.921fb6p+0f)
17 #define AbsMask (0x7fffffff)
18 
19 /* Fast implementation of SVE atanf based on
20    atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
21    z=-1/x and shift = pi/2.
22    Largest observed error is 2.9 ULP, close to +/-1.0:
23    __sv_atanf(0x1.0468f6p+0) got -0x1.967f06p-1
24 			    want -0x1.967fp-1.  */
25 sv_f32_t
26 __sv_atanf_x (sv_f32_t x, const svbool_t pg)
27 {
28   /* No need to trigger special case. Small cases, infs and nans
29      are supported by our approximation technique.  */
30   sv_u32_t ix = sv_as_u32_f32 (x);
31   sv_u32_t sign = svand_n_u32_x (pg, ix, ~AbsMask);
32 
33   /* Argument reduction:
34      y := arctan(x) for x < 1
35      y := pi/2 + arctan(-1/x) for x > 1
36      Hence, use z=-1/a if x>=1, otherwise z=a.  */
37   svbool_t red = svacgt_n_f32 (pg, x, 1.0f);
38   /* Avoid dependency in abs(x) in division (and comparison).  */
39   sv_f32_t z = svsel_f32 (red, svdiv_f32_x (pg, sv_f32 (-1.0f), x), x);
40   /* Use absolute value only when needed (odd powers of z).  */
41   sv_f32_t az = svabs_f32_x (pg, z);
42   az = svneg_f32_m (az, red, az);
43 
44   sv_f32_t y = __sv_atanf_common (pg, red, z, az, PiOver2);
45 
46   /* y = atan(x) if x>0, -atan(-x) otherwise.  */
47   return sv_as_f32_u32 (sveor_u32_x (pg, sv_as_u32_f32 (y), sign));
48 }
49 
50 PL_ALIAS (__sv_atanf_x, _ZGVsMxv_atanf)
51 
52 PL_SIG (SV, F, 1, atan, -3.1, 3.1)
53 PL_TEST_ULP (__sv_atanf, 2.9)
54 PL_TEST_INTERVAL (__sv_atanf, -10.0, 10.0, 50000)
55 PL_TEST_INTERVAL (__sv_atanf, -1.0, 1.0, 40000)
56 PL_TEST_INTERVAL (__sv_atanf, 0.0, 1.0, 40000)
57 PL_TEST_INTERVAL (__sv_atanf, 1.0, 100.0, 40000)
58 PL_TEST_INTERVAL (__sv_atanf, 1e6, 1e32, 40000)
59 #endif
60