1 //===-- Unittests for the FPBits class ------------------------------------===//
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
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
;
67 0b0'00000000000'0000000000000000000000000000000000000000000000000000_u64
,
70 0b0'01111111111'0000000000000000000000000000000000000000000000000000_u64
,
73 0b0'00000000000'0000000000000000000000000000000000000000000000000001_u64
,
74 u64(Rep::min_subnormal()));
76 0b0'00000000000'1111111111111111111111111111111111111111111111111111_u64
,
77 u64(Rep::max_subnormal()));
79 0b0'00000000001'0000000000000000000000000000000000000000000000000000_u64
,
80 u64(Rep::min_normal()));
82 0b0'11111111110'1111111111111111111111111111111111111111111111111111_u64
,
83 u64(Rep::max_normal()));
85 0b0'11111111111'0000000000000000000000000000000000000000000000000000_u64
,
88 0b0'11111111111'0100000000000000000000000000000000000000000000000000_u64
,
89 u64(Rep::signaling_nan()));
91 0b0'11111111111'1000000000000000000000000000000000000000000000000000_u64
,
92 u64(Rep::quiet_nan()));
95 TEST(LlvmLibcFPBitsTest
, FPType_IEEE754_Binary128
) {
96 using Rep
= FPRep
<FPType::IEEE754_Binary128
>;
99 0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128
,
100 UInt128(Rep::zero()));
102 0b0'011111111111111'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128
,
103 UInt128(Rep::one()));
105 0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001_u128
,
106 UInt128(Rep::min_subnormal()));
108 0b0'000000000000000'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111_u128
,
109 UInt128(Rep::max_subnormal()));
111 0b0'000000000000001'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128
,
112 UInt128(Rep::min_normal()));
114 0b0'111111111111110'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111_u128
,
115 UInt128(Rep::max_normal()));
117 0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128
,
118 UInt128(Rep::inf()));
120 0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000_u128
,
121 UInt128(Rep::signaling_nan()));
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
133 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000000_u128
,
134 UInt128(Rep::zero()));
136 0b0'0111111111111111000000000000000000000000000000000000000000000000000000000000000_u128
,
137 UInt128(Rep::one()));
139 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000001_u128
,
140 UInt128(Rep::min_subnormal()));
142 0b0'0000000000000000111111111111111111111111111111111111111111111111111111111111111_u128
,
143 UInt128(Rep::max_subnormal()));
145 0b0'0000000000000011000000000000000000000000000000000000000000000000000000000000000_u128
,
146 UInt128(Rep::min_normal()));
148 0b0'1111111111111101111111111111111111111111111111111111111111111111111111111111111_u128
,
149 UInt128(Rep::max_normal()));
151 0b0'1111111111111111000000000000000000000000000000000000000000000000000000000000000_u128
,
152 UInt128(Rep::inf()));
154 0b0'1111111111111111010000000000000000000000000000000000000000000000000000000000000_u128
,
155 UInt128(Rep::signaling_nan()));
157 0b0'1111111111111111100000000000000000000000000000000000000000000000000000000000000_u128
,
158 UInt128(Rep::quiet_nan()));
159 #elif __SIZEOF_LONG_DOUBLE__ == 12
161 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000000_u96
,
162 UInt
<96>(Rep::zero()));
164 0b0'0111111111111111000000000000000000000000000000000000000000000000000000000000000_u96
,
165 UInt
<96>(Rep::one()));
167 0b0'0000000000000000000000000000000000000000000000000000000000000000000000000000001_u96
,
168 UInt
<96>(Rep::min_subnormal()));
170 0b0'0000000000000000111111111111111111111111111111111111111111111111111111111111111_u96
,
171 UInt
<96>(Rep::max_subnormal()));
173 0b0'0000000000000011000000000000000000000000000000000000000000000000000000000000000_u96
,
174 UInt
<96>(Rep::min_normal()));
176 0b0'1111111111111101111111111111111111111111111111111111111111111111111111111111111_u96
,
177 UInt
<96>(Rep::max_normal()));
179 0b0'1111111111111111000000000000000000000000000000000000000000000000000000000000000_u96
,
180 UInt
<96>(Rep::inf()));
182 0b0'1111111111111111010000000000000000000000000000000000000000000000000000000000000_u96
,
183 UInt
<96>(Rep::signaling_nan()));
185 0b0'1111111111111111100000000000000000000000000000000000000000000000000000000000000_u96
,
186 UInt
<96>(Rep::quiet_nan()));
188 #error "unhandled long double type"
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
)
199 EXPECT_TRUE( // NAN : Pseudo Not a Number
200 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000001_u128
)
202 EXPECT_TRUE( // NAN : Pseudo Not a Number
203 Rep(0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000_u128
)
205 EXPECT_TRUE( // NAN : Signalling Not a Number
206 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000001_u128
)
208 EXPECT_TRUE( // NAN : Floating-point Indefinite
209 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000000_u128
)
211 EXPECT_TRUE( // NAN : Quiet Not a Number
212 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000001_u128
)
214 EXPECT_TRUE( // NAN : Unnormal
215 Rep(0b0'111111111111110'0000000000000000000000000000000000000000000000000000000000000000_u128
)
217 EXPECT_FALSE( // Zero
218 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000_u128
)
220 EXPECT_FALSE( // Subnormal
221 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000001_u128
)
223 EXPECT_FALSE( // Pseudo Denormal
224 Rep(0b0'000000000000000'1000000000000000000000000000000000000000000000000000000000000001_u128
)
226 EXPECT_FALSE( // Infinity
227 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000000_u128
)
229 EXPECT_FALSE( // Normalized
230 Rep(0b0'111111111111110'1000000000000000000000000000000000000000000000000000000000000000_u128
)
232 #elif __SIZEOF_LONG_DOUBLE__ == 12
233 EXPECT_TRUE( // NAN : Pseudo-Infinity
234 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000000_u96
)
236 EXPECT_TRUE( // NAN : Pseudo Not a Number
237 Rep(0b0'111111111111111'0000000000000000000000000000000000000000000000000000000000000001_u96
)
239 EXPECT_TRUE( // NAN : Pseudo Not a Number
240 Rep(0b0'111111111111111'0100000000000000000000000000000000000000000000000000000000000000_u96
)
242 EXPECT_TRUE( // NAN : Signalling Not a Number
243 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000001_u96
)
245 EXPECT_TRUE( // NAN : Floating-point Indefinite
246 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000000_u96
)
248 EXPECT_TRUE( // NAN : Quiet Not a Number
249 Rep(0b0'111111111111111'1100000000000000000000000000000000000000000000000000000000000001_u96
)
251 EXPECT_TRUE( // NAN : Unnormal
252 Rep(0b0'111111111111110'0000000000000000000000000000000000000000000000000000000000000000_u96
)
254 EXPECT_FALSE( // Zero
255 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000000_u96
)
257 EXPECT_FALSE( // Subnormal
258 Rep(0b0'000000000000000'0000000000000000000000000000000000000000000000000000000000000001_u96
)
260 EXPECT_FALSE( // Pseudo Denormal
261 Rep(0b0'000000000000000'1000000000000000000000000000000000000000000000000000000000000001_u96
)
263 EXPECT_FALSE( // Infinity
264 Rep(0b0'111111111111111'1000000000000000000000000000000000000000000000000000000000000000_u96
)
266 EXPECT_FALSE( // Normalized
267 Rep(0b0'111111111111110'1000000000000000000000000000000000000000000000000000000000000000_u96
)
270 #error "unhandled long double type"
273 #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
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
) {
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
);
309 return T::min_normal(sign
);
313 return T::max_normal(sign
);
316 case FP::SIGNALING_NAN
:
317 return T::signaling_nan(sign
);
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
);
330 ASSERT_EQ(value
.is_zero(), fp
== FP::ZERO
);
332 ASSERT_EQ(value
.is_inf_or_nan(), fp
== FP::INF
||
333 fp
== FP::SIGNALING_NAN
||
334 fp
== FP::QUIET_NAN
);
336 ASSERT_EQ(value
.is_finite(), fp
!= FP::INF
&& fp
!= FP::SIGNALING_NAN
&&
337 fp
!= FP::QUIET_NAN
);
339 ASSERT_EQ(value
.is_inf(), fp
== FP::INF
);
341 ASSERT_EQ(value
.is_nan(), fp
== FP::SIGNALING_NAN
|| fp
== FP::QUIET_NAN
);
343 ASSERT_EQ(value
.is_normal(),
344 fp
== FP::MIN_NORMAL
|| fp
== FP::ONE
|| fp
== FP::MAX_NORMAL
);
346 ASSERT_EQ(value
.is_quiet_nan(), fp
== FP::QUIET_NAN
);
348 ASSERT_EQ(value
.is_signaling_nan(), fp
== FP::SIGNALING_NAN
);
350 ASSERT_EQ(value
.is_subnormal(), fp
== FP::ZERO
||
351 fp
== FP::MIN_SUBNORMAL
||
352 fp
== FP::MAX_SUBNORMAL
);
354 ASSERT_EQ(value
.is_pos(), sign
== Sign::POS
);
355 ASSERT_EQ(value
.sign().is_pos(), sign
== Sign::POS
);
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
) {
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(),
389 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::inf(Sign::NEG
)).c_str(),
391 EXPECT_STREQ(LIBC_NAMESPACE::str(FloatBits::signaling_nan()).c_str(),
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_u
32);
407 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero
).c_str(),
408 "0x80000000 = (S: 1, E: 0x0000, M: 0x00000000)");
411 EXPECT_TRUE(one
.is_pos());
412 EXPECT_EQ(one
.get_biased_exponent(), 0x7F_u
16);
413 EXPECT_EQ(one
.get_mantissa(), 0_u32
);
414 EXPECT_EQ(one
.uintval(), 0x3F800000_u
32);
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_u
16);
421 EXPECT_EQ(negone
.get_mantissa(), 0_u32
);
422 EXPECT_EQ(negone
.uintval(), 0xBF800000_u
32);
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_u
16);
429 EXPECT_EQ(num
.get_mantissa(), 0x00100000_u
32);
430 EXPECT_EQ(num
.uintval(), 0x3F900000_u
32);
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_u
16);
437 EXPECT_EQ(negnum
.get_mantissa(), 0x00100000_u
32);
438 EXPECT_EQ(negnum
.uintval(), 0xBF900000_u
32);
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(),
451 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::inf(Sign::NEG
)).c_str(),
453 EXPECT_STREQ(LIBC_NAMESPACE::str(DoubleBits::signaling_nan()).c_str(),
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_u
64);
469 EXPECT_STREQ(LIBC_NAMESPACE::str(negzero
).c_str(),
470 "0x8000000000000000 = (S: 1, E: 0x0000, M: 0x0000000000000000)");
473 EXPECT_TRUE(one
.is_pos());
474 EXPECT_EQ(one
.get_biased_exponent(), 0x03FF_u
16);
475 EXPECT_EQ(one
.get_mantissa(), 0_u64
);
476 EXPECT_EQ(one
.uintval(), 0x3FF0000000000000_u
64);
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_u
16);
483 EXPECT_EQ(negone
.get_mantissa(), 0_u64
);
484 EXPECT_EQ(negone
.uintval(), 0xBFF0000000000000_u
64);
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_u
16);
491 EXPECT_EQ(num
.get_mantissa(), 0x0002000000000000_u
64);
492 EXPECT_EQ(num
.uintval(), 0x3FF2000000000000_u
64);
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_u
16);
499 EXPECT_EQ(negnum
.get_mantissa(), 0x0002000000000000_u
64);
500 EXPECT_EQ(negnum
.uintval(), 0xBFF2000000000000_u
64);
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(),
515 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG
)).c_str(),
517 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::signaling_nan()).c_str(),
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
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)");
535 #error "unhandled long double type"
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
);
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)");
554 #error "unhandled long double type"
557 LongDoubleBits
one(1.0l);
558 EXPECT_TRUE(one
.is_pos());
559 EXPECT_EQ(one
.get_biased_exponent(), 0x3FFF_u
16);
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
);
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)");
573 #error "unhandled long double type"
576 LongDoubleBits
negone(-1.0l);
577 EXPECT_TRUE(negone
.is_neg());
578 EXPECT_EQ(negone
.get_biased_exponent(), 0x3FFF_u
16);
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
);
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)");
592 #error "unhandled long double type"
595 LongDoubleBits
num(1.125l);
596 EXPECT_TRUE(num
.is_pos());
597 EXPECT_EQ(num
.get_biased_exponent(), 0x3FFF_u
16);
598 #if __SIZEOF_LONG_DOUBLE__ == 16
599 EXPECT_EQ(num
.get_mantissa(), 0x10000000'00000000_u128
);
600 EXPECT_EQ(num
.uintval(), 0x3FFF'90000000'00000000_u128
);
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)");
612 #error "unhandled long double type"
615 LongDoubleBits
negnum(-1.125l);
616 EXPECT_TRUE(negnum
.is_neg());
617 EXPECT_EQ(negnum
.get_biased_exponent(), 0x3FFF_u
16);
618 #if __SIZEOF_LONG_DOUBLE__ == 16
619 EXPECT_EQ(negnum
.get_mantissa(), 0x10000000'00000000_u128
);
620 EXPECT_EQ(negnum
.uintval(), 0xBFFF'90000000'00000000_u128
);
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)");
632 #error "unhandled long double type"
635 LongDoubleBits quiet_nan
= LongDoubleBits::quiet_nan();
636 EXPECT_EQ(quiet_nan
.is_quiet_nan(), true);
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.
643 using LongDoubleBits
= FPBits
<long double>;
645 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::POS
)).c_str(),
647 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG
)).c_str(),
649 EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::signaling_nan()).c_str(),
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_u
16);
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_u
16);
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_u
16);
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_u
16);
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);
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(),
718 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::inf(Sign::NEG
)).c_str(),
720 EXPECT_STREQ(LIBC_NAMESPACE::str(Float128Bits::signaling_nan()).c_str(),
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_u
16);
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_u
16);
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_u
16);
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_u
16);
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