@@ -3,7 +3,7 @@ use std::fmt;
33use either:: { Either , Left , Right } ;
44
55use rustc_apfloat:: {
6- ieee:: { Double , Single } ,
6+ ieee:: { Double , Half , Quad , Single } ,
77 Float ,
88} ;
99use rustc_macros:: HashStable ;
@@ -159,6 +159,13 @@ impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> {
159159 }
160160}
161161
162+ impl < Prov > From < Half > for Scalar < Prov > {
163+ #[ inline( always) ]
164+ fn from ( f : Half ) -> Self {
165+ Scalar :: from_f16 ( f)
166+ }
167+ }
168+
162169impl < Prov > From < Single > for Scalar < Prov > {
163170 #[ inline( always) ]
164171 fn from ( f : Single ) -> Self {
@@ -173,6 +180,13 @@ impl<Prov> From<Double> for Scalar<Prov> {
173180 }
174181}
175182
183+ impl < Prov > From < Quad > for Scalar < Prov > {
184+ #[ inline( always) ]
185+ fn from ( f : Quad ) -> Self {
186+ Scalar :: from_f128 ( f)
187+ }
188+ }
189+
176190impl < Prov > From < ScalarInt > for Scalar < Prov > {
177191 #[ inline( always) ]
178192 fn from ( ptr : ScalarInt ) -> Self {
@@ -281,6 +295,11 @@ impl<Prov> Scalar<Prov> {
281295 Self :: from_int ( i, cx. data_layout ( ) . pointer_size )
282296 }
283297
298+ #[ inline]
299+ pub fn from_f16 ( f : Half ) -> Self {
300+ Scalar :: Int ( f. into ( ) )
301+ }
302+
284303 #[ inline]
285304 pub fn from_f32 ( f : Single ) -> Self {
286305 Scalar :: Int ( f. into ( ) )
@@ -291,6 +310,11 @@ impl<Prov> Scalar<Prov> {
291310 Scalar :: Int ( f. into ( ) )
292311 }
293312
313+ #[ inline]
314+ pub fn from_f128 ( f : Quad ) -> Self {
315+ Scalar :: Int ( f. into ( ) )
316+ }
317+
294318 /// This is almost certainly not the method you want! You should dispatch on the type
295319 /// and use `to_{u8,u16,...}`/`scalar_to_ptr` to perform ptr-to-int / int-to-ptr casts as needed.
296320 ///
@@ -493,6 +517,12 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
493517 Ok ( i64:: try_from ( b) . unwrap ( ) )
494518 }
495519
520+ #[ inline]
521+ pub fn to_f16 ( self ) -> InterpResult < ' tcx , Half > {
522+ // Going through `u16` to check size and truncation.
523+ Ok ( Half :: from_bits ( self . to_u16 ( ) ?. into ( ) ) )
524+ }
525+
496526 #[ inline]
497527 pub fn to_f32 ( self ) -> InterpResult < ' tcx , Single > {
498528 // Going through `u32` to check size and truncation.
@@ -504,6 +534,12 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
504534 // Going through `u64` to check size and truncation.
505535 Ok ( Double :: from_bits ( self . to_u64 ( ) ?. into ( ) ) )
506536 }
537+
538+ #[ inline]
539+ pub fn to_f128 ( self ) -> InterpResult < ' tcx , Quad > {
540+ // Going through `u128` to check size and truncation.
541+ Ok ( Quad :: from_bits ( self . to_u128 ( ) ?. into ( ) ) )
542+ }
507543}
508544
509545/// Gets the bytes of a constant slice value.
0 commit comments