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