blob: b6b8938142fc176b6925c7baaffa3576cdd48358 (
plain) (
blame)
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
|
#![allow(unsafe_code)]
//! The native CPU instruction used for render-packet distance keys.
/// Reproduces Ngi32's squared length multiplied by `RSQRTSS` on x86-64.
/// Other architectures retain the portable distance and report that boundary.
#[must_use]
pub fn legacy_packet_distance_squared(squared: f32) -> Option<f32> {
if !squared.is_finite() || squared < 0.0 {
return None;
}
if squared == 0.0 {
static NOTICE: std::sync::Once = std::sync::Once::new();
NOTICE.call_once(|| {
eprintln!("render_coverage packet_zero_distance_fallback=1 (stable zero instead of native 0 * infinity)");
});
return Some(0.0);
}
#[cfg(target_arch = "x86_64")]
let distance = {
use core::arch::x86_64::{_mm_cvtss_f32, _mm_rsqrt_ss, _mm_set_ss};
// SAFETY: SSE is part of the x86-64 baseline; these intrinsics only
// operate on register values and do not access memory.
let inverse = unsafe { _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(squared))) };
squared * inverse
};
#[cfg(not(target_arch = "x86_64"))]
let distance = {
static NOTICE: std::sync::Once = std::sync::Once::new();
NOTICE.call_once(|| {
eprintln!("render_coverage packet_distance_native_rsqrt_unsupported=1 (using portable square root)");
});
squared.sqrt()
};
distance.is_finite().then_some(distance)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn packet_distance_preserves_the_native_approximation_error_bound() {
for squared in [0.000_001_f32, 1.0, 9.0, 11_393.854, 1_000_000.0] {
let distance = legacy_packet_distance_squared(squared).unwrap();
assert!((distance / squared.sqrt() - 1.0).abs() < 0.000_3);
}
assert_eq!(legacy_packet_distance_squared(0.0), Some(0.0));
for invalid in [-1.0, f32::NAN, f32::INFINITY] {
assert_eq!(legacy_packet_distance_squared(invalid), None);
}
}
}
|