Skip to content

Commit 2ddc838

Browse files
committed
support f*_algebraic
1 parent 9f63c72 commit 2ddc838

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/intrinsics/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
256256
let res = this.adjust_nan(res, &[f]);
257257
this.write_scalar(res, dest)?;
258258
}
259+
#[rustfmt::skip]
260+
| "fadd_algebraic"
261+
| "fsub_algebraic"
262+
| "fmul_algebraic"
263+
| "fdiv_algebraic"
264+
| "frem_algebraic"
265+
=> {
266+
let [a, b] = check_arg_count(args)?;
267+
let a = this.read_immediate(a)?;
268+
let b = this.read_immediate(b)?;
269+
let op = match intrinsic_name {
270+
"fadd_algebraic" => mir::BinOp::Add,
271+
"fsub_algebraic" => mir::BinOp::Sub,
272+
"fmul_algebraic" => mir::BinOp::Mul,
273+
"fdiv_algebraic" => mir::BinOp::Div,
274+
"frem_algebraic" => mir::BinOp::Rem,
275+
_ => bug!(),
276+
};
277+
let res = this.wrapping_binary_op(op, &a, &b)?;
278+
this.write_immediate(*res, dest)?;
279+
}
259280

260281
#[rustfmt::skip]
261282
| "fadd_fast"

tests/pass/float_algebraic_math.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(core_intrinsics)]
2+
3+
use std::intrinsics::{
4+
fadd_algebraic, fdiv_algebraic, fmul_algebraic, frem_algebraic, fsub_algebraic,
5+
};
6+
7+
#[inline(never)]
8+
pub fn test_operations_f64(a: f64, b: f64) {
9+
// make sure they all map to the correct operation
10+
assert_eq!(fadd_algebraic(a, b), a + b);
11+
assert_eq!(fsub_algebraic(a, b), a - b);
12+
assert_eq!(fmul_algebraic(a, b), a * b);
13+
assert_eq!(fdiv_algebraic(a, b), a / b);
14+
assert_eq!(frem_algebraic(a, b), a % b);
15+
}
16+
17+
#[inline(never)]
18+
pub fn test_operations_f32(a: f32, b: f32) {
19+
// make sure they all map to the correct operation
20+
assert_eq!(fadd_algebraic(a, b), a + b);
21+
assert_eq!(fsub_algebraic(a, b), a - b);
22+
assert_eq!(fmul_algebraic(a, b), a * b);
23+
assert_eq!(fdiv_algebraic(a, b), a / b);
24+
assert_eq!(frem_algebraic(a, b), a % b);
25+
}
26+
27+
fn main() {
28+
test_operations_f64(1., 2.);
29+
test_operations_f64(10., 5.);
30+
test_operations_f32(11., 2.);
31+
test_operations_f32(10., 15.);
32+
}

0 commit comments

Comments
 (0)