From a216643cd31c808a36d0c79c7f96dabc6eb6c8b7 Mon Sep 17 00:00:00 2001 From: Hubert Tong Date: Sat, 30 Jul 2016 13:38:51 +0000 Subject: [PATCH] MathExtras.h: add LLVM_CONSTEXPR where simple Summary: This change adds `LLVM_CONSTEXPR` to functions selected as follows: - the body is already valid under C++11 for a `constexpr` function, - the evaluation of the function, given constant arguments, will not fail during the evaluation of a constant expression, and - the above properties are easily verifiable at a glance. Note: the evaluation of the function cannot fail if the instantiation triggers a static assertion failure. Reviewers: faisalv, rsmith, aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D22824 llvm-svn: 277269 --- llvm/include/llvm/Support/MathExtras.h | 50 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index 806fc35..704561c 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -245,44 +245,44 @@ T reverseBits(T Val) { // ambiguity. /// Hi_32 - This function returns the high 32 bits of a 64 bit value. -inline uint32_t Hi_32(uint64_t Value) { +LLVM_CONSTEXPR inline uint32_t Hi_32(uint64_t Value) { return static_cast(Value >> 32); } /// Lo_32 - This function returns the low 32 bits of a 64 bit value. -inline uint32_t Lo_32(uint64_t Value) { +LLVM_CONSTEXPR inline uint32_t Lo_32(uint64_t Value) { return static_cast(Value); } /// Make_64 - This functions makes a 64-bit integer from a high / low pair of /// 32-bit integers. -inline uint64_t Make_64(uint32_t High, uint32_t Low) { +LLVM_CONSTEXPR inline uint64_t Make_64(uint32_t High, uint32_t Low) { return ((uint64_t)High << 32) | (uint64_t)Low; } /// isInt - Checks if an integer fits into the given bit width. template -inline bool isInt(int64_t x) { +LLVM_CONSTEXPR inline bool isInt(int64_t x) { return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1))); } // Template specializations to get better code for common cases. template<> -inline bool isInt<8>(int64_t x) { +LLVM_CONSTEXPR inline bool isInt<8>(int64_t x) { return static_cast(x) == x; } template<> -inline bool isInt<16>(int64_t x) { +LLVM_CONSTEXPR inline bool isInt<16>(int64_t x) { return static_cast(x) == x; } template<> -inline bool isInt<32>(int64_t x) { +LLVM_CONSTEXPR inline bool isInt<32>(int64_t x) { return static_cast(x) == x; } /// isShiftedInt - Checks if a signed integer is an N bit number shifted /// left by S. template -inline bool isShiftedInt(int64_t x) { +LLVM_CONSTEXPR inline bool isShiftedInt(int64_t x) { static_assert( N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number."); static_assert(N + S <= 64, "isShiftedInt with N + S > 64 is too wide."); @@ -298,32 +298,34 @@ inline bool isShiftedInt(int64_t x) { /// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting /// left too many places. template -inline typename std::enable_if<(N < 64), bool>::type isUInt(uint64_t X) { +LLVM_CONSTEXPR inline typename std::enable_if<(N < 64), bool>::type +isUInt(uint64_t X) { static_assert(N > 0, "isUInt<0> doesn't make sense"); return X < (UINT64_C(1) << (N)); } template -inline typename std::enable_if= 64, bool>::type isUInt(uint64_t X) { +LLVM_CONSTEXPR inline typename std::enable_if= 64, bool>::type +isUInt(uint64_t X) { return true; } // Template specializations to get better code for common cases. template<> -inline bool isUInt<8>(uint64_t x) { +LLVM_CONSTEXPR inline bool isUInt<8>(uint64_t x) { return static_cast(x) == x; } template<> -inline bool isUInt<16>(uint64_t x) { +LLVM_CONSTEXPR inline bool isUInt<16>(uint64_t x) { return static_cast(x) == x; } template<> -inline bool isUInt<32>(uint64_t x) { +LLVM_CONSTEXPR inline bool isUInt<32>(uint64_t x) { return static_cast(x) == x; } /// Checks if a unsigned integer is an N bit number shifted left by S. template -inline bool isShiftedUInt(uint64_t x) { +LLVM_CONSTEXPR inline bool isShiftedUInt(uint64_t x) { static_assert( N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)"); static_assert(N + S <= 64, @@ -375,39 +377,39 @@ inline bool isIntN(unsigned N, int64_t x) { /// isMask_32 - This function returns true if the argument is a non-empty /// sequence of ones starting at the least significant bit with the remainder /// zero (32 bit version). Ex. isMask_32(0x0000FFFFU) == true. -inline bool isMask_32(uint32_t Value) { +LLVM_CONSTEXPR inline bool isMask_32(uint32_t Value) { return Value && ((Value + 1) & Value) == 0; } /// isMask_64 - This function returns true if the argument is a non-empty /// sequence of ones starting at the least significant bit with the remainder /// zero (64 bit version). -inline bool isMask_64(uint64_t Value) { +LLVM_CONSTEXPR inline bool isMask_64(uint64_t Value) { return Value && ((Value + 1) & Value) == 0; } /// isShiftedMask_32 - This function returns true if the argument contains a /// non-empty sequence of ones with the remainder zero (32 bit version.) /// Ex. isShiftedMask_32(0x0000FF00U) == true. -inline bool isShiftedMask_32(uint32_t Value) { +LLVM_CONSTEXPR inline bool isShiftedMask_32(uint32_t Value) { return Value && isMask_32((Value - 1) | Value); } /// isShiftedMask_64 - This function returns true if the argument contains a /// non-empty sequence of ones with the remainder zero (64 bit version.) -inline bool isShiftedMask_64(uint64_t Value) { +LLVM_CONSTEXPR inline bool isShiftedMask_64(uint64_t Value) { return Value && isMask_64((Value - 1) | Value); } /// isPowerOf2_32 - This function returns true if the argument is a power of /// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.) -inline bool isPowerOf2_32(uint32_t Value) { +LLVM_CONSTEXPR inline bool isPowerOf2_32(uint32_t Value) { return Value && !(Value & (Value - 1)); } /// isPowerOf2_64 - This function returns true if the argument is a power of two /// > 0 (64 bit edition.) -inline bool isPowerOf2_64(uint64_t Value) { +LLVM_CONSTEXPR inline bool isPowerOf2_64(uint64_t Value) { return Value && !(Value & (Value - int64_t(1L))); } @@ -599,7 +601,7 @@ inline uint32_t FloatToBits(float Float) { /// MinAlign - A and B are either alignments or offsets. Return the minimum /// alignment that may be assumed after adding the two together. -inline uint64_t MinAlign(uint64_t A, uint64_t B) { +LLVM_CONSTEXPR inline uint64_t MinAlign(uint64_t A, uint64_t B) { // The largest power of 2 that divides both A and B. // // Replace "-Value" by "1+~Value" in the following commented code to avoid @@ -667,6 +669,7 @@ inline uint64_t PowerOf2Floor(uint64_t A) { /// alignTo(321, 255, 42) = 552 /// \endcode inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { + assert(Align != 0u && "Align can't be 0."); Skew %= Align; return (Value + Align - 1 - Skew) / Align * Align + Skew; } @@ -674,6 +677,7 @@ inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { /// Returns the largest uint64_t less than or equal to \p Value and is /// \p Skew mod \p Align. \p Align must be non-zero inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { + assert(Align != 0u && "Align can't be 0."); Skew %= Align; return (Value - Skew) / Align * Align + Skew; } @@ -687,7 +691,7 @@ inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) { /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. /// Requires 0 < B <= 32. -template inline int32_t SignExtend32(uint32_t X) { +template LLVM_CONSTEXPR inline int32_t SignExtend32(uint32_t X) { static_assert(B > 0, "Bit width can't be 0."); static_assert(B <= 32, "Bit width out of range."); return int32_t(X << (32 - B)) >> (32 - B); @@ -703,7 +707,7 @@ inline int32_t SignExtend32(uint32_t X, unsigned B) { /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. /// Requires 0 < B < 64. -template inline int64_t SignExtend64(uint64_t x) { +template LLVM_CONSTEXPR inline int64_t SignExtend64(uint64_t x) { static_assert(B > 0, "Bit width can't be 0."); static_assert(B <= 64, "Bit width out of range."); return int64_t(x << (64 - B)) >> (64 - B); -- 2.7.4