@@ -84,7 +84,7 @@ pub(crate) fn clif_int_or_float_cast(
8484 fx. bcx . ins ( ) . fcvt_from_uint ( to_ty, from)
8585 }
8686 } else if from_ty. is_float ( ) && to_ty. is_int ( ) {
87- if to_ty == types:: I128 {
87+ let val = if to_ty == types:: I128 {
8888 // _____sssf___
8989 // __fix sfti: f32 -> i128
9090 // __fix dfti: f64 -> i128
@@ -109,13 +109,9 @@ pub(crate) fn clif_int_or_float_cast(
109109
110110 let to_rust_ty = if to_signed { fx. tcx . types . i128 } else { fx. tcx . types . u128 } ;
111111
112- return fx
113- . easy_call ( & name, & [ CValue :: by_val ( from, fx. layout_of ( from_rust_ty) ) ] , to_rust_ty)
114- . load_scalar ( fx) ;
115- }
116-
117- // float -> int-like
118- if to_ty == types:: I8 || to_ty == types:: I16 {
112+ fx. easy_call ( & name, & [ CValue :: by_val ( from, fx. layout_of ( from_rust_ty) ) ] , to_rust_ty)
113+ . load_scalar ( fx)
114+ } else if to_ty == types:: I8 || to_ty == types:: I16 {
119115 // FIXME implement fcvt_to_*int_sat.i8/i16
120116 let val = if to_signed {
121117 fx. bcx . ins ( ) . fcvt_to_sint_sat ( types:: I32 , from)
@@ -146,6 +142,23 @@ pub(crate) fn clif_int_or_float_cast(
146142 fx. bcx . ins ( ) . fcvt_to_sint_sat ( to_ty, from)
147143 } else {
148144 fx. bcx . ins ( ) . fcvt_to_uint_sat ( to_ty, from)
145+ } ;
146+
147+ if let Some ( false ) = fx. tcx . sess . opts . debugging_opts . saturating_float_casts {
148+ return val;
149+ }
150+
151+ let is_not_nan = fx. bcx . ins ( ) . fcmp ( FloatCC :: Equal , from, from) ;
152+ if to_ty == types:: I128 {
153+ // FIXME(bytecodealliance/wasmtime#3963): select.i128 on fcmp eq miscompiles
154+ let ( lsb, msb) = fx. bcx . ins ( ) . isplit ( val) ;
155+ let zero = fx. bcx . ins ( ) . iconst ( types:: I64 , 0 ) ;
156+ let lsb = fx. bcx . ins ( ) . select ( is_not_nan, lsb, zero) ;
157+ let msb = fx. bcx . ins ( ) . select ( is_not_nan, msb, zero) ;
158+ fx. bcx . ins ( ) . iconcat ( lsb, msb)
159+ } else {
160+ let zero = fx. bcx . ins ( ) . iconst ( to_ty, 0 ) ;
161+ fx. bcx . ins ( ) . select ( is_not_nan, val, zero)
149162 }
150163 } else if from_ty. is_float ( ) && to_ty. is_float ( ) {
151164 // float -> float
0 commit comments