about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2019-06-08 23:53:10 +0200
committerRalf Jung <post@ralfj.de>2019-06-09 00:00:44 +0200
commit0803d75eb6083111504e9bae4fa6baf9104928fe (patch)
tree4de75084e4146f58b74700701d687f6646733dcb /src
parent3836573ae4610f75d41d467e35d855efd6b000b5 (diff)
downloadrust-0803d75eb6083111504e9bae4fa6baf9104928fe.tar.gz
rust-0803d75eb6083111504e9bae4fa6baf9104928fe.zip
offer ways to directly construct a Scalar from unsigned integers
Diffstat (limited to 'src')
-rw-r--r--src/librustc/mir/interpret/value.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs
index 1909f3cb998..8d4d6176483 100644
--- a/src/librustc/mir/interpret/value.rs
+++ b/src/librustc/mir/interpret/value.rs
@@ -281,6 +281,26 @@ impl<'tcx, Tag> Scalar<Tag> {
     }
 
     #[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();
         // `into` performed sign extension, we have to truncate
@@ -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]