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