1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Helper for single-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_f32.h"
10*f3087befSAndrew Turner
11*f3087befSAndrew Turner const static struct sv_sincospif_data
12*f3087befSAndrew Turner {
13*f3087befSAndrew Turner float c0, c2, c4;
14*f3087befSAndrew Turner float c1, c3, c5;
15*f3087befSAndrew Turner float range_val;
16*f3087befSAndrew Turner } sv_sincospif_data = {
17*f3087befSAndrew Turner /* Taylor series coefficents for sin(pi * x). */
18*f3087befSAndrew Turner .c0 = 0x1.921fb6p1f,
19*f3087befSAndrew Turner .c1 = -0x1.4abbcep2f,
20*f3087befSAndrew Turner .c2 = 0x1.466bc6p1f,
21*f3087befSAndrew Turner .c3 = -0x1.32d2ccp-1f,
22*f3087befSAndrew Turner .c4 = 0x1.50783p-4f,
23*f3087befSAndrew Turner .c5 = -0x1.e30750p-8f,
24*f3087befSAndrew Turner /* Exclusive upper bound for a signed integer. */
25*f3087befSAndrew Turner .range_val = 0x1p31f,
26*f3087befSAndrew Turner };
27*f3087befSAndrew Turner
28*f3087befSAndrew Turner /* Single-precision vector function allowing calculation of both sinpi and
29*f3087befSAndrew Turner cospi in one function call, using shared argument reduction and polynomials.
30*f3087befSAndrew Turner Worst-case error for sin is 3.04 ULP:
31*f3087befSAndrew Turner _ZGVsMxvl4l4_sincospif_sin(0x1.b51b8p-2) got 0x1.f28b5ep-1 want
32*f3087befSAndrew Turner 0x1.f28b58p-1.
33*f3087befSAndrew Turner Worst-case error for cos is 3.18 ULP:
34*f3087befSAndrew Turner _ZGVsMxvl4l4_sincospif_cos(0x1.d341a8p-5) got 0x1.f7cd56p-1 want
35*f3087befSAndrew Turner 0x1.f7cd5p-1. */
36*f3087befSAndrew Turner static inline svfloat32x2_t
sv_sincospif_inline(svbool_t pg,svfloat32_t x,const struct sv_sincospif_data * d)37*f3087befSAndrew Turner sv_sincospif_inline (svbool_t pg, svfloat32_t x,
38*f3087befSAndrew Turner const struct sv_sincospif_data *d)
39*f3087befSAndrew Turner {
40*f3087befSAndrew Turner const svbool_t pt = svptrue_b32 ();
41*f3087befSAndrew Turner
42*f3087befSAndrew Turner /* r = x - rint(x). */
43*f3087befSAndrew Turner svfloat32_t rx = svrinta_x (pg, x);
44*f3087befSAndrew Turner svfloat32_t sr = svsub_x (pt, x, rx);
45*f3087befSAndrew Turner
46*f3087befSAndrew Turner /* cospi(x) = sinpi(0.5 - abs(r)) for values -1/2 .. 1/2. */
47*f3087befSAndrew Turner svfloat32_t cr = svsubr_x (pt, svabs_x (pg, sr), 0.5f);
48*f3087befSAndrew Turner
49*f3087befSAndrew Turner /* Pairwise Horner approximation for y = sin(r * pi). */
50*f3087befSAndrew Turner svfloat32_t sr2 = svmul_x (pt, sr, sr);
51*f3087befSAndrew Turner svfloat32_t sr4 = svmul_x (pt, sr2, sr2);
52*f3087befSAndrew Turner svfloat32_t cr2 = svmul_x (pt, cr, cr);
53*f3087befSAndrew Turner svfloat32_t cr4 = svmul_x (pt, cr2, cr2);
54*f3087befSAndrew Turner
55*f3087befSAndrew Turner /* If rint(x) is odd, the sign of the result should be inverted for sinpi and
56*f3087befSAndrew Turner re-introduced for cospi. cmp filters rxs that saturate to max sint. */
57*f3087befSAndrew Turner svbool_t cmp = svaclt (pg, x, d->range_val);
58*f3087befSAndrew Turner svuint32_t odd = svlsl_x (pt, svreinterpret_u32 (svcvt_s32_z (pg, rx)), 31);
59*f3087befSAndrew Turner sr = svreinterpret_f32 (sveor_x (pt, svreinterpret_u32 (sr), odd));
60*f3087befSAndrew Turner cr = svreinterpret_f32 (sveor_m (cmp, svreinterpret_u32 (cr), odd));
61*f3087befSAndrew Turner
62*f3087befSAndrew Turner svfloat32_t c135 = svld1rq_f32 (svptrue_b32 (), &d->c1);
63*f3087befSAndrew Turner
64*f3087befSAndrew Turner svfloat32_t sp01 = svmla_lane (sv_f32 (d->c0), sr2, c135, 0);
65*f3087befSAndrew Turner svfloat32_t sp23 = svmla_lane (sv_f32 (d->c2), sr2, c135, 1);
66*f3087befSAndrew Turner svfloat32_t sp45 = svmla_lane (sv_f32 (d->c4), sr2, c135, 2);
67*f3087befSAndrew Turner
68*f3087befSAndrew Turner svfloat32_t cp01 = svmla_lane (sv_f32 (d->c0), cr2, c135, 0);
69*f3087befSAndrew Turner svfloat32_t cp23 = svmla_lane (sv_f32 (d->c2), cr2, c135, 1);
70*f3087befSAndrew Turner svfloat32_t cp45 = svmla_lane (sv_f32 (d->c4), cr2, c135, 2);
71*f3087befSAndrew Turner
72*f3087befSAndrew Turner svfloat32_t sp = svmla_x (pg, sp23, sr4, sp45);
73*f3087befSAndrew Turner svfloat32_t cp = svmla_x (pg, cp23, cr4, cp45);
74*f3087befSAndrew Turner
75*f3087befSAndrew Turner sp = svmla_x (pg, sp01, sr4, sp);
76*f3087befSAndrew Turner cp = svmla_x (pg, cp01, cr4, cp);
77*f3087befSAndrew Turner
78*f3087befSAndrew Turner svfloat32_t sinpix = svmul_x (pt, sp, sr);
79*f3087befSAndrew Turner svfloat32_t cospix = svmul_x (pt, cp, cr);
80*f3087befSAndrew Turner
81*f3087befSAndrew Turner return svcreate2 (sinpix, cospix);
82*f3087befSAndrew Turner }
83