Use wrapping_neg() to avoid fma errors on underflow

This commit is contained in:
Andrew Tribick 2022-09-30 14:11:15 +02:00
parent 8b42fe77b7
commit 98e9ab973a

View file

@ -126,8 +126,8 @@ pub fn fma(x: f64, y: f64, z: f64) -> f64 {
rlo = res;
rhi = rhi.wrapping_sub(zhi.wrapping_add(borrow as u64));
if (rhi >> 63) != 0 {
rlo = (-(rlo as i64)) as u64;
rhi = (-(rhi as i64)) as u64 - (rlo != 0) as u64;
rlo = (rlo as i64).wrapping_neg() as u64;
rhi = (rhi as i64).wrapping_neg() as u64 - (rlo != 0) as u64;
sign = (sign == 0) as i32;
}
nonzero = (rhi != 0) as i32;
@ -232,4 +232,12 @@ mod tests {
-3991680619069439e277
);
}
#[test]
fn fma_underflow() {
assert_eq!(
fma(1.1102230246251565e-16, -9.812526705433188e-305, 1.0894e-320),
0.0,
);
}
}