|
| 1 | +//@ compile-flags: -Zmir-opt-level=0 -Znext-solver |
| 2 | +//@ run-pass |
| 3 | +// ignore-tidy-linelength |
| 4 | + |
| 5 | +// This tests the float classification functions, for regular runtime code and for const evaluation. |
| 6 | + |
| 7 | +#![feature(const_float_bits_conv)] |
| 8 | +#![feature(const_float_classify)] |
| 9 | + |
| 10 | +use std::hint::black_box; |
| 11 | +use std::num::FpCategory::*; |
| 12 | + |
| 13 | +macro_rules! const_assert { |
| 14 | + ($a:expr, NonDet) => { |
| 15 | + { |
| 16 | + // Compute `a`, but do not compare with anything as the result is non-deterministic. |
| 17 | + const _: () = { let _val = $a; }; |
| 18 | + let _val = black_box($a); |
| 19 | + } |
| 20 | + }; |
| 21 | + ($a:expr, $b:ident) => { |
| 22 | + { |
| 23 | + const _: () = assert!(matches!($a, $b)); |
| 24 | + assert!(black_box($a) == black_box($b)); |
| 25 | + } |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +macro_rules! suite { |
| 30 | + ( $tyname:ident: $( $tt:tt )* ) => { |
| 31 | + fn f32() { |
| 32 | + type $tyname = f32; |
| 33 | + suite_inner!(f32 $($tt)*); |
| 34 | + } |
| 35 | + |
| 36 | + fn f64() { |
| 37 | + type $tyname = f64; |
| 38 | + suite_inner!(f64 $($tt)*); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +macro_rules! suite_inner { |
| 44 | + ( |
| 45 | + $ty:ident [$( $fn:ident ),*] |
| 46 | + $val:expr => [$($out:ident),*] |
| 47 | + |
| 48 | + $( $tail:tt )* |
| 49 | + ) => { |
| 50 | + $( const_assert!($ty::$fn($val), $out); )* |
| 51 | + suite_inner!($ty [$($fn),*] $($tail)*) |
| 52 | + }; |
| 53 | + |
| 54 | + ( $ty:ident [$( $fn:ident ),*]) => {}; |
| 55 | +} |
| 56 | + |
| 57 | +// The result of the `is_sign` methods are not checked for correctness, since we do not |
| 58 | +// guarantee anything about the signedness of NaNs. See |
| 59 | +// https://rust-lang.github.io/rfcs/3514-float-semantics.html. |
| 60 | + |
| 61 | +suite! { T: // type alias for the type we are testing |
| 62 | + [ classify, is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative] |
| 63 | + -0.0 / 0.0 => [ Nan, true, false, false, false, NonDet, NonDet] |
| 64 | + 0.0 / 0.0 => [ Nan, true, false, false, false, NonDet, NonDet] |
| 65 | + 1.0 => [ Normal, false, false, true, true, true, false] |
| 66 | + -1.0 => [ Normal, false, false, true, true, false, true] |
| 67 | + 0.0 => [ Zero, false, false, true, false, true, false] |
| 68 | + -0.0 => [ Zero, false, false, true, false, false, true] |
| 69 | + 1.0 / 0.0 => [ Infinite, false, true, false, false, true, false] |
| 70 | + -1.0 / 0.0 => [ Infinite, false, true, false, false, false, true] |
| 71 | + 1.0 / T::MAX => [Subnormal, false, false, true, false, true, false] |
| 72 | + -1.0 / T::MAX => [Subnormal, false, false, true, false, false, true] |
| 73 | +} |
| 74 | + |
| 75 | +fn main() { |
| 76 | + f32(); |
| 77 | + f64(); |
| 78 | + // FIXME(f16_f128): also test f16 and f128 |
| 79 | +} |
0 commit comments