Skip to content

Commit b73e613

Browse files
committed
De-duplicate and move adjust_nan to InterpCx
1 parent 8f8bee4 commit b73e613

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

compiler/rustc_const_eval/src/interpret/cast.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
334334
{
335335
use rustc_type_ir::TyKind::*;
336336

337-
fn adjust_nan<
338-
'tcx,
339-
M: Machine<'tcx>,
340-
F1: rustc_apfloat::Float + FloatConvert<F2>,
341-
F2: rustc_apfloat::Float,
342-
>(
343-
ecx: &InterpCx<'tcx, M>,
344-
f1: F1,
345-
f2: F2,
346-
) -> F2 {
347-
if f2.is_nan() { M::generate_nan(ecx, &[f1]) } else { f2 }
348-
}
349-
350337
match *dest_ty.kind() {
351338
// float -> uint
352339
Uint(t) => {
@@ -367,11 +354,17 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
367354
}
368355
// float -> float
369356
Float(fty) => match fty {
370-
FloatTy::F16 => Scalar::from_f16(adjust_nan(self, f, f.convert(&mut false).value)),
371-
FloatTy::F32 => Scalar::from_f32(adjust_nan(self, f, f.convert(&mut false).value)),
372-
FloatTy::F64 => Scalar::from_f64(adjust_nan(self, f, f.convert(&mut false).value)),
357+
FloatTy::F16 => {
358+
Scalar::from_f16(self.adjust_nan(f.convert(&mut false).value, &[f]))
359+
}
360+
FloatTy::F32 => {
361+
Scalar::from_f32(self.adjust_nan(f.convert(&mut false).value, &[f]))
362+
}
363+
FloatTy::F64 => {
364+
Scalar::from_f64(self.adjust_nan(f.convert(&mut false).value, &[f]))
365+
}
373366
FloatTy::F128 => {
374-
Scalar::from_f128(adjust_nan(self, f, f.convert(&mut false).value))
367+
Scalar::from_f128(self.adjust_nan(f.convert(&mut false).value, &[f]))
375368
}
376369
},
377370
// That's it.

compiler/rustc_const_eval/src/interpret/eval_context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,14 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
599599
pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
600600
Frame::generate_stacktrace_from_stack(self.stack())
601601
}
602+
603+
pub fn adjust_nan<F1, F2>(&self, f: F2, inputs: &[F1]) -> F2
604+
where
605+
F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
606+
F2: rustc_apfloat::Float,
607+
{
608+
if f.is_nan() { M::generate_nan(self, inputs) } else { f }
609+
}
602610
}
603611

604612
#[doc(hidden)]

compiler/rustc_const_eval/src/interpret/operator.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
6464
use rustc_middle::mir::BinOp::*;
6565

6666
// Performs appropriate non-deterministic adjustments of NaN results.
67-
let adjust_nan =
68-
|f: F| -> F { if f.is_nan() { M::generate_nan(self, &[l, r]) } else { f } };
67+
let adjust_nan = |f: F| -> F { self.adjust_nan(f, &[l, r]) };
6968

7069
match bin_op {
7170
Eq => ImmTy::from_bool(l == r, *self.tcx),

src/tools/miri/src/operator.rs

-4
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,4 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
115115
nan
116116
}
117117
}
118-
119-
fn adjust_nan<F1: Float + FloatConvert<F2>, F2: Float>(&self, f: F2, inputs: &[F1]) -> F2 {
120-
if f.is_nan() { self.generate_nan(inputs) } else { f }
121-
}
122118
}

0 commit comments

Comments
 (0)