@@ -3,8 +3,8 @@ extern crate rustc_apfloat;
3
3
4
4
use core:: cmp:: Ordering ;
5
5
use rustc_apfloat:: ieee:: {
6
- BFloat , Double , Float8E4M3B11FNUZ , Float8E4M3FN , Float8E4M3FNUZ , Float8E5M2 , Float8E5M2FNUZ , Half , Quad , Single ,
7
- X87DoubleExtended ,
6
+ BFloat , Double , Float8E4M3B11FNUZ , Float8E4M3FN , Float8E4M3FNUZ , Float8E5M2 , Float8E5M2FNUZ , FloatTF32 , Half , Quad ,
7
+ Single , X87DoubleExtended ,
8
8
} ;
9
9
use rustc_apfloat:: { Category , ExpInt , IEK_INF , IEK_NAN , IEK_ZERO } ;
10
10
use rustc_apfloat:: { Float , FloatConvert , Round , Status , StatusAnd } ;
@@ -35,6 +35,7 @@ define_for_each_float_type! {
35
35
Float8E4M3FNUZ ,
36
36
Float8E5M2FNUZ ,
37
37
Float8E4M3B11FNUZ ,
38
+ FloatTF32 ,
38
39
X87DoubleExtended ,
39
40
40
41
// NOTE(eddyb) tests for this are usually in `ppc.rs` but this works too.
@@ -95,6 +96,7 @@ impl ToF32LosslessViaConvertToSingle for Float8E4M3FN {}
95
96
impl ToF32LosslessViaConvertToSingle for Float8E4M3FNUZ { }
96
97
impl ToF32LosslessViaConvertToSingle for Float8E5M2FNUZ { }
97
98
impl ToF32LosslessViaConvertToSingle for Float8E4M3B11FNUZ { }
99
+ impl ToF32LosslessViaConvertToSingle for FloatTF32 { }
98
100
99
101
trait ToF64LosslessViaConvertToDouble : FloatConvert < Double > {
100
102
fn to_f64 ( self ) -> f64 {
@@ -754,6 +756,23 @@ fn denormal() {
754
756
t /= Quad :: from_u128 ( 2 ) . value ;
755
757
assert ! ( t. is_denormal( ) ) ;
756
758
}
759
+
760
+ // Test TF32
761
+ {
762
+ assert ! ( !FloatTF32 :: from_u128( 0 ) . value. is_denormal( ) ) ;
763
+
764
+ let mut t = "1.17549435082228750797e-38" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
765
+ assert ! ( !t. is_denormal( ) ) ;
766
+
767
+ t /= FloatTF32 :: from_u128 ( 2 ) . value ;
768
+ assert ! ( t. is_denormal( ) ) ;
769
+
770
+ let mut t = "-1.17549435082228750797e-38" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
771
+ assert ! ( !t. is_denormal( ) ) ;
772
+
773
+ t /= FloatTF32 :: from_u128 ( 2 ) . value ;
774
+ assert ! ( t. is_denormal( ) ) ;
775
+ }
757
776
}
758
777
759
778
#[ test]
@@ -1421,6 +1440,19 @@ fn nan() {
1421
1440
( 0x80 , true , false , 0xaa ) ,
1422
1441
( 0x80 , true , true , 0xaa ) ,
1423
1442
] ;
1443
+ let test_tf32 = [
1444
+ // ex. SNaN Neg payload
1445
+ ( 0x3fe00 , false , false , 0x00000000 ) ,
1446
+ ( 0x7fe00 , false , true , 0x00000000 ) ,
1447
+ ( 0x3feaa , false , false , 0xaa ) ,
1448
+ ( 0x3ffaa , false , false , 0xdaa ) ,
1449
+ ( 0x3ffaa , false , false , 0xfdaa ) ,
1450
+ ( 0x3fd00 , true , false , 0x00000000 ) ,
1451
+ ( 0x7fd00 , true , true , 0x00000000 ) ,
1452
+ ( 0x3fcaa , true , false , 0xaa ) ,
1453
+ ( 0x3fdaa , true , false , 0xfaa ) ,
1454
+ ( 0x3fdaa , true , false , 0x1aa ) ,
1455
+ ] ;
1424
1456
for ( expected, signaling, negative, payload) in tests_single {
1425
1457
assert_eq ! ( expected, nanbits_from_u128:: <Single >( signaling, negative, payload) ) ;
1426
1458
}
@@ -1436,6 +1468,9 @@ fn nan() {
1436
1468
for ( expected, signaling, negative, payload) in tests_8e4m3b11fnuz {
1437
1469
assert_eq ! ( expected, nanbits_from_u128:: <Float8E4M3B11FNUZ >( signaling, negative, payload) ) ;
1438
1470
}
1471
+ for ( expected, signaling, negative, payload) in test_tf32 {
1472
+ assert_eq ! ( expected, nanbits_from_u128:: <FloatTF32 >( signaling, negative, payload) ) ;
1473
+ }
1439
1474
}
1440
1475
1441
1476
#[ test]
@@ -1793,6 +1828,7 @@ fn largest() {
1793
1828
assert_eq ! ( 240.0 , Float8E4M3FNUZ :: largest( ) . to_f64( ) ) ;
1794
1829
assert_eq ! ( 57344.0 , Float8E5M2FNUZ :: largest( ) . to_f64( ) ) ;
1795
1830
assert_eq ! ( 30.0 , Float8E4M3B11FNUZ :: largest( ) . to_f64( ) ) ;
1831
+ assert_eq ! ( 3.40116213421e+38 , FloatTF32 :: largest( ) . to_f32( ) ) ;
1796
1832
}
1797
1833
1798
1834
#[ test]
@@ -1845,6 +1881,13 @@ fn smallest() {
1845
1881
assert ! ( test. is_finite_non_zero( ) ) ;
1846
1882
assert ! ( test. is_denormal( ) ) ;
1847
1883
assert ! ( test. bitwise_eq( expected) ) ;
1884
+
1885
+ let test = -FloatTF32 :: SMALLEST ;
1886
+ let expected = "-0x0.004p-126" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
1887
+ assert ! ( test. is_negative( ) ) ;
1888
+ assert ! ( test. is_finite_non_zero( ) ) ;
1889
+ assert ! ( test. is_denormal( ) ) ;
1890
+ assert ! ( test. bitwise_eq( expected) ) ;
1848
1891
}
1849
1892
1850
1893
#[ test]
@@ -1920,6 +1963,14 @@ fn smallest_normalized() {
1920
1963
assert ! ( !test. is_denormal( ) ) ;
1921
1964
assert ! ( test. bitwise_eq( expected) ) ;
1922
1965
assert ! ( test. is_smallest_normalized( ) ) ;
1966
+
1967
+ let test = FloatTF32 :: smallest_normalized ( ) ;
1968
+ let expected = "0x1p-126" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
1969
+ assert ! ( !test. is_negative( ) ) ;
1970
+ assert ! ( test. is_finite_non_zero( ) ) ;
1971
+ assert ! ( !test. is_denormal( ) ) ;
1972
+ assert ! ( test. bitwise_eq( expected) ) ;
1973
+ assert ! ( test. is_smallest_normalized( ) ) ;
1923
1974
}
1924
1975
1925
1976
#[ test]
@@ -1961,6 +2012,8 @@ fn zero() {
1961
2012
test :: < Float8E4M3FNUZ > ( true , false , 0 ) ;
1962
2013
test :: < Float8E4M3B11FNUZ > ( false , false , 0 ) ;
1963
2014
test :: < Float8E4M3B11FNUZ > ( true , false , 0 ) ;
2015
+ test :: < FloatTF32 > ( false , true , 0 ) ;
2016
+ test :: < FloatTF32 > ( true , true , 0x40000 ) ;
1964
2017
}
1965
2018
1966
2019
#[ test]
@@ -5476,6 +5529,32 @@ fn float8e4m3fnuz_to_f64() {
5476
5529
assert ! ( qnan. to_f64( ) . is_nan( ) ) ;
5477
5530
}
5478
5531
5532
+ #[ test]
5533
+ fn float_tf32_to_f64 ( ) {
5534
+ let one = "1.0" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
5535
+ assert_eq ! ( 1.0 , one. to_f64( ) ) ;
5536
+ let pos_largest = FloatTF32 :: largest ( ) ;
5537
+ assert_eq ! ( 3.401162134214653489792616e+38 , pos_largest. to_f64( ) ) ;
5538
+ let neg_largest = -FloatTF32 :: largest ( ) ;
5539
+ assert_eq ! ( -3.401162134214653489792616e+38 , neg_largest. to_f64( ) ) ;
5540
+ let pos_smallest = FloatTF32 :: smallest_normalized ( ) ;
5541
+ assert_eq ! ( 1.1754943508222875079687e-38 , pos_smallest. to_f64( ) ) ;
5542
+ let neg_smallest = -FloatTF32 :: smallest_normalized ( ) ;
5543
+ assert_eq ! ( -1.1754943508222875079687e-38 , neg_smallest. to_f64( ) ) ;
5544
+
5545
+ let smallest_denorm = FloatTF32 :: SMALLEST ;
5546
+ assert_eq ! ( 1.1479437019748901445007e-41 , smallest_denorm. to_f64( ) ) ;
5547
+ let largest_denorm = "0x1.FF8p-127" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
5548
+ assert_eq ! ( 1.1743464071203126178242e-38 , largest_denorm. to_f64( ) ) ;
5549
+
5550
+ let pos_inf = FloatTF32 :: INFINITY ;
5551
+ assert_eq ! ( f64 :: INFINITY , pos_inf. to_f64( ) ) ;
5552
+ let neg_inf = -FloatTF32 :: INFINITY ;
5553
+ assert_eq ! ( f64 :: NEG_INFINITY , neg_inf. to_f64( ) ) ;
5554
+ let qnan = FloatTF32 :: NAN ;
5555
+ assert ! ( qnan. to_f64( ) . is_nan( ) ) ;
5556
+ }
5557
+
5479
5558
#[ test]
5480
5559
fn float8e5m2fnuz_to_f32 ( ) {
5481
5560
let pos_zero = Float8E5M2FNUZ :: ZERO ;
@@ -5694,3 +5773,32 @@ fn float8e4m3fn_to_f32() {
5694
5773
let qnan = Float8E4M3FN :: qnan ( None ) ;
5695
5774
assert ! ( qnan. to_f32( ) . is_nan( ) ) ;
5696
5775
}
5776
+
5777
+ #[ test]
5778
+ fn float_tf32_to_f32 ( ) {
5779
+ let pos_zero = FloatTF32 :: ZERO ;
5780
+ assert ! ( Single :: from_f32( pos_zero. to_f32( ) ) . is_pos_zero( ) ) ;
5781
+ let neg_zero = -FloatTF32 :: ZERO ;
5782
+ assert ! ( Single :: from_f32( neg_zero. to_f32( ) ) . is_neg_zero( ) ) ;
5783
+
5784
+ let one = "1.0" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
5785
+ assert_eq ! ( 1.0 , one. to_f32( ) ) ;
5786
+ let two = "2.0" . parse :: < FloatTF32 > ( ) . unwrap ( ) ;
5787
+ assert_eq ! ( 2.0 , two. to_f32( ) ) ;
5788
+
5789
+ let pos_largest = FloatTF32 :: largest ( ) ;
5790
+ assert_eq ! ( 3.40116213421e+38 , pos_largest. to_f32( ) ) ;
5791
+ let neg_largest = -FloatTF32 :: largest ( ) ;
5792
+ assert_eq ! ( -3.40116213421e+38 , neg_largest. to_f32( ) ) ;
5793
+ let pos_smallest = FloatTF32 :: smallest_normalized ( ) ;
5794
+ assert_eq ! ( /*0x1.p-126*/ 1.1754943508222875e-38 , pos_smallest. to_f32( ) ) ;
5795
+ let neg_smallest = -FloatTF32 :: smallest_normalized ( ) ;
5796
+ assert_eq ! ( /*-0x1.p-126*/ -1.1754943508222875e-38 , neg_smallest. to_f32( ) ) ;
5797
+
5798
+ let smallest_denorm = FloatTF32 :: SMALLEST ;
5799
+ assert ! ( smallest_denorm. is_denormal( ) ) ;
5800
+ assert_eq ! ( /*0x0.004p-126*/ 1.148e-41 , smallest_denorm. to_f32( ) ) ;
5801
+
5802
+ let qnan = FloatTF32 :: qnan ( None ) ;
5803
+ assert ! ( qnan. to_f32( ) . is_nan( ) ) ;
5804
+ }
0 commit comments