1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // compile-flags: -Z saturating-float-casts
13 #![feature(test, i128, i128_type, stmt_expr_attributes)]
14 #![deny(overflowing_literals)]
18 use std::{u8, i8, u16, i16, u32, i32, u64, i64, u128, i128};
22 ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => (
23 // black_box disables constant evaluation to test run-time conversions:
24 assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
25 "run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
28 ($fval:expr, f* -> $ity:ident, $ival:expr) => (
29 test!($fval, f32 -> $ity, $ival);
30 test!($fval, f64 -> $ity, $ival);
34 // This macro tests const eval in addition to run-time evaluation.
35 // If and when saturating casts are adopted, this macro should be merged with test!() to ensure
36 // that run-time and const eval agree on inputs that currently trigger a const eval error.
38 ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ({
39 test!($val, $src_ty -> $dest_ty, $expected);
41 const X: $src_ty = $val;
42 const Y: $dest_ty = X as $dest_ty;
43 assert_eq!(Y, $expected,
44 "const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
48 ($fval:expr, f* -> $ity:ident, $ival:expr) => (
49 test!($fval, f32 -> $ity, $ival);
50 test!($fval, f64 -> $ity, $ival);
54 macro_rules! common_fptoi_tests {
55 ($fty:ident -> $($ity:ident)+) => ({ $(
56 test!($fty::NAN, $fty -> $ity, 0);
57 test!($fty::INFINITY, $fty -> $ity, $ity::MAX);
58 test!($fty::NEG_INFINITY, $fty -> $ity, $ity::MIN);
59 // These two tests are not solely float->int tests, in particular the latter relies on
60 // `u128::MAX as f32` not being UB. But that's okay, since this file tests int->float
61 // as well, the test is just slightly misplaced.
62 test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN);
63 test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX);
64 test_c!(0., $fty -> $ity, 0);
65 test_c!($fty::MIN_POSITIVE, $fty -> $ity, 0);
66 test!(-0.9, $fty -> $ity, 0);
67 test_c!(1., $fty -> $ity, 1);
68 test_c!(42., $fty -> $ity, 42);
71 (f* -> $($ity:ident)+) => ({
72 common_fptoi_tests!(f32 -> $($ity)+);
73 common_fptoi_tests!(f64 -> $($ity)+);
77 macro_rules! fptoui_tests {
78 ($fty: ident -> $($ity: ident)+) => ({ $(
79 test!(-0., $fty -> $ity, 0);
80 test!(-$fty::MIN_POSITIVE, $fty -> $ity, 0);
81 test!(-0.99999994, $fty -> $ity, 0);
82 test!(-1., $fty -> $ity, 0);
83 test!(-100., $fty -> $ity, 0);
84 test!(#[allow(overflowing_literals)] -1e50, $fty -> $ity, 0);
85 test!(#[allow(overflowing_literals)] -1e130, $fty -> $ity, 0);
88 (f* -> $($ity:ident)+) => ({
89 fptoui_tests!(f32 -> $($ity)+);
90 fptoui_tests!(f64 -> $($ity)+);
95 common_fptoi_tests!(f* -> i8 i16 i32 i64 i128 u8 u16 u32 u64 u128);
96 fptoui_tests!(f* -> u8 u16 u32 u64 u128);
98 // The following tests cover edge cases for some integer types.
101 test_c!(254., f* -> u8, 254);
102 test!(256., f* -> u8, 255);
105 test_c!(-127., f* -> i8, -127);
106 test!(-129., f* -> i8, -128);
107 test_c!(126., f* -> i8, 126);
108 test!(128., f* -> i8, 127);
111 // -2147483648. is i32::MIN (exactly)
112 test_c!(-2147483648., f* -> i32, i32::MIN);
113 // 2147483648. is i32::MAX rounded up
114 test!(2147483648., f32 -> i32, 2147483647);
115 // With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
116 // multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
117 test_c!(2147483520., f32 -> i32, 2147483520);
118 // Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
119 test!(-2147483904., f* -> i32, i32::MIN);
120 test_c!(-2147483520., f* -> i32, -2147483520);
123 // round(MAX) and nextUp(round(MAX))
124 test_c!(4294967040., f* -> u32, 4294967040);
125 test!(4294967296., f* -> u32, 4294967295);
129 test_c!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
130 // nextDown(f32::MAX) = 2^128 - 2 * 2^104
131 const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
132 test_c!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
135 // f32::MAX - 0.5 ULP and smaller should be rounded down
136 test_c!(0xfffffe00000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
137 test_c!(0xfffffe7fffffffffffffffffffffffff, u128 -> f32, SECOND_LARGEST_F32);
138 test_c!(0xfffffe80000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
139 // numbers within < 0.5 ULP of f32::MAX it should be rounded to f32::MAX
140 test_c!(0xfffffe80000000000000000000000001, u128 -> f32, f32::MAX);
141 test_c!(0xfffffeffffffffffffffffffffffffff, u128 -> f32, f32::MAX);
142 test_c!(0xffffff00000000000000000000000000, u128 -> f32, f32::MAX);
143 test_c!(0xffffff00000000000000000000000001, u128 -> f32, f32::MAX);
144 test_c!(0xffffff7fffffffffffffffffffffffff, u128 -> f32, f32::MAX);
145 // f32::MAX + 0.5 ULP and greater should be rounded to infinity
146 test_c!(0xffffff80000000000000000000000000, u128 -> f32, f32::INFINITY);
147 test_c!(0xffffff80000000f00000000000000000, u128 -> f32, f32::INFINITY);
148 test_c!(0xffffff87ffffffffffffffff00000001, u128 -> f32, f32::INFINITY);
150 // u128->f64 should not be affected by the u128->f32 checks
151 test_c!(0xffffff80000000000000000000000000, u128 -> f64,
152 340282356779733661637539395458142568448.0);
153 test_c!(u128::MAX, u128 -> f64, 340282366920938463463374607431768211455.0);