1
+ #[ cfg( has_f16) ]
2
+ use core:: f16;
1
3
use core:: mem:: size_of;
2
4
use core:: num:: Wrapping ;
3
5
use core:: { f32, f64} ;
@@ -101,6 +103,15 @@ pub trait ToPrimitive {
101
103
self . to_u64 ( ) . map ( From :: from)
102
104
}
103
105
106
+ /// Converts the value of `self` to an `f16`. Overflows may map to positive
107
+ /// or negative inifinity, otherwise `None` is returned if the value cannot
108
+ /// be represented by an `f16`.
109
+ #[ cfg( has_f16) ]
110
+ #[ inline]
111
+ fn to_f16 ( & self ) -> Option < f16 > {
112
+ self . to_f64 ( ) . as_ref ( ) . and_then ( ToPrimitive :: to_f16)
113
+ }
114
+
104
115
/// Converts the value of `self` to an `f32`. Overflows may map to positive
105
116
/// or negative inifinity, otherwise `None` is returned if the value cannot
106
117
/// be represented by an `f32`.
@@ -177,6 +188,11 @@ macro_rules! impl_to_primitive_int {
177
188
fn to_u128 -> u128 ;
178
189
}
179
190
191
+ #[ cfg( has_f16) ]
192
+ #[ inline]
193
+ fn to_f16( & self ) -> Option <f16> {
194
+ Some ( * self as f16)
195
+ }
180
196
#[ inline]
181
197
fn to_f32( & self ) -> Option <f32 > {
182
198
Some ( * self as f32 )
@@ -247,6 +263,11 @@ macro_rules! impl_to_primitive_uint {
247
263
fn to_u128 -> u128 ;
248
264
}
249
265
266
+ #[ cfg( has_f16) ]
267
+ #[ inline]
268
+ fn to_f16( & self ) -> Option <f16> {
269
+ Some ( * self as f16)
270
+ }
250
271
#[ inline]
251
272
fn to_f32( & self ) -> Option <f32 > {
252
273
Some ( * self as f32 )
@@ -267,8 +288,9 @@ impl_to_primitive_uint!(u64);
267
288
impl_to_primitive_uint ! ( u128 ) ;
268
289
269
290
macro_rules! impl_to_primitive_float_to_float {
270
- ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
291
+ ( $SrcT: ident : $( $ ( # [ $cfg : meta ] ) * fn $method: ident -> $DstT: ident ; ) * ) => { $(
271
292
#[ inline]
293
+ $( #[ $cfg] ) *
272
294
fn $method( & self ) -> Option <$DstT> {
273
295
// We can safely cast all values, whether NaN, +-inf, or finite.
274
296
// Finite values that are reducing size may saturate to +-inf.
@@ -364,13 +386,17 @@ macro_rules! impl_to_primitive_float {
364
386
}
365
387
366
388
impl_to_primitive_float_to_float! { $T:
389
+ #[ cfg( has_f16) ]
390
+ fn to_f16 -> f16;
367
391
fn to_f32 -> f32 ;
368
392
fn to_f64 -> f64 ;
369
393
}
370
394
}
371
395
} ;
372
396
}
373
397
398
+ #[ cfg( has_f16) ]
399
+ impl_to_primitive_float ! ( f16) ;
374
400
impl_to_primitive_float ! ( f32 ) ;
375
401
impl_to_primitive_float ! ( f64 ) ;
376
402
@@ -469,6 +495,14 @@ pub trait FromPrimitive: Sized {
469
495
n. to_u64 ( ) . and_then ( FromPrimitive :: from_u64)
470
496
}
471
497
498
+ /// Converts a `f16` to return an optional value of this type. If the
499
+ /// value cannot be represented by this type, then `None` is returned.
500
+ #[ cfg( has_f16) ]
501
+ #[ inline]
502
+ fn from_f16 ( n : f16 ) -> Option < Self > {
503
+ FromPrimitive :: from_f64 ( n as f64 )
504
+ }
505
+
472
506
/// Converts a `f32` to return an optional value of this type. If the
473
507
/// value cannot be represented by this type, then `None` is returned.
474
508
#[ inline]
@@ -545,6 +579,11 @@ macro_rules! impl_from_primitive {
545
579
n. $to_ty( )
546
580
}
547
581
582
+ #[ cfg( has_f16) ]
583
+ #[ inline]
584
+ fn from_f16( n: f16) -> Option <$T> {
585
+ n. $to_ty( )
586
+ }
548
587
#[ inline]
549
588
fn from_f32( n: f32 ) -> Option <$T> {
550
589
n. $to_ty( )
@@ -569,6 +608,8 @@ impl_from_primitive!(u16, to_u16);
569
608
impl_from_primitive ! ( u32 , to_u32) ;
570
609
impl_from_primitive ! ( u64 , to_u64) ;
571
610
impl_from_primitive ! ( u128 , to_u128) ;
611
+ #[ cfg( has_f16) ]
612
+ impl_from_primitive ! ( f16, to_f16) ;
572
613
impl_from_primitive ! ( f32 , to_f32) ;
573
614
impl_from_primitive ! ( f64 , to_f64) ;
574
615
@@ -598,6 +639,8 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
598
639
fn to_u64 -> u64 ;
599
640
fn to_u128 -> u128 ;
600
641
642
+ #[ cfg( has_f16) ]
643
+ fn to_f16 -> f16;
601
644
fn to_f32 -> f32 ;
602
645
fn to_f64 -> f64 ;
603
646
}
@@ -629,6 +672,8 @@ impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
629
672
fn from_u64( u64 ) ;
630
673
fn from_u128( u128 ) ;
631
674
675
+ #[ cfg( has_f16) ]
676
+ fn from_f16( f16) ;
632
677
fn from_f32( f32 ) ;
633
678
fn from_f64( f64 ) ;
634
679
}
@@ -692,6 +737,8 @@ impl_num_cast!(i32, to_i32);
692
737
impl_num_cast ! ( i64 , to_i64) ;
693
738
impl_num_cast ! ( i128 , to_i128) ;
694
739
impl_num_cast ! ( isize , to_isize) ;
740
+ #[ cfg( has_f16) ]
741
+ impl_num_cast ! ( f16, to_f16) ;
695
742
impl_num_cast ! ( f32 , to_f32) ;
696
743
impl_num_cast ! ( f64 , to_f64) ;
697
744
@@ -742,29 +789,31 @@ macro_rules! impl_as_primitive {
742
789
#[ inline] fn as_( self ) -> $U { self as $U }
743
790
}
744
791
} ;
745
- ( @ $T: ty => { $( $U: ty ) ,* } ) => { $(
746
- impl_as_primitive!( @ $T => impl $U) ;
792
+ ( @ $T: ty => { $( $( # [ $cfg : meta ] ) * $ U: ty ) ,* } ) => { $(
793
+ impl_as_primitive!( @ $T => $ ( # [ $cfg ] ) * impl $U) ;
747
794
) * } ;
748
- ( $T: ty => { $( $U: ty ) ,* } ) => {
749
- impl_as_primitive!( @ $T => { $( $U ) ,* } ) ;
795
+ ( $T: ty => { $( $( # [ $cfg : meta ] ) * $ U: ty ) ,* } ) => {
796
+ impl_as_primitive!( @ $T => { $( $( # [ $cfg ] ) * $ U ) ,* } ) ;
750
797
impl_as_primitive!( @ $T => { u8 , u16 , u32 , u64 , u128 , usize } ) ;
751
798
impl_as_primitive!( @ $T => { i8 , i16 , i32 , i64 , i128 , isize } ) ;
752
799
} ;
753
800
}
754
801
755
- impl_as_primitive ! ( u8 => { char , f32 , f64 } ) ;
756
- impl_as_primitive ! ( i8 => { f32 , f64 } ) ;
757
- impl_as_primitive ! ( u16 => { f32 , f64 } ) ;
758
- impl_as_primitive ! ( i16 => { f32 , f64 } ) ;
759
- impl_as_primitive ! ( u32 => { f32 , f64 } ) ;
760
- impl_as_primitive ! ( i32 => { f32 , f64 } ) ;
761
- impl_as_primitive ! ( u64 => { f32 , f64 } ) ;
762
- impl_as_primitive ! ( i64 => { f32 , f64 } ) ;
763
- impl_as_primitive ! ( u128 => { f32 , f64 } ) ;
764
- impl_as_primitive ! ( i128 => { f32 , f64 } ) ;
765
- impl_as_primitive ! ( usize => { f32 , f64 } ) ;
766
- impl_as_primitive ! ( isize => { f32 , f64 } ) ;
767
- impl_as_primitive ! ( f32 => { f32 , f64 } ) ;
768
- impl_as_primitive ! ( f64 => { f32 , f64 } ) ;
802
+ impl_as_primitive ! ( u8 => { char , #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
803
+ impl_as_primitive ! ( i8 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
804
+ impl_as_primitive ! ( u16 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
805
+ impl_as_primitive ! ( i16 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
806
+ impl_as_primitive ! ( u32 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
807
+ impl_as_primitive ! ( i32 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
808
+ impl_as_primitive ! ( u64 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
809
+ impl_as_primitive ! ( i64 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
810
+ impl_as_primitive ! ( u128 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
811
+ impl_as_primitive ! ( i128 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
812
+ impl_as_primitive ! ( usize => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
813
+ impl_as_primitive ! ( isize => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
814
+ #[ cfg( has_f16) ]
815
+ impl_as_primitive ! ( f16 => { f16, f32 , f64 } ) ;
816
+ impl_as_primitive ! ( f32 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
817
+ impl_as_primitive ! ( f64 => { #[ cfg( has_f16) ] f16, f32 , f64 } ) ;
769
818
impl_as_primitive ! ( char => { char } ) ;
770
819
impl_as_primitive ! ( bool => { } ) ;
0 commit comments