1- use rustc_apfloat:: ieee:: { Double , Single } ;
1+ use rustc_apfloat:: ieee:: { Double , Half , Quad , Single } ;
22use rustc_apfloat:: Float ;
33use rustc_errors:: { DiagArgValue , IntoDiagnosticArg } ;
44use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
@@ -369,6 +369,11 @@ impl ScalarInt {
369369 Ok ( F :: from_bits ( self . to_bits ( Size :: from_bits ( F :: BITS ) ) ?) )
370370 }
371371
372+ #[ inline]
373+ pub fn try_to_f16 ( self ) -> Result < Half , Size > {
374+ self . try_to_float ( )
375+ }
376+
372377 #[ inline]
373378 pub fn try_to_f32 ( self ) -> Result < Single , Size > {
374379 self . try_to_float ( )
@@ -378,6 +383,11 @@ impl ScalarInt {
378383 pub fn try_to_f64 ( self ) -> Result < Double , Size > {
379384 self . try_to_float ( )
380385 }
386+
387+ #[ inline]
388+ pub fn try_to_f128 ( self ) -> Result < Quad , Size > {
389+ self . try_to_float ( )
390+ }
381391}
382392
383393macro_rules! from {
@@ -450,6 +460,22 @@ impl TryFrom<ScalarInt> for char {
450460 }
451461}
452462
463+ impl From < Half > for ScalarInt {
464+ #[ inline]
465+ fn from ( f : Half ) -> Self {
466+ // We trust apfloat to give us properly truncated data.
467+ Self { data : f. to_bits ( ) , size : NonZero :: new ( ( Half :: BITS / 8 ) as u8 ) . unwrap ( ) }
468+ }
469+ }
470+
471+ impl TryFrom < ScalarInt > for Half {
472+ type Error = Size ;
473+ #[ inline]
474+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
475+ int. to_bits ( Size :: from_bytes ( 2 ) ) . map ( Self :: from_bits)
476+ }
477+ }
478+
453479impl From < Single > for ScalarInt {
454480 #[ inline]
455481 fn from ( f : Single ) -> Self {
@@ -482,6 +508,22 @@ impl TryFrom<ScalarInt> for Double {
482508 }
483509}
484510
511+ impl From < Quad > for ScalarInt {
512+ #[ inline]
513+ fn from ( f : Quad ) -> Self {
514+ // We trust apfloat to give us properly truncated data.
515+ Self { data : f. to_bits ( ) , size : NonZero :: new ( ( Quad :: BITS / 8 ) as u8 ) . unwrap ( ) }
516+ }
517+ }
518+
519+ impl TryFrom < ScalarInt > for Quad {
520+ type Error = Size ;
521+ #[ inline]
522+ fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
523+ int. to_bits ( Size :: from_bytes ( 16 ) ) . map ( Self :: from_bits)
524+ }
525+ }
526+
485527impl fmt:: Debug for ScalarInt {
486528 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
487529 // Dispatch to LowerHex below.
0 commit comments