[PATCH] D26538: [ADT/MathExtras] Introduce PowerOf2Ceil

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 10 18:32:09 PST 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL286549: [ADT/MathExtras] Introduce PowerOf2Ceil. (authored by davide).

Changed prior to commit:
  https://reviews.llvm.org/D26538?vs=77588&id=77589#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D26538

Files:
  llvm/trunk/include/llvm/Support/MathExtras.h
  llvm/trunk/unittests/Support/MathExtrasTest.cpp


Index: llvm/trunk/unittests/Support/MathExtrasTest.cpp
===================================================================
--- llvm/trunk/unittests/Support/MathExtrasTest.cpp
+++ llvm/trunk/unittests/Support/MathExtrasTest.cpp
@@ -165,6 +165,12 @@
   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));
Index: llvm/trunk/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h
+++ llvm/trunk/include/llvm/Support/MathExtras.h
@@ -641,6 +641,14 @@
   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.
 ///


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26538.77589.patch
Type: text/x-patch
Size: 1304 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161111/0e5777ce/attachment.bin>


More information about the llvm-commits mailing list