Skip to content

Commit

Permalink
offer ways to directly construct a Scalar from unsigned integers
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jun 8, 2019
1 parent 3836573 commit 0803d75
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/librustc/mir/interpret/value.rs
Expand Up @@ -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();
Expand All @@ -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]
Expand Down

0 comments on commit 0803d75

Please sign in to comment.