• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/writer/float_to_string.h"
16 
17 #include <cmath>
18 #include <cstring>
19 #include <limits>
20 
21 #include "gtest/gtest.h"
22 
23 namespace tint {
24 namespace writer {
25 namespace {
26 
27 // Makes an IEEE 754 binary32 floating point number with
28 // - 0 sign if sign is 0, 1 otherwise
29 // - 'exponent_bits' is placed in the exponent space.
30 //   So, the exponent bias must already be included.
MakeFloat(int sign,int biased_exponent,int mantissa)31 float MakeFloat(int sign, int biased_exponent, int mantissa) {
32   const uint32_t sign_bit = sign ? 0x80000000u : 0u;
33   // The binary32 exponent is 8 bits, just below the sign.
34   const uint32_t exponent_bits = (biased_exponent & 0xffu) << 23;
35   // The mantissa is the bottom 23 bits.
36   const uint32_t mantissa_bits = (mantissa & 0x7fffffu);
37 
38   uint32_t bits = sign_bit | exponent_bits | mantissa_bits;
39   float result = 0.0f;
40   static_assert(sizeof(result) == sizeof(bits),
41                 "expected float and uint32_t to be the same size");
42   std::memcpy(&result, &bits, sizeof(bits));
43   return result;
44 }
45 
TEST(FloatToStringTest,Zero)46 TEST(FloatToStringTest, Zero) {
47   EXPECT_EQ(FloatToString(0.0f), "0.0");
48 }
49 
TEST(FloatToStringTest,One)50 TEST(FloatToStringTest, One) {
51   EXPECT_EQ(FloatToString(1.0f), "1.0");
52 }
53 
TEST(FloatToStringTest,MinusOne)54 TEST(FloatToStringTest, MinusOne) {
55   EXPECT_EQ(FloatToString(-1.0f), "-1.0");
56 }
57 
TEST(FloatToStringTest,Billion)58 TEST(FloatToStringTest, Billion) {
59   EXPECT_EQ(FloatToString(1e9f), "1000000000.0");
60 }
61 
TEST(FloatToStringTest,Small)62 TEST(FloatToStringTest, Small) {
63   EXPECT_NE(FloatToString(std::numeric_limits<float>::epsilon()), "0.0");
64 }
65 
TEST(FloatToStringTest,Highest)66 TEST(FloatToStringTest, Highest) {
67   const auto highest = std::numeric_limits<float>::max();
68   const auto expected_highest = 340282346638528859811704183484516925440.0f;
69   if (highest < expected_highest || highest > expected_highest) {
70     GTEST_SKIP() << "std::numeric_limits<float>::max() is not as expected for "
71                     "this target";
72   }
73   EXPECT_EQ(FloatToString(std::numeric_limits<float>::max()),
74             "340282346638528859811704183484516925440.0");
75 }
76 
TEST(FloatToStringTest,Lowest)77 TEST(FloatToStringTest, Lowest) {
78   // Some compilers complain if you test floating point numbers for equality.
79   // So say it via two inequalities.
80   const auto lowest = std::numeric_limits<float>::lowest();
81   const auto expected_lowest = -340282346638528859811704183484516925440.0f;
82   if (lowest < expected_lowest || lowest > expected_lowest) {
83     GTEST_SKIP()
84         << "std::numeric_limits<float>::lowest() is not as expected for "
85            "this target";
86   }
87   EXPECT_EQ(FloatToString(std::numeric_limits<float>::lowest()),
88             "-340282346638528859811704183484516925440.0");
89 }
90 
TEST(FloatToStringTest,Precision)91 TEST(FloatToStringTest, Precision) {
92   EXPECT_EQ(FloatToString(1e-8f), "0.00000001");
93   EXPECT_EQ(FloatToString(1e-9f), "0.000000001");
94   EXPECT_EQ(FloatToString(1e-10f), "1.00000001e-10");
95   EXPECT_EQ(FloatToString(1e-20f), "9.99999968e-21");
96 }
97 
98 // FloatToBitPreservingString
99 //
100 // First replicate the tests for FloatToString
101 
TEST(FloatToBitPreservingStringTest,Zero)102 TEST(FloatToBitPreservingStringTest, Zero) {
103   EXPECT_EQ(FloatToBitPreservingString(0.0f), "0.0");
104 }
105 
TEST(FloatToBitPreservingStringTest,One)106 TEST(FloatToBitPreservingStringTest, One) {
107   EXPECT_EQ(FloatToBitPreservingString(1.0f), "1.0");
108 }
109 
TEST(FloatToBitPreservingStringTest,MinusOne)110 TEST(FloatToBitPreservingStringTest, MinusOne) {
111   EXPECT_EQ(FloatToBitPreservingString(-1.0f), "-1.0");
112 }
113 
TEST(FloatToBitPreservingStringTest,Billion)114 TEST(FloatToBitPreservingStringTest, Billion) {
115   EXPECT_EQ(FloatToBitPreservingString(1e9f), "1000000000.0");
116 }
117 
TEST(FloatToBitPreservingStringTest,Small)118 TEST(FloatToBitPreservingStringTest, Small) {
119   EXPECT_NE(FloatToBitPreservingString(std::numeric_limits<float>::epsilon()),
120             "0.0");
121 }
122 
TEST(FloatToBitPreservingStringTest,Highest)123 TEST(FloatToBitPreservingStringTest, Highest) {
124   const auto highest = std::numeric_limits<float>::max();
125   const auto expected_highest = 340282346638528859811704183484516925440.0f;
126   if (highest < expected_highest || highest > expected_highest) {
127     GTEST_SKIP() << "std::numeric_limits<float>::max() is not as expected for "
128                     "this target";
129   }
130   EXPECT_EQ(FloatToBitPreservingString(std::numeric_limits<float>::max()),
131             "340282346638528859811704183484516925440.0");
132 }
133 
TEST(FloatToBitPreservingStringTest,Lowest)134 TEST(FloatToBitPreservingStringTest, Lowest) {
135   // Some compilers complain if you test floating point numbers for equality.
136   // So say it via two inequalities.
137   const auto lowest = std::numeric_limits<float>::lowest();
138   const auto expected_lowest = -340282346638528859811704183484516925440.0f;
139   if (lowest < expected_lowest || lowest > expected_lowest) {
140     GTEST_SKIP()
141         << "std::numeric_limits<float>::lowest() is not as expected for "
142            "this target";
143   }
144   EXPECT_EQ(FloatToBitPreservingString(std::numeric_limits<float>::lowest()),
145             "-340282346638528859811704183484516925440.0");
146 }
147 
148 // Special cases for bit-preserving output.
149 
TEST(FloatToBitPreservingStringTest,NegativeZero)150 TEST(FloatToBitPreservingStringTest, NegativeZero) {
151   EXPECT_EQ(FloatToBitPreservingString(std::copysign(0.0f, -5.0f)), "-0.0");
152 }
153 
TEST(FloatToBitPreservingStringTest,ZeroAsBits)154 TEST(FloatToBitPreservingStringTest, ZeroAsBits) {
155   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0)), "0.0");
156   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 0)), "-0.0");
157 }
158 
TEST(FloatToBitPreservingStringTest,OneBits)159 TEST(FloatToBitPreservingStringTest, OneBits) {
160   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 127, 0)), "1.0");
161   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 127, 0)), "-1.0");
162 }
163 
TEST(FloatToBitPreservingStringTest,SmallestDenormal)164 TEST(FloatToBitPreservingStringTest, SmallestDenormal) {
165   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 1)), "0x1p-149");
166   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 1)), "-0x1p-149");
167 }
168 
TEST(FloatToBitPreservingStringTest,BiggerDenormal)169 TEST(FloatToBitPreservingStringTest, BiggerDenormal) {
170   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 2)), "0x1p-148");
171   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 2)), "-0x1p-148");
172 }
173 
TEST(FloatToBitPreservingStringTest,LargestDenormal)174 TEST(FloatToBitPreservingStringTest, LargestDenormal) {
175   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0x7fffff)),
176             "0x1.fffffcp-127");
177 }
178 
TEST(FloatToBitPreservingStringTest,Subnormal_cafebe)179 TEST(FloatToBitPreservingStringTest, Subnormal_cafebe) {
180   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0xcafebe)),
181             "0x1.2bfaf8p-127");
182   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 0xcafebe)),
183             "-0x1.2bfaf8p-127");
184 }
185 
TEST(FloatToBitPreservingStringTest,Subnormal_aaaaa)186 TEST(FloatToBitPreservingStringTest, Subnormal_aaaaa) {
187   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 0, 0xaaaaa)),
188             "0x1.55554p-130");
189   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 0, 0xaaaaa)),
190             "-0x1.55554p-130");
191 }
192 
TEST(FloatToBitPreservingStringTest,Infinity)193 TEST(FloatToBitPreservingStringTest, Infinity) {
194   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0)), "0x1p+128");
195   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0)), "-0x1p+128");
196 }
197 
198 // TODO(dneto): It's unclear how Infinity and NaN should be handled.
199 // https://github.com/gpuweb/gpuweb/issues/1769
200 // Windows x86-64 sets the high mantissa bit on NaNs.
201 // Disable NaN tests for now.
202 
TEST(FloatToBitPreservingStringTest,DISABLED_NaN_MsbOnly)203 TEST(FloatToBitPreservingStringTest, DISABLED_NaN_MsbOnly) {
204   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0x400000)),
205             "0x1.8p+128");
206   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0x400000)),
207             "-0x1.8p+128");
208 }
209 
TEST(FloatToBitPreservingStringTest,DISABLED_NaN_LsbOnly)210 TEST(FloatToBitPreservingStringTest, DISABLED_NaN_LsbOnly) {
211   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0x1)),
212             "0x1.000002p+128");
213   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0x1)),
214             "-0x1.000002p+128");
215 }
216 
TEST(FloatToBitPreservingStringTest,DISABLED_NaN_NonMsb)217 TEST(FloatToBitPreservingStringTest, DISABLED_NaN_NonMsb) {
218   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(0, 255, 0x20101f)),
219             "0x1.40203ep+128");
220   EXPECT_EQ(FloatToBitPreservingString(MakeFloat(1, 255, 0x20101f)),
221             "-0x1.40203ep+128");
222 }
223 
224 }  // namespace
225 }  // namespace writer
226 }  // namespace tint
227