1 /*
2 * Single-precision vector atan(x) function.
3 *
4 * Copyright (c) 2021-2024, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "sv_math.h"
9 #include "test_sig.h"
10 #include "test_defs.h"
11 #include "sv_poly_f32.h"
12
13 static const struct data
14 {
15 float32_t poly[8];
16 float32_t pi_over_2;
17 } data = {
18 /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
19 [2**-128, 1.0]. */
20 .poly = { -0x1.55555p-2f, 0x1.99935ep-3f, -0x1.24051ep-3f, 0x1.bd7368p-4f,
21 -0x1.491f0ep-4f, 0x1.93a2c0p-5f, -0x1.4c3c60p-6f, 0x1.01fd88p-8f },
22 .pi_over_2 = 0x1.921fb6p+0f,
23 };
24
25 #define SignMask (0x80000000)
26
27 /* Fast implementation of SVE atanf based on
28 atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
29 z=-1/x and shift = pi/2.
30 Largest observed error is 2.9 ULP, close to +/-1.0:
31 _ZGVsMxv_atanf (0x1.0468f6p+0) got -0x1.967f06p-1
32 want -0x1.967fp-1. */
SV_NAME_F1(atan)33 svfloat32_t SV_NAME_F1 (atan) (svfloat32_t x, const svbool_t pg)
34 {
35 const struct data *d = ptr_barrier (&data);
36
37 /* No need to trigger special case. Small cases, infs and nans
38 are supported by our approximation technique. */
39 svuint32_t ix = svreinterpret_u32 (x);
40 svuint32_t sign = svand_x (pg, ix, SignMask);
41
42 /* Argument reduction:
43 y := arctan(x) for x < 1
44 y := pi/2 + arctan(-1/x) for x > 1
45 Hence, use z=-1/a if x>=1, otherwise z=a. */
46 svbool_t red = svacgt (pg, x, 1.0f);
47 /* Avoid dependency in abs(x) in division (and comparison). */
48 svfloat32_t z = svsel (red, svdiv_x (pg, sv_f32 (1.0f), x), x);
49 /* Use absolute value only when needed (odd powers of z). */
50 svfloat32_t az = svabs_x (pg, z);
51 az = svneg_m (az, red, az);
52
53 /* Use split Estrin scheme for P(z^2) with deg(P)=7. */
54 svfloat32_t z2 = svmul_x (pg, z, z);
55 svfloat32_t z4 = svmul_x (pg, z2, z2);
56 svfloat32_t z8 = svmul_x (pg, z4, z4);
57
58 svfloat32_t y = sv_estrin_7_f32_x (pg, z2, z4, z8, d->poly);
59
60 /* y = shift + z + z^3 * P(z^2). */
61 svfloat32_t z3 = svmul_x (pg, z2, az);
62 y = svmla_x (pg, az, z3, y);
63
64 /* Apply shift as indicated by 'red' predicate. */
65 y = svadd_m (red, y, sv_f32 (d->pi_over_2));
66
67 /* y = atan(x) if x>0, -atan(-x) otherwise. */
68 return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (y), sign));
69 }
70
71 TEST_SIG (SV, F, 1, atan, -3.1, 3.1)
72 TEST_ULP (SV_NAME_F1 (atan), 2.9)
73 TEST_DISABLE_FENV (SV_NAME_F1 (atan))
74 TEST_INTERVAL (SV_NAME_F1 (atan), 0.0, 1.0, 40000)
75 TEST_INTERVAL (SV_NAME_F1 (atan), 1.0, 100.0, 40000)
76 TEST_INTERVAL (SV_NAME_F1 (atan), 100, inf, 40000)
77 TEST_INTERVAL (SV_NAME_F1 (atan), -0, -inf, 40000)
78 CLOSE_SVE_ATTR
79