From 0ea51e101e7c6bf56b83572e2d818a6da3c907e7 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 10 Mar 2023 22:28:31 -0500 Subject: [PATCH] Implement intrinsics for round_ties_even --- src/tools/miri/src/lib.rs | 1 + src/tools/miri/src/shims/intrinsics/mod.rs | 4 +++ src/tools/miri/tests/pass/float.rs | 30 ++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/tools/miri/src/lib.rs b/src/tools/miri/src/lib.rs index f64f216520f..5412507ecfa 100644 --- a/src/tools/miri/src/lib.rs +++ b/src/tools/miri/src/lib.rs @@ -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, diff --git a/src/tools/miri/src/shims/intrinsics/mod.rs b/src/tools/miri/src/shims/intrinsics/mod.rs index d21a1560699..9ecbb18ef5a 100644 --- a/src/tools/miri/src/shims/intrinsics/mod.rs +++ b/src/tools/miri/src/shims/intrinsics/mod.rs @@ -157,6 +157,7 @@ fn emulate_intrinsic_by_name( | "ceilf32" | "truncf32" | "roundf32" + | "rintf32" => { let [f] = check_arg_count(args)?; // FIXME: Using host floats. @@ -174,6 +175,7 @@ fn emulate_intrinsic_by_name( "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 @@ fn emulate_intrinsic_by_name( | "ceilf64" | "truncf64" | "roundf64" + | "rintf64" => { let [f] = check_arg_count(args)?; // FIXME: Using host floats. @@ -209,6 +212,7 @@ fn emulate_intrinsic_by_name( "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)?; diff --git a/src/tools/miri/tests/pass/float.rs b/src/tools/miri/tests/pass/float.rs index ce62fb0de04..fee5ca44ffb 100644 --- a/src/tools/miri/tests/pass/float.rs +++ b/src/tools/miri/tests/pass/float.rs @@ -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); +}