diff --git a/rust-version b/rust-version index 86928a6e6e..6e84524c8a 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -9709af79014a150df7807671e6250ed940c488eb +6dd75f0d6802f56564f5f9c947a85ded286d3986 diff --git a/src/machine.rs b/src/machine.rs index ff4b0ac382..4735db48e8 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -11,6 +11,7 @@ use std::{fmt, process}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use rustc_abi::{Align, ExternAbi, Size}; +use rustc_apfloat::{Float, FloatConvert}; use rustc_attr_parsing::InlineAttr; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; #[allow(unused)] @@ -1132,20 +1133,24 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> { } #[inline(always)] - fn generate_nan< - F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert, - F2: rustc_apfloat::Float, - >( + fn generate_nan, F2: Float>( ecx: &InterpCx<'tcx, Self>, inputs: &[F1], ) -> F2 { ecx.generate_nan(inputs) } + #[inline(always)] + fn equal_float_min_max(ecx: &MiriInterpCx<'tcx>, a: F, b: F) -> F { + ecx.equal_float_min_max(a, b) + } + + #[inline(always)] fn ub_checks(ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool> { interp_ok(ecx.tcx.sess.ub_checks()) } + #[inline(always)] fn thread_local_static_pointer( ecx: &mut MiriInterpCx<'tcx>, def_id: DefId, diff --git a/src/operator.rs b/src/operator.rs index 124b2a765a..c588b6fc7f 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -115,4 +115,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { nan } } + + fn equal_float_min_max(&self, a: F, b: F) -> F { + let this = self.eval_context_ref(); + // Return one side non-deterministically. + let mut rand = this.machine.rng.borrow_mut(); + if rand.random() { a } else { b } + } } diff --git a/tests/pass/float.rs b/tests/pass/float.rs index 4de315e358..2f4f64b1aa 100644 --- a/tests/pass/float.rs +++ b/tests/pass/float.rs @@ -31,6 +31,7 @@ fn main() { test_fast(); test_algebraic(); test_fmuladd(); + test_min_max_nondet(); } trait Float: Copy + PartialEq + Debug { @@ -1211,3 +1212,30 @@ fn test_fmuladd() { test_operations_f32(0.1, 0.2, 0.3); test_operations_f64(1.1, 1.2, 1.3); } + +/// `min` and `max` on equal arguments are non-deterministic. +fn test_min_max_nondet() { + /// Ensure that if we call the closure often enough, we see both `true` and `false.` + #[track_caller] + fn ensure_both(f: impl Fn() -> bool) { + let rounds = 16; + let first = f(); + for _ in 1..rounds { + if f() != first { + // We saw two different values! + return; + } + } + // We saw the same thing N times. + panic!("expected non-determinism, got {rounds} times the same result: {first:?}"); + } + + ensure_both(|| f16::min(0.0, -0.0).is_sign_positive()); + ensure_both(|| f16::max(0.0, -0.0).is_sign_positive()); + ensure_both(|| f32::min(0.0, -0.0).is_sign_positive()); + ensure_both(|| f32::max(0.0, -0.0).is_sign_positive()); + ensure_both(|| f64::min(0.0, -0.0).is_sign_positive()); + ensure_both(|| f64::max(0.0, -0.0).is_sign_positive()); + ensure_both(|| f128::min(0.0, -0.0).is_sign_positive()); + ensure_both(|| f128::max(0.0, -0.0).is_sign_positive()); +}