xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_sinpif_3u.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner  * Single-precision vector sinpi function.
3*5a02ffc3SAndrew Turner  *
4*5a02ffc3SAndrew Turner  * Copyright (c) 2023, Arm Limited.
5*5a02ffc3SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*5a02ffc3SAndrew Turner  */
7*5a02ffc3SAndrew Turner 
8*5a02ffc3SAndrew Turner #include "mathlib.h"
9*5a02ffc3SAndrew Turner #include "v_math.h"
10*5a02ffc3SAndrew Turner #include "poly_advsimd_f32.h"
11*5a02ffc3SAndrew Turner #include "pl_sig.h"
12*5a02ffc3SAndrew Turner #include "pl_test.h"
13*5a02ffc3SAndrew Turner 
14*5a02ffc3SAndrew Turner static const struct data
15*5a02ffc3SAndrew Turner {
16*5a02ffc3SAndrew Turner   float32x4_t poly[6];
17*5a02ffc3SAndrew Turner } data = {
18*5a02ffc3SAndrew Turner   /* Taylor series coefficents for sin(pi * x).  */
19*5a02ffc3SAndrew Turner   .poly = { V4 (0x1.921fb6p1f), V4 (-0x1.4abbcep2f), V4 (0x1.466bc6p1f),
20*5a02ffc3SAndrew Turner 	    V4 (-0x1.32d2ccp-1f), V4 (0x1.50783p-4f), V4 (-0x1.e30750p-8f) },
21*5a02ffc3SAndrew Turner };
22*5a02ffc3SAndrew Turner 
23*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
24*5a02ffc3SAndrew Turner # define TinyBound v_u32 (0x30000000) /* asuint32(0x1p-31f).  */
25*5a02ffc3SAndrew Turner # define Thresh v_u32 (0x1f000000)    /* asuint32(0x1p31f) - TinyBound.  */
26*5a02ffc3SAndrew Turner 
27*5a02ffc3SAndrew Turner static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,uint32x4_t odd,uint32x4_t cmp)28*5a02ffc3SAndrew Turner special_case (float32x4_t x, float32x4_t y, uint32x4_t odd, uint32x4_t cmp)
29*5a02ffc3SAndrew Turner {
30*5a02ffc3SAndrew Turner   /* Fall back to scalar code.  */
31*5a02ffc3SAndrew Turner   y = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));
32*5a02ffc3SAndrew Turner   return v_call_f32 (sinpif, x, y, cmp);
33*5a02ffc3SAndrew Turner }
34*5a02ffc3SAndrew Turner #endif
35*5a02ffc3SAndrew Turner 
36*5a02ffc3SAndrew Turner /* Approximation for vector single-precision sinpi(x)
37*5a02ffc3SAndrew Turner     Maximum Error 3.03 ULP:
38*5a02ffc3SAndrew Turner     _ZGVnN4v_sinpif(0x1.c597ccp-2) got 0x1.f7cd56p-1
39*5a02ffc3SAndrew Turner 				  want 0x1.f7cd5p-1.  */
V_NAME_F1(sinpi)40*5a02ffc3SAndrew Turner float32x4_t VPCS_ATTR V_NAME_F1 (sinpi) (float32x4_t x)
41*5a02ffc3SAndrew Turner {
42*5a02ffc3SAndrew Turner   const struct data *d = ptr_barrier (&data);
43*5a02ffc3SAndrew Turner 
44*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
45*5a02ffc3SAndrew Turner   uint32x4_t ir = vreinterpretq_u32_f32 (vabsq_f32 (x));
46*5a02ffc3SAndrew Turner   uint32x4_t cmp = vcgeq_u32 (vsubq_u32 (ir, TinyBound), Thresh);
47*5a02ffc3SAndrew Turner 
48*5a02ffc3SAndrew Turner   /* When WANT_SIMD_EXCEPT = 1, special lanes should be set to 0
49*5a02ffc3SAndrew Turner      to avoid them under/overflowing and throwing exceptions.  */
50*5a02ffc3SAndrew Turner   float32x4_t r = v_zerofy_f32 (x, cmp);
51*5a02ffc3SAndrew Turner #else
52*5a02ffc3SAndrew Turner   float32x4_t r = x;
53*5a02ffc3SAndrew Turner #endif
54*5a02ffc3SAndrew Turner 
55*5a02ffc3SAndrew Turner   /* If r is odd, the sign of the result should be inverted.  */
56*5a02ffc3SAndrew Turner   uint32x4_t odd
57*5a02ffc3SAndrew Turner       = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtaq_s32_f32 (r)), 31);
58*5a02ffc3SAndrew Turner 
59*5a02ffc3SAndrew Turner   /* r = x - rint(x). Range reduction to -1/2 .. 1/2.  */
60*5a02ffc3SAndrew Turner   r = vsubq_f32 (r, vrndaq_f32 (r));
61*5a02ffc3SAndrew Turner 
62*5a02ffc3SAndrew Turner   /* Pairwise Horner approximation for y = sin(r * pi).  */
63*5a02ffc3SAndrew Turner   float32x4_t r2 = vmulq_f32 (r, r);
64*5a02ffc3SAndrew Turner   float32x4_t r4 = vmulq_f32 (r2, r2);
65*5a02ffc3SAndrew Turner   float32x4_t y = vmulq_f32 (v_pw_horner_5_f32 (r2, r4, d->poly), r);
66*5a02ffc3SAndrew Turner 
67*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
68*5a02ffc3SAndrew Turner   if (unlikely (v_any_u32 (cmp)))
69*5a02ffc3SAndrew Turner     return special_case (x, y, odd, cmp);
70*5a02ffc3SAndrew Turner #endif
71*5a02ffc3SAndrew Turner 
72*5a02ffc3SAndrew Turner   return vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));
73*5a02ffc3SAndrew Turner }
74*5a02ffc3SAndrew Turner 
75*5a02ffc3SAndrew Turner PL_SIG (V, F, 1, sinpi, -0.9, 0.9)
76*5a02ffc3SAndrew Turner PL_TEST_ULP (V_NAME_F1 (sinpi), 2.54)
77*5a02ffc3SAndrew Turner PL_TEST_EXPECT_FENV (V_NAME_F1 (sinpi), WANT_SIMD_EXCEPT)
78*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_F1 (sinpi), 0, 0x1p-31, 5000)
79*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_F1 (sinpi), 0x1p-31, 0.5, 10000)
80*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_F1 (sinpi), 0.5, 0x1p31f, 10000)
81*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_F1 (sinpi), 0x1p31f, inf, 10000)
82