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