4 #include <gtest/gtest.h>
8 TEST(FP16Test, Simple) {
9 EXPECT_EQ(0x0000, fp64_to_fp16(0.0));
10 EXPECT_DOUBLE_EQ(0.0, fp16_to_fp64(0x0000));
12 EXPECT_EQ(0x3c00, fp64_to_fp16(1.0));
13 EXPECT_DOUBLE_EQ(1.0, fp16_to_fp64(0x3c00));
15 EXPECT_EQ(0x3555, fp64_to_fp16(1.0 / 3.0));
16 EXPECT_DOUBLE_EQ(0.333251953125, fp16_to_fp64(0x3555));
19 TEST(FP16Test, RoundToNearestEven) {
20 ASSERT_DOUBLE_EQ(1.0, fp16_to_fp64(0x3c00));
22 double x0 = fp16_to_fp64(0x3c00);
23 double x1 = fp16_to_fp64(0x3c01);
24 double x2 = fp16_to_fp64(0x3c02);
25 double x3 = fp16_to_fp64(0x3c03);
26 double x4 = fp16_to_fp64(0x3c04);
28 EXPECT_EQ(0x3c00, fp64_to_fp16(0.5 * (x0 + x1)));
29 EXPECT_EQ(0x3c02, fp64_to_fp16(0.5 * (x1 + x2)));
30 EXPECT_EQ(0x3c02, fp64_to_fp16(0.5 * (x2 + x3)));
31 EXPECT_EQ(0x3c04, fp64_to_fp16(0.5 * (x3 + x4)));
36 unsigned long long ll;
44 // Ignore the sign bit.
45 EXPECT_EQ(0x7e00, fp64_to_fp16(0.0 / 0.0) & 0x7fff);
46 EXPECT_TRUE(isnan(fp16_to_fp64(0xfe00)));
49 borderline_inf.ll = 0x7ff0000000000000ull;
51 borderline_nan.ll = 0x7ff0000000000001ull;
53 ASSERT_FALSE(isfinite(borderline_inf.f));
54 ASSERT_FALSE(isnan(borderline_inf.f));
56 ASSERT_FALSE(isfinite(borderline_nan.f));
57 ASSERT_TRUE(isnan(borderline_nan.f));
59 double borderline_inf_roundtrip = fp16_to_fp64(fp64_to_fp16(borderline_inf.f));
60 double borderline_nan_roundtrip = fp16_to_fp64(fp64_to_fp16(borderline_nan.f));
62 EXPECT_FALSE(isfinite(borderline_inf_roundtrip));
63 EXPECT_FALSE(isnan(borderline_inf_roundtrip));
65 EXPECT_FALSE(isfinite(borderline_nan_roundtrip));
66 EXPECT_TRUE(isnan(borderline_nan_roundtrip));
69 TEST(FP16Test, Denormals) {
70 const double smallest_fp16_denormal = 5.9604644775390625e-08;
71 EXPECT_EQ(0x0001, fp64_to_fp16(smallest_fp16_denormal));
72 EXPECT_EQ(0x0000, fp64_to_fp16(0.5 * smallest_fp16_denormal)); // Round-to-even.
73 EXPECT_EQ(0x0001, fp64_to_fp16(0.51 * smallest_fp16_denormal));
74 EXPECT_EQ(0x0002, fp64_to_fp16(1.5 * smallest_fp16_denormal));
76 const double smallest_fp16_non_denormal = 6.103515625e-05;
77 EXPECT_EQ(0x0400, fp64_to_fp16(smallest_fp16_non_denormal));
78 EXPECT_EQ(0x0400, fp64_to_fp16(smallest_fp16_non_denormal - 0.5 * smallest_fp16_denormal)); // Round-to-even.
79 EXPECT_EQ(0x03ff, fp64_to_fp16(smallest_fp16_non_denormal - smallest_fp16_denormal));
82 // Randomly test a large number of fp64 -> fp32 conversions, comparing
84 TEST(FP16Test, FP32ReferenceDownconvert) {
87 for (int i = 0; i < 1000000; ++i) {
92 union fp32 reference, result;
94 src.ll = (((unsigned long long)r1) << 33) ^ ((unsigned long long)r2 << 16) ^ r3;
95 reference.f = float(src.f);
96 result.u = fp64_to_fp32(src.f);
98 EXPECT_EQ(isnan(result.f), isnan(reference.f));
99 if (!isnan(result.f)) {
100 EXPECT_EQ(result.u, reference.u)
101 << src.f << " got rounded to " << result.u << " (" << result.f << ")";
106 // Randomly test a large number of fp32 -> fp64 conversions, comparing
108 TEST(FP16Test, FP32ReferenceUpconvert) {
111 for (int i = 0; i < 1000000; ++i) {
112 unsigned r1 = rand();
113 unsigned r2 = rand();
115 union fp64 reference, result;
117 src.u = ((unsigned long long)r1 << 16) ^ r2;
118 reference.f = double(src.f);
119 result.f = fp32_to_fp64(src.u);
121 EXPECT_EQ(isnan(result.f), isnan(reference.f));
122 if (!isnan(result.f)) {
123 EXPECT_EQ(result.ll, reference.ll)
124 << src.f << " got converted to " << result.ll << " (" << result.f << ")";