1
+ #[ cfg( has_f128) ]
2
+ use core:: f128;
1
3
#[ cfg( has_f16) ]
2
4
use core:: f16;
3
5
use core:: mem:: size_of;
@@ -134,6 +136,15 @@ pub trait ToPrimitive {
134
136
None => self . to_u64 ( ) . as_ref ( ) . and_then ( ToPrimitive :: to_f64) ,
135
137
}
136
138
}
139
+
140
+ /// Converts the value of `self` to an `f128`. Overflows may map to positive
141
+ /// or negative inifinity, otherwise `None` is returned if the value cannot
142
+ /// be represented by an `f128`.
143
+ #[ cfg( has_f128) ]
144
+ #[ inline]
145
+ fn to_f128 ( & self ) -> Option < f128 > {
146
+ self . to_f64 ( ) . as_ref ( ) . and_then ( ToPrimitive :: to_f128)
147
+ }
137
148
}
138
149
139
150
macro_rules! impl_to_primitive_int_to_int {
@@ -201,6 +212,11 @@ macro_rules! impl_to_primitive_int {
201
212
fn to_f64( & self ) -> Option <f64 > {
202
213
Some ( * self as f64 )
203
214
}
215
+ #[ cfg( has_f128) ]
216
+ #[ inline]
217
+ fn to_f128( & self ) -> Option <f128> {
218
+ Some ( * self as f128)
219
+ }
204
220
}
205
221
} ;
206
222
}
@@ -276,6 +292,11 @@ macro_rules! impl_to_primitive_uint {
276
292
fn to_f64( & self ) -> Option <f64 > {
277
293
Some ( * self as f64 )
278
294
}
295
+ #[ cfg( has_f128) ]
296
+ #[ inline]
297
+ fn to_f128( & self ) -> Option <f128> {
298
+ Some ( * self as f128)
299
+ }
279
300
}
280
301
} ;
281
302
}
@@ -390,6 +411,8 @@ macro_rules! impl_to_primitive_float {
390
411
fn to_f16 -> f16;
391
412
fn to_f32 -> f32 ;
392
413
fn to_f64 -> f64 ;
414
+ #[ cfg( has_f128) ]
415
+ fn to_f128 -> f128;
393
416
}
394
417
}
395
418
} ;
@@ -399,6 +422,8 @@ macro_rules! impl_to_primitive_float {
399
422
impl_to_primitive_float ! ( f16) ;
400
423
impl_to_primitive_float ! ( f32 ) ;
401
424
impl_to_primitive_float ! ( f64 ) ;
425
+ #[ cfg( has_f128) ]
426
+ impl_to_primitive_float ! ( f128) ;
402
427
403
428
/// A generic trait for converting a number to a value.
404
429
///
@@ -523,6 +548,14 @@ pub trait FromPrimitive: Sized {
523
548
None => n. to_u64 ( ) . and_then ( FromPrimitive :: from_u64) ,
524
549
}
525
550
}
551
+
552
+ /// Converts a `f128` to return an optional value of this type. If the
553
+ /// value cannot be represented by this type, then `None` is returned.
554
+ #[ cfg( has_f128) ]
555
+ #[ inline]
556
+ fn from_f128 ( n : f128 ) -> Option < Self > {
557
+ FromPrimitive :: from_f64 ( n as f64 )
558
+ }
526
559
}
527
560
528
561
macro_rules! impl_from_primitive {
@@ -592,6 +625,11 @@ macro_rules! impl_from_primitive {
592
625
fn from_f64( n: f64 ) -> Option <$T> {
593
626
n. $to_ty( )
594
627
}
628
+ #[ cfg( has_f128) ]
629
+ #[ inline]
630
+ fn from_f128( n: f128) -> Option <$T> {
631
+ n. $to_ty( )
632
+ }
595
633
}
596
634
} ;
597
635
}
@@ -612,6 +650,8 @@ impl_from_primitive!(u128, to_u128);
612
650
impl_from_primitive ! ( f16, to_f16) ;
613
651
impl_from_primitive ! ( f32 , to_f32) ;
614
652
impl_from_primitive ! ( f64 , to_f64) ;
653
+ #[ cfg( has_f128) ]
654
+ impl_from_primitive ! ( f128, to_f128) ;
615
655
616
656
macro_rules! impl_to_primitive_wrapping {
617
657
( $( $( #[ $cfg: meta] ) * fn $method: ident -> $i: ident ; ) * ) => { $(
@@ -643,6 +683,8 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
643
683
fn to_f16 -> f16;
644
684
fn to_f32 -> f32 ;
645
685
fn to_f64 -> f64 ;
686
+ #[ cfg( has_f128) ]
687
+ fn to_f128 -> f128;
646
688
}
647
689
}
648
690
@@ -676,6 +718,8 @@ impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
676
718
fn from_f16( f16) ;
677
719
fn from_f32( f32 ) ;
678
720
fn from_f64( f64 ) ;
721
+ #[ cfg( has_f128) ]
722
+ fn from_f128( f128) ;
679
723
}
680
724
}
681
725
@@ -741,6 +785,8 @@ impl_num_cast!(isize, to_isize);
741
785
impl_num_cast ! ( f16, to_f16) ;
742
786
impl_num_cast ! ( f32 , to_f32) ;
743
787
impl_num_cast ! ( f64 , to_f64) ;
788
+ #[ cfg( has_f128) ]
789
+ impl_num_cast ! ( f128, to_f128) ;
744
790
745
791
impl < T : NumCast > NumCast for Wrapping < T > {
746
792
fn from < U : ToPrimitive > ( n : U ) -> Option < Self > {
@@ -799,21 +845,23 @@ macro_rules! impl_as_primitive {
799
845
} ;
800
846
}
801
847
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 } ) ;
848
+ impl_as_primitive ! ( u8 => { char , #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
849
+ impl_as_primitive ! ( i8 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
850
+ impl_as_primitive ! ( u16 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
851
+ impl_as_primitive ! ( i16 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
852
+ impl_as_primitive ! ( u32 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
853
+ impl_as_primitive ! ( i32 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
854
+ impl_as_primitive ! ( u64 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
855
+ impl_as_primitive ! ( i64 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
856
+ impl_as_primitive ! ( u128 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
857
+ impl_as_primitive ! ( i128 => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
858
+ impl_as_primitive ! ( usize => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
859
+ impl_as_primitive ! ( isize => { #[ cfg( has_f16) ] f16, f32 , f64 , # [ cfg ( has_f128 ) ] f128 } ) ;
814
860
#[ 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 } ) ;
861
+ impl_as_primitive ! ( f16 => { f16, f32 , f64 , #[ cfg( has_f128) ] f128 } ) ;
862
+ impl_as_primitive ! ( f32 => { #[ cfg( has_f16) ] f16, f32 , f64 , #[ cfg( has_f128) ] f128 } ) ;
863
+ impl_as_primitive ! ( f64 => { #[ cfg( has_f16) ] f16, f32 , f64 , #[ cfg( has_f128) ] f128 } ) ;
864
+ #[ cfg( has_f128) ]
865
+ impl_as_primitive ! ( f128 => { #[ cfg( has_f16) ] f16, f32 , f64 , f128 } ) ;
818
866
impl_as_primitive ! ( char => { char } ) ;
819
867
impl_as_primitive ! ( bool => { } ) ;
0 commit comments