rust/tests/ui/intrinsics/intrinsic-fmuladd.rs
Jed Brown 0d8a978e8a intrinsics.fmuladdf{16,32,64,128}: expose llvm.fmuladd.* semantics
Add intrinsics `fmuladd{f16,f32,f64,f128}`. This computes `(a * b) +
c`, to be fused if the code generator determines that (i) the target
instruction set has support for a fused operation, and (ii) that the
fused operation is more efficient than the equivalent, separate pair
of `mul` and `add` instructions.

https://llvm.org/docs/LangRef.html#llvm-fmuladd-intrinsic

MIRI support is included for f32 and f64.

The codegen_cranelift uses the `fma` function from libc, which is a
correct implementation, but without the desired performance semantic. I
think this requires an update to cranelift to expose a suitable
instruction in its IR.

I have not tested with codegen_gcc, but it should behave the same
way (using `fma` from libc).
2024-10-11 15:32:56 -06:00

43 lines
1.6 KiB
Rust

//@ run-pass
#![feature(core_intrinsics)]
use std::intrinsics::*;
macro_rules! assert_approx_eq {
($a:expr, $b:expr) => {{
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b);
}};
}
fn main() {
unsafe {
let nan: f32 = f32::NAN;
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_approx_eq!(fmuladdf32(1.23, 4.5, 0.67), 6.205);
assert_approx_eq!(fmuladdf32(-1.23, -4.5, -0.67), 4.865);
assert_approx_eq!(fmuladdf32(0.0, 8.9, 1.2), 1.2);
assert_approx_eq!(fmuladdf32(3.4, -0.0, 5.6), 5.6);
assert!(fmuladdf32(nan, 7.8, 9.0).is_nan());
assert_eq!(fmuladdf32(inf, 7.8, 9.0), inf);
assert_eq!(fmuladdf32(neg_inf, 7.8, 9.0), neg_inf);
assert_eq!(fmuladdf32(8.9, inf, 3.2), inf);
assert_eq!(fmuladdf32(-3.2, 2.4, neg_inf), neg_inf);
}
unsafe {
let nan: f64 = f64::NAN;
let inf: f64 = f64::INFINITY;
let neg_inf: f64 = f64::NEG_INFINITY;
assert_approx_eq!(fmuladdf64(1.23, 4.5, 0.67), 6.205);
assert_approx_eq!(fmuladdf64(-1.23, -4.5, -0.67), 4.865);
assert_approx_eq!(fmuladdf64(0.0, 8.9, 1.2), 1.2);
assert_approx_eq!(fmuladdf64(3.4, -0.0, 5.6), 5.6);
assert!(fmuladdf64(nan, 7.8, 9.0).is_nan());
assert_eq!(fmuladdf64(inf, 7.8, 9.0), inf);
assert_eq!(fmuladdf64(neg_inf, 7.8, 9.0), neg_inf);
assert_eq!(fmuladdf64(8.9, inf, 3.2), inf);
assert_eq!(fmuladdf64(-3.2, 2.4, neg_inf), neg_inf);
}
}