[ADT/MathExtras] Introduce PowerOf2Ceil.

To be used in lld (and probably somewhere else in llvm).

Differential Revision:  https://reviews.llvm.org/D26538

llvm-svn: 286549
This commit is contained in:
Davide Italiano 2016-11-11 02:22:16 +00:00
parent 169e7c4edc
commit 1fb1b9cd00
2 changed files with 14 additions and 0 deletions

View File

@ -641,6 +641,14 @@ inline uint64_t PowerOf2Floor(uint64_t A) {
return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
}
/// Returns the power of two which is greater than or equal to the given value.
/// Essentially, it is a ceil operation across the domain of powers of two.
inline uint64_t PowerOf2Ceil(uint64_t A) {
if (!A)
return 0;
return NextPowerOf2(A - 1);
}
/// Returns the next integer (mod 2**64) that is greater than or equal to
/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
///

View File

@ -165,6 +165,12 @@ TEST(MathExtras, isPowerOf2_64) {
EXPECT_FALSE(isPowerOf2_64(0xABCDEF0ABCDEF0LL));
}
TEST(MathExtras, PowerOf2Ceil) {
EXPECT_EQ(0, PowerOf2Ceil(0));
EXPECT_EQ(8, PowerOf2Ceil(8));
EXPECT_EQ(8, PowerOf2Ceil(7));
}
TEST(MathExtras, ByteSwap_32) {
EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344));
EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD));