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

Commit c3d72c2

Browse files
author
Jethro Beekman
committed
Fix substract with borrow in FMA
Fixes #242
1 parent 1833424 commit c3d72c2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

libm/src/math/fma.rs

Lines changed: 12 additions & 4 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;
@@ -218,6 +218,14 @@ mod tests {
218218
-0.00000000000000022204460492503126,
219219
);
220220

221-
assert_eq!(fma(-0.992, -0.992, -0.992), -0.00793599999988632,);
221+
assert_eq!(fma(-0.992, -0.992, -0.992), -0.007936000000000007,);
222+
}
223+
224+
#[test]
225+
fn fma_sbb() {
226+
assert_eq!(
227+
fma(-(1.0 - f64::EPSILON), f64::MIN, f64::MIN),
228+
-3991680619069439e277
229+
);
222230
}
223231
}

0 commit comments

Comments
 (0)