Skip to content

Commit 70fd1ee

Browse files
authored
Merge pull request #4173 from RalfJung/rustup
Rustup
2 parents 73b483d + 9b06140 commit 70fd1ee

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9709af79014a150df7807671e6250ed940c488eb
1+
6dd75f0d6802f56564f5f9c947a85ded286d3986

src/machine.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{fmt, process};
1111
use rand::rngs::StdRng;
1212
use rand::{Rng, SeedableRng};
1313
use rustc_abi::{Align, ExternAbi, Size};
14+
use rustc_apfloat::{Float, FloatConvert};
1415
use rustc_attr_parsing::InlineAttr;
1516
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1617
#[allow(unused)]
@@ -1132,20 +1133,24 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
11321133
}
11331134

11341135
#[inline(always)]
1135-
fn generate_nan<
1136-
F1: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F2>,
1137-
F2: rustc_apfloat::Float,
1138-
>(
1136+
fn generate_nan<F1: Float + FloatConvert<F2>, F2: Float>(
11391137
ecx: &InterpCx<'tcx, Self>,
11401138
inputs: &[F1],
11411139
) -> F2 {
11421140
ecx.generate_nan(inputs)
11431141
}
11441142

1143+
#[inline(always)]
1144+
fn equal_float_min_max<F: Float>(ecx: &MiriInterpCx<'tcx>, a: F, b: F) -> F {
1145+
ecx.equal_float_min_max(a, b)
1146+
}
1147+
1148+
#[inline(always)]
11451149
fn ub_checks(ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool> {
11461150
interp_ok(ecx.tcx.sess.ub_checks())
11471151
}
11481152

1153+
#[inline(always)]
11491154
fn thread_local_static_pointer(
11501155
ecx: &mut MiriInterpCx<'tcx>,
11511156
def_id: DefId,

src/operator.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
115115
nan
116116
}
117117
}
118+
119+
fn equal_float_min_max<F: Float>(&self, a: F, b: F) -> F {
120+
let this = self.eval_context_ref();
121+
// Return one side non-deterministically.
122+
let mut rand = this.machine.rng.borrow_mut();
123+
if rand.random() { a } else { b }
124+
}
118125
}

tests/pass/float.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn main() {
3131
test_fast();
3232
test_algebraic();
3333
test_fmuladd();
34+
test_min_max_nondet();
3435
}
3536

3637
trait Float: Copy + PartialEq + Debug {
@@ -1211,3 +1212,30 @@ fn test_fmuladd() {
12111212
test_operations_f32(0.1, 0.2, 0.3);
12121213
test_operations_f64(1.1, 1.2, 1.3);
12131214
}
1215+
1216+
/// `min` and `max` on equal arguments are non-deterministic.
1217+
fn test_min_max_nondet() {
1218+
/// Ensure that if we call the closure often enough, we see both `true` and `false.`
1219+
#[track_caller]
1220+
fn ensure_both(f: impl Fn() -> bool) {
1221+
let rounds = 16;
1222+
let first = f();
1223+
for _ in 1..rounds {
1224+
if f() != first {
1225+
// We saw two different values!
1226+
return;
1227+
}
1228+
}
1229+
// We saw the same thing N times.
1230+
panic!("expected non-determinism, got {rounds} times the same result: {first:?}");
1231+
}
1232+
1233+
ensure_both(|| f16::min(0.0, -0.0).is_sign_positive());
1234+
ensure_both(|| f16::max(0.0, -0.0).is_sign_positive());
1235+
ensure_both(|| f32::min(0.0, -0.0).is_sign_positive());
1236+
ensure_both(|| f32::max(0.0, -0.0).is_sign_positive());
1237+
ensure_both(|| f64::min(0.0, -0.0).is_sign_positive());
1238+
ensure_both(|| f64::max(0.0, -0.0).is_sign_positive());
1239+
ensure_both(|| f128::min(0.0, -0.0).is_sign_positive());
1240+
ensure_both(|| f128::max(0.0, -0.0).is_sign_positive());
1241+
}

0 commit comments

Comments
 (0)