1
1
// run-pass
2
+ // compile-flags:-Zmir-opt-level=0
2
3
// Tests saturating float->int casts. See u128-as-f32.rs for the opposite direction.
3
4
//
4
5
// Some of these tests come from a similar file in miri,
5
- // tests/run-pass/float.rs. They're just duplicated currently but we may want
6
- // to merge this in the future.
6
+ // tests/run-pass/float.rs. Individual test cases are potentially duplicated
7
+ // with the previously existing tests, but since this runs so quickly anyway,
8
+ // we're not spending the time to figure out exactly which ones should be
9
+ // merged.
7
10
8
11
#![ feature( test, stmt_expr_attributes) ]
9
12
#![ feature( track_caller) ]
@@ -21,31 +24,18 @@ macro_rules! test {
21
24
// black_box disables constant evaluation to test run-time conversions:
22
25
assert_eq!( black_box:: <$src_ty>( $val) as $dest_ty, $expected,
23
26
"run-time {} -> {}" , stringify!( $src_ty) , stringify!( $dest_ty) ) ;
24
- ) ;
25
-
26
- ( $fval: expr, f* -> $ity: ident, $ival: expr) => (
27
- test!( $fval, f32 -> $ity, $ival) ;
28
- test!( $fval, f64 -> $ity, $ival) ;
29
- )
30
- }
31
27
32
- // This macro tests const eval in addition to run-time evaluation.
33
- // If and when saturating casts are adopted, this macro should be merged with test!() to ensure
34
- // that run-time and const eval agree on inputs that currently trigger a const eval error.
35
- macro_rules! test_c {
36
- ( $val: expr, $src_ty: ident -> $dest_ty: ident, $expected: expr) => ( {
37
- test!( $val, $src_ty -> $dest_ty, $expected) ;
38
28
{
39
29
const X : $src_ty = $val;
40
30
const Y : $dest_ty = X as $dest_ty;
41
31
assert_eq!( Y , $expected,
42
32
"const eval {} -> {}" , stringify!( $src_ty) , stringify!( $dest_ty) ) ;
43
33
}
44
- } ) ;
34
+ ) ;
45
35
46
36
( $fval: expr, f* -> $ity: ident, $ival: expr) => (
47
- test_c !( $fval, f32 -> $ity, $ival) ;
48
- test_c !( $fval, f64 -> $ity, $ival) ;
37
+ test !( $fval, f32 -> $ity, $ival) ;
38
+ test !( $fval, f64 -> $ity, $ival) ;
49
39
)
50
40
}
51
41
@@ -59,11 +49,11 @@ macro_rules! common_fptoi_tests {
59
49
// as well, the test is just slightly misplaced.
60
50
test!( $ity:: MIN as $fty, $fty -> $ity, $ity:: MIN ) ;
61
51
test!( $ity:: MAX as $fty, $fty -> $ity, $ity:: MAX ) ;
62
- test_c !( 0. , $fty -> $ity, 0 ) ;
63
- test_c !( $fty:: MIN_POSITIVE , $fty -> $ity, 0 ) ;
52
+ test !( 0. , $fty -> $ity, 0 ) ;
53
+ test !( $fty:: MIN_POSITIVE , $fty -> $ity, 0 ) ;
64
54
test!( -0.9 , $fty -> $ity, 0 ) ;
65
- test_c !( 1. , $fty -> $ity, 1 ) ;
66
- test_c !( 42. , $fty -> $ity, 42 ) ;
55
+ test !( 1. , $fty -> $ity, 1 ) ;
56
+ test !( 42. , $fty -> $ity, 42 ) ;
67
57
) + } ) ;
68
58
69
59
( f* -> $( $ity: ident) +) => ( {
@@ -217,39 +207,6 @@ where
217
207
assert_eq ! ( unsafe { x. cast_unchecked( ) } , y) ;
218
208
}
219
209
220
- fn basic ( ) {
221
- // basic arithmetic
222
- assert_eq ( 6.0_f32 * 6.0_f32 , 36.0_f32 ) ;
223
- assert_eq ( 6.0_f64 * 6.0_f64 , 36.0_f64 ) ;
224
- assert_eq ( -{ 5.0_f32 } , -5.0_f32 ) ;
225
- assert_eq ( -{ 5.0_f64 } , -5.0_f64 ) ;
226
- // infinities, NaN
227
- assert ! ( ( 5.0_f32 / 0.0 ) . is_infinite( ) ) ;
228
- assert_ne ! ( { 5.0_f32 / 0.0 } , { -5.0_f32 / 0.0 } ) ;
229
- assert ! ( ( 5.0_f64 / 0.0 ) . is_infinite( ) ) ;
230
- assert_ne ! ( { 5.0_f64 / 0.0 } , { 5.0_f64 / -0.0 } ) ;
231
- assert ! ( ( -5.0_f32 ) . sqrt( ) . is_nan( ) ) ;
232
- assert ! ( ( -5.0_f64 ) . sqrt( ) . is_nan( ) ) ;
233
- assert_ne ! ( f32 :: NAN , f32 :: NAN ) ;
234
- assert_ne ! ( f64 :: NAN , f64 :: NAN ) ;
235
- // negative zero
236
- let posz = 0.0f32 ;
237
- let negz = -0.0f32 ;
238
- assert_eq ( posz, negz) ;
239
- assert_ne ! ( posz. to_bits( ) , negz. to_bits( ) ) ;
240
- let posz = 0.0f64 ;
241
- let negz = -0.0f64 ;
242
- assert_eq ( posz, negz) ;
243
- assert_ne ! ( posz. to_bits( ) , negz. to_bits( ) ) ;
244
- // byte-level transmute
245
- let x: u64 = unsafe { std:: mem:: transmute ( 42.0_f64 ) } ;
246
- let y: f64 = unsafe { std:: mem:: transmute ( x) } ;
247
- assert_eq ( y, 42.0_f64 ) ;
248
- let x: u32 = unsafe { std:: mem:: transmute ( 42.0_f32 ) } ;
249
- let y: f32 = unsafe { std:: mem:: transmute ( x) } ;
250
- assert_eq ( y, 42.0_f32 ) ;
251
- }
252
-
253
210
fn casts ( ) {
254
211
// f32 -> i8
255
212
test_both_cast :: < f32 , i8 > ( 127.99 , 127 ) ;
@@ -500,42 +457,8 @@ fn casts() {
500
457
assert_eq :: < f32 > ( f64:: NEG_INFINITY as f32 , f32:: NEG_INFINITY ) ;
501
458
}
502
459
503
- fn ops ( ) {
504
- // f32 min/max
505
- assert_eq ( ( 1.0 as f32 ) . max ( -1.0 ) , 1.0 ) ;
506
- assert_eq ( ( 1.0 as f32 ) . min ( -1.0 ) , -1.0 ) ;
507
- assert_eq ( f32:: NAN . min ( 9.0 ) , 9.0 ) ;
508
- assert_eq ( f32:: NAN . max ( -9.0 ) , -9.0 ) ;
509
- assert_eq ( ( 9.0 as f32 ) . min ( f32:: NAN ) , 9.0 ) ;
510
- assert_eq ( ( -9.0 as f32 ) . max ( f32:: NAN ) , -9.0 ) ;
511
-
512
- // f64 min/max
513
- assert_eq ( ( 1.0 as f64 ) . max ( -1.0 ) , 1.0 ) ;
514
- assert_eq ( ( 1.0 as f64 ) . min ( -1.0 ) , -1.0 ) ;
515
- assert_eq ( f64:: NAN . min ( 9.0 ) , 9.0 ) ;
516
- assert_eq ( f64:: NAN . max ( -9.0 ) , -9.0 ) ;
517
- assert_eq ( ( 9.0 as f64 ) . min ( f64:: NAN ) , 9.0 ) ;
518
- assert_eq ( ( -9.0 as f64 ) . max ( f64:: NAN ) , -9.0 ) ;
519
-
520
- // f32 copysign
521
- assert_eq ( 3.5_f32 . copysign ( 0.42 ) , 3.5_f32 ) ;
522
- assert_eq ( 3.5_f32 . copysign ( -0.42 ) , -3.5_f32 ) ;
523
- assert_eq ( ( -3.5_f32 ) . copysign ( 0.42 ) , 3.5_f32 ) ;
524
- assert_eq ( ( -3.5_f32 ) . copysign ( -0.42 ) , -3.5_f32 ) ;
525
- assert ! ( f32 :: NAN . copysign( 1.0 ) . is_nan( ) ) ;
526
-
527
- // f64 copysign
528
- assert_eq ( 3.5_f64 . copysign ( 0.42 ) , 3.5_f64 ) ;
529
- assert_eq ( 3.5_f64 . copysign ( -0.42 ) , -3.5_f64 ) ;
530
- assert_eq ( ( -3.5_f64 ) . copysign ( 0.42 ) , 3.5_f64 ) ;
531
- assert_eq ( ( -3.5_f64 ) . copysign ( -0.42 ) , -3.5_f64 ) ;
532
- assert ! ( f64 :: NAN . copysign( 1.0 ) . is_nan( ) ) ;
533
- }
534
-
535
460
pub fn main ( ) {
536
- basic ( ) ;
537
- casts ( ) ;
538
- ops ( ) ;
461
+ casts ( ) ; // from miri's tests
539
462
540
463
common_fptoi_tests ! ( f* -> i8 i16 i32 i64 u8 u16 u32 u64 ) ;
541
464
fptoui_tests ! ( f* -> u8 u16 u32 u64 ) ;
@@ -549,39 +472,39 @@ pub fn main() {
549
472
// The following tests cover edge cases for some integer types.
550
473
551
474
// # u8
552
- test_c ! ( 254. , f* -> u8 , 254 ) ;
475
+ test ! ( 254. , f* -> u8 , 254 ) ;
553
476
test ! ( 256. , f* -> u8 , 255 ) ;
554
477
555
478
// # i8
556
- test_c ! ( -127. , f* -> i8 , -127 ) ;
479
+ test ! ( -127. , f* -> i8 , -127 ) ;
557
480
test ! ( -129. , f* -> i8 , -128 ) ;
558
- test_c ! ( 126. , f* -> i8 , 126 ) ;
481
+ test ! ( 126. , f* -> i8 , 126 ) ;
559
482
test ! ( 128. , f* -> i8 , 127 ) ;
560
483
561
484
// # i32
562
485
// -2147483648. is i32::MIN (exactly)
563
- test_c ! ( -2147483648. , f* -> i32 , i32 :: MIN ) ;
486
+ test ! ( -2147483648. , f* -> i32 , i32 :: MIN ) ;
564
487
// 2147483648. is i32::MAX rounded up
565
488
test ! ( 2147483648. , f32 -> i32 , 2147483647 ) ;
566
489
// With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
567
490
// multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
568
- test_c ! ( 2147483520. , f32 -> i32 , 2147483520 ) ;
491
+ test ! ( 2147483520. , f32 -> i32 , 2147483520 ) ;
569
492
// Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
570
493
test ! ( -2147483904. , f* -> i32 , i32 :: MIN ) ;
571
- test_c ! ( -2147483520. , f* -> i32 , -2147483520 ) ;
494
+ test ! ( -2147483520. , f* -> i32 , -2147483520 ) ;
572
495
573
496
// # u32
574
497
// round(MAX) and nextUp(round(MAX))
575
- test_c ! ( 4294967040. , f* -> u32 , 4294967040 ) ;
498
+ test ! ( 4294967040. , f* -> u32 , 4294967040 ) ;
576
499
test ! ( 4294967296. , f* -> u32 , 4294967295 ) ;
577
500
578
501
// # u128
579
502
#[ cfg( not( target_os = "emscripten" ) ) ]
580
503
{
581
504
// float->int:
582
- test_c ! ( f32 :: MAX , f32 -> u128 , 0xffffff00000000000000000000000000 ) ;
505
+ test ! ( f32 :: MAX , f32 -> u128 , 0xffffff00000000000000000000000000 ) ;
583
506
// nextDown(f32::MAX) = 2^128 - 2 * 2^104
584
507
const SECOND_LARGEST_F32 : f32 = 340282326356119256160033759537265639424. ;
585
- test_c ! ( SECOND_LARGEST_F32 , f32 -> u128 , 0xfffffe00000000000000000000000000 ) ;
508
+ test ! ( SECOND_LARGEST_F32 , f32 -> u128 , 0xfffffe00000000000000000000000000 ) ;
586
509
}
587
510
}
0 commit comments