1
- use rustc_apfloat:: ieee:: { Double , Single } ;
1
+ use rustc_apfloat:: ieee:: { Double , Half , Quad , Single } ;
2
2
use rustc_apfloat:: Float ;
3
3
use rustc_errors:: { DiagArgValue , IntoDiagnosticArg } ;
4
4
use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
@@ -369,6 +369,11 @@ impl ScalarInt {
369
369
Ok ( F :: from_bits ( self . to_bits ( Size :: from_bits ( F :: BITS ) ) ?) )
370
370
}
371
371
372
+ #[ inline]
373
+ pub fn try_to_f16 ( self ) -> Result < Half , Size > {
374
+ self . try_to_float ( )
375
+ }
376
+
372
377
#[ inline]
373
378
pub fn try_to_f32 ( self ) -> Result < Single , Size > {
374
379
self . try_to_float ( )
@@ -378,6 +383,11 @@ impl ScalarInt {
378
383
pub fn try_to_f64 ( self ) -> Result < Double , Size > {
379
384
self . try_to_float ( )
380
385
}
386
+
387
+ #[ inline]
388
+ pub fn try_to_f128 ( self ) -> Result < Quad , Size > {
389
+ self . try_to_float ( )
390
+ }
381
391
}
382
392
383
393
macro_rules! from {
@@ -450,6 +460,22 @@ impl TryFrom<ScalarInt> for char {
450
460
}
451
461
}
452
462
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
+
453
479
impl From < Single > for ScalarInt {
454
480
#[ inline]
455
481
fn from ( f : Single ) -> Self {
@@ -482,6 +508,22 @@ impl TryFrom<ScalarInt> for Double {
482
508
}
483
509
}
484
510
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
+
485
527
impl fmt:: Debug for ScalarInt {
486
528
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
487
529
// Dispatch to LowerHex below.
0 commit comments