xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/v_sincospif_common.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Helper for Single-precision vector sincospi function.
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 #include "mathlib.h"
8*f3087befSAndrew Turner #include "v_math.h"
9*f3087befSAndrew Turner #include "v_poly_f32.h"
10*f3087befSAndrew Turner 
11*f3087befSAndrew Turner const static struct v_sincospif_data
12*f3087befSAndrew Turner {
13*f3087befSAndrew Turner   float32x4_t poly[6], range_val;
14*f3087befSAndrew Turner } v_sincospif_data = {
15*f3087befSAndrew Turner   /* Taylor series coefficents for sin(pi * x).  */
16*f3087befSAndrew Turner   .poly = { V4 (0x1.921fb6p1f), V4 (-0x1.4abbcep2f), V4 (0x1.466bc6p1f),
17*f3087befSAndrew Turner 	    V4 (-0x1.32d2ccp-1f), V4 (0x1.50783p-4f), V4 (-0x1.e30750p-8f) },
18*f3087befSAndrew Turner   .range_val = V4 (0x1p31f),
19*f3087befSAndrew Turner };
20*f3087befSAndrew Turner 
21*f3087befSAndrew Turner /* Single-precision vector function allowing calculation of both sinpi and
22*f3087befSAndrew Turner    cospi in one function call, using shared argument reduction and polynomials.
23*f3087befSAndrew Turner    Worst-case error for sin is 3.04 ULP:
24*f3087befSAndrew Turner    _ZGVnN4v_sincospif_sin(0x1.1d341ap-1) got 0x1.f7cd56p-1 want 0x1.f7cd5p-1.
25*f3087befSAndrew Turner    Worst-case error for cos is 3.18 ULP:
26*f3087befSAndrew Turner    _ZGVnN4v_sincospif_cos(0x1.d341a8p-5) got 0x1.f7cd56p-1 want 0x1.f7cd5p-1.
27*f3087befSAndrew Turner  */
28*f3087befSAndrew Turner static inline float32x4x2_t
v_sincospif_inline(float32x4_t x,const struct v_sincospif_data * d)29*f3087befSAndrew Turner v_sincospif_inline (float32x4_t x, const struct v_sincospif_data *d)
30*f3087befSAndrew Turner {
31*f3087befSAndrew Turner   /* If r is odd, the sign of the result should be inverted for sinpi and
32*f3087befSAndrew Turner      reintroduced for cospi.  */
33*f3087befSAndrew Turner   uint32x4_t cmp = vcgeq_f32 (x, d->range_val);
34*f3087befSAndrew Turner   uint32x4_t odd = vshlq_n_u32 (
35*f3087befSAndrew Turner       vbicq_u32 (vreinterpretq_u32_s32 (vcvtaq_s32_f32 (x)), cmp), 31);
36*f3087befSAndrew Turner 
37*f3087befSAndrew Turner   /* r = x - rint(x).  */
38*f3087befSAndrew Turner   float32x4_t sr = vsubq_f32 (x, vrndaq_f32 (x));
39*f3087befSAndrew Turner   /* cospi(x) = sinpi(0.5 - abs(x)) for values -1/2 .. 1/2.  */
40*f3087befSAndrew Turner   float32x4_t cr = vsubq_f32 (v_f32 (0.5f), vabsq_f32 (sr));
41*f3087befSAndrew Turner 
42*f3087befSAndrew Turner   /* Pairwise Horner approximation for y = sin(r * pi).  */
43*f3087befSAndrew Turner   float32x4_t sr2 = vmulq_f32 (sr, sr);
44*f3087befSAndrew Turner   float32x4_t sr4 = vmulq_f32 (sr2, sr2);
45*f3087befSAndrew Turner   float32x4_t cr2 = vmulq_f32 (cr, cr);
46*f3087befSAndrew Turner   float32x4_t cr4 = vmulq_f32 (cr2, cr2);
47*f3087befSAndrew Turner 
48*f3087befSAndrew Turner   float32x4_t ss = vmulq_f32 (v_pw_horner_5_f32 (sr2, sr4, d->poly), sr);
49*f3087befSAndrew Turner   float32x4_t cc = vmulq_f32 (v_pw_horner_5_f32 (cr2, cr4, d->poly), cr);
50*f3087befSAndrew Turner 
51*f3087befSAndrew Turner   float32x4_t sinpix
52*f3087befSAndrew Turner       = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (ss), odd));
53*f3087befSAndrew Turner   float32x4_t cospix
54*f3087befSAndrew Turner       = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (cc), odd));
55*f3087befSAndrew Turner 
56*f3087befSAndrew Turner   return (float32x4x2_t){ sinpix, cospix };
57*f3087befSAndrew Turner }
58