xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/sv_sincospi_common.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Core approximation for double-precision SVE sincospi
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 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 "sv_poly_f64.h"
10*f3087befSAndrew Turner 
11*f3087befSAndrew Turner static const struct sv_sincospi_data
12*f3087befSAndrew Turner {
13*f3087befSAndrew Turner   double c0, c2, c4, c6, c8;
14*f3087befSAndrew Turner   double c1, c3, c5, c7, c9;
15*f3087befSAndrew Turner   double range_val;
16*f3087befSAndrew Turner } sv_sincospi_data = {
17*f3087befSAndrew Turner   /* Polynomial coefficients generated using Remez algorithm,
18*f3087befSAndrew Turner      see sinpi.sollya for details.  */
19*f3087befSAndrew Turner   .c0 = 0x1.921fb54442d184p1,
20*f3087befSAndrew Turner   .c1 = -0x1.4abbce625be53p2,
21*f3087befSAndrew Turner   .c2 = 0x1.466bc6775ab16p1,
22*f3087befSAndrew Turner   .c3 = -0x1.32d2cce62dc33p-1,
23*f3087befSAndrew Turner   .c4 = 0x1.507834891188ep-4,
24*f3087befSAndrew Turner   .c5 = -0x1.e30750a28c88ep-8,
25*f3087befSAndrew Turner   .c6 = 0x1.e8f48308acda4p-12,
26*f3087befSAndrew Turner   .c7 = -0x1.6fc0032b3c29fp-16,
27*f3087befSAndrew Turner   .c8 = 0x1.af86ae521260bp-21,
28*f3087befSAndrew Turner   .c9 = -0x1.012a9870eeb7dp-25,
29*f3087befSAndrew Turner   /* Exclusive upper bound for a signed integer.  */
30*f3087befSAndrew Turner   .range_val = 0x1p63
31*f3087befSAndrew Turner };
32*f3087befSAndrew Turner 
33*f3087befSAndrew Turner /* Double-precision vector function allowing calculation of both sinpi and
34*f3087befSAndrew Turner    cospi in one function call, using shared argument reduction and polynomials.
35*f3087befSAndrew Turner     Worst-case error for sin is 3.09 ULP:
36*f3087befSAndrew Turner     _ZGVsMxvl8l8_sincospi_sin(0x1.7a41deb4b21e1p+14) got 0x1.fd54d0b327cf1p-1
37*f3087befSAndrew Turner 						    want 0x1.fd54d0b327cf4p-1.
38*f3087befSAndrew Turner    Worst-case error for cos is 3.16 ULP:
39*f3087befSAndrew Turner     _ZGVsMxvl8l8_sincospi_cos(-0x1.11e3c7e284adep-5) got 0x1.fd2da484ff3ffp-1
40*f3087befSAndrew Turner 						    want 0x1.fd2da484ff402p-1.
41*f3087befSAndrew Turner  */
42*f3087befSAndrew Turner static inline svfloat64x2_t
sv_sincospi_inline(svbool_t pg,svfloat64_t x,const struct sv_sincospi_data * d)43*f3087befSAndrew Turner sv_sincospi_inline (svbool_t pg, svfloat64_t x,
44*f3087befSAndrew Turner 		    const struct sv_sincospi_data *d)
45*f3087befSAndrew Turner {
46*f3087befSAndrew Turner   const svbool_t pt = svptrue_b64 ();
47*f3087befSAndrew Turner 
48*f3087befSAndrew Turner   /* r = x - rint(x).  */
49*f3087befSAndrew Turner   /* pt hints unpredicated instruction.  */
50*f3087befSAndrew Turner   svfloat64_t rx = svrinta_x (pg, x);
51*f3087befSAndrew Turner   svfloat64_t sr = svsub_x (pt, x, rx);
52*f3087befSAndrew Turner 
53*f3087befSAndrew Turner   /* cospi(x) = sinpi(0.5 - abs(x)) for values -1/2 .. 1/2.  */
54*f3087befSAndrew Turner   svfloat64_t cr = svsubr_x (pg, svabs_x (pg, sr), 0.5);
55*f3087befSAndrew Turner 
56*f3087befSAndrew Turner   /* Pairwise Horner approximation for y = sin(r * pi).  */
57*f3087befSAndrew Turner   /* pt hints unpredicated instruction.  */
58*f3087befSAndrew Turner   svfloat64_t sr2 = svmul_x (pt, sr, sr);
59*f3087befSAndrew Turner   svfloat64_t cr2 = svmul_x (pt, cr, cr);
60*f3087befSAndrew Turner   svfloat64_t sr4 = svmul_x (pt, sr2, sr2);
61*f3087befSAndrew Turner   svfloat64_t cr4 = svmul_x (pt, cr2, cr2);
62*f3087befSAndrew Turner 
63*f3087befSAndrew Turner   /* If rint(x) is odd, the sign of the result should be inverted for sinpi and
64*f3087befSAndrew Turner     re-introduced for cospi. cmp filters rxs that saturate to max sint.  */
65*f3087befSAndrew Turner   svbool_t cmp = svaclt (pg, x, d->range_val);
66*f3087befSAndrew Turner   svuint64_t odd = svlsl_x (pt, svreinterpret_u64 (svcvt_s64_z (pg, rx)), 63);
67*f3087befSAndrew Turner   sr = svreinterpret_f64 (sveor_x (pt, svreinterpret_u64 (sr), odd));
68*f3087befSAndrew Turner   cr = svreinterpret_f64 (sveor_m (cmp, svreinterpret_u64 (cr), odd));
69*f3087befSAndrew Turner 
70*f3087befSAndrew Turner   svfloat64_t sinpix = svmul_x (
71*f3087befSAndrew Turner       pt, sv_lw_pw_horner_9_f64_x (pg, sr2, sr4, &(d->c0), &(d->c1)), sr);
72*f3087befSAndrew Turner   svfloat64_t cospix = svmul_x (
73*f3087befSAndrew Turner       pt, sv_lw_pw_horner_9_f64_x (pg, cr2, cr4, &(d->c0), &(d->c1)), cr);
74*f3087befSAndrew Turner 
75*f3087befSAndrew Turner   return svcreate2 (sinpix, cospix);
76*f3087befSAndrew Turner }
77