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

Commit ad3aa7d

Browse files
committed
Use wrapping_neg() to avoid fma errors on underflow
1 parent f88cc35 commit ad3aa7d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

libm/src/math/fma.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ pub fn fma(x: f64, y: f64, z: f64) -> f64 {
126126
rlo = res;
127127
rhi = rhi.wrapping_sub(zhi.wrapping_add(borrow as u64));
128128
if (rhi >> 63) != 0 {
129-
rlo = (-(rlo as i64)) as u64;
130-
rhi = (-(rhi as i64)) as u64 - (rlo != 0) as u64;
129+
rlo = (rlo as i64).wrapping_neg() as u64;
130+
rhi = (rhi as i64).wrapping_neg() as u64 - (rlo != 0) as u64;
131131
sign = (sign == 0) as i32;
132132
}
133133
nonzero = (rhi != 0) as i32;
@@ -232,4 +232,12 @@ mod tests {
232232
-3991680619069439e277
233233
);
234234
}
235+
236+
#[test]
237+
fn fma_underflow() {
238+
assert_eq!(
239+
fma(1.1102230246251565e-16, -9.812526705433188e-305, 1.0894e-320),
240+
0.0,
241+
);
242+
}
235243
}

0 commit comments

Comments
 (0)