Auto merge of #3694 - southball:fix/use_strict_ops_instead_of_checked_ops, r=RalfJung
Use strict ops instead of checked ops ## What Replace `checked_add(...).unwrap()` with `strict_add(...)`, etc. Resolves #3668.
This commit is contained in:
commit
be307ca809
14 changed files with 65 additions and 75 deletions
|
|
@ -97,7 +97,7 @@ impl GlobalStateInner {
|
|||
fn align_addr(addr: u64, align: u64) -> u64 {
|
||||
match addr % align {
|
||||
0 => addr,
|
||||
rem => addr.checked_add(align).unwrap() - rem,
|
||||
rem => addr.strict_add(align) - rem,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -303,7 +303,7 @@ pub fn create_ecx<'tcx>(
|
|||
let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
|
||||
for arg in config.args.iter() {
|
||||
// Make space for `0` terminator.
|
||||
let size = u64::try_from(arg.len()).unwrap().checked_add(1).unwrap();
|
||||
let size = u64::try_from(arg.len()).unwrap().strict_add(1);
|
||||
let arg_type = Ty::new_array(tcx, tcx.types.u8, size);
|
||||
let arg_place =
|
||||
ecx.allocate(ecx.layout_of(arg_type)?, MiriMemoryKind::Machine.into())?;
|
||||
|
|
|
|||
|
|
@ -973,7 +973,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
|
||||
// terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
|
||||
let string_length = u64::try_from(c_str.len()).unwrap();
|
||||
let string_length = string_length.checked_add(1).unwrap();
|
||||
let string_length = string_length.strict_add(1);
|
||||
if size < string_length {
|
||||
return Ok((false, string_length));
|
||||
}
|
||||
|
|
@ -1037,7 +1037,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
|
||||
// 0x0000 terminator to memory would cause an out-of-bounds access.
|
||||
let string_length = u64::try_from(wide_str.len()).unwrap();
|
||||
let string_length = string_length.checked_add(1).unwrap();
|
||||
let string_length = string_length.strict_add(1);
|
||||
if size < string_length {
|
||||
return Ok((false, string_length));
|
||||
}
|
||||
|
|
@ -1406,7 +1406,7 @@ pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
|
|||
if success {
|
||||
// If the function succeeds, the return value is the number of characters stored in the target buffer,
|
||||
// not including the terminating null character.
|
||||
u32::try_from(len.checked_sub(1).unwrap()).unwrap()
|
||||
u32::try_from(len.strict_sub(1)).unwrap()
|
||||
} else {
|
||||
// If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
|
||||
// required to hold the string and its terminating null character.
|
||||
|
|
|
|||
|
|
@ -402,7 +402,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
});
|
||||
let (_, addr) = ptr.into_parts(); // we know the offset is absolute
|
||||
// Cannot panic since `align` is a power of 2 and hence non-zero.
|
||||
if addr.bytes().checked_rem(align.bytes()).unwrap() != 0 {
|
||||
if addr.bytes().strict_rem(align.bytes()) != 0 {
|
||||
throw_unsup_format!(
|
||||
"`miri_promise_symbolic_alignment`: pointer is not actually aligned"
|
||||
);
|
||||
|
|
@ -714,7 +714,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
// That is probably overly cautious, but there also is no fundamental
|
||||
// reason to have `strcpy` destroy pointer provenance.
|
||||
// This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
|
||||
let n = this.read_c_str(ptr_src)?.len().checked_add(1).unwrap();
|
||||
let n = this.read_c_str(ptr_src)?.len().strict_add(1);
|
||||
this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
|
||||
this.write_pointer(ptr_dest, dest)?;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -165,7 +165,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
("tm_hour", dt.hour().into()),
|
||||
("tm_mday", dt.day().into()),
|
||||
("tm_mon", dt.month0().into()),
|
||||
("tm_year", dt.year().checked_sub(1900).unwrap().into()),
|
||||
("tm_year", dt.year().strict_sub(1900).into()),
|
||||
("tm_wday", dt.weekday().num_days_from_sunday().into()),
|
||||
("tm_yday", dt.ordinal0().into()),
|
||||
("tm_isdst", tm_isdst),
|
||||
|
|
|
|||
|
|
@ -81,10 +81,8 @@ impl<'tcx> UnixEnvVars<'tcx> {
|
|||
return Ok(None);
|
||||
};
|
||||
// The offset is used to strip the "{name}=" part of the string.
|
||||
let var_ptr = var_ptr.offset(
|
||||
Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()),
|
||||
ecx,
|
||||
)?;
|
||||
let var_ptr = var_ptr
|
||||
.offset(Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)), ecx)?;
|
||||
Ok(Some(var_ptr))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -240,7 +240,7 @@ impl FdTable {
|
|||
let new_fd = candidate_new_fd.unwrap_or_else(|| {
|
||||
// find_map ran out of BTreeMap entries before finding a free fd, use one plus the
|
||||
// maximum fd in the map
|
||||
self.fds.last_key_value().map(|(fd, _)| fd.checked_add(1).unwrap()).unwrap_or(min_fd)
|
||||
self.fds.last_key_value().map(|(fd, _)| fd.strict_add(1)).unwrap_or(min_fd)
|
||||
});
|
||||
|
||||
self.fds.try_insert(new_fd, file_handle).unwrap();
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ impl FileDescription for SocketPair {
|
|||
};
|
||||
let mut writebuf = writebuf.borrow_mut();
|
||||
let data_size = writebuf.buf.len();
|
||||
let available_space = MAX_SOCKETPAIR_BUFFER_CAPACITY.checked_sub(data_size).unwrap();
|
||||
let available_space = MAX_SOCKETPAIR_BUFFER_CAPACITY.strict_sub(data_size);
|
||||
if available_space == 0 {
|
||||
if self.is_nonblock {
|
||||
// Non-blocking socketpair with a full buffer.
|
||||
|
|
|
|||
|
|
@ -647,7 +647,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
// If the function succeeds, the return value is the length of the string that
|
||||
// is copied to the buffer, in characters, not including the terminating null
|
||||
// character.
|
||||
this.write_int(size_needed.checked_sub(1).unwrap(), dest)?;
|
||||
this.write_int(size_needed.strict_sub(1), dest)?;
|
||||
} else {
|
||||
// If the buffer is too small to hold the module name, the string is truncated
|
||||
// to nSize characters including the terminating null character, the function
|
||||
|
|
@ -689,7 +689,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
throw_unsup_format!("FormatMessageW: buffer not big enough");
|
||||
}
|
||||
// The return value is the number of characters stored *excluding* the null terminator.
|
||||
this.write_int(length.checked_sub(1).unwrap(), dest)?;
|
||||
this.write_int(length.strict_sub(1), dest)?;
|
||||
}
|
||||
|
||||
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ impl Handle {
|
|||
/// None of this layout is guaranteed to applications by Windows or Miri.
|
||||
fn to_packed(self) -> u32 {
|
||||
let disc_size = Self::packed_disc_size();
|
||||
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
|
||||
let data_size = u32::BITS.strict_sub(disc_size);
|
||||
|
||||
let discriminant = self.discriminant();
|
||||
let data = self.data();
|
||||
|
|
@ -103,7 +103,7 @@ impl Handle {
|
|||
/// see docs for `to_packed`
|
||||
fn from_packed(handle: u32) -> Option<Self> {
|
||||
let disc_size = Self::packed_disc_size();
|
||||
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
|
||||
let data_size = u32::BITS.strict_sub(disc_size);
|
||||
|
||||
// the lower `data_size` bits of this mask are 1
|
||||
#[allow(clippy::arithmetic_side_effects)] // cannot overflow
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
assert_eq!(dest_len, mask_len);
|
||||
|
||||
let mask_item_size = mask.layout.field(this, 0).size;
|
||||
let high_bit_offset = mask_item_size.bits().checked_sub(1).unwrap();
|
||||
let high_bit_offset = mask_item_size.bits().strict_sub(1);
|
||||
|
||||
let scale = this.read_scalar(scale)?.to_i8()?;
|
||||
if !matches!(scale, 1 | 2 | 4 | 8) {
|
||||
|
|
@ -93,8 +93,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let offset =
|
||||
i64::try_from(this.read_scalar(&offset)?.to_int(offset.layout.size)?)
|
||||
.unwrap();
|
||||
let ptr = slice
|
||||
.wrapping_signed_offset(offset.checked_mul(scale).unwrap(), &this.tcx);
|
||||
let ptr = slice.wrapping_signed_offset(offset.strict_mul(scale), &this.tcx);
|
||||
// Unaligned copy, which is what we want.
|
||||
this.mem_copy(
|
||||
ptr,
|
||||
|
|
@ -124,22 +123,22 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let (dest, dest_len) = this.mplace_to_simd(dest)?;
|
||||
|
||||
assert_eq!(left_len, right_len);
|
||||
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
|
||||
assert_eq!(dest_len.strict_mul(2), left_len);
|
||||
|
||||
for i in 0..dest_len {
|
||||
let j1 = i.checked_mul(2).unwrap();
|
||||
let j1 = i.strict_mul(2);
|
||||
let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_i16()?;
|
||||
let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i16()?;
|
||||
|
||||
let j2 = j1.checked_add(1).unwrap();
|
||||
let j2 = j1.strict_add(1);
|
||||
let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_i16()?;
|
||||
let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i16()?;
|
||||
|
||||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
// Multiplications are i16*i16->i32, which will not overflow.
|
||||
let mul1 = i32::from(left1).checked_mul(right1.into()).unwrap();
|
||||
let mul2 = i32::from(left2).checked_mul(right2.into()).unwrap();
|
||||
let mul1 = i32::from(left1).strict_mul(right1.into());
|
||||
let mul2 = i32::from(left2).strict_mul(right2.into());
|
||||
// However, this addition can overflow in the most extreme case
|
||||
// (-0x8000)*(-0x8000)+(-0x8000)*(-0x8000) = 0x80000000
|
||||
let res = mul1.wrapping_add(mul2);
|
||||
|
|
@ -161,22 +160,22 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let (dest, dest_len) = this.mplace_to_simd(dest)?;
|
||||
|
||||
assert_eq!(left_len, right_len);
|
||||
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
|
||||
assert_eq!(dest_len.strict_mul(2), left_len);
|
||||
|
||||
for i in 0..dest_len {
|
||||
let j1 = i.checked_mul(2).unwrap();
|
||||
let j1 = i.strict_mul(2);
|
||||
let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_u8()?;
|
||||
let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i8()?;
|
||||
|
||||
let j2 = j1.checked_add(1).unwrap();
|
||||
let j2 = j1.strict_add(1);
|
||||
let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_u8()?;
|
||||
let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i8()?;
|
||||
|
||||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
// Multiplication of a u8 and an i8 into an i16 cannot overflow.
|
||||
let mul1 = i16::from(left1).checked_mul(right1.into()).unwrap();
|
||||
let mul2 = i16::from(left2).checked_mul(right2.into()).unwrap();
|
||||
let mul1 = i16::from(left1).strict_mul(right1.into());
|
||||
let mul2 = i16::from(left2).strict_mul(right2.into());
|
||||
let res = mul1.saturating_add(mul2);
|
||||
|
||||
this.write_scalar(Scalar::from_i16(res), &dest)?;
|
||||
|
|
@ -309,7 +308,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
|
||||
for i in 0..2 {
|
||||
let dest = this.project_index(&dest, i)?;
|
||||
let src = match (imm >> i.checked_mul(4).unwrap()) & 0b11 {
|
||||
let src = match (imm >> i.strict_mul(4)) & 0b11 {
|
||||
0 => this.project_index(&left, 0)?,
|
||||
1 => this.project_index(&left, 1)?,
|
||||
2 => this.project_index(&right, 0)?,
|
||||
|
|
@ -336,14 +335,14 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let (dest, dest_len) = this.mplace_to_simd(dest)?;
|
||||
|
||||
assert_eq!(left_len, right_len);
|
||||
assert_eq!(left_len, dest_len.checked_mul(8).unwrap());
|
||||
assert_eq!(left_len, dest_len.strict_mul(8));
|
||||
|
||||
for i in 0..dest_len {
|
||||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
let mut acc: u16 = 0;
|
||||
for j in 0..8 {
|
||||
let src_index = i.checked_mul(8).unwrap().checked_add(j).unwrap();
|
||||
let src_index = i.strict_mul(8).strict_add(j);
|
||||
|
||||
let left = this.project_index(&left, src_index)?;
|
||||
let left = this.read_scalar(&left)?.to_u8()?;
|
||||
|
|
@ -351,7 +350,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let right = this.project_index(&right, src_index)?;
|
||||
let right = this.read_scalar(&right)?.to_u8()?;
|
||||
|
||||
acc = acc.checked_add(left.abs_diff(right).into()).unwrap();
|
||||
acc = acc.strict_add(left.abs_diff(right).into());
|
||||
}
|
||||
|
||||
this.write_scalar(Scalar::from_u64(acc.into()), &dest)?;
|
||||
|
|
@ -377,7 +376,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
|
||||
let res = if right & 0x80 == 0 {
|
||||
// Shuffle each 128-bit (16-byte) block independently.
|
||||
let j = u64::from(right % 16).checked_add(i & !15).unwrap();
|
||||
let j = u64::from(right % 16).strict_add(i & !15);
|
||||
this.read_scalar(&this.project_index(&left, j)?)?
|
||||
} else {
|
||||
// If the highest bit in `right` is 1, write zero.
|
||||
|
|
|
|||
|
|
@ -441,8 +441,7 @@ fn apply_random_float_error<F: rustc_apfloat::Float>(
|
|||
) -> F {
|
||||
let rng = this.machine.rng.get_mut();
|
||||
// generates rand(0, 2^64) * 2^(scale - 64) = rand(0, 1) * 2^scale
|
||||
let err =
|
||||
F::from_u128(rng.gen::<u64>().into()).value.scalbn(err_scale.checked_sub(64).unwrap());
|
||||
let err = F::from_u128(rng.gen::<u64>().into()).value.scalbn(err_scale.strict_sub(64));
|
||||
// give it a random sign
|
||||
let err = if rng.gen::<bool>() { -err } else { err };
|
||||
// multiple the value with (1+err)
|
||||
|
|
@ -793,7 +792,7 @@ fn split_simd_to_128bit_chunks<'tcx, P: Projectable<'tcx, Provenance>>(
|
|||
|
||||
assert_eq!(simd_layout.size.bits() % 128, 0);
|
||||
let num_chunks = simd_layout.size.bits() / 128;
|
||||
let items_per_chunk = simd_len.checked_div(num_chunks).unwrap();
|
||||
let items_per_chunk = simd_len.strict_div(num_chunks);
|
||||
|
||||
// Transmute to `[[T; items_per_chunk]; num_chunks]`
|
||||
let chunked_layout = this
|
||||
|
|
@ -841,13 +840,11 @@ fn horizontal_bin_op<'tcx>(
|
|||
for j in 0..items_per_chunk {
|
||||
// `j` is the index in `dest`
|
||||
// `k` is the index of the 2-item chunk in `src`
|
||||
let (k, src) =
|
||||
if j < middle { (j, &left) } else { (j.checked_sub(middle).unwrap(), &right) };
|
||||
let (k, src) = if j < middle { (j, &left) } else { (j.strict_sub(middle), &right) };
|
||||
// `base_i` is the index of the first item of the 2-item chunk in `src`
|
||||
let base_i = k.checked_mul(2).unwrap();
|
||||
let base_i = k.strict_mul(2);
|
||||
let lhs = this.read_immediate(&this.project_index(src, base_i)?)?;
|
||||
let rhs =
|
||||
this.read_immediate(&this.project_index(src, base_i.checked_add(1).unwrap())?)?;
|
||||
let rhs = this.read_immediate(&this.project_index(src, base_i.strict_add(1))?)?;
|
||||
|
||||
let res = if saturating {
|
||||
Immediate::from(this.saturating_arith(which, &lhs, &rhs)?)
|
||||
|
|
@ -900,7 +897,7 @@ fn conditional_dot_product<'tcx>(
|
|||
// for the initial value because the representation of 0.0 is all zero bits.
|
||||
let mut sum = ImmTy::from_int(0u8, element_layout);
|
||||
for j in 0..items_per_chunk {
|
||||
if imm & (1 << j.checked_add(4).unwrap()) != 0 {
|
||||
if imm & (1 << j.strict_add(4)) != 0 {
|
||||
let left = this.read_immediate(&this.project_index(&left, j)?)?;
|
||||
let right = this.read_immediate(&this.project_index(&right, j)?)?;
|
||||
|
||||
|
|
@ -971,7 +968,7 @@ fn test_high_bits_masked<'tcx>(
|
|||
|
||||
assert_eq!(op_len, mask_len);
|
||||
|
||||
let high_bit_offset = op.layout.field(this, 0).size.bits().checked_sub(1).unwrap();
|
||||
let high_bit_offset = op.layout.field(this, 0).size.bits().strict_sub(1);
|
||||
|
||||
let mut direct = true;
|
||||
let mut negated = true;
|
||||
|
|
@ -1002,7 +999,7 @@ fn mask_load<'tcx>(
|
|||
assert_eq!(dest_len, mask_len);
|
||||
|
||||
let mask_item_size = mask.layout.field(this, 0).size;
|
||||
let high_bit_offset = mask_item_size.bits().checked_sub(1).unwrap();
|
||||
let high_bit_offset = mask_item_size.bits().strict_sub(1);
|
||||
|
||||
let ptr = this.read_pointer(ptr)?;
|
||||
for i in 0..dest_len {
|
||||
|
|
@ -1035,7 +1032,7 @@ fn mask_store<'tcx>(
|
|||
assert_eq!(value_len, mask_len);
|
||||
|
||||
let mask_item_size = mask.layout.field(this, 0).size;
|
||||
let high_bit_offset = mask_item_size.bits().checked_sub(1).unwrap();
|
||||
let high_bit_offset = mask_item_size.bits().strict_sub(1);
|
||||
|
||||
let ptr = this.read_pointer(ptr)?;
|
||||
for i in 0..value_len {
|
||||
|
|
@ -1077,15 +1074,15 @@ fn mpsadbw<'tcx>(
|
|||
let (_, _, right) = split_simd_to_128bit_chunks(this, right)?;
|
||||
let (_, dest_items_per_chunk, dest) = split_simd_to_128bit_chunks(this, dest)?;
|
||||
|
||||
assert_eq!(op_items_per_chunk, dest_items_per_chunk.checked_mul(2).unwrap());
|
||||
assert_eq!(op_items_per_chunk, dest_items_per_chunk.strict_mul(2));
|
||||
|
||||
let imm = this.read_scalar(imm)?.to_uint(imm.layout.size)?;
|
||||
// Bit 2 of `imm` specifies the offset for indices of `left`.
|
||||
// The offset is 0 when the bit is 0 or 4 when the bit is 1.
|
||||
let left_offset = u64::try_from((imm >> 2) & 1).unwrap().checked_mul(4).unwrap();
|
||||
let left_offset = u64::try_from((imm >> 2) & 1).unwrap().strict_mul(4);
|
||||
// Bits 0..=1 of `imm` specify the offset for indices of
|
||||
// `right` in blocks of 4 elements.
|
||||
let right_offset = u64::try_from(imm & 0b11).unwrap().checked_mul(4).unwrap();
|
||||
let right_offset = u64::try_from(imm & 0b11).unwrap().strict_mul(4);
|
||||
|
||||
for i in 0..num_chunks {
|
||||
let left = this.project_index(&left, i)?;
|
||||
|
|
@ -1093,18 +1090,16 @@ fn mpsadbw<'tcx>(
|
|||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
for j in 0..dest_items_per_chunk {
|
||||
let left_offset = left_offset.checked_add(j).unwrap();
|
||||
let left_offset = left_offset.strict_add(j);
|
||||
let mut res: u16 = 0;
|
||||
for k in 0..4 {
|
||||
let left = this
|
||||
.read_scalar(&this.project_index(&left, left_offset.checked_add(k).unwrap())?)?
|
||||
.read_scalar(&this.project_index(&left, left_offset.strict_add(k))?)?
|
||||
.to_u8()?;
|
||||
let right = this
|
||||
.read_scalar(
|
||||
&this.project_index(&right, right_offset.checked_add(k).unwrap())?,
|
||||
)?
|
||||
.read_scalar(&this.project_index(&right, right_offset.strict_add(k))?)?
|
||||
.to_u8()?;
|
||||
res = res.checked_add(left.abs_diff(right).into()).unwrap();
|
||||
res = res.strict_add(left.abs_diff(right).into());
|
||||
}
|
||||
this.write_scalar(Scalar::from_u16(res), &this.project_index(&dest, j)?)?;
|
||||
}
|
||||
|
|
@ -1138,8 +1133,7 @@ fn pmulhrsw<'tcx>(
|
|||
let right = this.read_scalar(&this.project_index(&right, i)?)?.to_i16()?;
|
||||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
let res =
|
||||
(i32::from(left).checked_mul(right.into()).unwrap() >> 14).checked_add(1).unwrap() >> 1;
|
||||
let res = (i32::from(left).strict_mul(right.into()) >> 14).strict_add(1) >> 1;
|
||||
|
||||
// The result of this operation can overflow a signed 16-bit integer.
|
||||
// When `left` and `right` are -0x8000, the result is 0x8000.
|
||||
|
|
@ -1235,7 +1229,7 @@ fn pack_generic<'tcx>(
|
|||
let (_, _, right) = split_simd_to_128bit_chunks(this, right)?;
|
||||
let (_, dest_items_per_chunk, dest) = split_simd_to_128bit_chunks(this, dest)?;
|
||||
|
||||
assert_eq!(dest_items_per_chunk, op_items_per_chunk.checked_mul(2).unwrap());
|
||||
assert_eq!(dest_items_per_chunk, op_items_per_chunk.strict_mul(2));
|
||||
|
||||
for i in 0..num_chunks {
|
||||
let left = this.project_index(&left, i)?;
|
||||
|
|
@ -1246,8 +1240,7 @@ fn pack_generic<'tcx>(
|
|||
let left = this.read_scalar(&this.project_index(&left, j)?)?;
|
||||
let right = this.read_scalar(&this.project_index(&right, j)?)?;
|
||||
let left_dest = this.project_index(&dest, j)?;
|
||||
let right_dest =
|
||||
this.project_index(&dest, j.checked_add(op_items_per_chunk).unwrap())?;
|
||||
let right_dest = this.project_index(&dest, j.strict_add(op_items_per_chunk))?;
|
||||
|
||||
let left_res = f(left)?;
|
||||
let right_res = f(right)?;
|
||||
|
|
|
|||
|
|
@ -47,22 +47,22 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let (dest, dest_len) = this.mplace_to_simd(dest)?;
|
||||
|
||||
assert_eq!(left_len, right_len);
|
||||
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
|
||||
assert_eq!(dest_len.strict_mul(2), left_len);
|
||||
|
||||
for i in 0..dest_len {
|
||||
let j1 = i.checked_mul(2).unwrap();
|
||||
let j1 = i.strict_mul(2);
|
||||
let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_i16()?;
|
||||
let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i16()?;
|
||||
|
||||
let j2 = j1.checked_add(1).unwrap();
|
||||
let j2 = j1.strict_add(1);
|
||||
let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_i16()?;
|
||||
let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i16()?;
|
||||
|
||||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
// Multiplications are i16*i16->i32, which will not overflow.
|
||||
let mul1 = i32::from(left1).checked_mul(right1.into()).unwrap();
|
||||
let mul2 = i32::from(left2).checked_mul(right2.into()).unwrap();
|
||||
let mul1 = i32::from(left1).strict_mul(right1.into());
|
||||
let mul2 = i32::from(left2).strict_mul(right2.into());
|
||||
// However, this addition can overflow in the most extreme case
|
||||
// (-0x8000)*(-0x8000)+(-0x8000)*(-0x8000) = 0x80000000
|
||||
let res = mul1.wrapping_add(mul2);
|
||||
|
|
@ -94,14 +94,14 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
let mut res: u16 = 0;
|
||||
let n = left_len.checked_div(dest_len).unwrap();
|
||||
let n = left_len.strict_div(dest_len);
|
||||
for j in 0..n {
|
||||
let op_i = j.checked_add(i.checked_mul(n).unwrap()).unwrap();
|
||||
let op_i = j.strict_add(i.strict_mul(n));
|
||||
let left = this.read_scalar(&this.project_index(&left, op_i)?)?.to_u8()?;
|
||||
let right =
|
||||
this.read_scalar(&this.project_index(&right, op_i)?)?.to_u8()?;
|
||||
|
||||
res = res.checked_add(left.abs_diff(right).into()).unwrap();
|
||||
res = res.strict_add(left.abs_diff(right).into());
|
||||
}
|
||||
|
||||
this.write_scalar(Scalar::from_u64(res.into()), &dest)?;
|
||||
|
|
|
|||
|
|
@ -89,22 +89,22 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
|||
let (dest, dest_len) = this.mplace_to_simd(dest)?;
|
||||
|
||||
assert_eq!(left_len, right_len);
|
||||
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
|
||||
assert_eq!(dest_len.strict_mul(2), left_len);
|
||||
|
||||
for i in 0..dest_len {
|
||||
let j1 = i.checked_mul(2).unwrap();
|
||||
let j1 = i.strict_mul(2);
|
||||
let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_u8()?;
|
||||
let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i8()?;
|
||||
|
||||
let j2 = j1.checked_add(1).unwrap();
|
||||
let j2 = j1.strict_add(1);
|
||||
let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_u8()?;
|
||||
let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i8()?;
|
||||
|
||||
let dest = this.project_index(&dest, i)?;
|
||||
|
||||
// Multiplication of a u8 and an i8 into an i16 cannot overflow.
|
||||
let mul1 = i16::from(left1).checked_mul(right1.into()).unwrap();
|
||||
let mul2 = i16::from(left2).checked_mul(right2.into()).unwrap();
|
||||
let mul1 = i16::from(left1).strict_mul(right1.into());
|
||||
let mul2 = i16::from(left2).strict_mul(right2.into());
|
||||
let res = mul1.saturating_add(mul2);
|
||||
|
||||
this.write_scalar(Scalar::from_i16(res), &dest)?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue