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

Commit 7ef6fc2

Browse files
committed
Fix segfaults in FMA for some inputs
1 parent 308daa3 commit 7ef6fc2

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

crates/libm-test/tests/unit.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,19 @@ fn j1f_2488() {
344344
fn y1f_2002() {
345345
assert_eq!(y1f(2.0000002_f32), -0.10703229_f32);
346346
}
347+
348+
#[test]
349+
fn fma_segfault_bug() {
350+
// An attempt to substract with overflow was causing a segfault
351+
// on FMA for these inputs:
352+
assert_eq!(
353+
fma(
354+
-0.0000000000000002220446049250313,
355+
-0.0000000000000002220446049250313,
356+
-0.0000000000000002220446049250313
357+
),
358+
-0.00000000000000022204460492503126
359+
);
360+
361+
assert_eq!(fma(-0.992, -0.992, -0.992), -0.00793599999988632);
362+
}

crates/libm/src/math/fma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ pub extern "C" fn fma(x: f64, y: f64, z: f64) -> f64 {
126126
} else {
127127
/* r -= z */
128128
let t = rlo;
129-
rlo -= zlo;
130-
rhi = rhi - zhi - (t < rlo) as u64;
129+
rlo = rlo.wrapping_sub(zlo);
130+
rhi = rhi.wrapping_sub(zhi.wrapping_sub((t < rlo) as u64));
131131
if (rhi >> 63) != 0 {
132132
rlo = (-(rlo as i64)) as u64;
133133
rhi = (-(rhi as i64)) as u64 - (rlo != 0) as u64;

0 commit comments

Comments
 (0)