Index: src/utils.h |
=================================================================== |
--- src/utils.h (revision 10282) |
+++ src/utils.h (working copy) |
@@ -157,7 +157,8 @@ |
// number that is already a power of two, it is returned as is. |
// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
// figure 3-3, page 48, where the function is called clp2. |
-inline uint32_t RoundUpToPowerOf2(uint32_t x) { |
+template<typename uint_type> |
+inline uint_type RoundUpToPowerOf2(uint_type x) { |
ASSERT(x <= 0x80000000u); |
x = x - 1; |
x = x | (x >> 1); |
@@ -169,6 +170,20 @@ |
} |
+template<typename int_type> |
+inline int SignedRoundUpToPowerOf2(int_type x_argument) { |
+ uintptr_t x = static_cast<uintptr_t>(x_argument); |
Vyacheslav Egorov (Chromium)
2011/12/21 13:22:12
inline int SignedRoundUpToPowerOf2(int_type x_argu
|
+ ASSERT(x <= 0x80000000u); |
+ x = x - 1; |
+ x = x | (x >> 1); |
+ x = x | (x >> 2); |
+ x = x | (x >> 4); |
+ x = x | (x >> 8); |
+ x = x | (x >> 16); |
+ return static_cast<int>(x + 1); |
+} |
+ |
+ |
inline uint32_t RoundDownToPowerOf2(uint32_t x) { |
uint32_t rounded_up = RoundUpToPowerOf2(x); |
if (rounded_up > x) return rounded_up >> 1; |