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