diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index 90f14a69fac3..58d7f8ef8c63 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -1036,6 +1036,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { interp_ok(()) } + fn float_round( + &mut self, + x: Scalar, + mode: rustc_apfloat::Round, + ) -> InterpResult<'tcx, Scalar> + where + F: rustc_apfloat::Float + rustc_apfloat::FloatConvert + Into>, + { + let x: F = x.to_float()?; + let res = x.round_to_integral(mode).value; + let res = self.adjust_nan(res, &[x]); + interp_ok(res.into()) + } + fn float_round_intrinsic( &mut self, args: &[OpTy<'tcx, M::Provenance>], @@ -1045,9 +1059,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { where F: rustc_apfloat::Float + rustc_apfloat::FloatConvert + Into>, { - let x: F = self.read_scalar(&args[0])?.to_float()?; - let res = x.round_to_integral(mode).value; - let res = self.adjust_nan(res, &[x]); + let res = self.float_round::(self.read_scalar(&args[0])?, mode)?; self.write_scalar(res, dest)?; interp_ok(()) } diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics/simd.rs b/compiler/rustc_const_eval/src/interpret/intrinsics/simd.rs index 13b6623accd2..f580dc6bebc3 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics/simd.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics/simd.rs @@ -134,20 +134,11 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { intrinsic_name ) }; + let op = op.to_scalar(); match float_ty { FloatTy::F16 => unimplemented!("f16_f128"), - FloatTy::F32 => { - let f = op.to_scalar().to_f32()?; - let res = f.round_to_integral(rounding).value; - let res = self.adjust_nan(res, &[f]); - Scalar::from_f32(res) - } - FloatTy::F64 => { - let f = op.to_scalar().to_f64()?; - let res = f.round_to_integral(rounding).value; - let res = self.adjust_nan(res, &[f]); - Scalar::from_f64(res) - } + FloatTy::F32 => self.float_round::(op, rounding)?, + FloatTy::F64 => self.float_round::(op, rounding)?, FloatTy::F128 => unimplemented!("f16_f128"), } }