xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/sinpi.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision SVE sinpi(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2023-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 "mathlib.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner #include "sv_poly_f64.h"
13*f3087befSAndrew Turner 
14*f3087befSAndrew Turner static const struct data
15*f3087befSAndrew Turner {
16*f3087befSAndrew Turner   double poly[10], range_val;
17*f3087befSAndrew Turner } data = {
18*f3087befSAndrew Turner   /* Polynomial coefficients generated using Remez algorithm,
19*f3087befSAndrew Turner      see sinpi.sollya for details.  */
20*f3087befSAndrew Turner   .poly = { 0x1.921fb54442d184p1, -0x1.4abbce625be53p2, 0x1.466bc6775ab16p1,
21*f3087befSAndrew Turner 	    -0x1.32d2cce62dc33p-1, 0x1.507834891188ep-4, -0x1.e30750a28c88ep-8,
22*f3087befSAndrew Turner 	    0x1.e8f48308acda4p-12, -0x1.6fc0032b3c29fp-16,
23*f3087befSAndrew Turner 	    0x1.af86ae521260bp-21, -0x1.012a9870eeb7dp-25 },
24*f3087befSAndrew Turner   .range_val = 0x1p63,
25*f3087befSAndrew Turner };
26*f3087befSAndrew Turner 
27*f3087befSAndrew Turner /* A fast SVE implementation of sinpi.
28*f3087befSAndrew Turner    Maximum error 3.10 ULP:
29*f3087befSAndrew Turner    _ZGVsMxv_sinpi(0x1.df1a14f1b235p-2) got 0x1.fd64f541606cp-1
30*f3087befSAndrew Turner 				      want 0x1.fd64f541606c3p-1.  */
SV_NAME_D1(sinpi)31*f3087befSAndrew Turner svfloat64_t SV_NAME_D1 (sinpi) (svfloat64_t x, const svbool_t pg)
32*f3087befSAndrew Turner {
33*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
34*f3087befSAndrew Turner 
35*f3087befSAndrew Turner   /* range reduction into -1/2 .. 1/2)
36*f3087befSAndrew Turner      with n = rint(x) and r = r - n.  */
37*f3087befSAndrew Turner   svfloat64_t n = svrinta_x (pg, x);
38*f3087befSAndrew Turner   svfloat64_t r = svsub_x (pg, x, n);
39*f3087befSAndrew Turner 
40*f3087befSAndrew Turner   /* Result should be negated based on if n is odd or not.  */
41*f3087befSAndrew Turner   svbool_t cmp = svaclt (pg, x, d->range_val);
42*f3087befSAndrew Turner   svuint64_t intn = svreinterpret_u64 (svcvt_s64_z (pg, n));
43*f3087befSAndrew Turner   svuint64_t sign = svlsl_z (cmp, intn, 63);
44*f3087befSAndrew Turner 
45*f3087befSAndrew Turner   /* y = sin(r).  */
46*f3087befSAndrew Turner   svfloat64_t r2 = svmul_x (pg, r, r);
47*f3087befSAndrew Turner   svfloat64_t r4 = svmul_x (pg, r2, r2);
48*f3087befSAndrew Turner   svfloat64_t y = sv_pw_horner_9_f64_x (pg, r2, r4, d->poly);
49*f3087befSAndrew Turner   y = svmul_x (pg, y, r);
50*f3087befSAndrew Turner 
51*f3087befSAndrew Turner   return svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (y), sign));
52*f3087befSAndrew Turner }
53*f3087befSAndrew Turner 
54*f3087befSAndrew Turner #if WANT_TRIGPI_TESTS
55*f3087befSAndrew Turner TEST_ULP (SV_NAME_D1 (sinpi), 2.61)
56*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D1 (sinpi))
57*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0, 0x1p-63, 5000)
58*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0x1p-63, 0.5, 10000)
59*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0.5, 0x1p51, 10000)
60*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (sinpi), 0x1p51, inf, 10000)
61*f3087befSAndrew Turner #endif
62*f3087befSAndrew Turner CLOSE_SVE_ATTR
63