Skip to content

Commit 6c410ba

Browse files
committed
Refactor float rounding intrinsics a bit
1 parent 487d6d8 commit 6c410ba

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
@@ -1014,6 +1014,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10141014
interp_ok(())
10151015
}
10161016

1017+
fn float_round<F>(
1018+
&mut self,
1019+
x: Scalar<M::Provenance>,
1020+
mode: rustc_apfloat::Round,
1021+
) -> InterpResult<'tcx, Scalar<M::Provenance>>
1022+
where
1023+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
1024+
{
1025+
let x: F = x.to_float()?;
1026+
let res = x.round_to_integral(mode).value;
1027+
let res = self.adjust_nan(res, &[x]);
1028+
interp_ok(res.into())
1029+
}
1030+
10171031
fn float_round_intrinsic<F>(
10181032
&mut self,
10191033
args: &[OpTy<'tcx, M::Provenance>],
@@ -1023,9 +1037,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10231037
where
10241038
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
10251039
{
1026-
let x: F = self.read_scalar(&args[0])?.to_float()?;
1027-
let res = x.round_to_integral(mode).value;
1028-
let res = self.adjust_nan(res, &[x]);
1040+
let res = self.float_round::<F>(self.read_scalar(&args[0])?, mode)?;
10291041
self.write_scalar(res, dest)?;
10301042
interp_ok(())
10311043
}

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)