|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
| 9 | +#include "src/__support/CPP/bit.h" // bit_cast |
9 | 10 | #include "src/__support/CPP/optional.h"
|
| 11 | +#include "src/__support/CPP/type_traits.h" // is_trivially_constructible |
10 | 12 | #include "src/__support/UInt.h"
|
| 13 | +#include "src/__support/macros/properties/float.h" // LIBC_COMPILER_HAS_FLOAT128 |
11 | 14 |
|
12 | 15 | #include "test/UnitTest/Test.h"
|
13 | 16 |
|
14 |
| -// We want to test LIBC_NAMESPACE::cpp::UInt<128> explicitly. So, for |
| 17 | +#include <math.h> // HUGE_VALF, HUGE_VALF |
| 18 | + |
| 19 | +namespace LIBC_NAMESPACE { |
| 20 | + |
| 21 | +using LL_UInt64 = cpp::UInt<64>; |
| 22 | +// We want to test cpp::UInt<128> explicitly. So, for |
15 | 23 | // convenience, we use a sugar which does not conflict with the UInt128 type
|
16 | 24 | // which can resolve to __uint128_t if the platform has it.
|
17 |
| -using LL_UInt128 = LIBC_NAMESPACE::cpp::UInt<128>; |
18 |
| -using LL_UInt192 = LIBC_NAMESPACE::cpp::UInt<192>; |
19 |
| -using LL_UInt256 = LIBC_NAMESPACE::cpp::UInt<256>; |
20 |
| -using LL_UInt320 = LIBC_NAMESPACE::cpp::UInt<320>; |
21 |
| -using LL_UInt512 = LIBC_NAMESPACE::cpp::UInt<512>; |
22 |
| -using LL_UInt1024 = LIBC_NAMESPACE::cpp::UInt<1024>; |
| 25 | +using LL_UInt128 = cpp::UInt<128>; |
| 26 | +using LL_UInt192 = cpp::UInt<192>; |
| 27 | +using LL_UInt256 = cpp::UInt<256>; |
| 28 | +using LL_UInt320 = cpp::UInt<320>; |
| 29 | +using LL_UInt512 = cpp::UInt<512>; |
| 30 | +using LL_UInt1024 = cpp::UInt<1024>; |
| 31 | + |
| 32 | +using LL_Int128 = cpp::Int<128>; |
| 33 | +using LL_Int192 = cpp::Int<192>; |
| 34 | + |
| 35 | +TEST(LlvmLibcUIntClassTest, BitCastToFromDouble) { |
| 36 | + static_assert(cpp::is_trivially_constructible<LL_UInt64>::value); |
| 37 | + static_assert(cpp::is_trivially_copyable<LL_UInt64>::value); |
| 38 | + static_assert(sizeof(LL_UInt64) == sizeof(double)); |
| 39 | + const double inf = HUGE_VAL; |
| 40 | + const double max = DBL_MAX; |
| 41 | + const double array[] = {0.0, 0.1, 1.0, max, inf}; |
| 42 | + for (double value : array) { |
| 43 | + LL_UInt64 back = cpp::bit_cast<LL_UInt64>(value); |
| 44 | + double forth = cpp::bit_cast<double>(back); |
| 45 | + EXPECT_TRUE(value == forth); |
| 46 | + } |
| 47 | +} |
23 | 48 |
|
24 |
| -using LL_Int128 = LIBC_NAMESPACE::cpp::Int<128>; |
25 |
| -using LL_Int192 = LIBC_NAMESPACE::cpp::Int<192>; |
| 49 | +#ifdef __SIZEOF_INT128__ |
| 50 | +TEST(LlvmLibcUIntClassTest, BitCastToFromNativeUint128) { |
| 51 | + static_assert(cpp::is_trivially_constructible<LL_UInt128>::value); |
| 52 | + static_assert(cpp::is_trivially_copyable<LL_UInt128>::value); |
| 53 | + static_assert(sizeof(LL_UInt128) == sizeof(__uint128_t)); |
| 54 | + const __uint128_t array[] = {0, 1, ~__uint128_t(0)}; |
| 55 | + for (__uint128_t value : array) { |
| 56 | + LL_UInt128 back = cpp::bit_cast<LL_UInt128>(value); |
| 57 | + __uint128_t forth = cpp::bit_cast<__uint128_t>(back); |
| 58 | + EXPECT_TRUE(value == forth); |
| 59 | + } |
| 60 | +} |
| 61 | +#endif |
| 62 | + |
| 63 | +#ifdef LIBC_COMPILER_HAS_FLOAT128 |
| 64 | +TEST(LlvmLibcUIntClassTest, BitCastToFromNativeFloat128) { |
| 65 | + static_assert(cpp::is_trivially_constructible<LL_UInt128>::value); |
| 66 | + static_assert(cpp::is_trivially_copyable<LL_UInt128>::value); |
| 67 | + static_assert(sizeof(LL_UInt128) == sizeof(float128)); |
| 68 | + const float128 array[] = {0, 0.1, 1}; |
| 69 | + for (float128 value : array) { |
| 70 | + LL_UInt128 back = cpp::bit_cast<LL_UInt128>(value); |
| 71 | + float128 forth = cpp::bit_cast<float128>(back); |
| 72 | + EXPECT_TRUE(value == forth); |
| 73 | + } |
| 74 | +} |
| 75 | +#endif |
26 | 76 |
|
27 | 77 | TEST(LlvmLibcUIntClassTest, BasicInit) {
|
28 | 78 | LL_UInt128 half_val(12345);
|
@@ -634,3 +684,5 @@ TEST(LlvmLibcUIntClassTest, ConstructorFromUInt128Tests) {
|
634 | 684 | }
|
635 | 685 |
|
636 | 686 | #endif // __SIZEOF_INT128__
|
| 687 | + |
| 688 | +} // namespace LIBC_NAMESPACE |
0 commit comments