Skip to content
Permalink
Browse files

offer ways to directly construct a Scalar from unsigned integers

  • Loading branch information...
RalfJung committed Jun 8, 2019
1 parent 3836573 commit 0803d75eb6083111504e9bae4fa6baf9104928fe
Showing with 24 additions and 2 deletions.
  1. +24 −2 src/librustc/mir/interpret/value.rs
@@ -280,6 +280,26 @@ impl<'tcx, Tag> Scalar<Tag> {
Scalar::Raw { data: i, size: size.bytes() as u8 }
}

#[inline]
pub fn from_u8(i: u8) -> Self {
Scalar::Raw { data: i as u128, size: 1 }
}

#[inline]
pub fn from_u16(i: u16) -> Self {
Scalar::Raw { data: i as u128, size: 2 }
}

#[inline]
pub fn from_u32(i: u32) -> Self {
Scalar::Raw { data: i as u128, size: 4 }
}

#[inline]
pub fn from_u64(i: u64) -> Self {
Scalar::Raw { data: i as u128, size: 8 }
}

#[inline]
pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
let i = i.into();
@@ -294,12 +314,14 @@ impl<'tcx, Tag> Scalar<Tag> {

#[inline]
pub fn from_f32(f: Single) -> Self {
Scalar::Raw { data: f.to_bits() as u128, size: 4 }
// We trust apfloat to give us properly truncated data
Scalar::Raw { data: f.to_bits(), size: 4 }
}

#[inline]
pub fn from_f64(f: Double) -> Self {
Scalar::Raw { data: f.to_bits() as u128, size: 8 }
// We trust apfloat to give us properly truncated data
Scalar::Raw { data: f.to_bits(), size: 8 }
}

#[inline]

0 comments on commit 0803d75

Please sign in to comment.
You can’t perform that action at this time.