44#![ feature( cfg_target_has_reliable_f16_f128) ]
55
66mod exhaustive;
7+ mod host;
78
89use io:: IsTerminal ;
910use io:: Read ;
@@ -17,6 +18,8 @@ use num_traits::{FromPrimitive, ToPrimitive};
1718use rustc_apfloat:: ieee:: { Double , Single } ;
1819use rustc_apfloat:: { Float , FloatConvert , Round , Status , StatusAnd , ieee} ;
1920
21+ use crate :: host:: HostFloat ;
22+
2023#[ derive( Clone , Parser , Debug ) ]
2124struct Args {
2225 /// Disable comparison with C++ (LLVM's original) APFloat
@@ -1034,137 +1037,6 @@ fn eval_host<F: HostFloat>(
10341037 Some ( res. map ( F :: to_bits) )
10351038}
10361039
1037- /// Abstraction over host float operations. If the requested rounding mode is not supported,
1038- /// return `None`.
1039- trait HostFloat : Copy + Sized + fmt:: Debug {
1040- type UInt : Copy + fmt:: LowerHex ;
1041- fn from_bits ( bits : Self :: UInt ) -> Self ;
1042- fn to_bits ( self ) -> Self :: UInt ;
1043- fn neg ( self ) -> Self ;
1044- fn add_r ( self , other : Self , rm : Round ) -> Option < StatusAnd < Self > > ;
1045- fn sub_r ( self , other : Self , rm : Round ) -> Option < StatusAnd < Self > > ;
1046- fn mul_r ( self , other : Self , rm : Round ) -> Option < StatusAnd < Self > > ;
1047- fn div_r ( self , other : Self , rm : Round ) -> Option < StatusAnd < Self > > ;
1048- fn rem ( self , other : Self ) -> Self ;
1049- fn mul_add_r ( self , mul : Self , add : Self , rm : Round ) -> Option < StatusAnd < Self > > ;
1050- fn to_i128_r ( self , rm : Round ) -> Option < StatusAnd < i128 > > ;
1051- fn from_i128_r ( x : i128 , rm : Round ) -> Option < StatusAnd < Self > > ;
1052- fn to_u128_r ( self , rm : Round ) -> Option < StatusAnd < u128 > > ;
1053- fn from_u128_r ( x : u128 , rm : Round ) -> Option < StatusAnd < Self > > ;
1054- fn to_double_r ( self , rm : Round ) -> Option < StatusAnd < f64 > > ;
1055- fn from_double_r ( x : f64 , rm : Round ) -> Option < StatusAnd < Self > > ;
1056- fn to_single_r ( self , rm : Round ) -> Option < StatusAnd < f32 > > ;
1057- fn from_single_r ( x : f32 , rm : Round ) -> Option < StatusAnd < Self > > ;
1058- }
1059-
1060- macro_rules! impl_host_float {
1061- ( $ty: ty, $ity: ty) => {
1062- impl HostFloat for $ty {
1063- type UInt = $ity;
1064- fn from_bits( bits: Self :: UInt ) -> Self {
1065- Self :: from_bits( bits)
1066- }
1067- fn to_bits( self ) -> Self :: UInt {
1068- self . to_bits( )
1069- }
1070- fn neg( self ) -> Self {
1071- -self
1072- }
1073- fn add_r( self , other: Self , rm: Round ) -> Option <StatusAnd <Self >> {
1074- match rm {
1075- Round :: NearestTiesToEven => Some ( Status :: OK . and( self + other) ) ,
1076- _ => None ,
1077- }
1078- }
1079- fn sub_r( self , other: Self , rm: Round ) -> Option <StatusAnd <Self >> {
1080- match rm {
1081- Round :: NearestTiesToEven => Some ( Status :: OK . and( self - other) ) ,
1082- _ => None ,
1083- }
1084- }
1085- fn mul_r( self , other: Self , rm: Round ) -> Option <StatusAnd <Self >> {
1086- match rm {
1087- Round :: NearestTiesToEven => Some ( Status :: OK . and( self * other) ) ,
1088- _ => None ,
1089- }
1090- }
1091- fn div_r( self , other: Self , rm: Round ) -> Option <StatusAnd <Self >> {
1092- match rm {
1093- Round :: NearestTiesToEven => Some ( Status :: OK . and( self / other) ) ,
1094- _ => None ,
1095- }
1096- }
1097- fn rem( self , other: Self ) -> Self {
1098- self % other
1099- }
1100- fn mul_add_r( self , mul: Self , add: Self , rm: Round ) -> Option <StatusAnd <Self >> {
1101- match rm {
1102- Round :: NearestTiesToEven => Some ( Status :: OK . and( self . mul_add( mul, add) ) ) ,
1103- _ => None ,
1104- }
1105- }
1106-
1107- /* float->int casts are toward zero */
1108- fn to_i128_r( self , rm: Round ) -> Option <StatusAnd <i128 >> {
1109- match rm {
1110- Round :: TowardZero => Some ( Status :: OK . and( self as i128 ) ) ,
1111- _ => None ,
1112- }
1113- }
1114- fn to_u128_r( self , rm: Round ) -> Option <StatusAnd <u128 >> {
1115- match rm {
1116- Round :: TowardZero => Some ( Status :: OK . and( self as u128 ) ) ,
1117- _ => None ,
1118- }
1119- }
1120-
1121- fn from_i128_r( x: i128 , rm: Round ) -> Option <StatusAnd <Self >> {
1122- match rm {
1123- Round :: NearestTiesToEven => Some ( Status :: OK . and( x as Self ) ) ,
1124- _ => None ,
1125- }
1126- }
1127- fn from_u128_r( x: u128 , rm: Round ) -> Option <StatusAnd <Self >> {
1128- match rm {
1129- Round :: NearestTiesToEven => Some ( Status :: OK . and( x as Self ) ) ,
1130- _ => None ,
1131- }
1132- }
1133- fn to_double_r( self , rm: Round ) -> Option <StatusAnd <f64 >> {
1134- match rm {
1135- Round :: NearestTiesToEven => Some ( Status :: OK . and( self as f64 ) ) ,
1136- _ => None ,
1137- }
1138- }
1139- fn from_double_r( x: f64 , rm: Round ) -> Option <StatusAnd <Self >> {
1140- match rm {
1141- Round :: NearestTiesToEven => Some ( Status :: OK . and( x as Self ) ) ,
1142- _ => None ,
1143- }
1144- }
1145- fn to_single_r( self , rm: Round ) -> Option <StatusAnd <f32 >> {
1146- match rm {
1147- Round :: NearestTiesToEven => Some ( Status :: OK . and( self as f32 ) ) ,
1148- _ => None ,
1149- }
1150- }
1151- fn from_single_r( x: f32 , rm: Round ) -> Option <StatusAnd <Self >> {
1152- match rm {
1153- Round :: NearestTiesToEven => Some ( Status :: OK . and( x as Self ) ) ,
1154- _ => None ,
1155- }
1156- }
1157- }
1158- } ;
1159- }
1160-
1161- #[ cfg( target_has_reliable_f16) ]
1162- impl_host_float ! ( f16, u16 ) ;
1163- impl_host_float ! ( f32 , u32 ) ;
1164- impl_host_float ! ( f64 , u64 ) ;
1165- #[ cfg( target_has_reliable_f128) ]
1166- impl_host_float ! ( f128, u128 ) ;
1167-
11681040struct FloatPrintHelper < F : FloatRepr > ( F ) ;
11691041impl < F : FloatRepr > fmt:: Debug for FloatPrintHelper < F > {
11701042 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
0 commit comments