Skip to content

Commit f672282

Browse files
committed
factor SIMD bool handling into helper functions
1 parent e05a543 commit f672282

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/helpers.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,3 +758,18 @@ pub fn immty_from_uint_checked<'tcx>(
758758
err_unsup_format!("unsigned value {:#x} does not fit in {} bits", int, layout.size.bits())
759759
})?)
760760
}
761+
762+
pub fn bool_to_simd_element(b: bool, size: Size) -> Scalar<Tag> {
763+
// SIMD uses all-1 as pattern for "true"
764+
let val = if b { -1 } else { 0 };
765+
Scalar::from_int(val, size)
766+
}
767+
768+
pub fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx, bool> {
769+
let val = elem.to_scalar()?.to_int(elem.layout.size)?;
770+
Ok(match val {
771+
0 => false,
772+
-1 => true,
773+
_ => throw_ub_format!("each element of a SIMD mask must be all-0-bits or all-1-bits"),
774+
})
775+
}

src/shims/intrinsics.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::{mir, mir::BinOp, ty, ty::FloatTy};
88
use rustc_target::abi::{Align, Integer};
99

1010
use crate::*;
11-
use helpers::check_arg_count;
11+
use helpers::{bool_to_simd_element, check_arg_count, simd_element_to_bool};
1212

1313
pub enum AtomicOp {
1414
MirOp(mir::BinOp, bool),
@@ -365,8 +365,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
365365
// Special handling for boolean-returning operations
366366
assert_eq!(ty, this.tcx.types.bool);
367367
let val = val.to_bool().unwrap();
368-
let val = if val { -1 } else { 0 }; // SIMD uses all-1 as pattern for "true"
369-
let val = Scalar::from_int(val, dest.layout.size);
368+
let val = bool_to_simd_element(val, dest.layout.size);
370369
this.write_scalar(val, &dest.into())?;
371370
} else {
372371
assert_eq!(ty, dest.layout.ty);
@@ -381,16 +380,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
381380
let mut res = false; // the neutral element
382381
for i in 0..arg_len {
383382
let op = this.read_immediate(&this.mplace_index(&arg, i)?.into())?;
384-
// We convert it to a *signed* integer and expect either 0 or -1 (the latter means all bits were set).
385-
let val = op.to_scalar()?.to_int(op.layout.size)?;
386-
let val = match val {
387-
0 => false,
388-
-1 => true,
389-
_ =>
390-
throw_ub_format!(
391-
"each element of a simd_reduce_any operand must be all-0-bits or all-1-bits"
392-
),
393-
};
383+
let val = simd_element_to_bool(op)?;
394384
res = res | val;
395385
}
396386

0 commit comments

Comments
 (0)