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 #if SV_SUPPORTED
13
14 #define NegPio2_1 (sv_f32 (-0x1.921fb6p+0f))
15 #define NegPio2_2 (sv_f32 (0x1.777a5cp-25f))
16 #define NegPio2_3 (sv_f32 (0x1.ee59dap-50f))
17 #define RangeVal (sv_f32 (0x1p20f))
18 #define InvPio2 (sv_f32 (0x1.45f306p-1f))
19 /* Original shift used in Neon cosf,
20 plus a contribution to set the bit #0 of q
21 as expected by trigonometric instructions. */
22 #define Shift (sv_f32 (0x1.800002p+23f))
23 #define AbsMask (0x7fffffff)
24
25 static NOINLINE sv_f32_t
__sv_cosf_specialcase(sv_f32_t x,sv_f32_t y,svbool_t cmp)26 __sv_cosf_specialcase (sv_f32_t x, sv_f32_t y, svbool_t cmp)
27 {
28 return sv_call_f32 (cosf, x, y, cmp);
29 }
30
31 /* A fast SVE implementation of cosf based on trigonometric
32 instructions (FTMAD, FTSSEL, FTSMUL).
33 Maximum measured error: 2.06 ULPs.
34 __sv_cosf(0x1.dea2f2p+19) got 0x1.fffe7ap-6
35 want 0x1.fffe76p-6. */
36 sv_f32_t
__sv_cosf_x(sv_f32_t x,const svbool_t pg)37 __sv_cosf_x (sv_f32_t x, const svbool_t pg)
38 {
39 sv_f32_t n, r, r2, y;
40 svbool_t cmp;
41
42 r = sv_as_f32_u32 (svand_n_u32_x (pg, sv_as_u32_f32 (x), AbsMask));
43 cmp = svcmpge_u32 (pg, sv_as_u32_f32 (r), sv_as_u32_f32 (RangeVal));
44
45 /* n = rint(|x|/(pi/2)). */
46 sv_f32_t q = sv_fma_f32_x (pg, InvPio2, r, Shift);
47 n = svsub_f32_x (pg, q, Shift);
48
49 /* r = |x| - n*(pi/2) (range reduction into -pi/4 .. pi/4). */
50 r = sv_fma_f32_x (pg, NegPio2_1, n, r);
51 r = sv_fma_f32_x (pg, NegPio2_2, n, r);
52 r = sv_fma_f32_x (pg, NegPio2_3, n, r);
53
54 /* Final multiplicative factor: 1.0 or x depending on bit #0 of q. */
55 sv_f32_t f = svtssel_f32 (r, sv_as_u32_f32 (q));
56
57 /* cos(r) poly approx. */
58 r2 = svtsmul_f32 (r, sv_as_u32_f32 (q));
59 y = sv_f32 (0.0f);
60 y = svtmad_f32 (y, r2, 4);
61 y = svtmad_f32 (y, r2, 3);
62 y = svtmad_f32 (y, r2, 2);
63 y = svtmad_f32 (y, r2, 1);
64 y = svtmad_f32 (y, r2, 0);
65
66 /* Apply factor. */
67 y = svmul_f32_x (pg, f, y);
68
69 /* No need to pass pg to specialcase here since cmp is a strict subset,
70 guaranteed by the cmpge above. */
71 if (unlikely (svptest_any (pg, cmp)))
72 return __sv_cosf_specialcase (x, y, cmp);
73 return y;
74 }
75
76 PL_ALIAS (__sv_cosf_x, _ZGVsMxv_cosf)
77
78 PL_SIG (SV, F, 1, cos, -3.1, 3.1)
79 PL_TEST_ULP (__sv_cosf, 1.57)
80 PL_TEST_INTERVAL (__sv_cosf, 0, 0xffff0000, 10000)
81 PL_TEST_INTERVAL (__sv_cosf, 0x1p-4, 0x1p4, 500000)
82 #endif
83