[llvm] r277269 - MathExtras.h: add LLVM_CONSTEXPR where simple

Hubert Tong via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 30 06:38:52 PDT 2016


Author: hubert.reinterpretcast
Date: Sat Jul 30 08:38:51 2016
New Revision: 277269

URL: http://llvm.org/viewvc/llvm-project?rev=277269&view=rev
Log:
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

Modified:
    llvm/trunk/include/llvm/Support/MathExtras.h

Modified: llvm/trunk/include/llvm/Support/MathExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=277269&r1=277268&r2=277269&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Sat Jul 30 08:38:51 2016
@@ -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<uint32_t>(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<uint32_t>(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<unsigned N>
-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<int8_t>(x) == x;
 }
 template<>
-inline bool isInt<16>(int64_t x) {
+LLVM_CONSTEXPR inline bool isInt<16>(int64_t x) {
   return static_cast<int16_t>(x) == x;
 }
 template<>
-inline bool isInt<32>(int64_t x) {
+LLVM_CONSTEXPR inline bool isInt<32>(int64_t x) {
   return static_cast<int32_t>(x) == x;
 }
 
 /// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
 ///                     left by S.
 template<unsigned N, unsigned S>
-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<N, S> 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 <unsigned N>
-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 <unsigned N>
-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) {
   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<uint8_t>(x) == x;
 }
 template<>
-inline bool isUInt<16>(uint64_t x) {
+LLVM_CONSTEXPR inline bool isUInt<16>(uint64_t x) {
   return static_cast<uint16_t>(x) == x;
 }
 template<>
-inline bool isUInt<32>(uint64_t x) {
+LLVM_CONSTEXPR inline bool isUInt<32>(uint64_t x) {
   return static_cast<uint32_t>(x) == x;
 }
 
 /// Checks if a unsigned integer is an N bit number shifted left by S.
 template<unsigned N, unsigned S>
-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,
 /// 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
 
 /// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
 /// Requires 0 < B <= 32.
-template <unsigned B> inline int32_t SignExtend32(uint32_t X) {
+template <unsigned B> 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,
 
 /// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
 /// Requires 0 < B < 64.
-template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
+template <unsigned B> 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);




More information about the llvm-commits mailing list