4 #include <gtest/gtest.h>
9 fp16_int_t make_fp16(unsigned short x)
16 fp32_int_t make_fp32(unsigned int x)
25 TEST(FP16Test, Simple) {
26 EXPECT_EQ(0x0000, fp64_to_fp16(0.0).val);
27 EXPECT_DOUBLE_EQ(0.0, fp16_to_fp64(make_fp16(0x0000)));
29 EXPECT_EQ(0x3c00, fp64_to_fp16(1.0).val);
30 EXPECT_DOUBLE_EQ(1.0, fp16_to_fp64(make_fp16(0x3c00)));
32 EXPECT_EQ(0x3555, fp64_to_fp16(1.0 / 3.0).val);
33 EXPECT_DOUBLE_EQ(0.333251953125, fp16_to_fp64(make_fp16(0x3555)));
36 TEST(FP16Test, RoundToNearestEven) {
37 ASSERT_DOUBLE_EQ(1.0, fp16_to_fp64(make_fp16(0x3c00)));
39 double x0 = fp16_to_fp64(make_fp16(0x3c00));
40 double x1 = fp16_to_fp64(make_fp16(0x3c01));
41 double x2 = fp16_to_fp64(make_fp16(0x3c02));
42 double x3 = fp16_to_fp64(make_fp16(0x3c03));
43 double x4 = fp16_to_fp64(make_fp16(0x3c04));
45 EXPECT_EQ(0x3c00, fp64_to_fp16(0.5 * (x0 + x1)).val);
46 EXPECT_EQ(0x3c02, fp64_to_fp16(0.5 * (x1 + x2)).val);
47 EXPECT_EQ(0x3c02, fp64_to_fp16(0.5 * (x2 + x3)).val);
48 EXPECT_EQ(0x3c04, fp64_to_fp16(0.5 * (x3 + x4)).val);
53 unsigned long long ll;
61 // Ignore the sign bit.
62 EXPECT_EQ(0x7e00, fp64_to_fp16(0.0 / 0.0).val & 0x7fff);
63 EXPECT_TRUE(isnan(fp16_to_fp64(make_fp16(0xfe00))));
66 borderline_inf.ll = 0x7ff0000000000000ull;
68 borderline_nan.ll = 0x7ff0000000000001ull;
70 ASSERT_FALSE(isfinite(borderline_inf.f));
71 ASSERT_FALSE(isnan(borderline_inf.f));
73 ASSERT_FALSE(isfinite(borderline_nan.f));
74 ASSERT_TRUE(isnan(borderline_nan.f));
76 double borderline_inf_roundtrip = fp16_to_fp64(fp64_to_fp16(borderline_inf.f));
77 double borderline_nan_roundtrip = fp16_to_fp64(fp64_to_fp16(borderline_nan.f));
79 EXPECT_FALSE(isfinite(borderline_inf_roundtrip));
80 EXPECT_FALSE(isnan(borderline_inf_roundtrip));
82 EXPECT_FALSE(isfinite(borderline_nan_roundtrip));
83 EXPECT_TRUE(isnan(borderline_nan_roundtrip));
86 TEST(FP16Test, Denormals) {
87 const double smallest_fp16_denormal = 5.9604644775390625e-08;
88 EXPECT_EQ(0x0001, fp64_to_fp16(smallest_fp16_denormal).val);
89 EXPECT_EQ(0x0000, fp64_to_fp16(0.5 * smallest_fp16_denormal).val); // Round-to-even.
90 EXPECT_EQ(0x0001, fp64_to_fp16(0.51 * smallest_fp16_denormal).val);
91 EXPECT_EQ(0x0002, fp64_to_fp16(1.5 * smallest_fp16_denormal).val);
93 const double smallest_fp16_non_denormal = 6.103515625e-05;
94 EXPECT_EQ(0x0400, fp64_to_fp16(smallest_fp16_non_denormal).val);
95 EXPECT_EQ(0x0400, fp64_to_fp16(smallest_fp16_non_denormal - 0.5 * smallest_fp16_denormal).val); // Round-to-even.
96 EXPECT_EQ(0x03ff, fp64_to_fp16(smallest_fp16_non_denormal - smallest_fp16_denormal).val);
99 // Randomly test a large number of fp64 -> fp32 conversions, comparing
101 TEST(FP16Test, FP32ReferenceDownconvert) {
104 for (int i = 0; i < 1000000; ++i) {
105 unsigned r1 = rand();
106 unsigned r2 = rand();
107 unsigned r3 = rand();
109 union fp32 reference, result;
111 src.ll = (((unsigned long long)r1) << 33) ^ ((unsigned long long)r2 << 16) ^ r3;
112 reference.f = float(src.f);
113 result.u = fp64_to_fp32(src.f).val;
115 EXPECT_EQ(isnan(result.f), isnan(reference.f));
116 if (!isnan(result.f)) {
117 EXPECT_EQ(result.u, reference.u)
118 << src.f << " got rounded to " << result.u << " (" << result.f << ")";
123 // Randomly test a large number of fp32 -> fp64 conversions, comparing
125 TEST(FP16Test, FP32ReferenceUpconvert) {
128 for (int i = 0; i < 1000000; ++i) {
129 unsigned r1 = rand();
130 unsigned r2 = rand();
132 union fp64 reference, result;
134 src.u = ((unsigned long long)r1 << 16) ^ r2;
135 reference.f = double(src.f);
136 result.f = fp32_to_fp64(make_fp32(src.u));
138 EXPECT_EQ(isnan(result.f), isnan(reference.f));
139 if (!isnan(result.f)) {
140 EXPECT_EQ(result.ll, reference.ll)
141 << src.f << " got converted to " << result.ll << " (" << result.f << ")";