Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 7a6b787

Browse files
author
Jethro Beekman
committed
Fix substract with borrow in FMA
Fixes #242
1 parent d39a3d8 commit 7a6b787

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/math/fma.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ pub fn fma(x: f64, y: f64, z: f64) -> f64 {
122122
rhi += zhi + (rlo < zlo) as u64;
123123
} else {
124124
/* r -= z */
125-
let t = rlo;
126-
rlo = rlo.wrapping_sub(zlo);
127-
rhi = rhi.wrapping_sub(zhi.wrapping_sub((t < rlo) as u64));
125+
let (res, borrow) = rlo.overflowing_sub(zlo);
126+
rlo = res;
127+
rhi = rhi.wrapping_sub(zhi.wrapping_add(borrow as u64));
128128
if (rhi >> 63) != 0 {
129129
rlo = (-(rlo as i64)) as u64;
130130
rhi = (-(rhi as i64)) as u64 - (rlo != 0) as u64;
@@ -220,4 +220,9 @@ mod tests {
220220

221221
assert_eq!(fma(-0.992, -0.992, -0.992), -0.00793599999988632,);
222222
}
223+
224+
#[test]
225+
fn fma_sbb() {
226+
assert_eq!(fma(-(1.0 - f64::EPSILON), f64::MIN, f64::MIN), -3991680619069439e277);
227+
}
223228
}

0 commit comments

Comments
 (0)