Skip to content

Commit 20d95e4

Browse files
committed
Auto merge of #2809 - saethlin:round_ties_even, r=oli-obk
Implement round_ties_even For tests, I just copied over the standard library's tests for this feature. It looks like the library uses an approximate equality check for most/all float tests, and I've replaced that check with our float equality check pattern.
2 parents bf77f52 + 2318f21 commit 20d95e4

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(nonzero_ops)]
1010
#![feature(local_key_cell_methods)]
1111
#![feature(is_terminal)]
12+
#![feature(round_ties_even)]
1213
// Configure clippy and other lints
1314
#![allow(
1415
clippy::collapsible_else_if,

src/shims/intrinsics/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
157157
| "ceilf32"
158158
| "truncf32"
159159
| "roundf32"
160+
| "rintf32"
160161
=> {
161162
let [f] = check_arg_count(args)?;
162163
// FIXME: Using host floats.
@@ -174,6 +175,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
174175
"ceilf32" => f.ceil(),
175176
"truncf32" => f.trunc(),
176177
"roundf32" => f.round(),
178+
"rintf32" => f.round_ties_even(),
177179
_ => bug!(),
178180
};
179181
this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
@@ -192,6 +194,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
192194
| "ceilf64"
193195
| "truncf64"
194196
| "roundf64"
197+
| "rintf64"
195198
=> {
196199
let [f] = check_arg_count(args)?;
197200
// FIXME: Using host floats.
@@ -209,6 +212,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
209212
"ceilf64" => f.ceil(),
210213
"truncf64" => f.trunc(),
211214
"roundf64" => f.round(),
215+
"rintf64" => f.round_ties_even(),
212216
_ => bug!(),
213217
};
214218
this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;

tests/pass/float.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(stmt_expr_attributes)]
2+
#![feature(round_ties_even)]
23
#![allow(arithmetic_overflow)]
34
use std::fmt::Debug;
45
use std::hint::black_box;
@@ -9,6 +10,7 @@ fn main() {
910
more_casts();
1011
ops();
1112
nan_casts();
13+
rounding();
1214
}
1315

1416
// Helper function to avoid promotion so that this tests "run-time" casts, not CTFE.
@@ -553,3 +555,31 @@ fn nan_casts() {
553555
assert!(nan1_32.is_nan());
554556
assert!(nan2_32.is_nan());
555557
}
558+
559+
fn rounding() {
560+
// Test cases taken from the library's tests for this feature
561+
// f32
562+
assert_eq(2.5f32.round_ties_even(), 2.0f32);
563+
assert_eq(1.0f32.round_ties_even(), 1.0f32);
564+
assert_eq(1.3f32.round_ties_even(), 1.0f32);
565+
assert_eq(1.5f32.round_ties_even(), 2.0f32);
566+
assert_eq(1.7f32.round_ties_even(), 2.0f32);
567+
assert_eq(0.0f32.round_ties_even(), 0.0f32);
568+
assert_eq((-0.0f32).round_ties_even(), -0.0f32);
569+
assert_eq((-1.0f32).round_ties_even(), -1.0f32);
570+
assert_eq((-1.3f32).round_ties_even(), -1.0f32);
571+
assert_eq((-1.5f32).round_ties_even(), -2.0f32);
572+
assert_eq((-1.7f32).round_ties_even(), -2.0f32);
573+
// f64
574+
assert_eq(2.5f64.round_ties_even(), 2.0f64);
575+
assert_eq(1.0f64.round_ties_even(), 1.0f64);
576+
assert_eq(1.3f64.round_ties_even(), 1.0f64);
577+
assert_eq(1.5f64.round_ties_even(), 2.0f64);
578+
assert_eq(1.7f64.round_ties_even(), 2.0f64);
579+
assert_eq(0.0f64.round_ties_even(), 0.0f64);
580+
assert_eq((-0.0f64).round_ties_even(), -0.0f64);
581+
assert_eq((-1.0f64).round_ties_even(), -1.0f64);
582+
assert_eq((-1.3f64).round_ties_even(), -1.0f64);
583+
assert_eq((-1.5f64).round_ties_even(), -2.0f64);
584+
assert_eq((-1.7f64).round_ties_even(), -2.0f64);
585+
}

0 commit comments

Comments
 (0)