Refactor float rounding intrinsics a bit

This commit is contained in:
sayantn 2025-10-10 22:00:13 +05:30
parent d335ea91b5
commit ebf2e10238
No known key found for this signature in database
GPG key ID: B60412E056614AA4
2 changed files with 18 additions and 15 deletions

View file

@ -1036,6 +1036,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
interp_ok(())
}
fn float_round<F>(
&mut self,
x: Scalar<M::Provenance>,
mode: rustc_apfloat::Round,
) -> InterpResult<'tcx, Scalar<M::Provenance>>
where
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
{
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<F>(
&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<F> + Into<Scalar<M::Provenance>>,
{
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::<F>(self.read_scalar(&args[0])?, mode)?;
self.write_scalar(res, dest)?;
interp_ok(())
}

View file

@ -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::<Single>(op, rounding)?,
FloatTy::F64 => self.float_round::<Double>(op, rounding)?,
FloatTy::F128 => unimplemented!("f16_f128"),
}
}