2 * Single-precision vector cos function.
4 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 * See https://llvm.org/LICENSE.txt for license information.
6 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13 static const float Poly
[] = {
20 #define Pi1 v_f32 (0x1.921fb6p+1f)
21 #define Pi2 v_f32 (-0x1.777a5cp-24f)
22 #define Pi3 v_f32 (-0x1.ee59dap-49f)
23 #define A3 v_f32 (Poly[3])
24 #define A5 v_f32 (Poly[2])
25 #define A7 v_f32 (Poly[1])
26 #define A9 v_f32 (Poly[0])
27 #define RangeVal v_f32 (0x1p20f)
28 #define InvPi v_f32 (0x1.45f306p-2f)
29 #define Shift v_f32 (0x1.8p+23f)
30 #define AbsMask v_u32 (0x7fffffff)
31 #define HalfPi v_f32 (0x1.921fb6p0f)
35 specialcase (v_f32_t x
, v_f32_t y
, v_u32_t cmp
)
37 /* Fall back to scalar code. */
38 return v_call_f32 (cosf
, x
, y
, cmp
);
43 V_NAME(cosf
) (v_f32_t x
)
48 r
= v_as_f32_u32 (v_as_u32_f32 (x
) & AbsMask
);
49 cmp
= v_cond_u32 (v_as_u32_f32 (r
) >= v_as_u32_f32 (RangeVal
));
51 /* n = rint((|x|+pi/2)/pi) - 0.5 */
52 n
= v_fma_f32 (InvPi
, r
+ HalfPi
, Shift
);
53 odd
= v_as_u32_f32 (n
) << 31;
57 /* r = |x| - n*pi (range reduction into -pi/2 .. pi/2) */
58 r
= v_fma_f32 (-Pi1
, n
, r
);
59 r
= v_fma_f32 (-Pi2
, n
, r
);
60 r
= v_fma_f32 (-Pi3
, n
, r
);
64 y
= v_fma_f32 (A9
, r2
, A7
);
65 y
= v_fma_f32 (y
, r2
, A5
);
66 y
= v_fma_f32 (y
, r2
, A3
);
67 y
= v_fma_f32 (y
* r2
, r
, r
);
70 y
= v_as_f32_u32 (v_as_u32_f32 (y
) ^ odd
);
72 if (unlikely (v_any_u32 (cmp
)))
73 return specialcase (x
, y
, cmp
);