4 #![deny(overflowing_literals)]
10 ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ({
12 const X: $src_ty = $val;
13 const Y: $dest_ty = X as $dest_ty;
14 assert_eq!(Y, $expected,
15 "const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
17 // black_box disables constant evaluation to test run-time conversions:
18 assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
19 "run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
24 // nextDown(f32::MAX) = 2^128 - 2 * 2^104
25 const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
27 // f32::MAX - 0.5 ULP and smaller should be rounded down
28 test!(0xfffffe00000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
29 test!(0xfffffe7fffffffffffffffffffffffff, u128 -> f32, SECOND_LARGEST_F32);
30 test!(0xfffffe80000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
31 // numbers within < 0.5 ULP of f32::MAX it should be rounded to f32::MAX
32 test!(0xfffffe80000000000000000000000001, u128 -> f32, f32::MAX);
33 test!(0xfffffeffffffffffffffffffffffffff, u128 -> f32, f32::MAX);
34 test!(0xffffff00000000000000000000000000, u128 -> f32, f32::MAX);
35 test!(0xffffff00000000000000000000000001, u128 -> f32, f32::MAX);
36 test!(0xffffff7fffffffffffffffffffffffff, u128 -> f32, f32::MAX);
37 // f32::MAX + 0.5 ULP and greater should be rounded to infinity
38 test!(0xffffff80000000000000000000000000, u128 -> f32, f32::INFINITY);
39 test!(0xffffff80000000f00000000000000000, u128 -> f32, f32::INFINITY);
40 test!(0xffffff87ffffffffffffffff00000001, u128 -> f32, f32::INFINITY);
42 // u128->f64 should not be affected by the u128->f32 checks
43 test!(0xffffff80000000000000000000000000, u128 -> f64,
44 340282356779733661637539395458142568448.0);
45 test!(u128::MAX, u128 -> f64, 340282366920938463463374607431768211455.0);