]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/float.rs
use standard black_box function
[rust.git] / tests / run-pass / float.rs
1 // compile-flags: -Zmir-opt-level=0
2 // FIXME: Using opt-level 2 here makes the test take forever (https://github.com/rust-lang/rust/issues/76433).
3 #![feature(stmt_expr_attributes, test)]
4 use std::fmt::Debug;
5 use std::hint::black_box;
6
7 // Helper function to avoid promotion so that this tests "run-time" casts, not CTFE.
8 // Doesn't make a big difference when running this in Miri, but it means we can compare this
9 // with the LLVM backend by running `rustc -Zmir-opt-level=0 -Zsaturating-float-casts`.
10 #[track_caller]
11 #[inline(never)]
12 fn assert_eq<T: PartialEq + Debug>(x: T, y: T) {
13     assert_eq!(x, y);
14 }
15
16 trait FloatToInt<Int>: Copy {
17     fn cast(self) -> Int;
18     unsafe fn cast_unchecked(self) -> Int;
19 }
20
21 impl FloatToInt<i8> for f32 {
22     fn cast(self) -> i8 { self as _ }
23     unsafe fn cast_unchecked(self) -> i8 { self.to_int_unchecked() }
24 }
25 impl FloatToInt<i32> for f32 {
26     fn cast(self) -> i32 { self as _ }
27     unsafe fn cast_unchecked(self) -> i32 { self.to_int_unchecked() }
28 }
29 impl FloatToInt<u32> for f32 {
30     fn cast(self) -> u32 { self as _ }
31     unsafe fn cast_unchecked(self) -> u32 { self.to_int_unchecked() }
32 }
33 impl FloatToInt<i64> for f32 {
34     fn cast(self) -> i64 { self as _ }
35     unsafe fn cast_unchecked(self) -> i64 { self.to_int_unchecked() }
36 }
37 impl FloatToInt<u64> for f32 {
38     fn cast(self) -> u64 { self as _ }
39     unsafe fn cast_unchecked(self) -> u64 { self.to_int_unchecked() }
40 }
41
42 impl FloatToInt<i8> for f64 {
43     fn cast(self) -> i8 { self as _ }
44     unsafe fn cast_unchecked(self) -> i8 { self.to_int_unchecked() }
45 }
46 impl FloatToInt<i32> for f64 {
47     fn cast(self) -> i32 { self as _ }
48     unsafe fn cast_unchecked(self) -> i32 { self.to_int_unchecked() }
49 }
50 impl FloatToInt<u32> for f64 {
51     fn cast(self) -> u32 { self as _ }
52     unsafe fn cast_unchecked(self) -> u32 { self.to_int_unchecked() }
53 }
54 impl FloatToInt<i64> for f64 {
55     fn cast(self) -> i64 { self as _ }
56     unsafe fn cast_unchecked(self) -> i64 { self.to_int_unchecked() }
57 }
58 impl FloatToInt<u64> for f64 {
59     fn cast(self) -> u64 { self as _ }
60     unsafe fn cast_unchecked(self) -> u64 { self.to_int_unchecked() }
61 }
62 impl FloatToInt<i128> for f64 {
63     fn cast(self) -> i128 { self as _ }
64     unsafe fn cast_unchecked(self) -> i128 { self.to_int_unchecked() }
65 }
66 impl FloatToInt<u128> for f64 {
67     fn cast(self) -> u128 { self as _ }
68     unsafe fn cast_unchecked(self) -> u128 { self.to_int_unchecked() }
69 }
70
71 /// Test this cast both via `as` and via `approx_unchecked` (i.e., it must not saturate).
72 #[track_caller]
73 #[inline(never)]
74 fn test_both_cast<F, I>(x: F, y: I)
75     where F: FloatToInt<I>, I: PartialEq + Debug
76 {
77     assert_eq!(x.cast(), y);
78     assert_eq!(unsafe { x.cast_unchecked() }, y);
79 }
80
81 fn main() {
82     basic();
83     casts();
84     more_casts();
85     ops();
86 }
87
88 fn basic() {
89     // basic arithmetic
90     assert_eq(6.0_f32*6.0_f32, 36.0_f32);
91     assert_eq(6.0_f64*6.0_f64, 36.0_f64);
92     assert_eq(-{5.0_f32}, -5.0_f32);
93     assert_eq(-{5.0_f64}, -5.0_f64);
94     // infinities, NaN
95     assert!((5.0_f32/0.0).is_infinite());
96     assert_ne!({5.0_f32/0.0}, {-5.0_f32/0.0});
97     assert!((5.0_f64/0.0).is_infinite());
98     assert_ne!({5.0_f64/0.0}, {5.0_f64/-0.0});
99     assert!((-5.0_f32).sqrt().is_nan());
100     assert!((-5.0_f64).sqrt().is_nan());
101     assert_ne!(f32::NAN, f32::NAN);
102     assert_ne!(f64::NAN, f64::NAN);
103     // negative zero
104     let posz = 0.0f32;
105     let negz = -0.0f32;
106     assert_eq(posz, negz);
107     assert_ne!(posz.to_bits(), negz.to_bits());
108     let posz = 0.0f64;
109     let negz = -0.0f64;
110     assert_eq(posz, negz);
111     assert_ne!(posz.to_bits(), negz.to_bits());
112     // byte-level transmute
113     let x: u64 = unsafe { std::mem::transmute(42.0_f64) };
114     let y: f64 = unsafe { std::mem::transmute(x) };
115     assert_eq(y, 42.0_f64);
116     let x: u32 = unsafe { std::mem::transmute(42.0_f32) };
117     let y: f32 = unsafe { std::mem::transmute(x) };
118     assert_eq(y, 42.0_f32);
119 }
120
121 /// Many of these test values are taken from
122 /// https://github.com/WebAssembly/testsuite/blob/master/conversions.wast.
123 fn casts() {
124     // f32 -> i8
125     test_both_cast::<f32, i8>(127.99, 127);
126     test_both_cast::<f32, i8>(-128.99, -128);
127
128     // f32 -> i32
129     test_both_cast::<f32, i32>(0.0, 0);
130     test_both_cast::<f32, i32>(-0.0, 0);
131     test_both_cast::<f32, i32>(/*0x1p-149*/ f32::from_bits(0x00000001), 0);
132     test_both_cast::<f32, i32>(/*-0x1p-149*/ f32::from_bits(0x80000001), 0);
133     test_both_cast::<f32, i32>(/*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd), 1);
134     test_both_cast::<f32, i32>(/*-0x1.19999ap+0*/ f32::from_bits(0xbf8ccccd), -1);
135     test_both_cast::<f32, i32>(1.9, 1);
136     test_both_cast::<f32, i32>(-1.9, -1);
137     test_both_cast::<f32, i32>(5.0, 5);
138     test_both_cast::<f32, i32>(-5.0, -5);
139     test_both_cast::<f32, i32>(2147483520.0, 2147483520);
140     test_both_cast::<f32, i32>(-2147483648.0, -2147483648);
141     // unrepresentable casts
142     assert_eq::<i32>(2147483648.0f32 as i32, i32::MAX);
143     assert_eq::<i32>(-2147483904.0f32 as i32, i32::MIN);
144     assert_eq::<i32>(f32::MAX as i32, i32::MAX);
145     assert_eq::<i32>(f32::MIN as i32, i32::MIN);
146     assert_eq::<i32>(f32::INFINITY as i32, i32::MAX);
147     assert_eq::<i32>(f32::NEG_INFINITY as i32, i32::MIN);
148     assert_eq::<i32>(f32::NAN as i32, 0);
149     assert_eq::<i32>((-f32::NAN) as i32, 0);
150
151     // f32 -> u32
152     test_both_cast::<f32, u32>(0.0, 0);
153     test_both_cast::<f32, u32>(-0.0, 0);
154     test_both_cast::<f32, u32>(-0.9999999, 0);
155     test_both_cast::<f32, u32>(/*0x1p-149*/ f32::from_bits(0x1), 0);
156     test_both_cast::<f32, u32>(/*-0x1p-149*/ f32::from_bits(0x80000001), 0);
157     test_both_cast::<f32, u32>(/*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd), 1);
158     test_both_cast::<f32, u32>(1.9, 1);
159     test_both_cast::<f32, u32>(5.0, 5);
160     test_both_cast::<f32, u32>(2147483648.0, 0x8000_0000);
161     test_both_cast::<f32, u32>(4294967040.0, 0u32.wrapping_sub(256));
162     test_both_cast::<f32, u32>(/*-0x1.ccccccp-1*/ f32::from_bits(0xbf666666), 0);
163     test_both_cast::<f32, u32>(/*-0x1.fffffep-1*/ f32::from_bits(0xbf7fffff), 0);
164     test_both_cast::<f32, u32>((u32::MAX-128) as f32, u32::MAX-255); // rounding loss
165     // unrepresentable casts
166     assert_eq::<u32>((u32::MAX-127) as f32 as u32, u32::MAX); // rounds up and then becomes unrepresentable
167     assert_eq::<u32>(4294967296.0f32 as u32, u32::MAX);
168     assert_eq::<u32>(-5.0f32 as u32, 0);
169     assert_eq::<u32>(f32::MAX as u32, u32::MAX);
170     assert_eq::<u32>(f32::MIN as u32, 0);
171     assert_eq::<u32>(f32::INFINITY as u32, u32::MAX);
172     assert_eq::<u32>(f32::NEG_INFINITY as u32, 0);
173     assert_eq::<u32>(f32::NAN as u32, 0);
174     assert_eq::<u32>((-f32::NAN) as u32, 0);
175
176     // f32 -> i64
177     test_both_cast::<f32, i64>(4294967296.0, 4294967296);
178     test_both_cast::<f32, i64>(-4294967296.0, -4294967296);
179     test_both_cast::<f32, i64>(9223371487098961920.0, 9223371487098961920);
180     test_both_cast::<f32, i64>(-9223372036854775808.0, -9223372036854775808);
181
182     // f64 -> i8
183     test_both_cast::<f64, i8>(127.99, 127);
184     test_both_cast::<f64, i8>(-128.99, -128);
185
186     // f64 -> i32
187     test_both_cast::<f64, i32>(0.0, 0);
188     test_both_cast::<f64, i32>(-0.0, 0);
189     test_both_cast::<f64, i32>(/*0x1.199999999999ap+0*/ f64::from_bits(0x3ff199999999999a), 1);
190     test_both_cast::<f64, i32>(/*-0x1.199999999999ap+0*/ f64::from_bits(0xbff199999999999a), -1);
191     test_both_cast::<f64, i32>(1.9, 1);
192     test_both_cast::<f64, i32>(-1.9, -1);
193     test_both_cast::<f64, i32>(1e8, 100_000_000);
194     test_both_cast::<f64, i32>(2147483647.0, 2147483647);
195     test_both_cast::<f64, i32>(-2147483648.0, -2147483648);
196     // unrepresentable casts
197     assert_eq::<i32>(2147483648.0f64 as i32, i32::MAX);
198     assert_eq::<i32>(-2147483649.0f64 as i32, i32::MIN);
199
200     // f64 -> i64
201     test_both_cast::<f64, i64>(0.0, 0);
202     test_both_cast::<f64, i64>(-0.0, 0);
203     test_both_cast::<f64, i64>(/*0x0.0000000000001p-1022*/ f64::from_bits(0x1), 0);
204     test_both_cast::<f64, i64>(/*-0x0.0000000000001p-1022*/ f64::from_bits(0x8000000000000001), 0);
205     test_both_cast::<f64, i64>(/*0x1.199999999999ap+0*/ f64::from_bits(0x3ff199999999999a), 1);
206     test_both_cast::<f64, i64>(/*-0x1.199999999999ap+0*/ f64::from_bits(0xbff199999999999a), -1);
207     test_both_cast::<f64, i64>(5.0, 5);
208     test_both_cast::<f64, i64>(5.9, 5);
209     test_both_cast::<f64, i64>(-5.0, -5);
210     test_both_cast::<f64, i64>(-5.9, -5);
211     test_both_cast::<f64, i64>(4294967296.0, 4294967296);
212     test_both_cast::<f64, i64>(-4294967296.0, -4294967296);
213     test_both_cast::<f64, i64>(9223372036854774784.0, 9223372036854774784);
214     test_both_cast::<f64, i64>(-9223372036854775808.0, -9223372036854775808);
215     // unrepresentable casts
216     assert_eq::<i64>(9223372036854775808.0f64 as i64, i64::MAX);
217     assert_eq::<i64>(-9223372036854777856.0f64 as i64, i64::MIN);
218     assert_eq::<i64>(f64::MAX as i64, i64::MAX);
219     assert_eq::<i64>(f64::MIN as i64, i64::MIN);
220     assert_eq::<i64>(f64::INFINITY as i64, i64::MAX);
221     assert_eq::<i64>(f64::NEG_INFINITY as i64, i64::MIN);
222     assert_eq::<i64>(f64::NAN as i64, 0);
223     assert_eq::<i64>((-f64::NAN) as i64, 0);
224
225     // f64 -> u64
226     test_both_cast::<f64, u64>(0.0, 0);
227     test_both_cast::<f64, u64>(-0.0, 0);
228     test_both_cast::<f64, u64>(-0.99999999999, 0);
229     test_both_cast::<f64, u64>(5.0, 5);
230     test_both_cast::<f64, u64>(1e16, 10000000000000000);
231     test_both_cast::<f64, u64>((u64::MAX-1024) as f64, u64::MAX-2047); // rounding loss
232     test_both_cast::<f64, u64>(9223372036854775808.0, 9223372036854775808);
233     // unrepresentable casts
234     assert_eq::<u64>(-5.0f64 as u64, 0);
235     assert_eq::<u64>((u64::MAX-1023) as f64 as u64, u64::MAX); // rounds up and then becomes unrepresentable
236     assert_eq::<u64>(18446744073709551616.0f64 as u64, u64::MAX);
237     assert_eq::<u64>(f64::MAX as u64, u64::MAX);
238     assert_eq::<u64>(f64::MIN as u64, 0);
239     assert_eq::<u64>(f64::INFINITY as u64, u64::MAX);
240     assert_eq::<u64>(f64::NEG_INFINITY as u64, 0);
241     assert_eq::<u64>(f64::NAN as u64, 0);
242     assert_eq::<u64>((-f64::NAN) as u64, 0);
243
244     // f64 -> i128
245     assert_eq::<i128>(f64::MAX as i128, i128::MAX);
246     assert_eq::<i128>(f64::MIN as i128, i128::MIN);
247
248     // f64 -> u128
249     assert_eq::<u128>(f64::MAX as u128, u128::MAX);
250     assert_eq::<u128>(f64::MIN as u128, 0);
251
252     // int -> f32
253     assert_eq::<f32>(127i8 as f32, 127.0);
254     assert_eq::<f32>(2147483647i32 as f32, 2147483648.0);
255     assert_eq::<f32>((-2147483648i32) as f32, -2147483648.0);
256     assert_eq::<f32>(1234567890i32 as f32, /*0x1.26580cp+30*/ f32::from_bits(0x4e932c06));
257     assert_eq::<f32>(16777217i32 as f32, 16777216.0);
258     assert_eq::<f32>((-16777217i32) as f32, -16777216.0);
259     assert_eq::<f32>(16777219i32 as f32, 16777220.0);
260     assert_eq::<f32>((-16777219i32) as f32, -16777220.0);
261     assert_eq::<f32>(0x7fffff4000000001i64 as f32, /*0x1.fffffep+62*/ f32::from_bits(0x5effffff));
262     assert_eq::<f32>(0x8000004000000001u64 as i64 as f32, /*-0x1.fffffep+62*/ f32::from_bits(0xdeffffff));
263     assert_eq::<f32>(0x0020000020000001i64 as f32, /*0x1.000002p+53*/ f32::from_bits(0x5a000001));
264     assert_eq::<f32>(0xffdfffffdfffffffu64 as i64 as f32, /*-0x1.000002p+53*/ f32::from_bits(0xda000001));
265     assert_eq::<f32>(i128::MIN as f32, -170141183460469231731687303715884105728.0f32);
266     assert_eq::<f32>(u128::MAX as f32, f32::INFINITY); // saturation
267
268     // int -> f64
269     assert_eq::<f64>(127i8 as f64, 127.0);
270     assert_eq::<f64>(i16::MIN as f64, -32768.0f64);
271     assert_eq::<f64>(2147483647i32 as f64, 2147483647.0);
272     assert_eq::<f64>(-2147483648i32 as f64, -2147483648.0);
273     assert_eq::<f64>(987654321i32 as f64, 987654321.0);
274     assert_eq::<f64>(9223372036854775807i64 as f64, 9223372036854775807.0);
275     assert_eq::<f64>(-9223372036854775808i64 as f64, -9223372036854775808.0);
276     assert_eq::<f64>(4669201609102990i64 as f64, 4669201609102990.0); // Feigenbaum (?)
277     assert_eq::<f64>(9007199254740993i64 as f64, 9007199254740992.0);
278     assert_eq::<f64>(-9007199254740993i64 as f64, -9007199254740992.0);
279     assert_eq::<f64>(9007199254740995i64 as f64, 9007199254740996.0);
280     assert_eq::<f64>(-9007199254740995i64 as f64, -9007199254740996.0);
281     assert_eq::<f64>(u128::MAX as f64, 340282366920938463463374607431768211455.0f64); // even that fits...
282
283     // f32 -> f64
284     assert_eq::<u64>((0.0f32 as f64).to_bits(), 0.0f64.to_bits());
285     assert_eq::<u64>(((-0.0f32) as f64).to_bits(), (-0.0f64).to_bits());
286     assert_eq::<f64>(5.0f32 as f64, 5.0f64);
287     assert_eq::<f64>(/*0x1p-149*/ f32::from_bits(0x1) as f64, /*0x1p-149*/ f64::from_bits(0x36a0000000000000));
288     assert_eq::<f64>(/*-0x1p-149*/ f32::from_bits(0x80000001) as f64, /*-0x1p-149*/ f64::from_bits(0xb6a0000000000000));
289     assert_eq::<f64>(/*0x1.fffffep+127*/ f32::from_bits(0x7f7fffff) as f64, /*0x1.fffffep+127*/ f64::from_bits(0x47efffffe0000000));
290     assert_eq::<f64>(/*-0x1.fffffep+127*/ (-f32::from_bits(0x7f7fffff)) as f64, /*-0x1.fffffep+127*/ -f64::from_bits(0x47efffffe0000000));
291     assert_eq::<f64>(/*0x1p-119*/ f32::from_bits(0x4000000) as f64, /*0x1p-119*/ f64::from_bits(0x3880000000000000));
292     assert_eq::<f64>(/*0x1.8f867ep+125*/ f32::from_bits(0x7e47c33f) as f64, 6.6382536710104395e+37);
293     assert_eq::<f64>(f32::INFINITY as f64, f64::INFINITY);
294     assert_eq::<f64>(f32::NEG_INFINITY as f64, f64::NEG_INFINITY);
295
296     // f64 -> f32
297     assert_eq::<u32>((0.0f64 as f32).to_bits(), 0.0f32.to_bits());
298     assert_eq::<u32>(((-0.0f64) as f32).to_bits(), (-0.0f32).to_bits());
299     assert_eq::<f32>(5.0f64 as f32, 5.0f32);
300     assert_eq::<f32>(/*0x0.0000000000001p-1022*/ f64::from_bits(0x1) as f32, 0.0);
301     assert_eq::<f32>(/*-0x0.0000000000001p-1022*/ (-f64::from_bits(0x1)) as f32, -0.0);
302     assert_eq::<f32>(/*0x1.fffffe0000000p-127*/ f64::from_bits(0x380fffffe0000000) as f32, /*0x1p-149*/ f32::from_bits(0x800000));
303     assert_eq::<f32>(/*0x1.4eae4f7024c7p+108*/ f64::from_bits(0x46b4eae4f7024c70) as f32, /*0x1.4eae5p+108*/ f32::from_bits(0x75a75728));
304     assert_eq::<f32>(f64::MAX as f32, f32::INFINITY);
305     assert_eq::<f32>(f64::MIN as f32, f32::NEG_INFINITY);
306     assert_eq::<f32>(f64::INFINITY as f32, f32::INFINITY);
307     assert_eq::<f32>(f64::NEG_INFINITY as f32, f32::NEG_INFINITY);
308 }
309
310 fn ops() {
311     // f32 min/max
312     assert_eq((1.0 as f32).max(-1.0), 1.0);
313     assert_eq((1.0 as f32).min(-1.0), -1.0);
314     assert_eq(f32::NAN.min(9.0), 9.0);
315     assert_eq(f32::NAN.max(-9.0), -9.0);
316     assert_eq((9.0 as f32).min(f32::NAN), 9.0);
317     assert_eq((-9.0 as f32).max(f32::NAN), -9.0);
318
319     // f64 min/max
320     assert_eq((1.0 as f64).max(-1.0), 1.0);
321     assert_eq((1.0 as f64).min(-1.0), -1.0);
322     assert_eq(f64::NAN.min(9.0), 9.0);
323     assert_eq(f64::NAN.max(-9.0), -9.0);
324     assert_eq((9.0 as f64).min(f64::NAN), 9.0);
325     assert_eq((-9.0 as f64).max(f64::NAN), -9.0);
326
327     // f32 copysign
328     assert_eq(3.5_f32.copysign(0.42), 3.5_f32);
329     assert_eq(3.5_f32.copysign(-0.42), -3.5_f32);
330     assert_eq((-3.5_f32).copysign(0.42), 3.5_f32);
331     assert_eq((-3.5_f32).copysign(-0.42), -3.5_f32);
332     assert!(f32::NAN.copysign(1.0).is_nan());
333
334     // f64 copysign
335     assert_eq(3.5_f64.copysign(0.42), 3.5_f64);
336     assert_eq(3.5_f64.copysign(-0.42), -3.5_f64);
337     assert_eq((-3.5_f64).copysign(0.42), 3.5_f64);
338     assert_eq((-3.5_f64).copysign(-0.42), -3.5_f64);
339     assert!(f64::NAN.copysign(1.0).is_nan());
340 }
341
342 /// Tests taken from rustc test suite.
343 ///
344
345 macro_rules! test {
346     ($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => (
347         // black_box disables constant evaluation to test run-time conversions:
348         assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
349                     "run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
350
351         {
352             const X: $src_ty = $val;
353             const Y: $dest_ty = X as $dest_ty;
354             assert_eq!(Y, $expected,
355                         "const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
356         }
357     );
358
359     ($fval:expr, f* -> $ity:ident, $ival:expr) => (
360         test!($fval, f32 -> $ity, $ival);
361         test!($fval, f64 -> $ity, $ival);
362     )
363 }
364
365 macro_rules! common_fptoi_tests {
366     ($fty:ident -> $($ity:ident)+) => ({ $(
367         test!($fty::NAN, $fty -> $ity, 0);
368         test!($fty::INFINITY, $fty -> $ity, $ity::MAX);
369         test!($fty::NEG_INFINITY, $fty -> $ity, $ity::MIN);
370         // These two tests are not solely float->int tests, in particular the latter relies on
371         // `u128::MAX as f32` not being UB. But that's okay, since this file tests int->float
372         // as well, the test is just slightly misplaced.
373         test!($ity::MIN as $fty, $fty -> $ity, $ity::MIN);
374         test!($ity::MAX as $fty, $fty -> $ity, $ity::MAX);
375         test!(0., $fty -> $ity, 0);
376         test!($fty::MIN_POSITIVE, $fty -> $ity, 0);
377         test!(-0.9, $fty -> $ity, 0);
378         test!(1., $fty -> $ity, 1);
379         test!(42., $fty -> $ity, 42);
380     )+ });
381
382     (f* -> $($ity:ident)+) => ({
383         common_fptoi_tests!(f32 -> $($ity)+);
384         common_fptoi_tests!(f64 -> $($ity)+);
385     })
386 }
387
388 macro_rules! fptoui_tests {
389     ($fty: ident -> $($ity: ident)+) => ({ $(
390         test!(-0., $fty -> $ity, 0);
391         test!(-$fty::MIN_POSITIVE, $fty -> $ity, 0);
392         test!(-0.99999994, $fty -> $ity, 0);
393         test!(-1., $fty -> $ity, 0);
394         test!(-100., $fty -> $ity, 0);
395         test!(#[allow(overflowing_literals)] -1e50, $fty -> $ity, 0);
396         test!(#[allow(overflowing_literals)] -1e130, $fty -> $ity, 0);
397     )+ });
398
399     (f* -> $($ity:ident)+) => ({
400         fptoui_tests!(f32 -> $($ity)+);
401         fptoui_tests!(f64 -> $($ity)+);
402     })
403 }
404
405 fn more_casts() {
406     common_fptoi_tests!(f* -> i8 i16 i32 i64 u8 u16 u32 u64);
407     fptoui_tests!(f* -> u8 u16 u32 u64);
408     common_fptoi_tests!(f* -> i128 u128);
409     fptoui_tests!(f* -> u128);
410
411     // The following tests cover edge cases for some integer types.
412
413     // # u8
414     test!(254., f* -> u8, 254);
415     test!(256., f* -> u8, 255);
416
417     // # i8
418     test!(-127., f* -> i8, -127);
419     test!(-129., f* -> i8, -128);
420     test!(126., f* -> i8, 126);
421     test!(128., f* -> i8, 127);
422
423     // # i32
424     // -2147483648. is i32::MIN (exactly)
425     test!(-2147483648., f* -> i32, i32::MIN);
426     // 2147483648. is i32::MAX rounded up
427     test!(2147483648., f32 -> i32, 2147483647);
428     // With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
429     // multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
430     test!(2147483520., f32 -> i32, 2147483520);
431     // Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
432     test!(-2147483904., f* -> i32, i32::MIN);
433     test!(-2147483520., f* -> i32, -2147483520);
434
435     // # u32
436     // round(MAX) and nextUp(round(MAX))
437     test!(4294967040., f* -> u32, 4294967040);
438     test!(4294967296., f* -> u32, 4294967295);
439
440     // # u128
441     // float->int:
442     test!(f32::MAX, f32 -> u128, 0xffffff00000000000000000000000000);
443     // nextDown(f32::MAX) = 2^128 - 2 * 2^104
444     const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
445     test!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
446 }