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.
This commit is contained in:
bors 2023-03-11 22:30:34 +00:00
commit a99494776a
3 changed files with 35 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#![feature(nonzero_ops)]
#![feature(local_key_cell_methods)]
#![feature(is_terminal)]
#![feature(round_ties_even)]
// Configure clippy and other lints
#![allow(
clippy::collapsible_else_if,

View file

@ -157,6 +157,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
| "ceilf32"
| "truncf32"
| "roundf32"
| "rintf32"
=> {
let [f] = check_arg_count(args)?;
// FIXME: Using host floats.
@ -174,6 +175,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
"ceilf32" => f.ceil(),
"truncf32" => f.trunc(),
"roundf32" => f.round(),
"rintf32" => f.round_ties_even(),
_ => bug!(),
};
this.write_scalar(Scalar::from_u32(f.to_bits()), dest)?;
@ -192,6 +194,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
| "ceilf64"
| "truncf64"
| "roundf64"
| "rintf64"
=> {
let [f] = check_arg_count(args)?;
// FIXME: Using host floats.
@ -209,6 +212,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
"ceilf64" => f.ceil(),
"truncf64" => f.trunc(),
"roundf64" => f.round(),
"rintf64" => f.round_ties_even(),
_ => bug!(),
};
this.write_scalar(Scalar::from_u64(f.to_bits()), dest)?;

View file

@ -1,4 +1,5 @@
#![feature(stmt_expr_attributes)]
#![feature(round_ties_even)]
#![allow(arithmetic_overflow)]
use std::fmt::Debug;
use std::hint::black_box;
@ -9,6 +10,7 @@ fn main() {
more_casts();
ops();
nan_casts();
rounding();
}
// Helper function to avoid promotion so that this tests "run-time" casts, not CTFE.
@ -553,3 +555,31 @@ fn nan_casts() {
assert!(nan1_32.is_nan());
assert!(nan2_32.is_nan());
}
fn rounding() {
// Test cases taken from the library's tests for this feature
// f32
assert_eq(2.5f32.round_ties_even(), 2.0f32);
assert_eq(1.0f32.round_ties_even(), 1.0f32);
assert_eq(1.3f32.round_ties_even(), 1.0f32);
assert_eq(1.5f32.round_ties_even(), 2.0f32);
assert_eq(1.7f32.round_ties_even(), 2.0f32);
assert_eq(0.0f32.round_ties_even(), 0.0f32);
assert_eq((-0.0f32).round_ties_even(), -0.0f32);
assert_eq((-1.0f32).round_ties_even(), -1.0f32);
assert_eq((-1.3f32).round_ties_even(), -1.0f32);
assert_eq((-1.5f32).round_ties_even(), -2.0f32);
assert_eq((-1.7f32).round_ties_even(), -2.0f32);
// f64
assert_eq(2.5f64.round_ties_even(), 2.0f64);
assert_eq(1.0f64.round_ties_even(), 1.0f64);
assert_eq(1.3f64.round_ties_even(), 1.0f64);
assert_eq(1.5f64.round_ties_even(), 2.0f64);
assert_eq(1.7f64.round_ties_even(), 2.0f64);
assert_eq(0.0f64.round_ties_even(), 0.0f64);
assert_eq((-0.0f64).round_ties_even(), -0.0f64);
assert_eq((-1.0f64).round_ties_even(), -1.0f64);
assert_eq((-1.3f64).round_ties_even(), -1.0f64);
assert_eq((-1.5f64).round_ties_even(), -2.0f64);
assert_eq((-1.7f64).round_ties_even(), -2.0f64);
}