Skip to content

Commit

Permalink
support f*_algebraic
Browse files Browse the repository at this point in the history
  • Loading branch information
bend-n committed May 11, 2024
1 parent 9f63c72 commit 2ddc838
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let res = this.adjust_nan(res, &[f]);
this.write_scalar(res, dest)?;
}
#[rustfmt::skip]
| "fadd_algebraic"
| "fsub_algebraic"
| "fmul_algebraic"
| "fdiv_algebraic"
| "frem_algebraic"
=> {
let [a, b] = check_arg_count(args)?;
let a = this.read_immediate(a)?;
let b = this.read_immediate(b)?;
let op = match intrinsic_name {
"fadd_algebraic" => mir::BinOp::Add,
"fsub_algebraic" => mir::BinOp::Sub,
"fmul_algebraic" => mir::BinOp::Mul,
"fdiv_algebraic" => mir::BinOp::Div,
"frem_algebraic" => mir::BinOp::Rem,
_ => bug!(),
};
let res = this.wrapping_binary_op(op, &a, &b)?;
this.write_immediate(*res, dest)?;
}

#[rustfmt::skip]
| "fadd_fast"
Expand Down
32 changes: 32 additions & 0 deletions tests/pass/float_algebraic_math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![feature(core_intrinsics)]

use std::intrinsics::{
fadd_algebraic, fdiv_algebraic, fmul_algebraic, frem_algebraic, fsub_algebraic,
};

#[inline(never)]
pub fn test_operations_f64(a: f64, b: f64) {
// make sure they all map to the correct operation
assert_eq!(fadd_algebraic(a, b), a + b);
assert_eq!(fsub_algebraic(a, b), a - b);
assert_eq!(fmul_algebraic(a, b), a * b);
assert_eq!(fdiv_algebraic(a, b), a / b);
assert_eq!(frem_algebraic(a, b), a % b);
}

#[inline(never)]
pub fn test_operations_f32(a: f32, b: f32) {
// make sure they all map to the correct operation
assert_eq!(fadd_algebraic(a, b), a + b);
assert_eq!(fsub_algebraic(a, b), a - b);
assert_eq!(fmul_algebraic(a, b), a * b);
assert_eq!(fdiv_algebraic(a, b), a / b);
assert_eq!(frem_algebraic(a, b), a % b);
}

fn main() {
test_operations_f64(1., 2.);
test_operations_f64(10., 5.);
test_operations_f32(11., 2.);
test_operations_f32(10., 15.);
}

0 comments on commit 2ddc838

Please sign in to comment.