/// Floating multiply add (f32) /// /// Computes `(x*y)+z`, rounded as one ternary operation: /// Computes the value (as if) to infinite precision and rounds once to the result format, /// according to the rounding mode characterized by the value of FLT_ROUNDS. #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] pubfn fmaf(x: f32, y: f32, mut z: f32) -> f32 { let xy: f64; letmut result: f64; letmut ui: u64; let e: i32;
xy = x as f64 * y as f64;
result = xy + z as f64;
ui = result.to_bits();
e = (ui >> 52) as i32 & 0x7ff; /* Common case: The double precision result is fine. */ if ( /* not a halfway case */
ui & 0x1fffffff) != 0x10000000 || /* NaN */
e == 0x7ff || /* exact */
(result - xy == z as f64 && result - z as f64 == xy) || /* not round-to-nearest */
fegetround() != FE_TONEAREST
{ /* underflowmaynotberaisedcorrectly,example: fmaf(0x1p-120f,0x1p-120f,0x1p-149f)
*/ if e < 0x3ff - 126 && e >= 0x3ff - 149 && fetestexcept(FE_INEXACT) != 0 {
feclearexcept(FE_INEXACT); // prevent `xy + vz` from being CSE'd with `xy + z` above let vz: f32 = unsafe { read_volatile(&z) };
result = xy + vz as f64; if fetestexcept(FE_INEXACT) != 0 {
feraiseexcept(FE_UNDERFLOW);
} else {
feraiseexcept(FE_INEXACT);
}
}
z = result as f32; return z;
}
/* *Ifresultisinexact,andexactlyhalfwaybetweentwofloatvalues, *weneedtoadjustthelow-orderbitinthedirectionoftheerror.
*/ let neg = ui >> 63 != 0; let err = if neg == (z as f64 > xy) {
xy - result + z as f64
} else {
z as f64 - result + xy
}; if neg == (err < 0.0) {
ui += 1;
} else {
ui -= 1;
}
f64::from_bits(ui) as f32
}
#[cfg(test)] mod tests { #[test] fn issue_263() { let a = f32::from_bits(1266679807); let b = f32::from_bits(1300234242); let c = f32::from_bits(1115553792); let expected = f32::from_bits(1501560833);
assert_eq!(super::fmaf(a, b, c), expected);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.