Skip to content

Commit bf6d4a5

Browse files
davidotlinusg
authored andcommitted
AK: Make truncating UFixedBigInts constexpr
Also add some tests and shift tests while we're at it.
1 parent 66d07a4 commit bf6d4a5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

AK/UFixedBigInt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ requires(sizeof(T) >= sizeof(u64) && IsUnsigned<T>) class UFixedBigInt {
8787
}
8888

8989
template<Unsigned U>
90-
requires(sizeof(T) >= sizeof(U)) explicit operator U() const
90+
requires(sizeof(T) >= sizeof(U)) constexpr explicit operator U() const
9191
{
9292
return static_cast<U>(m_low);
9393
}

Tests/AK/TestUFixedBigInt.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ TEST_CASE(div_mod)
8888
}
8989
}
9090

91+
TEST_CASE(shifts)
92+
{
93+
u128 val { 0x1234ULL };
94+
EXPECT_EQ(val << 1u, u128(0x2468ull));
95+
EXPECT_EQ(val << 4u, u128(0x12340ull));
96+
EXPECT_EQ(val << 64u, u128(0ull, 0x1234ull));
97+
}
98+
99+
TEST_CASE(constexpr_truncae)
100+
{
101+
static constexpr u256 wide = u256(u128 { 0x8a4b08d32f8b8e48ULL, 0x8459322f67b8e26dULL }, u128 { 0xeea82af4312d1931ULL, 0x654fb5cfe82dbd58ULL });
102+
static constexpr u64 val = static_cast<u64>(wide);
103+
EXPECT_EQ(val, 0x8a4b08d32f8b8e48ULL);
104+
}
105+
91106
TEST_CASE(mod_hardcoded)
92107
{
93108
EXPECT_EQ(u256(u128 { 0x8a4b08d32f8b8e48ULL, 0x8459322f67b8e26dULL }, u128 { 0xeea82af4312d1931ULL, 0x654fb5cfe82dbd58ULL }) % u256(u128 { 0x40a58652868d5d66ULL, 0x81d674bf7d6d6861ULL }, u128 { 0xa8314900e6188a82ULL, 0xc273ca947237b4aaULL }), u256(u128 { 0x8a4b08d32f8b8e48ULL, 0x8459322f67b8e26dULL }, u128 { 0xeea82af4312d1931ULL, 0x654fb5cfe82dbd58ULL }));

0 commit comments

Comments
 (0)