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

Commit 2481690

Browse files
authored
Merge pull request #209 from gnzlbg/fma
Fix overflow bugs in fma
2 parents fb44a09 + 92ffb04 commit 2481690

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

libm/src/math/fma.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ pub fn fma(x: f64, y: f64, z: f64) -> f64 {
123123
} else {
124124
/* r -= z */
125125
let t = rlo;
126-
rlo -= zlo;
127-
rhi = rhi - zhi - (t < rlo) as u64;
126+
rlo = rlo.wrapping_sub(zlo);
127+
rhi = rhi.wrapping_sub(zhi.wrapping_sub((t < rlo) 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;
@@ -202,3 +202,22 @@ pub fn fma(x: f64, y: f64, z: f64) -> f64 {
202202
}
203203
scalbn(r, e)
204204
}
205+
206+
#[cfg(test)]
207+
mod tests {
208+
use super::*;
209+
#[test]
210+
fn fma_segfault() {
211+
// These two inputs cause fma to segfault on release due to overflow:
212+
assert_eq!(
213+
fma(
214+
-0.0000000000000002220446049250313,
215+
-0.0000000000000002220446049250313,
216+
-0.0000000000000002220446049250313
217+
),
218+
-0.00000000000000022204460492503126,
219+
);
220+
221+
assert_eq!(fma(-0.992, -0.992, -0.992), -0.00793599999988632,);
222+
}
223+
}

0 commit comments

Comments
 (0)