xref: /freebsd-src/contrib/arm-optimized-routines/math/aarch64/advsimd/cospif.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1 /*
2  * Single-precision vector cospi function.
3  *
4  * Copyright (c) 2023-2024, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "mathlib.h"
9 #include "v_math.h"
10 #include "v_poly_f32.h"
11 #include "test_sig.h"
12 #include "test_defs.h"
13 
14 static const struct data
15 {
16   float32x4_t poly[6];
17   float32x4_t range_val;
18 } data = {
19   /* Taylor series coefficents for sin(pi * x).  */
20   .poly = { V4 (0x1.921fb6p1f), V4 (-0x1.4abbcep2f), V4 (0x1.466bc6p1f),
21 	    V4 (-0x1.32d2ccp-1f), V4 (0x1.50783p-4f), V4 (-0x1.e30750p-8f) },
22   .range_val = V4 (0x1p31f),
23 };
24 
25 static float32x4_t VPCS_ATTR NOINLINE
26 special_case (float32x4_t x, float32x4_t y, uint32x4_t odd, uint32x4_t cmp)
27 {
28   y = vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));
29   return v_call_f32 (arm_math_cospif, x, y, cmp);
30 }
31 
32 /* Approximation for vector single-precision cospi(x)
33     Maximum Error: 3.17 ULP:
34     _ZGVnN4v_cospif(0x1.d341a8p-5) got 0x1.f7cd56p-1
35 				  want 0x1.f7cd5p-1.  */
36 float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (cospi) (float32x4_t x)
37 {
38   const struct data *d = ptr_barrier (&data);
39 
40 #if WANT_SIMD_EXCEPT
41   float32x4_t r = vabsq_f32 (x);
42   uint32x4_t cmp = vcaleq_f32 (v_f32 (0x1p32f), x);
43 
44   /* When WANT_SIMD_EXCEPT = 1, special lanes should be zero'd
45      to avoid them overflowing and throwing exceptions.  */
46   r = v_zerofy_f32 (r, cmp);
47   uint32x4_t odd = vshlq_n_u32 (vcvtnq_u32_f32 (r), 31);
48 
49 #else
50   float32x4_t r = x;
51   uint32x4_t cmp = vcageq_f32 (r, d->range_val);
52 
53   uint32x4_t odd
54       = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtaq_s32_f32 (r)), 31);
55 
56 #endif
57 
58   /* r = x - rint(x).  */
59   r = vsubq_f32 (r, vrndaq_f32 (r));
60 
61   /* cospi(x) = sinpi(0.5 - abs(x)) for values -1/2 .. 1/2.  */
62   r = vsubq_f32 (v_f32 (0.5f), vabsq_f32 (r));
63 
64   /* Pairwise Horner approximation for y = sin(r * pi).  */
65   float32x4_t r2 = vmulq_f32 (r, r);
66   float32x4_t r4 = vmulq_f32 (r2, r2);
67   float32x4_t y = vmulq_f32 (v_pw_horner_5_f32 (r2, r4, d->poly), r);
68 
69   /* Fallback to scalar.  */
70   if (unlikely (v_any_u32 (cmp)))
71     return special_case (x, y, odd, cmp);
72 
73   /* Reintroduce the sign bit for inputs which round to odd.  */
74   return vreinterpretq_f32_u32 (veorq_u32 (vreinterpretq_u32_f32 (y), odd));
75 }
76 
77 HALF_WIDTH_ALIAS_F1 (cospi)
78 
79 #if WANT_TRIGPI_TESTS
80 TEST_ULP (V_NAME_F1 (cospi), 2.67)
81 TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (cospi), WANT_SIMD_EXCEPT)
82 TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0, 0x1p-31, 5000)
83 TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0x1p-31, 0.5, 10000)
84 TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0.5, 0x1p32f, 10000)
85 TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0x1p32f, inf, 10000)
86 #endif
87