implement SIMD sub, mul, div; also test i32 binops

This commit is contained in:
Ralf Jung 2021-11-18 09:42:30 -05:00
parent 0766da6fbe
commit b816cb94e7
2 changed files with 30 additions and 3 deletions

View File

@ -306,7 +306,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
// SIMD operations
"simd_add" => {
"simd_add" | "simd_sub" | "simd_mul" | "simd_div" => {
let &[ref left, ref right] = check_arg_count(args)?;
let (left, left_len) = this.operand_to_simd(left)?;
let (right, right_len) = this.operand_to_simd(right)?;
@ -315,11 +315,19 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
assert_eq!(dest_len, left_len);
assert_eq!(dest_len, right_len);
let op = match intrinsic_name {
"simd_add" => mir::BinOp::Add,
"simd_sub" => mir::BinOp::Sub,
"simd_mul" => mir::BinOp::Mul,
"simd_div" => mir::BinOp::Div,
_ => unreachable!(),
};
for i in 0..dest_len {
let left = this.read_immediate(&this.mplace_index(&left, i)?.into())?;
let right = this.read_immediate(&this.mplace_index(&right, i)?.into())?;
let dest = this.mplace_index(&dest, i)?.into();
this.binop_ignore_overflow(mir::BinOp::Add, &left, &right, &dest)?;
this.binop_ignore_overflow(op, &left, &right, &dest)?;
}
}

View File

@ -1,8 +1,27 @@
#![feature(portable_simd)]
use std::simd::*;
fn main() {
fn simd_ops_f32() {
let a = f32x4::splat(10.0);
let b = f32x4::from_array([1.0, 2.0, 3.0, 4.0]);
assert_eq!(a + b, f32x4::from_array([11.0, 12.0, 13.0, 14.0]));
assert_eq!(a - b, f32x4::from_array([9.0, 8.0, 7.0, 6.0]));
assert_eq!(a * b, f32x4::from_array([10.0, 20.0, 30.0, 40.0]));
assert_eq!(b / a, f32x4::from_array([0.1, 0.2, 0.3, 0.4]));
assert_eq!(a / 2.0, f32x4::splat(5.0));
}
fn simd_ops_i32() {
let a = i32x4::splat(10);
let b = i32x4::from_array([1, 2, 3, 4]);
assert_eq!(a + b, i32x4::from_array([11, 12, 13, 14]));
assert_eq!(a - b, i32x4::from_array([9, 8, 7, 6]));
assert_eq!(a * b, i32x4::from_array([10, 20, 30, 40]));
assert_eq!(a / b, i32x4::from_array([10, 5, 3, 2]));
assert_eq!(a / 2, i32x4::splat(5));
}
fn main() {
simd_ops_f32();
simd_ops_i32();
}