rust/tests/ui/float/classify-runtime-const.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
4.7 KiB
Rust
Raw Permalink Normal View History

2024-08-31 15:23:13 -05:00
//@ run-pass
2024-09-11 03:53:39 -05:00
//@ revisions: opt noopt ctfe
//@[opt] compile-flags: -O
//@[noopt] compile-flags: -Zmir-opt-level=0
2024-08-31 15:23:13 -05:00
// ignore-tidy-linelength
2020-05-21 20:51:47 -05:00
2024-08-31 15:23:13 -05:00
// This tests the float classification functions, for regular runtime code and for const evaluation.
#![feature(f16)]
#![feature(f128)]
2020-05-21 20:51:47 -05:00
2024-08-31 15:23:13 -05:00
use std::num::FpCategory::*;
2020-05-21 20:51:47 -05:00
2024-09-11 03:53:39 -05:00
#[cfg(not(ctfe))]
use std::hint::black_box;
#[cfg(ctfe)]
#[allow(unused)]
const fn black_box<T>(x: T) -> T { x }
#[cfg(not(ctfe))]
macro_rules! assert_test {
($a:expr, NonDet) => {
{
// Compute `a`, but do not compare with anything as the result is non-deterministic.
let _val = $a;
}
};
($a:expr, $b:ident) => {
{
// Let-bind to avoid promotion.
// No black_box here! That can mask x87 failures.
let a = $a;
let b = $b;
assert_eq!(a, b, "{} produces wrong result", stringify!($a));
}
};
}
#[cfg(ctfe)]
macro_rules! assert_test {
2024-08-31 15:23:13 -05:00
($a:expr, NonDet) => {
2020-05-21 20:51:47 -05:00
{
2024-08-31 15:23:13 -05:00
// Compute `a`, but do not compare with anything as the result is non-deterministic.
const _: () = { let _val = $a; };
}
};
($a:expr, $b:ident) => {
{
const _: () = assert!(matches!($a, $b));
2020-05-21 20:51:47 -05:00
}
};
}
macro_rules! suite {
2024-09-11 03:53:39 -05:00
( $tyname:ident => $( $tt:tt )* ) => {
2020-05-21 20:51:47 -05:00
fn f32() {
2024-09-11 03:53:39 -05:00
#[allow(unused)]
2024-08-31 15:23:13 -05:00
type $tyname = f32;
2024-09-11 03:53:39 -05:00
suite_inner!(f32 => $($tt)*);
2020-05-21 20:51:47 -05:00
}
fn f64() {
2024-09-11 03:53:39 -05:00
#[allow(unused)]
2024-08-31 15:23:13 -05:00
type $tyname = f64;
2024-09-11 03:53:39 -05:00
suite_inner!(f64 => $($tt)*);
2020-05-21 20:51:47 -05:00
}
}
}
macro_rules! suite_inner {
(
2024-09-11 03:53:39 -05:00
$ty:ident => [$( $fn:ident ),*]:
$(@cfg: $attr:meta)?
$val:expr => [$($out:ident),*],
2020-05-21 20:51:47 -05:00
$( $tail:tt )*
) => {
2024-09-11 03:53:39 -05:00
$(#[cfg($attr)])?
{
// No black_box here! That can mask x87 failures.
$( assert_test!($ty::$fn($val), $out); )*
}
suite_inner!($ty => [$($fn),*]: $($tail)*)
2020-05-21 20:51:47 -05:00
};
2024-09-11 03:53:39 -05:00
( $ty:ident => [$( $fn:ident ),*]:) => {};
2020-05-21 20:51:47 -05:00
}
2024-08-31 15:23:13 -05:00
// The result of the `is_sign` methods are not checked for correctness, since we do not
2020-05-21 20:51:47 -05:00
// guarantee anything about the signedness of NaNs. See
2024-08-31 15:23:13 -05:00
// https://rust-lang.github.io/rfcs/3514-float-semantics.html.
2020-05-21 20:51:47 -05:00
2024-09-11 03:53:39 -05:00
suite! { T => // type alias for the type we are testing
[ classify, is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]:
black_box(0.0) / black_box(0.0) =>
[ Nan, true, false, false, false, NonDet, NonDet],
black_box(0.0) / black_box(-0.0) =>
[ Nan, true, false, false, false, NonDet, NonDet],
black_box(0.0) * black_box(T::INFINITY) =>
[ Nan, true, false, false, false, NonDet, NonDet],
black_box(0.0) * black_box(T::NEG_INFINITY) =>
[ Nan, true, false, false, false, NonDet, NonDet],
1.0 => [ Normal, false, false, true, true, true, false],
-1.0 => [ Normal, false, false, true, true, false, true],
0.0 => [ Zero, false, false, true, false, true, false],
-0.0 => [ Zero, false, false, true, false, false, true],
1.0 / black_box(0.0) =>
[ Infinite, false, true, false, false, true, false],
-1.0 / black_box(0.0) =>
[ Infinite, false, true, false, false, false, true],
2.0 * black_box(T::MAX) =>
[ Infinite, false, true, false, false, true, false],
-2.0 * black_box(T::MAX) =>
[ Infinite, false, true, false, false, false, true],
1.0 / black_box(T::MAX) =>
[Subnormal, false, false, true, false, true, false],
-1.0 / black_box(T::MAX) =>
[Subnormal, false, false, true, false, false, true],
// This specific expression causes trouble on x87 due to
// <https://github.com/rust-lang/rust/issues/114479>.
@cfg: not(all(target_arch = "x86", not(target_feature = "sse2")))
{ let x = black_box(T::MAX); x * x } =>
[ Infinite, false, true, false, false, true, false],
2020-05-21 20:51:47 -05:00
}
fn main() {
f32();
f64();
2024-08-31 15:23:13 -05:00
// FIXME(f16_f128): also test f16 and f128
2020-05-21 20:51:47 -05:00
}