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
//! Lane maps of the correctly rounded F64 functions in
//! [`crate::algorithm::correctly_rounded`]. Not a stable API.
//!
//! One `#[inline(always)]` lane body compiles twice on x86_64: inside a
//! `#[target_feature(enable = "fma")]` function, and plain. `f64::mul_add` is a single
//! rounding in both, through the FMA instruction or libm `fma`, so both copies give the
//! host functions' bits. Other targets compile the plain copy only.
use crate::algorithm::correctly_rounded::traced::{
acos_traced, exp_traced, log_traced, log1p_traced, pow_traced,
};
/// The function a lane map applies.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CrFunction {
/// [`crate::algorithm::correctly_rounded::exp`].
Exp,
/// [`crate::algorithm::correctly_rounded::log`].
Log,
/// [`crate::algorithm::correctly_rounded::log1p`].
Log1p,
/// [`crate::algorithm::correctly_rounded::acos`].
Acos,
/// [`crate::algorithm::correctly_rounded::pow`] with this exponent.
PowScalar(f64),
}
/// Which compiled copy of the lane body runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LaneCopy {
/// The FMA copy when the CPU has FMA, the plain copy otherwise.
Best,
/// The plain copy, which reaches `f64::mul_add` through libm `fma` on x86_64.
Plain,
}
/// True when [`LaneCopy::Best`] runs the FMA copy on this CPU.
pub fn fused_copy_available() -> bool {
#[cfg(target_arch = "x86_64")]
{
std::arch::is_x86_feature_detected!("fma")
}
#[cfg(not(target_arch = "x86_64"))]
{
false
}
}
/// Write `function(input[i])` to `output[i]` for every index both slices hold.
pub fn map_f64(function: CrFunction, input: &[f64], output: &mut [f64], copy: LaneCopy) {
#[cfg(target_arch = "x86_64")]
if copy == LaneCopy::Best && fused_copy_available() {
// SAFETY: the CPU reports FMA, the only feature the copy enables.
unsafe { lanes_fused(function, input, output) };
return;
}
#[cfg(not(target_arch = "x86_64"))]
let _ = copy;
lanes(function, input, output);
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "fma")]
fn lanes_fused(function: CrFunction, input: &[f64], output: &mut [f64]) {
lanes(function, input, output);
}
/// The lane body. The match runs once per call, outside the loop.
///
/// Plain `for` loops keep each kernel body inside the calling copy. An iterator adaptor
/// such as `for_each` compiles to one shared out-of-line function, without FMA.
#[inline(always)]
fn lanes(function: CrFunction, input: &[f64], output: &mut [f64]) {
match function {
CrFunction::Exp => {
for (y, &x) in output.iter_mut().zip(input) {
*y = exp_traced(x).0;
}
}
CrFunction::Log => {
for (y, &x) in output.iter_mut().zip(input) {
*y = log_traced(x).0;
}
}
CrFunction::Log1p => {
for (y, &x) in output.iter_mut().zip(input) {
*y = log1p_traced(x).0;
}
}
CrFunction::Acos => {
for (y, &x) in output.iter_mut().zip(input) {
*y = acos_traced(x).0;
}
}
CrFunction::PowScalar(e) => {
for (y, &x) in output.iter_mut().zip(input) {
*y = pow_traced(x, e).0;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::algorithm::correctly_rounded::{acos, exp, log, log1p, pow};
const FUNCTIONS: [CrFunction; 7] = [
CrFunction::Exp,
CrFunction::Log,
CrFunction::Log1p,
CrFunction::Acos,
CrFunction::PowScalar(4.0 / 3.0),
CrFunction::PowScalar(-1.5),
CrFunction::PowScalar(3.0),
];
fn host(function: CrFunction, x: f64) -> f64 {
match function {
CrFunction::Exp => exp(x),
CrFunction::Log => log(x),
CrFunction::Log1p => log1p(x),
CrFunction::Acos => acos(x),
CrFunction::PowScalar(e) => pow(x, e),
}
}
/// Specials, binade edges, inputs near 1 and -1, and fixed-seed random bit patterns.
fn inputs() -> Vec<f64> {
let mut v = vec![
0.0,
-0.0,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NAN,
f64::from_bits(0x7ff0_0000_0000_0001),
f64::from_bits(0xfff8_0000_0000_beef),
f64::from_bits(1),
f64::MIN_POSITIVE,
f64::MAX,
1.0,
-1.0,
709.782_712_893_384,
-745.133_219_101_941_1,
];
for k in 0..64u64 {
v.push(f64::from_bits(1.0f64.to_bits() + k));
v.push(f64::from_bits(1.0f64.to_bits() - k));
v.push(-f64::from_bits(1.0f64.to_bits() - k));
}
let mut state = 0x9e37_79b9_7f4a_7c15u64;
for _ in 0..1 << 14 {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
v.push(f64::from_bits(state));
// Positive inputs with exponents 2^-15 to 2^112 keep log and pow off the specials.
v.push(f64::from_bits(
((0x3f0 + (state & 0x7f)) << 52) | (state >> 12),
));
// Signed inputs with exponents 2^-64 to 2^-1 reach every acos branch.
v.push(f64::from_bits(
(state & (1 << 63)) | ((0x3bf + ((state >> 1) & 0x3f)) << 52) | (state >> 12),
));
}
v
}
#[test]
fn test_both_copies_equal_host_bits() {
let input = inputs();
for function in FUNCTIONS {
for copy in [LaneCopy::Best, LaneCopy::Plain] {
let mut output = vec![0.0; input.len()];
map_f64(function, &input, &mut output, copy);
for (&x, &y) in input.iter().zip(&output) {
assert_eq!(
y.to_bits(),
host(function, x).to_bits(),
"{function:?} {copy:?} x={:#018x}",
x.to_bits()
);
}
}
}
}
#[test]
fn test_map_stops_at_shorter_slice() {
let input = [0.0, 1.0, 2.0];
let mut output = [7.0; 2];
map_f64(CrFunction::Exp, &input, &mut output, LaneCopy::Best);
assert_eq!(output, [1.0, exp(1.0)]);
let mut wide = [7.0; 4];
map_f64(CrFunction::Exp, &input, &mut wide, LaneCopy::Plain);
assert_eq!(wide[3], 7.0);
}
}