Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / __support / FPUtil / fpbits_test.cpp
blob916523e2826693595968701c561d1349d8b6bccc
1 //===-- Unittests for the DyadicFloat class -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/FPUtil/FPBits.h"
10 #include "src/__support/FPUtil/fpbits_str.h"
11 #include "test/UnitTest/Test.h"
13 using LIBC_NAMESPACE::fputil::FPBits;
15 TEST(LlvmLibcFPBitsTest, FloatType) {
16 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<float>::inf()).c_str(),
17 "(+Infinity)");
18 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<float>::neg_inf()).c_str(),
19 "(-Infinity)");
20 EXPECT_STREQ(
21 LIBC_NAMESPACE::str(FPBits<float>(FPBits<float>::build_nan(1))).c_str(),
22 "(NaN)");
24 FPBits<float> zero(0.0f);
25 EXPECT_EQ(zero.get_sign(), false);
26 EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0));
27 EXPECT_EQ(zero.get_mantissa(), static_cast<uint32_t>(0));
28 EXPECT_EQ(zero.uintval(), static_cast<uint32_t>(0x00000000));
29 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
30 "0x00000000 = (S: 0, E: 0x0000, M: 0x00000000)");
32 FPBits<float> negzero(-0.0f);
33 EXPECT_EQ(negzero.get_sign(), true);
34 EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0));
35 EXPECT_EQ(negzero.get_mantissa(), static_cast<uint32_t>(0));
36 EXPECT_EQ(negzero.uintval(), static_cast<uint32_t>(0x80000000));
37 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
38 "0x80000000 = (S: 1, E: 0x0000, M: 0x00000000)");
40 FPBits<float> one(1.0f);
41 EXPECT_EQ(one.get_sign(), false);
42 EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
43 EXPECT_EQ(one.get_mantissa(), static_cast<uint32_t>(0));
44 EXPECT_EQ(one.uintval(), static_cast<uint32_t>(0x3F800000));
45 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
46 "0x3F800000 = (S: 0, E: 0x007F, M: 0x00000000)");
48 FPBits<float> negone(-1.0f);
49 EXPECT_EQ(negone.get_sign(), true);
50 EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
51 EXPECT_EQ(negone.get_mantissa(), static_cast<uint32_t>(0));
52 EXPECT_EQ(negone.uintval(), static_cast<uint32_t>(0xBF800000));
53 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
54 "0xBF800000 = (S: 1, E: 0x007F, M: 0x00000000)");
56 FPBits<float> num(1.125f);
57 EXPECT_EQ(num.get_sign(), false);
58 EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
59 EXPECT_EQ(num.get_mantissa(), static_cast<uint32_t>(0x00100000));
60 EXPECT_EQ(num.uintval(), static_cast<uint32_t>(0x3F900000));
61 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
62 "0x3F900000 = (S: 0, E: 0x007F, M: 0x00100000)");
64 FPBits<float> negnum(-1.125f);
65 EXPECT_EQ(negnum.get_sign(), true);
66 EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
67 EXPECT_EQ(negnum.get_mantissa(), static_cast<uint32_t>(0x00100000));
68 EXPECT_EQ(negnum.uintval(), static_cast<uint32_t>(0xBF900000));
69 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
70 "0xBF900000 = (S: 1, E: 0x007F, M: 0x00100000)");
73 TEST(LlvmLibcFPBitsTest, DoubleType) {
74 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<double>::inf()).c_str(),
75 "(+Infinity)");
76 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<double>::neg_inf()).c_str(),
77 "(-Infinity)");
78 EXPECT_STREQ(
79 LIBC_NAMESPACE::str(FPBits<double>(FPBits<double>::build_nan(1))).c_str(),
80 "(NaN)");
82 FPBits<double> zero(0.0);
83 EXPECT_EQ(zero.get_sign(), false);
84 EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
85 EXPECT_EQ(zero.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
86 EXPECT_EQ(zero.uintval(), static_cast<uint64_t>(0x0000000000000000));
87 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
88 "0x0000000000000000 = (S: 0, E: 0x0000, M: 0x0000000000000000)");
90 FPBits<double> negzero(-0.0);
91 EXPECT_EQ(negzero.get_sign(), true);
92 EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
93 EXPECT_EQ(negzero.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
94 EXPECT_EQ(negzero.uintval(), static_cast<uint64_t>(0x8000000000000000));
95 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
96 "0x8000000000000000 = (S: 1, E: 0x0000, M: 0x0000000000000000)");
98 FPBits<double> one(1.0);
99 EXPECT_EQ(one.get_sign(), false);
100 EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
101 EXPECT_EQ(one.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
102 EXPECT_EQ(one.uintval(), static_cast<uint64_t>(0x3FF0000000000000));
103 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
104 "0x3FF0000000000000 = (S: 0, E: 0x03FF, M: 0x0000000000000000)");
106 FPBits<double> negone(-1.0);
107 EXPECT_EQ(negone.get_sign(), true);
108 EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
109 EXPECT_EQ(negone.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
110 EXPECT_EQ(negone.uintval(), static_cast<uint64_t>(0xBFF0000000000000));
111 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
112 "0xBFF0000000000000 = (S: 1, E: 0x03FF, M: 0x0000000000000000)");
114 FPBits<double> num(1.125);
115 EXPECT_EQ(num.get_sign(), false);
116 EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
117 EXPECT_EQ(num.get_mantissa(), static_cast<uint64_t>(0x0002000000000000));
118 EXPECT_EQ(num.uintval(), static_cast<uint64_t>(0x3FF2000000000000));
119 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
120 "0x3FF2000000000000 = (S: 0, E: 0x03FF, M: 0x0002000000000000)");
122 FPBits<double> negnum(-1.125);
123 EXPECT_EQ(negnum.get_sign(), true);
124 EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
125 EXPECT_EQ(negnum.get_mantissa(), static_cast<uint64_t>(0x0002000000000000));
126 EXPECT_EQ(negnum.uintval(), static_cast<uint64_t>(0xBFF2000000000000));
127 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
128 "0xBFF2000000000000 = (S: 1, E: 0x03FF, M: 0x0002000000000000)");
131 #ifdef LIBC_TARGET_ARCH_IS_X86
132 TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {
133 if constexpr (sizeof(long double) == sizeof(double))
134 return; // The tests for the "double" type cover for this case.
136 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<long double>::inf()).c_str(),
137 "(+Infinity)");
138 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<long double>::neg_inf()).c_str(),
139 "(-Infinity)");
140 EXPECT_STREQ(LIBC_NAMESPACE::str(
141 FPBits<long double>(FPBits<long double>::build_nan(1)))
142 .c_str(),
143 "(NaN)");
145 FPBits<long double> zero(0.0l);
146 EXPECT_EQ(zero.get_sign(), false);
147 EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
148 EXPECT_EQ(zero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
149 << 64);
150 EXPECT_EQ(zero.uintval(), static_cast<UInt128>(0x0000000000000000) << 64);
151 EXPECT_STREQ(
152 LIBC_NAMESPACE::str(zero).c_str(),
153 "0x00000000000000000000000000000000 = "
154 "(S: 0, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");
156 FPBits<long double> negzero(-0.0l);
157 EXPECT_EQ(negzero.get_sign(), true);
158 EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
159 EXPECT_EQ(negzero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
160 << 64);
161 EXPECT_EQ(negzero.uintval(), static_cast<UInt128>(0x1) << 79);
162 EXPECT_STREQ(
163 LIBC_NAMESPACE::str(negzero).c_str(),
164 "0x00000000000080000000000000000000 = "
165 "(S: 1, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");
167 FPBits<long double> one(1.0l);
168 EXPECT_EQ(one.get_sign(), false);
169 EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
170 EXPECT_EQ(one.get_mantissa(), static_cast<UInt128>(0x0000000000000000) << 64);
171 EXPECT_EQ(one.uintval(), static_cast<UInt128>(0x3FFF8) << 60);
172 EXPECT_STREQ(
173 LIBC_NAMESPACE::str(one).c_str(),
174 "0x0000000000003FFF8000000000000000 = "
175 "(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");
177 FPBits<long double> negone(-1.0l);
178 EXPECT_EQ(negone.get_sign(), true);
179 EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
180 EXPECT_EQ(negone.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
181 << 64);
182 EXPECT_EQ(negone.uintval(), static_cast<UInt128>(0xBFFF8) << 60);
183 EXPECT_STREQ(
184 LIBC_NAMESPACE::str(negone).c_str(),
185 "0x000000000000BFFF8000000000000000 = "
186 "(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");
188 FPBits<long double> num(1.125l);
189 EXPECT_EQ(num.get_sign(), false);
190 EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
191 EXPECT_EQ(num.get_mantissa(), static_cast<UInt128>(0x1) << 60);
192 EXPECT_EQ(num.uintval(), static_cast<UInt128>(0x3FFF9) << 60);
193 EXPECT_STREQ(
194 LIBC_NAMESPACE::str(num).c_str(),
195 "0x0000000000003FFF9000000000000000 = "
196 "(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
198 FPBits<long double> negnum(-1.125l);
199 EXPECT_EQ(negnum.get_sign(), true);
200 EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
201 EXPECT_EQ(negnum.get_mantissa(), static_cast<UInt128>(0x1) << 60);
202 EXPECT_EQ(negnum.uintval(), static_cast<UInt128>(0xBFFF9) << 60);
203 EXPECT_STREQ(
204 LIBC_NAMESPACE::str(negnum).c_str(),
205 "0x000000000000BFFF9000000000000000 = "
206 "(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
208 #else
209 TEST(LlvmLibcFPBitsTest, LongDoubleType) {
210 #if defined(LONG_DOUBLE_IS_DOUBLE)
211 return; // The tests for the "double" type cover for this case.
212 #else
213 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<long double>::inf()).c_str(),
214 "(+Infinity)");
215 EXPECT_STREQ(LIBC_NAMESPACE::str(FPBits<long double>::neg_inf()).c_str(),
216 "(-Infinity)");
217 EXPECT_STREQ(LIBC_NAMESPACE::str(
218 FPBits<long double>(FPBits<long double>::build_nan(1)))
219 .c_str(),
220 "(NaN)");
222 FPBits<long double> zero(0.0l);
223 EXPECT_EQ(zero.get_sign(), false);
224 EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
225 EXPECT_EQ(zero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
226 << 64);
227 EXPECT_EQ(zero.uintval(), static_cast<UInt128>(0x0000000000000000) << 64);
228 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
229 "0x00000000000000000000000000000000 = "
230 "(S: 0, E: 0x0000, M: 0x00000000000000000000000000000000)");
232 FPBits<long double> negzero(-0.0l);
233 EXPECT_EQ(negzero.get_sign(), true);
234 EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
235 EXPECT_EQ(negzero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
236 << 64);
237 EXPECT_EQ(negzero.uintval(), static_cast<UInt128>(0x1) << 127);
238 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
239 "0x80000000000000000000000000000000 = "
240 "(S: 1, E: 0x0000, M: 0x00000000000000000000000000000000)");
242 FPBits<long double> one(1.0l);
243 EXPECT_EQ(one.get_sign(), false);
244 EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
245 EXPECT_EQ(one.get_mantissa(), static_cast<UInt128>(0x0000000000000000) << 64);
246 EXPECT_EQ(one.uintval(), static_cast<UInt128>(0x3FFF) << 112);
247 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
248 "0x3FFF0000000000000000000000000000 = "
249 "(S: 0, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
251 FPBits<long double> negone(-1.0l);
252 EXPECT_EQ(negone.get_sign(), true);
253 EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
254 EXPECT_EQ(negone.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
255 << 64);
256 EXPECT_EQ(negone.uintval(), static_cast<UInt128>(0xBFFF) << 112);
257 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
258 "0xBFFF0000000000000000000000000000 = "
259 "(S: 1, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
261 FPBits<long double> num(1.125l);
262 EXPECT_EQ(num.get_sign(), false);
263 EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
264 EXPECT_EQ(num.get_mantissa(), static_cast<UInt128>(0x2) << 108);
265 EXPECT_EQ(num.uintval(), static_cast<UInt128>(0x3FFF2) << 108);
266 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
267 "0x3FFF2000000000000000000000000000 = "
268 "(S: 0, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
270 FPBits<long double> negnum(-1.125l);
271 EXPECT_EQ(negnum.get_sign(), true);
272 EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
273 EXPECT_EQ(negnum.get_mantissa(), static_cast<UInt128>(0x2) << 108);
274 EXPECT_EQ(negnum.uintval(), static_cast<UInt128>(0xBFFF2) << 108);
275 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
276 "0xBFFF2000000000000000000000000000 = "
277 "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
278 #endif
280 #endif