Skip to content

Commit ebf2e10

Browse files
committed
Refactor float rounding intrinsics a bit
1 parent d335ea9 commit ebf2e10

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10361036
interp_ok(())
10371037
}
10381038

1039+
fn float_round<F>(
1040+
&mut self,
1041+
x: Scalar<M::Provenance>,
1042+
mode: rustc_apfloat::Round,
1043+
) -> InterpResult<'tcx, Scalar<M::Provenance>>
1044+
where
1045+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
1046+
{
1047+
let x: F = x.to_float()?;
1048+
let res = x.round_to_integral(mode).value;
1049+
let res = self.adjust_nan(res, &[x]);
1050+
interp_ok(res.into())
1051+
}
1052+
10391053
fn float_round_intrinsic<F>(
10401054
&mut self,
10411055
args: &[OpTy<'tcx, M::Provenance>],
@@ -1045,9 +1059,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10451059
where
10461060
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
10471061
{
1048-
let x: F = self.read_scalar(&args[0])?.to_float()?;
1049-
let res = x.round_to_integral(mode).value;
1050-
let res = self.adjust_nan(res, &[x]);
1062+
let res = self.float_round::<F>(self.read_scalar(&args[0])?, mode)?;
10511063
self.write_scalar(res, dest)?;
10521064
interp_ok(())
10531065
}

compiler/rustc_const_eval/src/interpret/intrinsics/simd.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,11 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
134134
intrinsic_name
135135
)
136136
};
137+
let op = op.to_scalar();
137138
match float_ty {
138139
FloatTy::F16 => unimplemented!("f16_f128"),
139-
FloatTy::F32 => {
140-
let f = op.to_scalar().to_f32()?;
141-
let res = f.round_to_integral(rounding).value;
142-
let res = self.adjust_nan(res, &[f]);
143-
Scalar::from_f32(res)
144-
}
145-
FloatTy::F64 => {
146-
let f = op.to_scalar().to_f64()?;
147-
let res = f.round_to_integral(rounding).value;
148-
let res = self.adjust_nan(res, &[f]);
149-
Scalar::from_f64(res)
150-
}
140+
FloatTy::F32 => self.float_round::<Single>(op, rounding)?,
141+
FloatTy::F64 => self.float_round::<Double>(op, rounding)?,
151142
FloatTy::F128 => unimplemented!("f16_f128"),
152143
}
153144
}

0 commit comments

Comments
 (0)