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
use super::F32;
impl F32 {
pub fn invsqrt(self) -> Self {
Self::from_bits(0x5f37_5a86 - (self.to_bits() >> 1))
}
}
#[cfg(test)]
mod tests {
use super::F32;
use crate::float::sqrt::tests::TEST_VECTORS;
const MAX_ERROR: f32 = 0.05;
#[test]
fn sanity_check() {
for (x, expected) in TEST_VECTORS {
let expected = 1.0 / expected;
let invsqrt_x = F32(*x).invsqrt().0;
let allowed_delta = x * MAX_ERROR;
let actual_delta = invsqrt_x - expected;
assert!(
actual_delta <= allowed_delta,
"delta {} too large: {} vs {}",
actual_delta,
invsqrt_x,
expected
);
}
}
}