1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Trig family (sin, cos, tan) f64 dispatch.
//! Same threshold/ISA-selection logic as every unary family (`dispatch.rs`).
use super::super::{SimdLevel, detect_simd};
use super::dispatch::{SIMD_THRESHOLD, is_simd_supported};
use super::unary_scalar_f64;
use crate::ops::UnaryOp;
/// Dispatch a trig op (Sin/Cos/Tan) for f64.
///
/// # Safety
/// - `a` and `out` must be valid pointers to `len` elements
#[inline]
pub(super) unsafe fn trig_f64(op: UnaryOp, a: *const f64, out: *mut f64, len: usize) {
let level = detect_simd();
if len < SIMD_THRESHOLD || level == SimdLevel::Scalar || !is_simd_supported(op) {
return unsafe { unary_scalar_f64(op, a, out, len) };
}
unsafe {
#[cfg(target_arch = "x86_64")]
match level {
SimdLevel::Avx512 => super::x86_64::avx512::unary_f64(op, a, out, len),
SimdLevel::Avx2Fma => super::x86_64::avx2::unary_f64(op, a, out, len),
_ => unary_scalar_f64(op, a, out, len),
}
#[cfg(target_arch = "aarch64")]
match level {
SimdLevel::Neon | SimdLevel::NeonFp16 => {
super::aarch64::neon::unary_f64(op, a, out, len)
}
_ => unary_scalar_f64(op, a, out, len),
}
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
unary_scalar_f64(op, a, out, len);
}
}
#[cfg(test)]
mod tests {
use super::super::test_support::{fill_range_f64, fill_sweep_f64, rel_err_f64};
use super::*;
/// Arguments that expose every weak point of a π/2 range reduction: the
/// multiples of π/2, where the reduced argument cancels down to almost
/// nothing, and large |x|, where a single rounded π/2 leaves an absolute
/// error proportional to |x|. A uniform sweep alone hits neither.
fn trig_probe_points_f64(len: usize) -> Vec<f64> {
let mut a: Vec<f64> = Vec::with_capacity(len);
// Every multiple of π/2 out to |x| = 100, and its immediate
// neighbourhood. One of sin or cos crosses zero at each of them, so the
// reduced argument carries the whole result.
for k in -64i32..=64 {
let c = (k as f64) * std::f64::consts::FRAC_PI_2;
a.push(c);
for step in 1i32..=3 {
a.push(c + (step as f64) * 1e-13);
a.push(c - (step as f64) * 1e-13);
}
}
// Magnitudes far past the test sweep, where reduction error dominates
// polynomial error. The stride is prime-ish so the points do not line
// up with any multiple of π/2.
for k in 0i32..200 {
let d = 1e3 + (k as f64) * 977.0;
a.push(d);
a.push(-d);
}
fill_sweep_f64(&mut a, len, 100.0);
a
}
/// f64 sin must reach double precision across the reduction range.
///
/// The degree-9 Taylor series this replaced left ~2.6e-8 of truncation
/// error near the ends of [-π/4, π/4], and reducing with a single rounded
/// π/2 left ~7e-12 at |x| = 2e5. Both fail this bound outright.
#[test]
fn test_unary_sin_f64_double_precision() {
const LEN: usize = 2048;
let a = trig_probe_points_f64(LEN);
let mut out = vec![0.0f64; LEN];
unsafe { trig_f64(UnaryOp::Sin, a.as_ptr(), out.as_mut_ptr(), LEN) }
for i in 0..LEN {
let expected = a[i].sin();
let rel_err = rel_err_f64(out[i], expected);
assert!(
rel_err < 1e-14,
"sin({}) = {}, expected {}, rel_err = {} at index {}",
a[i],
out[i],
expected,
rel_err,
i
);
}
}
/// f64 cos must reach double precision across the reduction range.
///
/// Building cos as `sin(x + π/2)` rounds the sum before reduction, so this
/// also fails for any x large enough that the addition is inexact.
#[test]
fn test_unary_cos_f64_double_precision() {
const LEN: usize = 2048;
let a = trig_probe_points_f64(LEN);
let mut out = vec![0.0f64; LEN];
unsafe { trig_f64(UnaryOp::Cos, a.as_ptr(), out.as_mut_ptr(), LEN) }
for i in 0..LEN {
let expected = a[i].cos();
let rel_err = rel_err_f64(out[i], expected);
assert!(
rel_err < 1e-14,
"cos({}) = {}, expected {}, rel_err = {} at index {}",
a[i],
out[i],
expected,
rel_err,
i
);
}
}
/// f64 tan must reach double precision away from its poles.
///
/// The truncated Taylor series this replaced left ~5e-5 of relative error
/// at the edge of the reduction interval, which ±π/4 targets directly.
#[test]
fn test_unary_tan_f64_double_precision() {
const LEN: usize = 2048;
let mut a: Vec<f64> = Vec::with_capacity(LEN);
// ±π/4 is the edge of the reduction interval and the worst point of any
// fixed-degree polynomial in the reduced argument.
for &b in &[std::f64::consts::FRAC_PI_4, 0.5, 1.0] {
for k in -60i32..=60 {
let d = b + (k as f64) * 1e-13;
a.push(d);
a.push(-d);
}
}
// Multiples of π, where tan crosses zero and the reduction cancels.
for k in -30i32..=30 {
a.push((k as f64) * std::f64::consts::PI);
}
// Large |x|, where reduction error dominates. Points near a pole are
// dropped: there the result itself is ill-conditioned, not the kernel.
for k in 0i32..200 {
let d = 1e3 + (k as f64) * 977.0 + 0.37;
if d.cos().abs() > 1e-3 {
a.push(d);
a.push(-d);
}
}
fill_range_f64(&mut a, LEN, -1.5, 1.5);
let mut out = vec![0.0f64; LEN];
unsafe { trig_f64(UnaryOp::Tan, a.as_ptr(), out.as_mut_ptr(), LEN) }
for i in 0..LEN {
let expected = a[i].tan();
let rel_err = rel_err_f64(out[i], expected);
assert!(
rel_err < 1e-14,
"tan({}) = {}, expected {}, rel_err = {} at index {}",
a[i],
out[i],
expected,
rel_err,
i
);
}
}
#[test]
fn test_unary_trig_f64_domain_edges() {
const LEN: usize = 2048;
// ±inf and NaN have no finite reduction, so all three are NaN there.
// ±0 must keep its own sign, which `x - j*π/2` destroys for j = -0.
let edges = [
0.0f64,
-0.0,
1.0,
-1.0,
std::f64::consts::FRAC_PI_2,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NAN,
];
let a: Vec<f64> = (0..LEN).map(|i| edges[i % edges.len()]).collect();
let mut out = vec![0.0f64; LEN];
for (op, reference) in [
(UnaryOp::Sin, f64::sin as fn(f64) -> f64),
(UnaryOp::Cos, f64::cos as fn(f64) -> f64),
(UnaryOp::Tan, f64::tan as fn(f64) -> f64),
] {
unsafe { trig_f64(op, a.as_ptr(), out.as_mut_ptr(), LEN) }
for i in 0..LEN {
if !a[i].is_finite() {
assert!(
out[i].is_nan(),
"{:?}({}) = {}, expected NaN",
op,
a[i],
out[i]
);
continue;
}
let expected = reference(a[i]);
assert!(
rel_err_f64(out[i], expected) < 1e-14,
"{:?}({}) = {}, expected {}",
op,
a[i],
out[i],
expected
);
if expected == 0.0 {
assert_eq!(
out[i].is_sign_negative(),
expected.is_sign_negative(),
"{:?}({}) lost the sign of zero",
op,
a[i]
);
}
}
}
}
}