xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_sin_3u5.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*
2  * Double-precision SVE sin(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_pi, pi_1, pi_2, pi_3, shift, range_val;
15   double poly[7];
16 } data = {
17   .poly = { -0x1.555555555547bp-3, 0x1.1111111108a4dp-7, -0x1.a01a019936f27p-13,
18             0x1.71de37a97d93ep-19, -0x1.ae633919987c6p-26,
19             0x1.60e277ae07cecp-33, -0x1.9e9540300a1p-41, },
20 
21   .inv_pi = 0x1.45f306dc9c883p-2,
22   .pi_1 = 0x1.921fb54442d18p+1,
23   .pi_2 = 0x1.1a62633145c06p-53,
24   .pi_3 = 0x1.c1cd129024e09p-106,
25   .shift = 0x1.8p52,
26   .range_val = 0x1p23,
27 };
28 
29 #define C(i) sv_f64 (d->poly[i])
30 
31 static svfloat64_t NOINLINE
32 special_case (svfloat64_t x, svfloat64_t y, svbool_t cmp)
33 {
34   return sv_call_f64 (sin, x, y, cmp);
35 }
36 
37 /* A fast SVE implementation of sin.
38    Maximum observed error in [-pi/2, pi/2], where argument is not reduced,
39    is 2.87 ULP:
40    _ZGVsMxv_sin (0x1.921d5c6a07142p+0) got 0x1.fffffffa7dc02p-1
41 				      want 0x1.fffffffa7dc05p-1
42    Maximum observed error in the entire non-special domain ([-2^23, 2^23])
43    is 3.22 ULP:
44    _ZGVsMxv_sin (0x1.5702447b6f17bp+22) got 0x1.ffdcd125c84fbp-3
45 				       want 0x1.ffdcd125c84f8p-3.  */
46 svfloat64_t SV_NAME_D1 (sin) (svfloat64_t x, const svbool_t pg)
47 {
48   const struct data *d = ptr_barrier (&data);
49 
50   /* Load some values in quad-word chunks to minimise memory access.  */
51   const svbool_t ptrue = svptrue_b64 ();
52   svfloat64_t shift = sv_f64 (d->shift);
53   svfloat64_t inv_pi_and_pi1 = svld1rq (ptrue, &d->inv_pi);
54   svfloat64_t pi2_and_pi3 = svld1rq (ptrue, &d->pi_2);
55 
56   /* n = rint(|x|/pi).  */
57   svfloat64_t n = svmla_lane (shift, x, inv_pi_and_pi1, 0);
58   svuint64_t odd = svlsl_x (pg, svreinterpret_u64 (n), 63);
59   n = svsub_x (pg, n, shift);
60 
61   /* r = |x| - n*(pi/2)  (range reduction into -pi/2 .. pi/2).  */
62   svfloat64_t r = x;
63   r = svmls_lane (r, n, inv_pi_and_pi1, 1);
64   r = svmls_lane (r, n, pi2_and_pi3, 0);
65   r = svmls_lane (r, n, pi2_and_pi3, 1);
66 
67   /* sin(r) poly approx.  */
68   svfloat64_t r2 = svmul_x (pg, r, r);
69   svfloat64_t r3 = svmul_x (pg, r2, r);
70   svfloat64_t r4 = svmul_x (pg, r2, r2);
71 
72   svfloat64_t t1 = svmla_x (pg, C (4), C (5), r2);
73   svfloat64_t t2 = svmla_x (pg, C (2), C (3), r2);
74   svfloat64_t t3 = svmla_x (pg, C (0), C (1), r2);
75 
76   svfloat64_t y = svmla_x (pg, t1, C (6), r4);
77   y = svmla_x (pg, t2, y, r4);
78   y = svmla_x (pg, t3, y, r4);
79   y = svmla_x (pg, r, y, r3);
80 
81   svbool_t cmp = svacle (pg, x, d->range_val);
82   cmp = svnot_z (pg, cmp);
83   if (unlikely (svptest_any (pg, cmp)))
84     return special_case (x,
85 			 svreinterpret_f64 (sveor_z (
86 			     svnot_z (pg, cmp), svreinterpret_u64 (y), odd)),
87 			 cmp);
88 
89   /* Copy sign.  */
90   return svreinterpret_f64 (sveor_z (pg, svreinterpret_u64 (y), odd));
91 }
92 
93 PL_SIG (SV, D, 1, sin, -3.1, 3.1)
94 PL_TEST_ULP (SV_NAME_D1 (sin), 2.73)
95 PL_TEST_SYM_INTERVAL (SV_NAME_D1 (sin), 0, 0x1p23, 1000000)
96 PL_TEST_SYM_INTERVAL (SV_NAME_D1 (sin), 0x1p23, inf, 10000)
97