Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/cos.c
48378 views
1
/*
2
* Double-precision SVE cos(x) function.
3
*
4
* Copyright (c) 2019-2024, Arm Limited.
5
* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6
*/
7
8
#include "sv_math.h"
9
#include "test_sig.h"
10
#include "test_defs.h"
11
12
static const struct data
13
{
14
double inv_pio2, pio2_1, pio2_2, pio2_3, shift;
15
} data = {
16
/* Polynomial coefficients are hardwired in FTMAD instructions. */
17
.inv_pio2 = 0x1.45f306dc9c882p-1,
18
.pio2_1 = 0x1.921fb50000000p+0,
19
.pio2_2 = 0x1.110b460000000p-26,
20
.pio2_3 = 0x1.1a62633145c07p-54,
21
/* Original shift used in AdvSIMD cos,
22
plus a contribution to set the bit #0 of q
23
as expected by trigonometric instructions. */
24
.shift = 0x1.8000000000001p52
25
};
26
27
#define RangeVal 0x4160000000000000 /* asuint64 (0x1p23). */
28
29
static svfloat64_t NOINLINE
30
special_case (svfloat64_t x, svfloat64_t y, svbool_t oob)
31
{
32
return sv_call_f64 (cos, x, y, oob);
33
}
34
35
/* A fast SVE implementation of cos based on trigonometric
36
instructions (FTMAD, FTSSEL, FTSMUL).
37
Maximum measured error: 2.108 ULPs.
38
SV_NAME_D1 (cos)(0x1.9b0ba158c98f3p+7) got -0x1.fddd4c65c7f07p-3
39
want -0x1.fddd4c65c7f05p-3. */
40
svfloat64_t SV_NAME_D1 (cos) (svfloat64_t x, const svbool_t pg)
41
{
42
const struct data *d = ptr_barrier (&data);
43
44
svfloat64_t r = svabs_x (pg, x);
45
svbool_t oob = svcmpge (pg, svreinterpret_u64 (r), RangeVal);
46
47
/* Load some constants in quad-word chunks to minimise memory access. */
48
svbool_t ptrue = svptrue_b64 ();
49
svfloat64_t invpio2_and_pio2_1 = svld1rq (ptrue, &d->inv_pio2);
50
svfloat64_t pio2_23 = svld1rq (ptrue, &d->pio2_2);
51
52
/* n = rint(|x|/(pi/2)). */
53
svfloat64_t q = svmla_lane (sv_f64 (d->shift), r, invpio2_and_pio2_1, 0);
54
svfloat64_t n = svsub_x (pg, q, d->shift);
55
56
/* r = |x| - n*(pi/2) (range reduction into -pi/4 .. pi/4). */
57
r = svmls_lane (r, n, invpio2_and_pio2_1, 1);
58
r = svmls_lane (r, n, pio2_23, 0);
59
r = svmls_lane (r, n, pio2_23, 1);
60
61
/* cos(r) poly approx. */
62
svfloat64_t r2 = svtsmul (r, svreinterpret_u64 (q));
63
svfloat64_t y = sv_f64 (0.0);
64
y = svtmad (y, r2, 7);
65
y = svtmad (y, r2, 6);
66
y = svtmad (y, r2, 5);
67
y = svtmad (y, r2, 4);
68
y = svtmad (y, r2, 3);
69
y = svtmad (y, r2, 2);
70
y = svtmad (y, r2, 1);
71
y = svtmad (y, r2, 0);
72
73
/* Final multiplicative factor: 1.0 or x depending on bit #0 of q. */
74
svfloat64_t f = svtssel (r, svreinterpret_u64 (q));
75
76
if (unlikely (svptest_any (pg, oob)))
77
return special_case (x, svmul_x (svnot_z (pg, oob), y, f), oob);
78
79
/* Apply factor. */
80
return svmul_x (pg, f, y);
81
}
82
83
TEST_SIG (SV, D, 1, cos, -3.1, 3.1)
84
TEST_ULP (SV_NAME_D1 (cos), 1.61)
85
TEST_DISABLE_FENV (SV_NAME_D1 (cos))
86
TEST_INTERVAL (SV_NAME_D1 (cos), 0, 0xffff0000, 10000)
87
TEST_INTERVAL (SV_NAME_D1 (cos), 0x1p-4, 0x1p4, 500000)
88
CLOSE_SVE_ATTR
89
90