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