generalize truncate and sign_extend to take a Size
This commit is contained in:
parent
ad2de8b4ee
commit
7483ea8176
@ -1085,8 +1085,9 @@ pub fn compare_const_vals<'a, 'tcx>(
|
|||||||
},
|
},
|
||||||
ty::TyInt(_) => {
|
ty::TyInt(_) => {
|
||||||
let layout = tcx.layout_of(ty).ok()?;
|
let layout = tcx.layout_of(ty).ok()?;
|
||||||
let a = interpret::sign_extend(a, layout);
|
assert!(layout.abi.is_signed());
|
||||||
let b = interpret::sign_extend(b, layout);
|
let a = interpret::sign_extend(a, layout.size);
|
||||||
|
let b = interpret::sign_extend(b, layout.size);
|
||||||
Some((a as i128).cmp(&(b as i128)))
|
Some((a as i128).cmp(&(b as i128)))
|
||||||
},
|
},
|
||||||
_ => Some(a.cmp(&b)),
|
_ => Some(a.cmp(&b)),
|
||||||
|
@ -1020,11 +1020,12 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sign_extend(&self, value: u128, ty: TyLayout<'_>) -> u128 {
|
pub fn sign_extend(&self, value: u128, ty: TyLayout<'_>) -> u128 {
|
||||||
super::sign_extend(value, ty)
|
assert!(ty.abi.is_signed());
|
||||||
|
super::sign_extend(value, ty.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
|
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
|
||||||
super::truncate(value, ty)
|
super::truncate(value, ty.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dump_field_name(&self, s: &mut String, ty: Ty<'tcx>, i: usize, variant: usize) -> ::std::fmt::Result {
|
fn dump_field_name(&self, s: &mut String, ty: Ty<'tcx>, i: usize, variant: usize) -> ::std::fmt::Result {
|
||||||
@ -1095,9 +1096,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sign_extend(value: u128, layout: TyLayout<'_>) -> u128 {
|
pub fn sign_extend(value: u128, size: Size) -> u128 {
|
||||||
let size = layout.size.bits();
|
let size = size.bits();
|
||||||
assert!(layout.abi.is_signed());
|
|
||||||
// sign extend
|
// sign extend
|
||||||
let shift = 128 - size;
|
let shift = 128 - size;
|
||||||
// shift the unsigned value to the left
|
// shift the unsigned value to the left
|
||||||
@ -1105,8 +1105,8 @@ pub fn sign_extend(value: u128, layout: TyLayout<'_>) -> u128 {
|
|||||||
(((value << shift) as i128) >> shift) as u128
|
(((value << shift) as i128) >> shift) as u128
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn truncate(value: u128, layout: TyLayout<'_>) -> u128 {
|
pub fn truncate(value: u128, size: Size) -> u128 {
|
||||||
let size = layout.size.bits();
|
let size = size.bits();
|
||||||
let shift = 128 - size;
|
let shift = 128 - size;
|
||||||
// truncate (shift left to drop out leftover values, shift right to fill with zeroes)
|
// truncate (shift left to drop out leftover values, shift right to fill with zeroes)
|
||||||
(value << shift) >> shift
|
(value << shift) >> shift
|
||||||
|
Loading…
Reference in New Issue
Block a user