1 /*
2  * Single-precision SVE cos(x) function.
3  *
4  * Copyright (c) 2019-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 static const struct data
13 {
14   float neg_pio2_1, neg_pio2_2, neg_pio2_3, inv_pio2, shift;
15 } data = {
16   /* Polynomial coefficients are hard-wired in FTMAD instructions.  */
17   .neg_pio2_1 = -0x1.921fb6p+0f,
18   .neg_pio2_2 = 0x1.777a5cp-25f,
19   .neg_pio2_3 = 0x1.ee59dap-50f,
20   .inv_pio2 = 0x1.45f306p-1f,
21   /* Original shift used in AdvSIMD cosf,
22      plus a contribution to set the bit #0 of q
23      as expected by trigonometric instructions.  */
24   .shift = 0x1.800002p+23f
25 };
26 
27 #define RangeVal 0x49800000 /* asuint32(0x1p20f).  */
28 
29 static svfloat32_t NOINLINE
30 special_case (svfloat32_t x, svfloat32_t y, svbool_t oob)
31 {
32   return sv_call_f32 (cosf, x, y, oob);
33 }
34 
35 /* A fast SVE implementation of cosf based on trigonometric
36    instructions (FTMAD, FTSSEL, FTSMUL).
37    Maximum measured error: 2.06 ULPs.
38    SV_NAME_F1 (cos)(0x1.dea2f2p+19) got 0x1.fffe7ap-6
39 				   want 0x1.fffe76p-6.  */
40 svfloat32_t SV_NAME_F1 (cos) (svfloat32_t x, const svbool_t pg)
41 {
42   const struct data *d = ptr_barrier (&data);
43 
44   svfloat32_t r = svabs_x (pg, x);
45   svbool_t oob = svcmpge (pg, svreinterpret_u32 (r), RangeVal);
46 
47   /* Load some constants in quad-word chunks to minimise memory access.  */
48   svfloat32_t negpio2_and_invpio2 = svld1rq (svptrue_b32 (), &d->neg_pio2_1);
49 
50   /* n = rint(|x|/(pi/2)).  */
51   svfloat32_t q = svmla_lane (sv_f32 (d->shift), r, negpio2_and_invpio2, 3);
52   svfloat32_t n = svsub_x (pg, q, d->shift);
53 
54   /* r = |x| - n*(pi/2)  (range reduction into -pi/4 .. pi/4).  */
55   r = svmla_lane (r, n, negpio2_and_invpio2, 0);
56   r = svmla_lane (r, n, negpio2_and_invpio2, 1);
57   r = svmla_lane (r, n, negpio2_and_invpio2, 2);
58 
59   /* Final multiplicative factor: 1.0 or x depending on bit #0 of q.  */
60   svfloat32_t f = svtssel (r, svreinterpret_u32 (q));
61 
62   /* cos(r) poly approx.  */
63   svfloat32_t r2 = svtsmul (r, svreinterpret_u32 (q));
64   svfloat32_t y = sv_f32 (0.0f);
65   y = svtmad (y, r2, 4);
66   y = svtmad (y, r2, 3);
67   y = svtmad (y, r2, 2);
68   y = svtmad (y, r2, 1);
69   y = svtmad (y, r2, 0);
70 
71   if (unlikely (svptest_any (pg, oob)))
72     return special_case (x, svmul_x (svnot_z (pg, oob), f, y), oob);
73   /* Apply factor.  */
74   return svmul_x (pg, f, y);
75 }
76 
77 PL_SIG (SV, F, 1, cos, -3.1, 3.1)
78 PL_TEST_ULP (SV_NAME_F1 (cos), 1.57)
79 PL_TEST_INTERVAL (SV_NAME_F1 (cos), 0, 0xffff0000, 10000)
80 PL_TEST_INTERVAL (SV_NAME_F1 (cos), 0x1p-4, 0x1p4, 500000)
81