Skip to content

Commit fdb8bc0

Browse files
committed
Port TF32 tests
1 parent 50a208a commit fdb8bc0

File tree

1 file changed

+110
-2
lines changed

1 file changed

+110
-2
lines changed

tests/ieee.rs

+110-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ extern crate rustc_apfloat;
33

44
use core::cmp::Ordering;
55
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,
88
};
99
use rustc_apfloat::{Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO};
1010
use rustc_apfloat::{Float, FloatConvert, Round, Status, StatusAnd};
@@ -35,6 +35,7 @@ define_for_each_float_type! {
3535
Float8E4M3FNUZ,
3636
Float8E5M2FNUZ,
3737
Float8E4M3B11FNUZ,
38+
FloatTF32,
3839
X87DoubleExtended,
3940

4041
// NOTE(eddyb) tests for this are usually in `ppc.rs` but this works too.
@@ -95,6 +96,7 @@ impl ToF32LosslessViaConvertToSingle for Float8E4M3FN {}
9596
impl ToF32LosslessViaConvertToSingle for Float8E4M3FNUZ {}
9697
impl ToF32LosslessViaConvertToSingle for Float8E5M2FNUZ {}
9798
impl ToF32LosslessViaConvertToSingle for Float8E4M3B11FNUZ {}
99+
impl ToF32LosslessViaConvertToSingle for FloatTF32 {}
98100

99101
trait ToF64LosslessViaConvertToDouble: FloatConvert<Double> {
100102
fn to_f64(self) -> f64 {
@@ -754,6 +756,23 @@ fn denormal() {
754756
t /= Quad::from_u128(2).value;
755757
assert!(t.is_denormal());
756758
}
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+
}
757776
}
758777

759778
#[test]
@@ -1421,6 +1440,19 @@ fn nan() {
14211440
(0x80, true, false, 0xaa),
14221441
(0x80, true, true, 0xaa),
14231442
];
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+
];
14241456
for (expected, signaling, negative, payload) in tests_single {
14251457
assert_eq!(expected, nanbits_from_u128::<Single>(signaling, negative, payload));
14261458
}
@@ -1436,6 +1468,9 @@ fn nan() {
14361468
for (expected, signaling, negative, payload) in tests_8e4m3b11fnuz {
14371469
assert_eq!(expected, nanbits_from_u128::<Float8E4M3B11FNUZ>(signaling, negative, payload));
14381470
}
1471+
for (expected, signaling, negative, payload) in test_tf32 {
1472+
assert_eq!(expected, nanbits_from_u128::<FloatTF32>(signaling, negative, payload));
1473+
}
14391474
}
14401475

14411476
#[test]
@@ -1793,6 +1828,7 @@ fn largest() {
17931828
assert_eq!(240.0, Float8E4M3FNUZ::largest().to_f64());
17941829
assert_eq!(57344.0, Float8E5M2FNUZ::largest().to_f64());
17951830
assert_eq!(30.0, Float8E4M3B11FNUZ::largest().to_f64());
1831+
assert_eq!(3.40116213421e+38, FloatTF32::largest().to_f32());
17961832
}
17971833

17981834
#[test]
@@ -1845,6 +1881,13 @@ fn smallest() {
18451881
assert!(test.is_finite_non_zero());
18461882
assert!(test.is_denormal());
18471883
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));
18481891
}
18491892

18501893
#[test]
@@ -1920,6 +1963,14 @@ fn smallest_normalized() {
19201963
assert!(!test.is_denormal());
19211964
assert!(test.bitwise_eq(expected));
19221965
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());
19231974
}
19241975

19251976
#[test]
@@ -1961,6 +2012,8 @@ fn zero() {
19612012
test::<Float8E4M3FNUZ>(true, false, 0);
19622013
test::<Float8E4M3B11FNUZ>(false, false, 0);
19632014
test::<Float8E4M3B11FNUZ>(true, false, 0);
2015+
test::<FloatTF32>(false, true, 0);
2016+
test::<FloatTF32>(true, true, 0x40000);
19642017
}
19652018

19662019
#[test]
@@ -5476,6 +5529,32 @@ fn float8e4m3fnuz_to_f64() {
54765529
assert!(qnan.to_f64().is_nan());
54775530
}
54785531

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+
54795558
#[test]
54805559
fn float8e5m2fnuz_to_f32() {
54815560
let pos_zero = Float8E5M2FNUZ::ZERO;
@@ -5694,3 +5773,32 @@ fn float8e4m3fn_to_f32() {
56945773
let qnan = Float8E4M3FN::qnan(None);
56955774
assert!(qnan.to_f32().is_nan());
56965775
}
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

Comments
 (0)