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