Implement intrinsics for round_ties_even

This commit is contained in:
Ben Kimock 2023-03-10 22:28:31 -05:00
parent 0f1f7b0840
commit 0ea51e101e
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 @@ 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)?;

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);
}