1 /*
2 * Single-precision vector cospi function.
3 *
4 * Copyright (c) 2023, 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 "poly_advsimd_f32.h"
11 #include "pl_sig.h"
12 #include "pl_test.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
special_case(float32x4_t x,float32x4_t y,uint32x4_t odd,uint32x4_t cmp)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 (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. */
V_NAME_F1(cospi)36 float32x4_t VPCS_ATTR 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 PL_SIG (V, F, 1, cospi, -0.9, 0.9)
78 PL_TEST_ULP (V_NAME_F1 (cospi), 2.67)
79 PL_TEST_EXPECT_FENV (V_NAME_F1 (cospi), WANT_SIMD_EXCEPT)
80 PL_TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0, 0x1p-31, 5000)
81 PL_TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0x1p-31, 0.5, 10000)
82 PL_TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0.5, 0x1p32f, 10000)
83 PL_TEST_SYM_INTERVAL (V_NAME_F1 (cospi), 0x1p32f, inf, 10000)
84