Skip to content

Commit

Permalink
portable SIMD: add rem intrinsic; test div and rem intrinsic UB
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Nov 25, 2021
1 parent 4f0faed commit a534bbb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/shims/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}

// SIMD operations
"simd_add" | "simd_sub" | "simd_mul" | "simd_div" => {
"simd_add" | "simd_sub" | "simd_mul" | "simd_div" | "simd_rem" => {
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)?;
Expand All @@ -320,6 +320,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
"simd_sub" => mir::BinOp::Sub,
"simd_mul" => mir::BinOp::Mul,
"simd_div" => mir::BinOp::Div,
"simd_rem" => mir::BinOp::Rem,
_ => unreachable!(),
};

Expand Down
15 changes: 15 additions & 0 deletions tests/compile-fail/intrinsics/simd-div-by-zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(platform_intrinsics, repr_simd)]

extern "platform-intrinsic" {
pub(crate) fn simd_div<T>(x: T, y: T) -> T;
}

#[repr(simd)]
#[allow(non_camel_case_types)]
struct i32x2(i32, i32);

fn main() { unsafe {
let x = i32x2(1, 1);
let y = i32x2(1, 0);
simd_div(x, y); //~ERROR Undefined Behavior: dividing by zero
} }
15 changes: 15 additions & 0 deletions tests/compile-fail/intrinsics/simd-rem-by-zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(platform_intrinsics, repr_simd)]

extern "platform-intrinsic" {
pub(crate) fn simd_rem<T>(x: T, y: T) -> T;
}

#[repr(simd)]
#[allow(non_camel_case_types)]
struct i32x2(i32, i32);

fn main() { unsafe {
let x = i32x2(1, 1);
let y = i32x2(1, 0);
simd_rem(x, y); //~ERROR Undefined Behavior: calculating the remainder with a divisor of zero
} }
2 changes: 2 additions & 0 deletions tests/run-pass/portable-simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn simd_ops_f32() {
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));
assert_eq!(a % b, f32x4::from_array([0.0, 0.0, 1.0, 2.0]));
}

fn simd_ops_i32() {
Expand All @@ -19,6 +20,7 @@ fn simd_ops_i32() {
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));
assert_eq!(a % b, i32x4::from_array([0, 0, 1, 2]));
}

fn main() {
Expand Down

0 comments on commit a534bbb

Please sign in to comment.