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