xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/sincosf.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  * Single-precision vector sincos function.
3  *
4  * Copyright (c) 2023-2024, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 /* Define _GNU_SOURCE in order to include sincosf declaration. If building
9    pre-GLIBC 2.1, or on a non-GNU conforming system, this routine will need to
10    be linked against the scalar sincosf from math/.  */
11 #define _GNU_SOURCE
12 #include <math.h>
13 
14 #include "v_sincosf_common.h"
15 #include "v_math.h"
16 #include "test_defs.h"
17 
18 /* sincos not available for all scalar libm implementations.  */
19 #if defined(_MSC_VER) || !defined(__GLIBC__)
20 static void
21 sincosf (float x, float *out_sin, float *out_cos)
22 {
23   *out_sin = sinf (x);
24   *out_cos = cosf (x);
25 }
26 #endif
27 
28 static void VPCS_ATTR NOINLINE
29 special_case (float32x4_t x, uint32x4_t special, float *out_sin,
30 	      float *out_cos)
31 {
32   for (int i = 0; i < 4; i++)
33     if (special[i])
34       sincosf (x[i], out_sin + i, out_cos + i);
35 }
36 
37 /* Single-precision vector function allowing calculation of both sin and cos in
38    one function call, using shared argument reduction and separate low-order
39    polynomials.
40    Worst-case error for sin is 1.67 ULP:
41    v_sincosf_sin(0x1.c704c4p+19) got 0x1.fff698p-5 want 0x1.fff69cp-5
42    Worst-case error for cos is 1.81 ULP:
43    v_sincosf_cos(0x1.e506fp+19) got -0x1.ffec6ep-6 want -0x1.ffec72p-6.  */
44 VPCS_ATTR void
45 _ZGVnN4vl4l4_sincosf (float32x4_t x, float *out_sin, float *out_cos)
46 {
47   const struct v_sincosf_data *d = ptr_barrier (&v_sincosf_data);
48   uint32x4_t special = check_ge_rangeval (x, d);
49 
50   float32x4x2_t sc = v_sincosf_inline (x, d);
51 
52   vst1q_f32 (out_sin, sc.val[0]);
53   vst1q_f32 (out_cos, sc.val[1]);
54 
55   if (unlikely (v_any_u32 (special)))
56     special_case (x, special, out_sin, out_cos);
57 }
58 
59 TEST_DISABLE_FENV (_ZGVnN4v_sincosf_sin)
60 TEST_DISABLE_FENV (_ZGVnN4v_sincosf_cos)
61 TEST_ULP (_ZGVnN4v_sincosf_sin, 1.17)
62 TEST_ULP (_ZGVnN4v_sincosf_cos, 1.31)
63 #define V_SINCOSF_INTERVAL(lo, hi, n)                                         \
64   TEST_INTERVAL (_ZGVnN4v_sincosf_sin, lo, hi, n)                             \
65   TEST_INTERVAL (_ZGVnN4v_sincosf_cos, lo, hi, n)
66 V_SINCOSF_INTERVAL (0, 0x1p-31, 50000)
67 V_SINCOSF_INTERVAL (0x1p-31, 0x1p20, 500000)
68 V_SINCOSF_INTERVAL (0x1p20, inf, 10000)
69