Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 68687e2

Browse files
committed
rustc_middle: add Scalar::from_i8 and Scalar::from_i16 and use them in Miri
1 parent 76e59c7 commit 68687e2

File tree

2 files changed

+15
-9
lines changed
  • compiler/rustc_middle/src/mir/interpret
  • src/tools/miri/src/shims/x86

2 files changed

+15
-9
lines changed

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ impl<Prov> Scalar<Prov> {
266266
.unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
267267
}
268268

269+
#[inline]
270+
pub fn from_i8(i: i8) -> Self {
271+
Self::from_int(i, Size::from_bits(8))
272+
}
273+
274+
#[inline]
275+
pub fn from_i16(i: i16) -> Self {
276+
Self::from_int(i, Size::from_bits(16))
277+
}
278+
269279
#[inline]
270280
pub fn from_i32(i: i32) -> Self {
271281
Self::from_int(i, Size::from_bits(32))

src/tools/miri/src/shims/x86/sse2.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_apfloat::{
55
use rustc_middle::ty::layout::LayoutOf as _;
66
use rustc_middle::ty::Ty;
77
use rustc_span::Symbol;
8-
use rustc_target::abi::Size;
98
use rustc_target::spec::abi::Abi;
109

1110
use super::FloatCmpOp;
@@ -112,7 +111,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
112111

113112
// Values are expanded from i16 to i32, so multiplication cannot overflow.
114113
let res = i32::from(left).checked_mul(i32::from(right)).unwrap() >> 16;
115-
this.write_scalar(Scalar::from_int(res, Size::from_bits(16)), &dest)?;
114+
this.write_scalar(Scalar::from_i16(res.try_into().unwrap()), &dest)?;
116115
}
117116
}
118117
// Used to implement the _mm_mulhi_epu16 function.
@@ -431,11 +430,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
431430
let right_res =
432431
i8::try_from(right).unwrap_or(if right < 0 { i8::MIN } else { i8::MAX });
433432

434-
this.write_scalar(Scalar::from_int(left_res, Size::from_bits(8)), &left_dest)?;
435-
this.write_scalar(
436-
Scalar::from_int(right_res, Size::from_bits(8)),
437-
&right_dest,
438-
)?;
433+
this.write_scalar(Scalar::from_i8(left_res.try_into().unwrap()), &left_dest)?;
434+
this.write_scalar(Scalar::from_i8(right_res.try_into().unwrap()), &right_dest)?;
439435
}
440436
}
441437
// Used to implement the _mm_packus_epi16 function.
@@ -495,9 +491,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
495491
let right_res =
496492
i16::try_from(right).unwrap_or(if right < 0 { i16::MIN } else { i16::MAX });
497493

498-
this.write_scalar(Scalar::from_int(left_res, Size::from_bits(16)), &left_dest)?;
494+
this.write_scalar(Scalar::from_i16(left_res.try_into().unwrap()), &left_dest)?;
499495
this.write_scalar(
500-
Scalar::from_int(right_res, Size::from_bits(16)),
496+
Scalar::from_i16(right_res.try_into().unwrap()),
501497
&right_dest,
502498
)?;
503499
}

0 commit comments

Comments
 (0)