Skip to content

Commit 153ab3b

Browse files
committed
Refactor float rounding intrinsics a bit
1 parent cbd3969 commit 153ab3b

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
@@ -1032,6 +1032,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10321032
interp_ok(())
10331033
}
10341034

1035+
fn float_round<F>(
1036+
&mut self,
1037+
x: Scalar<M::Provenance>,
1038+
mode: rustc_apfloat::Round,
1039+
) -> InterpResult<'tcx, Scalar<M::Provenance>>
1040+
where
1041+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
1042+
{
1043+
let x: F = x.to_float()?;
1044+
let res = x.round_to_integral(mode).value;
1045+
let res = self.adjust_nan(res, &[x]);
1046+
interp_ok(res.into())
1047+
}
1048+
10351049
fn float_round_intrinsic<F>(
10361050
&mut self,
10371051
args: &[OpTy<'tcx, M::Provenance>],
@@ -1041,9 +1055,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10411055
where
10421056
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
10431057
{
1044-
let x: F = self.read_scalar(&args[0])?.to_float()?;
1045-
let res = x.round_to_integral(mode).value;
1046-
let res = self.adjust_nan(res, &[x]);
1058+
let res = self.float_round::<F>(self.read_scalar(&args[0])?, mode)?;
10471059
self.write_scalar(res, dest)?;
10481060
interp_ok(())
10491061
}

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)