[RISCV][NFC] Remove Redundant Inline Asm Logic (#124202)
[llvm-project.git] / libc / test / src / __support / FPUtil / fpbits_test.cpp
blob6953d3aace58f395d1ab5cac87f74a96560fd58d
1 //===-- Unittests for the FPBits 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 "src/__support/big_int.h"
12 #include "src/__support/integer_literals.h"
13 #include "src/__support/macros/properties/types.h"
14 #include "src/__support/sign.h" // Sign
15 #include "test/UnitTest/Test.h"
17 using LIBC_NAMESPACE::Sign;
18 using LIBC_NAMESPACE::UInt;
19 using LIBC_NAMESPACE::fputil::FPBits;
20 using LIBC_NAMESPACE::fputil::FPType;
21 using LIBC_NAMESPACE::fputil::internal::FPRep;
23 using LIBC_NAMESPACE::operator""_u16;
24 using LIBC_NAMESPACE::operator""_u32;
25 using LIBC_NAMESPACE::operator""_u64;
26 using LIBC_NAMESPACE::operator""_u96;
27 using LIBC_NAMESPACE::operator""_u128;
29 TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary16) {
30 using Rep = FPRep<FPType::IEEE754_Binary16>;
31 using u16 = typename Rep::StorageType;
33 EXPECT_EQ(0b0'00000'0000000000_u16, u16(Rep::zero()));
34 EXPECT_EQ(0b0'01111'0000000000_u16, u16(Rep::one()));
35 EXPECT_EQ(0b0'00000'0000000001_u16, u16(Rep::min_subnormal()));
36 EXPECT_EQ(0b0'00000'1111111111_u16, u16(Rep::max_subnormal()));
37 EXPECT_EQ(0b0'00001'0000000000_u16, u16(Rep::min_normal()));
38 EXPECT_EQ(0b0'11110'1111111111_u16, u16(Rep::max_normal()));
39 EXPECT_EQ(0b0'11111'0000000000_u16, u16(Rep::inf()));
40 EXPECT_EQ(0b0'11111'0100000000_u16, u16(Rep::signaling_nan()));
41 EXPECT_EQ(0b0'11111'1000000000_u16, u16(Rep::quiet_nan()));
44 TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary32) {
45 using Rep = FPRep<FPType::IEEE754_Binary32>;
46 using u32 = typename Rep::StorageType;
48 EXPECT_EQ(0b0'00000000'00000000000000000000000_u32, u32(Rep::zero()));
49 EXPECT_EQ(0b0'01111111'00000000000000000000000_u32, u32(Rep::one()));
50 EXPECT_EQ(0b0'00000000'00000000000000000000001_u32,
51 u32(Rep::min_subnormal()));
52 EXPECT_EQ(0b0'00000000'11111111111111111111111_u32,
53 u32(Rep::max_subnormal()));
54 EXPECT_EQ(0b0'00000001'00000000000000000000000_u32, u32(Rep::min_normal()));
55 EXPECT_EQ(0b0'11111110'11111111111111111111111_u32, u32(Rep::max_normal()));
56 EXPECT_EQ(0b0'11111111'00000000000000000000000_u32, u32(Rep::inf()));
57 EXPECT_EQ(0b0'11111111'01000000000000000000000_u32,
58 u32(Rep::signaling_nan()));
59 EXPECT_EQ(0b0'11111111'10000000000000000000000_u32, u32(Rep::quiet_nan()));
62 TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary64) {
63 using Rep = FPRep<FPType::IEEE754_Binary64>;
64 using u64 = typename Rep::StorageType;
66 EXPECT_EQ(
67 0b0'00000000000'0000000000000000000000000000000000000000000000000000_u64,
68 u64(Rep::zero()));
69 EXPECT_EQ(
70 0b0'01111111111'0000000000000000000000000000000000000000000000000000_u64,
71 u64(Rep::one()));
72 EXPECT_EQ(
73 0b0'00000000000'0000000000000000000000000000000000000000000000000001_u64,
74 u64(Rep::min_subnormal()));
75 EXPECT_EQ(
76 0b0'00000000000'1111111111111111111111111111111111111111111111111111_u64,
77 u64(Rep::max_subnormal()));
78 EXPECT_EQ(
79 0b0'00000000001'0000000000000000000000000000000000000000000000000000_u64,
80 u64(Rep::min_normal()));
81 EXPECT_EQ(
82 0b0'11111111110'1111111111111111111111111111111111111111111111111111_u64,
83 u64(Rep::max_normal()));
84 EXPECT_EQ(
85 0b0'11111111111'0000000000000000000000000000000000000000000000000000_u64,
86 u64(Rep::inf()));
87 EXPECT_EQ(
88 0b0'11111111111'0100000000000000000000000000000000000000000000000000_u64,
89 u64(Rep::signaling_nan()));
90 EXPECT_EQ(
91 0b0'11111111111'1000000000000000000000000000000000000000000000000000_u64,
92 u64(Rep::quiet_nan()));
95 TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary128) {
96 using Rep = FPRep<FPType::IEEE754_Binary128>;
98 EXPECT_EQ(
99 0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
100 UInt128(Rep::zero()));
101 EXPECT_EQ(
102 0b0'011111111111111'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
103 UInt128(Rep::one()));
104 EXPECT_EQ(
105 0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001_u128,
106 UInt128(Rep::min_subnormal()));
107 EXPECT_EQ(
108 0b0'000000000000000'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111_u128,
109 UInt128(Rep::max_subnormal()));
110 EXPECT_EQ(
111 0b0'000000000000001'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
112 UInt128(Rep::min_normal()));
113 EXPECT_EQ(
114 0b0'111111111111110'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111_u128,
115 UInt128(Rep::max_normal()));
116 EXPECT_EQ(
117 0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
118 UInt128(Rep::inf()));
119 EXPECT_EQ(
120 0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
121 UInt128(Rep::signaling_nan()));
122 EXPECT_EQ(
123 0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
124 UInt128(Rep::quiet_nan()));
127 #ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
128 TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80) {
129 using Rep = FPRep<FPType::X86_Binary80>;
131 #if __SIZEOF_LONG_DOUBLE__ == 16
132 EXPECT_EQ(
133 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000000_u128,
134 UInt128(Rep::zero()));
135 EXPECT_EQ(
136 0b0'0111111111111111000000000000000000000000000000000000000000000000000000000000000_u128,
137 UInt128(Rep::one()));
138 EXPECT_EQ(
139 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000001_u128,
140 UInt128(Rep::min_subnormal()));
141 EXPECT_EQ(
142 0b0'0000000000000000111111111111111111111111111111111111111111111111111111111111111_u128,
143 UInt128(Rep::max_subnormal()));
144 EXPECT_EQ(
145 0b0'0000000000000011000000000000000000000000000000000000000000000000000000000000000_u128,
146 UInt128(Rep::min_normal()));
147 EXPECT_EQ(
148 0b0'1111111111111101111111111111111111111111111111111111111111111111111111111111111_u128,
149 UInt128(Rep::max_normal()));
150 EXPECT_EQ(
151 0b0'1111111111111111000000000000000000000000000000000000000000000000000000000000000_u128,
152 UInt128(Rep::inf()));
153 EXPECT_EQ(
154 0b0'1111111111111111010000000000000000000000000000000000000000000000000000000000000_u128,
155 UInt128(Rep::signaling_nan()));
156 EXPECT_EQ(
157 0b0'1111111111111111100000000000000000000000000000000000000000000000000000000000000_u128,
158 UInt128(Rep::quiet_nan()));
159 #elif __SIZEOF_LONG_DOUBLE__ == 12
160 EXPECT_EQ(
161 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000000_u96,
162 UInt<96>(Rep::zero()));
163 EXPECT_EQ(
164 0b0'0111111111111111000000000000000000000000000000000000000000000000000000000000000_u96,
165 UInt<96>(Rep::one()));
166 EXPECT_EQ(
167 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000001_u96,
168 UInt<96>(Rep::min_subnormal()));
169 EXPECT_EQ(
170 0b0'0000000000000000111111111111111111111111111111111111111111111111111111111111111_u96,
171 UInt<96>(Rep::max_subnormal()));
172 EXPECT_EQ(
173 0b0'0000000000000011000000000000000000000000000000000000000000000000000000000000000_u96,
174 UInt<96>(Rep::min_normal()));
175 EXPECT_EQ(
176 0b0'1111111111111101111111111111111111111111111111111111111111111111111111111111111_u96,
177 UInt<96>(Rep::max_normal()));
178 EXPECT_EQ(
179 0b0'1111111111111111000000000000000000000000000000000000000000000000000000000000000_u96,
180 UInt<96>(Rep::inf()));
181 EXPECT_EQ(
182 0b0'1111111111111111010000000000000000000000000000000000000000000000000000000000000_u96,
183 UInt<96>(Rep::signaling_nan()));
184 EXPECT_EQ(
185 0b0'1111111111111111100000000000000000000000000000000000000000000000000000000000000_u96,
186 UInt<96>(Rep::quiet_nan()));
187 #else
188 #error "unhandled long double type"
189 #endif
192 TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80_IsNan) {
193 using Rep = FPRep<FPType::X86_Binary80>;
195 #if __SIZEOF_LONG_DOUBLE__ == 16
196 EXPECT_TRUE( // NAN : Pseudo-Infinity
197 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000000_u128)
198 .is_nan());
199 EXPECT_TRUE( // NAN : Pseudo Not a Number
200 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000001_u128)
201 .is_nan());
202 EXPECT_TRUE( // NAN : Pseudo Not a Number
203 Rep(0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000_u128)
204 .is_nan());
205 EXPECT_TRUE( // NAN : Signalling Not a Number
206 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000001_u128)
207 .is_nan());
208 EXPECT_TRUE( // NAN : Floating-point Indefinite
209 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000000_u128)
210 .is_nan());
211 EXPECT_TRUE( // NAN : Quiet Not a Number
212 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000001_u128)
213 .is_nan());
214 EXPECT_TRUE( // NAN : Unnormal
215 Rep(0b0'111111111111110'0000000000000000000000000000000000000000000000000000000000000000_u128)
216 .is_nan());
217 EXPECT_FALSE( // Zero
218 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000_u128)
219 .is_nan());
220 EXPECT_FALSE( // Subnormal
221 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000001_u128)
222 .is_nan());
223 EXPECT_FALSE( // Pseudo Denormal
224 Rep(0b0'000000000000000'1000000000000000000000000000000000000000000000000000000000000001_u128)
225 .is_nan());
226 EXPECT_FALSE( // Infinity
227 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000000_u128)
228 .is_nan());
229 EXPECT_FALSE( // Normalized
230 Rep(0b0'111111111111110'1000000000000000000000000000000000000000000000000000000000000000_u128)
231 .is_nan());
232 #elif __SIZEOF_LONG_DOUBLE__ == 12
233 EXPECT_TRUE( // NAN : Pseudo-Infinity
234 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000000_u96)
235 .is_nan());
236 EXPECT_TRUE( // NAN : Pseudo Not a Number
237 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000001_u96)
238 .is_nan());
239 EXPECT_TRUE( // NAN : Pseudo Not a Number
240 Rep(0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000_u96)
241 .is_nan());
242 EXPECT_TRUE( // NAN : Signalling Not a Number
243 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000001_u96)
244 .is_nan());
245 EXPECT_TRUE( // NAN : Floating-point Indefinite
246 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000000_u96)
247 .is_nan());
248 EXPECT_TRUE( // NAN : Quiet Not a Number
249 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000001_u96)
250 .is_nan());
251 EXPECT_TRUE( // NAN : Unnormal
252 Rep(0b0'111111111111110'0000000000000000000000000000000000000000000000000000000000000000_u96)
253 .is_nan());
254 EXPECT_FALSE( // Zero
255 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000_u96)
256 .is_nan());
257 EXPECT_FALSE( // Subnormal
258 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000001_u96)
259 .is_nan());
260 EXPECT_FALSE( // Pseudo Denormal
261 Rep(0b0'000000000000000'1000000000000000000000000000000000000000000000000000000000000001_u96)
262 .is_nan());
263 EXPECT_FALSE( // Infinity
264 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000000_u96)
265 .is_nan());
266 EXPECT_FALSE( // Normalized
267 Rep(0b0'111111111111110'1000000000000000000000000000000000000000000000000000000000000000_u96)
268 .is_nan());
269 #else
270 #error "unhandled long double type"
271 #endif
273 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
275 enum class FP {
276 ZERO,
277 MIN_SUBNORMAL,
278 MAX_SUBNORMAL,
279 MIN_NORMAL,
280 ONE,
281 MAX_NORMAL,
282 INF,
283 SIGNALING_NAN,
284 QUIET_NAN
287 constexpr FP all_fp_values[] = {
288 FP::ZERO, FP::MIN_SUBNORMAL, FP::MAX_SUBNORMAL,
289 FP::MIN_NORMAL, FP::ONE, FP::MAX_NORMAL,
290 FP::INF, FP::SIGNALING_NAN, FP::QUIET_NAN,
293 constexpr Sign all_signs[] = {Sign::POS, Sign::NEG};
295 using FPTypes = LIBC_NAMESPACE::testing::TypeList<
296 FPRep<FPType::IEEE754_Binary16>, FPRep<FPType::IEEE754_Binary32>,
297 FPRep<FPType::IEEE754_Binary64>, FPRep<FPType::IEEE754_Binary128>,
298 FPRep<FPType::X86_Binary80>>;
300 template <typename T> constexpr auto make(Sign sign, FP fp) {
301 switch (fp) {
302 case FP::ZERO:
303 return T::zero(sign);
304 case FP::MIN_SUBNORMAL:
305 return T::min_subnormal(sign);
306 case FP::MAX_SUBNORMAL:
307 return T::max_subnormal(sign);
308 case FP::MIN_NORMAL:
309 return T::min_normal(sign);
310 case FP::ONE:
311 return T::one(sign);
312 case FP::MAX_NORMAL:
313 return T::max_normal(sign);
314 case FP::INF:
315 return T::inf(sign);
316 case FP::SIGNALING_NAN:
317 return T::signaling_nan(sign);
318 case FP::QUIET_NAN:
319 return T::quiet_nan(sign);
321 __builtin_unreachable();
324 // Tests all properties for all types of float.
325 TYPED_TEST(LlvmLibcFPBitsTest, Properties, FPTypes) {
326 for (Sign sign : all_signs) {
327 for (FP fp : all_fp_values) {
328 const T value = make<T>(sign, fp);
329 // is_zero
330 ASSERT_EQ(value.is_zero(), fp == FP::ZERO);
331 // is_inf_or_nan
332 ASSERT_EQ(value.is_inf_or_nan(), fp == FP::INF ||
333 fp == FP::SIGNALING_NAN ||
334 fp == FP::QUIET_NAN);
335 // is_finite
336 ASSERT_EQ(value.is_finite(), fp != FP::INF && fp != FP::SIGNALING_NAN &&
337 fp != FP::QUIET_NAN);
338 // is_inf
339 ASSERT_EQ(value.is_inf(), fp == FP::INF);
340 // is_nan
341 ASSERT_EQ(value.is_nan(), fp == FP::SIGNALING_NAN || fp == FP::QUIET_NAN);
342 // is_normal
343 ASSERT_EQ(value.is_normal(),
344 fp == FP::MIN_NORMAL || fp == FP::ONE || fp == FP::MAX_NORMAL);
345 // is_quiet_nan
346 ASSERT_EQ(value.is_quiet_nan(), fp == FP::QUIET_NAN);
347 // is_signaling_nan
348 ASSERT_EQ(value.is_signaling_nan(), fp == FP::SIGNALING_NAN);
349 // is_subnormal
350 ASSERT_EQ(value.is_subnormal(), fp == FP::ZERO ||
351 fp == FP::MIN_SUBNORMAL ||
352 fp == FP::MAX_SUBNORMAL);
353 // is_pos
354 ASSERT_EQ(value.is_pos(), sign == Sign::POS);
355 ASSERT_EQ(value.sign().is_pos(), sign == Sign::POS);
356 // is_neg
357 ASSERT_EQ(value.is_neg(), sign == Sign::NEG);
358 ASSERT_EQ(value.sign().is_neg(), sign == Sign::NEG);
363 #define ASSERT_SAME_REP(A, B) ASSERT_EQ(A.uintval(), B.uintval());
365 TYPED_TEST(LlvmLibcFPBitsTest, NextTowardInf, FPTypes) {
366 struct {
367 FP before, after;
368 } TEST_CASES[] = {
369 {FP::ZERO, FP::MIN_SUBNORMAL}, //
370 {FP::MAX_SUBNORMAL, FP::MIN_NORMAL}, //
371 {FP::MAX_NORMAL, FP::INF}, //
372 {FP::INF, FP::INF}, //
373 {FP::QUIET_NAN, FP::QUIET_NAN}, //
374 {FP::SIGNALING_NAN, FP::SIGNALING_NAN}, //
376 for (Sign sign : all_signs) {
377 for (auto tc : TEST_CASES) {
378 T val = make<T>(sign, tc.before);
379 ASSERT_SAME_REP(val.next_toward_inf(), make<T>(sign, tc.after));
384 TEST(LlvmLibcFPBitsTest, FloatType) {
385 using FloatBits = FPBits<float>;
387 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::inf(Sign::POS)).c_str(),
388 "(+Infinity)");
389 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::inf(Sign::NEG)).c_str(),
390 "(-Infinity)");
391 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::signaling_nan()).c_str(),
392 "(NaN)");
394 FloatBits zero(0.0f);
395 EXPECT_TRUE(zero.is_pos());
396 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
397 EXPECT_EQ(zero.get_mantissa(), 0_u32);
398 EXPECT_EQ(zero.uintval(), 0_u32);
399 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
400 "0x00000000 = (S: 0, E: 0x0000, M: 0x00000000)");
402 FloatBits negzero(-0.0f);
403 EXPECT_TRUE(negzero.is_neg());
404 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
405 EXPECT_EQ(negzero.get_mantissa(), 0_u32);
406 EXPECT_EQ(negzero.uintval(), 0x80000000_u32);
407 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
408 "0x80000000 = (S: 1, E: 0x0000, M: 0x00000000)");
410 FloatBits one(1.0f);
411 EXPECT_TRUE(one.is_pos());
412 EXPECT_EQ(one.get_biased_exponent(), 0x7F_u16);
413 EXPECT_EQ(one.get_mantissa(), 0_u32);
414 EXPECT_EQ(one.uintval(), 0x3F800000_u32);
415 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
416 "0x3F800000 = (S: 0, E: 0x007F, M: 0x00000000)");
418 FloatBits negone(-1.0f);
419 EXPECT_TRUE(negone.is_neg());
420 EXPECT_EQ(negone.get_biased_exponent(), 0x7F_u16);
421 EXPECT_EQ(negone.get_mantissa(), 0_u32);
422 EXPECT_EQ(negone.uintval(), 0xBF800000_u32);
423 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
424 "0xBF800000 = (S: 1, E: 0x007F, M: 0x00000000)");
426 FloatBits num(1.125f);
427 EXPECT_TRUE(num.is_pos());
428 EXPECT_EQ(num.get_biased_exponent(), 0x7F_u16);
429 EXPECT_EQ(num.get_mantissa(), 0x00100000_u32);
430 EXPECT_EQ(num.uintval(), 0x3F900000_u32);
431 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
432 "0x3F900000 = (S: 0, E: 0x007F, M: 0x00100000)");
434 FloatBits negnum(-1.125f);
435 EXPECT_TRUE(negnum.is_neg());
436 EXPECT_EQ(negnum.get_biased_exponent(), 0x7F_u16);
437 EXPECT_EQ(negnum.get_mantissa(), 0x00100000_u32);
438 EXPECT_EQ(negnum.uintval(), 0xBF900000_u32);
439 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
440 "0xBF900000 = (S: 1, E: 0x007F, M: 0x00100000)");
442 FloatBits quiet_nan = FloatBits::quiet_nan();
443 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
446 TEST(LlvmLibcFPBitsTest, DoubleType) {
447 using DoubleBits = FPBits<double>;
449 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::inf(Sign::POS)).c_str(),
450 "(+Infinity)");
451 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::inf(Sign::NEG)).c_str(),
452 "(-Infinity)");
453 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::signaling_nan()).c_str(),
454 "(NaN)");
456 DoubleBits zero(0.0);
457 EXPECT_TRUE(zero.is_pos());
458 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
459 EXPECT_EQ(zero.get_mantissa(), 0_u64);
460 EXPECT_EQ(zero.uintval(), 0_u64);
461 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
462 "0x0000000000000000 = (S: 0, E: 0x0000, M: 0x0000000000000000)");
464 DoubleBits negzero(-0.0);
465 EXPECT_TRUE(negzero.is_neg());
466 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
467 EXPECT_EQ(negzero.get_mantissa(), 0_u64);
468 EXPECT_EQ(negzero.uintval(), 0x8000000000000000_u64);
469 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
470 "0x8000000000000000 = (S: 1, E: 0x0000, M: 0x0000000000000000)");
472 DoubleBits one(1.0);
473 EXPECT_TRUE(one.is_pos());
474 EXPECT_EQ(one.get_biased_exponent(), 0x03FF_u16);
475 EXPECT_EQ(one.get_mantissa(), 0_u64);
476 EXPECT_EQ(one.uintval(), 0x3FF0000000000000_u64);
477 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
478 "0x3FF0000000000000 = (S: 0, E: 0x03FF, M: 0x0000000000000000)");
480 DoubleBits negone(-1.0);
481 EXPECT_TRUE(negone.is_neg());
482 EXPECT_EQ(negone.get_biased_exponent(), 0x03FF_u16);
483 EXPECT_EQ(negone.get_mantissa(), 0_u64);
484 EXPECT_EQ(negone.uintval(), 0xBFF0000000000000_u64);
485 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
486 "0xBFF0000000000000 = (S: 1, E: 0x03FF, M: 0x0000000000000000)");
488 DoubleBits num(1.125);
489 EXPECT_TRUE(num.is_pos());
490 EXPECT_EQ(num.get_biased_exponent(), 0x03FF_u16);
491 EXPECT_EQ(num.get_mantissa(), 0x0002000000000000_u64);
492 EXPECT_EQ(num.uintval(), 0x3FF2000000000000_u64);
493 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
494 "0x3FF2000000000000 = (S: 0, E: 0x03FF, M: 0x0002000000000000)");
496 DoubleBits negnum(-1.125);
497 EXPECT_TRUE(negnum.is_neg());
498 EXPECT_EQ(negnum.get_biased_exponent(), 0x03FF_u16);
499 EXPECT_EQ(negnum.get_mantissa(), 0x0002000000000000_u64);
500 EXPECT_EQ(negnum.uintval(), 0xBFF2000000000000_u64);
501 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
502 "0xBFF2000000000000 = (S: 1, E: 0x03FF, M: 0x0002000000000000)");
504 DoubleBits quiet_nan = DoubleBits::quiet_nan();
505 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
508 #ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
509 TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {
510 using LongDoubleBits = FPBits<long double>;
511 using Rep = FPRep<FPType::X86_Binary80>;
513 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::POS)).c_str(),
514 "(+Infinity)");
515 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG)).c_str(),
516 "(-Infinity)");
517 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::signaling_nan()).c_str(),
518 "(NaN)");
520 LongDoubleBits zero(0.0l);
521 EXPECT_TRUE(zero.is_pos());
522 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
523 EXPECT_EQ(zero.get_mantissa(), LongDoubleBits::StorageType(Rep::zero()));
524 EXPECT_EQ(zero.uintval(), LongDoubleBits::StorageType(Rep::zero()));
525 #if __SIZEOF_LONG_DOUBLE__ == 16
526 EXPECT_STREQ(
527 LIBC_NAMESPACE::str(zero).c_str(),
528 "0x00000000000000000000000000000000 = "
529 "(S: 0, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");
530 #elif __SIZEOF_LONG_DOUBLE__ == 12
531 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
532 "0x000000000000000000000000 = "
533 "(S: 0, E: 0x0000, I: 0, M: 0x000000000000000000000000)");
534 #else
535 #error "unhandled long double type"
536 #endif
538 LongDoubleBits negzero(-0.0l);
539 EXPECT_TRUE(negzero.is_neg());
540 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
541 EXPECT_EQ(negzero.get_mantissa(), LongDoubleBits::StorageType(Rep::zero()));
542 #if __SIZEOF_LONG_DOUBLE__ == 16
543 EXPECT_EQ(negzero.uintval(), 0x8000'00000000'00000000_u128);
544 EXPECT_STREQ(
545 LIBC_NAMESPACE::str(negzero).c_str(),
546 "0x00000000000080000000000000000000 = "
547 "(S: 1, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");
548 #elif __SIZEOF_LONG_DOUBLE__ == 12
549 EXPECT_EQ(negzero.uintval(), 0x8000'00000000'00000000_u96);
550 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
551 "0x000080000000000000000000 = "
552 "(S: 1, E: 0x0000, I: 0, M: 0x000000000000000000000000)");
553 #else
554 #error "unhandled long double type"
555 #endif
557 LongDoubleBits one(1.0l);
558 EXPECT_TRUE(one.is_pos());
559 EXPECT_EQ(one.get_biased_exponent(), 0x3FFF_u16);
560 EXPECT_EQ(one.get_mantissa(), LongDoubleBits::StorageType(Rep::zero()));
561 #if __SIZEOF_LONG_DOUBLE__ == 16
562 EXPECT_EQ(one.uintval(), 0x3FFF'80000000'00000000_u128);
563 EXPECT_STREQ(
564 LIBC_NAMESPACE::str(one).c_str(),
565 "0x0000000000003FFF8000000000000000 = "
566 "(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");
567 #elif __SIZEOF_LONG_DOUBLE__ == 12
568 EXPECT_EQ(one.uintval(), 0x3FFF'80000000'00000000_u96);
569 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
570 "0x00003FFF8000000000000000 = "
571 "(S: 0, E: 0x3FFF, I: 1, M: 0x000000000000000000000000)");
572 #else
573 #error "unhandled long double type"
574 #endif
576 LongDoubleBits negone(-1.0l);
577 EXPECT_TRUE(negone.is_neg());
578 EXPECT_EQ(negone.get_biased_exponent(), 0x3FFF_u16);
579 EXPECT_EQ(negone.get_mantissa(), LongDoubleBits::StorageType(Rep::zero()));
580 #if __SIZEOF_LONG_DOUBLE__ == 16
581 EXPECT_EQ(negone.uintval(), 0xBFFF'80000000'00000000_u128);
582 EXPECT_STREQ(
583 LIBC_NAMESPACE::str(negone).c_str(),
584 "0x000000000000BFFF8000000000000000 = "
585 "(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");
586 #elif __SIZEOF_LONG_DOUBLE__ == 12
587 EXPECT_EQ(negone.uintval(), 0xBFFF'80000000'00000000_u96);
588 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
589 "0x0000BFFF8000000000000000 = "
590 "(S: 1, E: 0x3FFF, I: 1, M: 0x000000000000000000000000)");
591 #else
592 #error "unhandled long double type"
593 #endif
595 LongDoubleBits num(1.125l);
596 EXPECT_TRUE(num.is_pos());
597 EXPECT_EQ(num.get_biased_exponent(), 0x3FFF_u16);
598 #if __SIZEOF_LONG_DOUBLE__ == 16
599 EXPECT_EQ(num.get_mantissa(), 0x10000000'00000000_u128);
600 EXPECT_EQ(num.uintval(), 0x3FFF'90000000'00000000_u128);
601 EXPECT_STREQ(
602 LIBC_NAMESPACE::str(num).c_str(),
603 "0x0000000000003FFF9000000000000000 = "
604 "(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
605 #elif __SIZEOF_LONG_DOUBLE__ == 12
606 EXPECT_EQ(num.get_mantissa(), 0x10000000'00000000_u96);
607 EXPECT_EQ(num.uintval(), 0x3FFF'90000000'00000000_u96);
608 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
609 "0x00003FFF9000000000000000 = "
610 "(S: 0, E: 0x3FFF, I: 1, M: 0x000000001000000000000000)");
611 #else
612 #error "unhandled long double type"
613 #endif
615 LongDoubleBits negnum(-1.125l);
616 EXPECT_TRUE(negnum.is_neg());
617 EXPECT_EQ(negnum.get_biased_exponent(), 0x3FFF_u16);
618 #if __SIZEOF_LONG_DOUBLE__ == 16
619 EXPECT_EQ(negnum.get_mantissa(), 0x10000000'00000000_u128);
620 EXPECT_EQ(negnum.uintval(), 0xBFFF'90000000'00000000_u128);
621 EXPECT_STREQ(
622 LIBC_NAMESPACE::str(negnum).c_str(),
623 "0x000000000000BFFF9000000000000000 = "
624 "(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
625 #elif __SIZEOF_LONG_DOUBLE__ == 12
626 EXPECT_EQ(negnum.get_mantissa(), 0x10000000'00000000_u96);
627 EXPECT_EQ(negnum.uintval(), 0xBFFF'90000000'00000000_u96);
628 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
629 "0x0000BFFF9000000000000000 = "
630 "(S: 1, E: 0x3FFF, I: 1, M: 0x000000001000000000000000)");
631 #else
632 #error "unhandled long double type"
633 #endif
635 LongDoubleBits quiet_nan = LongDoubleBits::quiet_nan();
636 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
638 #else
639 TEST(LlvmLibcFPBitsTest, LongDoubleType) {
640 #if defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64)
641 return; // The tests for the "double" type cover for this case.
642 #else
643 using LongDoubleBits = FPBits<long double>;
645 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::POS)).c_str(),
646 "(+Infinity)");
647 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG)).c_str(),
648 "(-Infinity)");
649 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::signaling_nan()).c_str(),
650 "(NaN)");
652 LongDoubleBits zero(0.0l);
653 EXPECT_TRUE(zero.is_pos());
654 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
655 EXPECT_EQ(zero.get_mantissa(), 0_u128);
656 EXPECT_EQ(zero.uintval(), 0_u128);
657 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
658 "0x00000000000000000000000000000000 = "
659 "(S: 0, E: 0x0000, M: 0x00000000000000000000000000000000)");
661 LongDoubleBits negzero(-0.0l);
662 EXPECT_TRUE(negzero.is_neg());
663 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
664 EXPECT_EQ(negzero.get_mantissa(), 0_u128);
665 EXPECT_EQ(negzero.uintval(), 0x80000000'00000000'00000000'00000000_u128);
666 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
667 "0x80000000000000000000000000000000 = "
668 "(S: 1, E: 0x0000, M: 0x00000000000000000000000000000000)");
670 LongDoubleBits one(1.0l);
671 EXPECT_TRUE(one.is_pos());
672 EXPECT_EQ(one.get_biased_exponent(), 0x3FFF_u16);
673 EXPECT_EQ(one.get_mantissa(), 0_u128);
674 EXPECT_EQ(one.uintval(), 0x3FFF0000'00000000'00000000'00000000_u128);
675 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
676 "0x3FFF0000000000000000000000000000 = "
677 "(S: 0, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
679 LongDoubleBits negone(-1.0l);
680 EXPECT_TRUE(negone.is_neg());
681 EXPECT_EQ(negone.get_biased_exponent(), 0x3FFF_u16);
682 EXPECT_EQ(negone.get_mantissa(), 0_u128);
683 EXPECT_EQ(negone.uintval(), 0xBFFF0000'00000000'00000000'00000000_u128);
684 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
685 "0xBFFF0000000000000000000000000000 = "
686 "(S: 1, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
688 LongDoubleBits num(1.125l);
689 EXPECT_TRUE(num.is_pos());
690 EXPECT_EQ(num.get_biased_exponent(), 0x3FFF_u16);
691 EXPECT_EQ(num.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
692 EXPECT_EQ(num.uintval(), 0x3FFF2000'00000000'00000000'00000000_u128);
693 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
694 "0x3FFF2000000000000000000000000000 = "
695 "(S: 0, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
697 LongDoubleBits negnum(-1.125l);
698 EXPECT_TRUE(negnum.is_neg());
699 EXPECT_EQ(negnum.get_biased_exponent(), 0x3FFF_u16);
700 EXPECT_EQ(negnum.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
701 EXPECT_EQ(negnum.uintval(), 0xBFFF2000'00000000'00000000'00000000_u128);
702 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
703 "0xBFFF2000000000000000000000000000 = "
704 "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
706 LongDoubleBits quiet_nan = LongDoubleBits::quiet_nan();
707 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
708 #endif
710 #endif
712 #if defined(LIBC_TYPES_HAS_FLOAT128)
713 TEST(LlvmLibcFPBitsTest, Float128Type) {
714 using Float128Bits = FPBits<float128>;
716 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::inf(Sign::POS)).c_str(),
717 "(+Infinity)");
718 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::inf(Sign::NEG)).c_str(),
719 "(-Infinity)");
720 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::signaling_nan()).c_str(),
721 "(NaN)");
723 Float128Bits zero = Float128Bits::zero(Sign::POS);
724 EXPECT_TRUE(zero.is_pos());
725 EXPECT_EQ(zero.get_biased_exponent(), 0_u16);
726 EXPECT_EQ(zero.get_mantissa(), 0_u128);
727 EXPECT_EQ(zero.uintval(), 0_u128);
728 EXPECT_STREQ(LIBC_NAMESPACE::str(zero).c_str(),
729 "0x00000000000000000000000000000000 = "
730 "(S: 0, E: 0x0000, M: 0x00000000000000000000000000000000)");
732 Float128Bits negzero = Float128Bits::zero(Sign::NEG);
733 EXPECT_TRUE(negzero.is_neg());
734 EXPECT_EQ(negzero.get_biased_exponent(), 0_u16);
735 EXPECT_EQ(negzero.get_mantissa(), 0_u128);
736 EXPECT_EQ(negzero.uintval(), 0x80000000'00000000'00000000'00000000_u128);
737 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero).c_str(),
738 "0x80000000000000000000000000000000 = "
739 "(S: 1, E: 0x0000, M: 0x00000000000000000000000000000000)");
741 Float128Bits one(float128(1.0));
742 EXPECT_TRUE(one.is_pos());
743 EXPECT_EQ(one.get_biased_exponent(), 0x3FFF_u16);
744 EXPECT_EQ(one.get_mantissa(), 0_u128);
745 EXPECT_EQ(one.uintval(), 0x3FFF0000'00000000'00000000'00000000_u128);
746 EXPECT_STREQ(LIBC_NAMESPACE::str(one).c_str(),
747 "0x3FFF0000000000000000000000000000 = "
748 "(S: 0, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
750 Float128Bits negone(float128(-1.0));
751 EXPECT_TRUE(negone.is_neg());
752 EXPECT_EQ(negone.get_biased_exponent(), 0x3FFF_u16);
753 EXPECT_EQ(negone.get_mantissa(), 0_u128);
754 EXPECT_EQ(negone.uintval(), 0xBFFF0000'00000000'00000000'00000000_u128);
755 EXPECT_STREQ(LIBC_NAMESPACE::str(negone).c_str(),
756 "0xBFFF0000000000000000000000000000 = "
757 "(S: 1, E: 0x3FFF, M: 0x00000000000000000000000000000000)");
759 Float128Bits num(float128(1.125));
760 EXPECT_TRUE(num.is_pos());
761 EXPECT_EQ(num.get_biased_exponent(), 0x3FFF_u16);
762 EXPECT_EQ(num.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
763 EXPECT_EQ(num.uintval(), 0x3FFF2000'00000000'00000000'00000000_u128);
764 EXPECT_STREQ(LIBC_NAMESPACE::str(num).c_str(),
765 "0x3FFF2000000000000000000000000000 = "
766 "(S: 0, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
768 Float128Bits negnum(float128(-1.125));
769 EXPECT_TRUE(negnum.is_neg());
770 EXPECT_EQ(negnum.get_biased_exponent(), 0x3FFF_u16);
771 EXPECT_EQ(negnum.get_mantissa(), 0x2000'00000000'00000000'00000000_u128);
772 EXPECT_EQ(negnum.uintval(), 0xBFFF2000'00000000'00000000'00000000_u128);
773 EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
774 "0xBFFF2000000000000000000000000000 = "
775 "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
777 Float128Bits quiet_nan = Float128Bits::quiet_nan();
778 EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
780 #endif // LIBC_TYPES_HAS_FLOAT128