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

Commit f27ad31

Browse files
committed
Fix overflow bug in fma
1 parent 4420289 commit f27ad31

File tree

2 files changed

+3
-6
lines changed

2 files changed

+3
-6
lines changed

src/math/fma.rs

Lines changed: 2 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
} 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;

tests/unit.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@ fn fma_segfault() {
1212
-0.00000000000000022204460492503126,
1313
);
1414

15-
assert_eq!(
16-
fma(-0.992, -0.992, -0.992),
17-
-0.00793599999988632,
18-
);
15+
assert_eq!(fma(-0.992, -0.992, -0.992), -0.00793599999988632,);
1916
}

0 commit comments

Comments
 (0)