Skip to content

Commit 1e977f7

Browse files
committed
reduce test diff
1 parent e095b40 commit 1e977f7

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

src/intrinsics/mod.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
254254
let res = apply_random_float_error_ulp(
255255
this,
256256
res,
257-
4
257+
4, // log2(16)
258258
);
259259
let res = this.adjust_nan(res, &[f]);
260260
this.write_scalar(res, dest)?;
@@ -289,7 +289,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
289289
let res = apply_random_float_error_ulp(
290290
this,
291291
res,
292-
4
292+
4, // log2(16)
293293
);
294294
let res = this.adjust_nan(res, &[f]);
295295
this.write_scalar(res, dest)?;
@@ -411,7 +411,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
411411
// `binary_op` already called `generate_nan` if needed.
412412
// Apply a relative error of 16ULP to simulate non-deterministic precision loss
413413
// due to optimizations.
414-
let res = apply_random_float_error_to_imm(this, res)?;
414+
let res = apply_random_float_error_to_imm(this, res, 4 /* log2(16) */)?;
415415
this.write_immediate(*res, dest)?;
416416
}
417417

@@ -464,7 +464,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
464464
}
465465
// Apply a relative error of 16ULP to simulate non-deterministic precision loss
466466
// due to optimizations.
467-
let res = apply_random_float_error_to_imm(this, res)?;
467+
let res = apply_random_float_error_to_imm(this, res, 4 /* log2(16) */)?;
468468
this.write_immediate(*res, dest)?;
469469
}
470470

@@ -503,13 +503,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
503503
fn apply_random_float_error_to_imm<'tcx>(
504504
ecx: &mut MiriInterpCx<'tcx>,
505505
val: ImmTy<'tcx>,
506+
ulp_exponent: u32,
506507
) -> InterpResult<'tcx, ImmTy<'tcx>> {
507508
let scalar = val.to_scalar_int()?;
508509
let res: ScalarInt = match val.layout.ty.kind() {
509-
ty::Float(FloatTy::F16) => apply_random_float_error_ulp(ecx, scalar.to_f16(), 4).into(),
510-
ty::Float(FloatTy::F32) => apply_random_float_error_ulp(ecx, scalar.to_f32(), 4).into(),
511-
ty::Float(FloatTy::F64) => apply_random_float_error_ulp(ecx, scalar.to_f64(), 4).into(),
512-
ty::Float(FloatTy::F128) => apply_random_float_error_ulp(ecx, scalar.to_f128(), 4).into(),
510+
ty::Float(FloatTy::F16) =>
511+
apply_random_float_error_ulp(ecx, scalar.to_f16(), ulp_exponent).into(),
512+
ty::Float(FloatTy::F32) =>
513+
apply_random_float_error_ulp(ecx, scalar.to_f32(), ulp_exponent).into(),
514+
ty::Float(FloatTy::F64) =>
515+
apply_random_float_error_ulp(ecx, scalar.to_f64(), ulp_exponent).into(),
516+
ty::Float(FloatTy::F128) =>
517+
apply_random_float_error_ulp(ecx, scalar.to_f128(), ulp_exponent).into(),
513518
_ => bug!("intrinsic called with non-float input type"),
514519
};
515520

src/shims/foreign_items.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
770770
let res = math::apply_random_float_error_ulp(
771771
this,
772772
res.to_soft(),
773-
4
773+
4, // log2(16)
774774
);
775775
let res = this.adjust_nan(res, &[f]);
776776
this.write_scalar(res, dest)?;
@@ -799,7 +799,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
799799
let res = math::apply_random_float_error_ulp(
800800
this,
801801
res,
802-
4
802+
4, // log2(16)
803803
);
804804
let res = this.adjust_nan(res, &[f1, f2]);
805805
this.write_scalar(res, dest)?;
@@ -844,7 +844,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
844844
let res = math::apply_random_float_error_ulp(
845845
this,
846846
res.to_soft(),
847-
4
847+
4, // log2(16)
848848
);
849849
let res = this.adjust_nan(res, &[f]);
850850
this.write_scalar(res, dest)?;
@@ -873,7 +873,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
873873
let res = math::apply_random_float_error_ulp(
874874
this,
875875
res,
876-
4
876+
4, // log2(16)
877877
);
878878
let res = this.adjust_nan(res, &[f1, f2]);
879879
this.write_scalar(res, dest)?;
@@ -902,7 +902,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
902902
this.write_int(sign, &signp)?;
903903
// Apply a relative error of 16ULP to introduce some non-determinism
904904
// simulating imprecise implementations and optimizations.
905-
let res = math::apply_random_float_error_ulp(this, res.to_soft(), 4);
905+
let res =
906+
math::apply_random_float_error_ulp(this, res.to_soft(), 4 /* log2(16) */);
906907
let res = this.adjust_nan(res, &[x]);
907908
this.write_scalar(res, dest)?;
908909
}
@@ -916,7 +917,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
916917
this.write_int(sign, &signp)?;
917918
// Apply a relative error of 16ULP to introduce some non-determinism
918919
// simulating imprecise implementations and optimizations.
919-
let res = math::apply_random_float_error_ulp(this, res.to_soft(), 4);
920+
let res =
921+
math::apply_random_float_error_ulp(this, res.to_soft(), 4 /* log2(16) */);
920922
let res = this.adjust_nan(res, &[x]);
921923
this.write_scalar(res, dest)?;
922924
}

tests/pass/float.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ macro_rules! assert_approx_eq {
2727
($a:expr, $b:expr, $ulp:expr) => {{
2828
let (actual, expected) = ($a, $b);
2929
let allowed_ulp_diff = $ulp;
30+
let _force_same_type = actual == expected;
3031
// Approximate the ULP by taking half the distance between the number one place "up"
3132
// and the number one place "down".
3233
let ulp = (expected.next_up() - expected.next_down()) / 2.0;
@@ -49,8 +50,8 @@ fn main() {
4950
ops();
5051
nan_casts();
5152
rounding();
52-
libm();
5353
mul_add();
54+
libm();
5455
test_fast();
5556
test_algebraic();
5657
test_fmuladd();
@@ -1028,8 +1029,8 @@ pub fn libm() {
10281029

10291030
#[allow(deprecated)]
10301031
{
1031-
assert_approx_eq!(5.0f32.abs_sub(3.0), 2.0f32);
1032-
assert_approx_eq!(3.0f64.abs_sub(5.0), 0.0f64);
1032+
assert_approx_eq!(5.0f32.abs_sub(3.0), 2.0);
1033+
assert_approx_eq!(3.0f64.abs_sub(5.0), 0.0);
10331034
}
10341035

10351036
assert_approx_eq!(27.0f32.cbrt(), 3.0f32);
@@ -1046,8 +1047,8 @@ pub fn libm() {
10461047

10471048
assert_approx_eq!(0f32.sin(), 0f32);
10481049
assert_approx_eq!((f64::consts::PI / 2f64).sin(), 1f64);
1049-
assert_approx_eq!(f32::consts::FRAC_PI_6.sin(), 0.5f32);
1050-
assert_approx_eq!(f64::consts::FRAC_PI_6.sin(), 0.5f64);
1050+
assert_approx_eq!(f32::consts::FRAC_PI_6.sin(), 0.5);
1051+
assert_approx_eq!(f64::consts::FRAC_PI_6.sin(), 0.5);
10511052
assert_approx_eq!(f32::consts::FRAC_PI_4.sin().asin(), f32::consts::FRAC_PI_4);
10521053
assert_approx_eq!(f64::consts::FRAC_PI_4.sin().asin(), f64::consts::FRAC_PI_4);
10531054

@@ -1058,8 +1059,8 @@ pub fn libm() {
10581059

10591060
assert_approx_eq!(0f32.cos(), 1f32);
10601061
assert_approx_eq!((f64::consts::PI * 2f64).cos(), 1f64);
1061-
assert_approx_eq!(f32::consts::FRAC_PI_3.cos(), 0.5f32);
1062-
assert_approx_eq!(f64::consts::FRAC_PI_3.cos(), 0.5f64);
1062+
assert_approx_eq!(f32::consts::FRAC_PI_3.cos(), 0.5);
1063+
assert_approx_eq!(f64::consts::FRAC_PI_3.cos(), 0.5);
10631064
assert_approx_eq!(f32::consts::FRAC_PI_4.cos().acos(), f32::consts::FRAC_PI_4);
10641065
assert_approx_eq!(f64::consts::FRAC_PI_4.cos().acos(), f64::consts::FRAC_PI_4);
10651066

@@ -1086,8 +1087,8 @@ pub fn libm() {
10861087
assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
10871088
assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
10881089

1089-
assert_approx_eq!(5.0f32.gamma(), 24.0f32);
1090-
assert_approx_eq!(5.0f64.gamma(), 24.0f64);
1090+
assert_approx_eq!(5.0f32.gamma(), 24.0);
1091+
assert_approx_eq!(5.0f64.gamma(), 24.0);
10911092
assert_approx_eq!((-0.5f32).gamma(), (-2.0) * f32::consts::PI.sqrt());
10921093
assert_approx_eq!((-0.5f64).gamma(), (-2.0) * f64::consts::PI.sqrt());
10931094

0 commit comments

Comments
 (0)