xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_sinpi_3u1.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner  * Double-precision SVE sinpi(x) function.
3*5a02ffc3SAndrew Turner  *
4*5a02ffc3SAndrew Turner  * Copyright (c) 2023, Arm Limited.
5*5a02ffc3SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*5a02ffc3SAndrew Turner  */
7*5a02ffc3SAndrew Turner 
8*5a02ffc3SAndrew Turner #include "mathlib.h"
9*5a02ffc3SAndrew Turner #include "sv_math.h"
10*5a02ffc3SAndrew Turner #include "pl_sig.h"
11*5a02ffc3SAndrew Turner #include "pl_test.h"
12*5a02ffc3SAndrew Turner #include "poly_sve_f64.h"
13*5a02ffc3SAndrew Turner 
14*5a02ffc3SAndrew Turner static const struct data
15*5a02ffc3SAndrew Turner {
16*5a02ffc3SAndrew Turner   double poly[10];
17*5a02ffc3SAndrew Turner } data = {
18*5a02ffc3SAndrew Turner   /* Polynomial coefficients generated using Remez algorithm,
19*5a02ffc3SAndrew Turner      see sinpi.sollya for details.  */
20*5a02ffc3SAndrew Turner   .poly = { 0x1.921fb54442d184p1, -0x1.4abbce625be53p2, 0x1.466bc6775ab16p1,
21*5a02ffc3SAndrew Turner 	    -0x1.32d2cce62dc33p-1, 0x1.507834891188ep-4, -0x1.e30750a28c88ep-8,
22*5a02ffc3SAndrew Turner 	    0x1.e8f48308acda4p-12, -0x1.6fc0032b3c29fp-16,
23*5a02ffc3SAndrew Turner 	    0x1.af86ae521260bp-21, -0x1.012a9870eeb7dp-25 },
24*5a02ffc3SAndrew Turner };
25*5a02ffc3SAndrew Turner 
26*5a02ffc3SAndrew Turner /* A fast SVE implementation of sinpi.
27*5a02ffc3SAndrew Turner    Maximum error 3.10 ULP:
28*5a02ffc3SAndrew Turner    _ZGVsMxv_sinpi(0x1.df1a14f1b235p-2) got 0x1.fd64f541606cp-1
29*5a02ffc3SAndrew Turner 				      want 0x1.fd64f541606c3p-1.  */
SV_NAME_D1(sinpi)30*5a02ffc3SAndrew Turner svfloat64_t SV_NAME_D1 (sinpi) (svfloat64_t x, const svbool_t pg)
31*5a02ffc3SAndrew Turner {
32*5a02ffc3SAndrew Turner   const struct data *d = ptr_barrier (&data);
33*5a02ffc3SAndrew Turner 
34*5a02ffc3SAndrew Turner   /* range reduction into -1/2 .. 1/2)
35*5a02ffc3SAndrew Turner      with n = rint(x) and r = r - n.  */
36*5a02ffc3SAndrew Turner   svfloat64_t n = svrinta_x (pg, x);
37*5a02ffc3SAndrew Turner   svfloat64_t r = svsub_x (pg, x, n);
38*5a02ffc3SAndrew Turner 
39*5a02ffc3SAndrew Turner   /* Result should be negated based on if n is odd or not.  */
40*5a02ffc3SAndrew Turner   svuint64_t intn = svreinterpret_u64 (svcvt_s64_x (pg, n));
41*5a02ffc3SAndrew Turner   svuint64_t sign = svlsl_z (pg, intn, 63);
42*5a02ffc3SAndrew Turner 
43*5a02ffc3SAndrew Turner   /* y = sin(r).  */
44*5a02ffc3SAndrew Turner   svfloat64_t r2 = svmul_x (pg, r, r);
45*5a02ffc3SAndrew Turner   svfloat64_t r4 = svmul_x (pg, r2, r2);
46*5a02ffc3SAndrew Turner   svfloat64_t y = sv_pw_horner_9_f64_x (pg, r2, r4, d->poly);
47*5a02ffc3SAndrew Turner   y = svmul_x (pg, y, r);
48*5a02ffc3SAndrew Turner 
49*5a02ffc3SAndrew Turner   return svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (y), sign));
50*5a02ffc3SAndrew Turner }
51*5a02ffc3SAndrew Turner 
52*5a02ffc3SAndrew Turner PL_SIG (SV, D, 1, sinpi, -0.9, 0.9)
53*5a02ffc3SAndrew Turner PL_TEST_ULP (SV_NAME_D1 (sinpi), 2.61)
54*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0, 0x1p-63, 5000)
55*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0x1p-63, 0.5, 10000)
56*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0.5, 0x1p51, 10000)
57*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0x1p51, inf, 10000)
58