xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_cos_2u5.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1 /*
2  * Double-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   double inv_pio2, pio2_1, pio2_2, pio2_3, shift;
15 } data = {
16   /* Polynomial coefficients are hardwired in FTMAD instructions.  */
17   .inv_pio2 = 0x1.45f306dc9c882p-1,
18   .pio2_1 = 0x1.921fb50000000p+0,
19   .pio2_2 = 0x1.110b460000000p-26,
20   .pio2_3 = 0x1.1a62633145c07p-54,
21   /* Original shift used in AdvSIMD cos,
22      plus a contribution to set the bit #0 of q
23      as expected by trigonometric instructions.  */
24   .shift = 0x1.8000000000001p52
25 };
26 
27 #define RangeVal 0x4160000000000000 /* asuint64 (0x1p23).  */
28 
29 static svfloat64_t NOINLINE
special_case(svfloat64_t x,svfloat64_t y,svbool_t oob)30 special_case (svfloat64_t x, svfloat64_t y, svbool_t oob)
31 {
32   return sv_call_f64 (cos, x, y, oob);
33 }
34 
35 /* A fast SVE implementation of cos based on trigonometric
36    instructions (FTMAD, FTSSEL, FTSMUL).
37    Maximum measured error: 2.108 ULPs.
38    SV_NAME_D1 (cos)(0x1.9b0ba158c98f3p+7) got -0x1.fddd4c65c7f07p-3
39 					 want -0x1.fddd4c65c7f05p-3.  */
SV_NAME_D1(cos)40 svfloat64_t SV_NAME_D1 (cos) (svfloat64_t x, const svbool_t pg)
41 {
42   const struct data *d = ptr_barrier (&data);
43 
44   svfloat64_t r = svabs_x (pg, x);
45   svbool_t oob = svcmpge (pg, svreinterpret_u64 (r), RangeVal);
46 
47   /* Load some constants in quad-word chunks to minimise memory access.  */
48   svbool_t ptrue = svptrue_b64 ();
49   svfloat64_t invpio2_and_pio2_1 = svld1rq (ptrue, &d->inv_pio2);
50   svfloat64_t pio2_23 = svld1rq (ptrue, &d->pio2_2);
51 
52   /* n = rint(|x|/(pi/2)).  */
53   svfloat64_t q = svmla_lane (sv_f64 (d->shift), r, invpio2_and_pio2_1, 0);
54   svfloat64_t n = svsub_x (pg, q, d->shift);
55 
56   /* r = |x| - n*(pi/2)  (range reduction into -pi/4 .. pi/4).  */
57   r = svmls_lane (r, n, invpio2_and_pio2_1, 1);
58   r = svmls_lane (r, n, pio2_23, 0);
59   r = svmls_lane (r, n, pio2_23, 1);
60 
61   /* cos(r) poly approx.  */
62   svfloat64_t r2 = svtsmul (r, svreinterpret_u64 (q));
63   svfloat64_t y = sv_f64 (0.0);
64   y = svtmad (y, r2, 7);
65   y = svtmad (y, r2, 6);
66   y = svtmad (y, r2, 5);
67   y = svtmad (y, r2, 4);
68   y = svtmad (y, r2, 3);
69   y = svtmad (y, r2, 2);
70   y = svtmad (y, r2, 1);
71   y = svtmad (y, r2, 0);
72 
73   /* Final multiplicative factor: 1.0 or x depending on bit #0 of q.  */
74   svfloat64_t f = svtssel (r, svreinterpret_u64 (q));
75 
76   if (unlikely (svptest_any (pg, oob)))
77     return special_case (x, svmul_x (svnot_z (pg, oob), y, f), oob);
78 
79   /* Apply factor.  */
80   return svmul_x (pg, f, y);
81 }
82 
83 PL_SIG (SV, D, 1, cos, -3.1, 3.1)
84 PL_TEST_ULP (SV_NAME_D1 (cos), 1.61)
85 PL_TEST_INTERVAL (SV_NAME_D1 (cos), 0, 0xffff0000, 10000)
86 PL_TEST_INTERVAL (SV_NAME_D1 (cos), 0x1p-4, 0x1p4, 500000)
87