Skip to content

Commit 4133584

Browse files
author
Justin Lebar
committedJul 18, 2016
Write isUInt using template specializations to work around an incorrect MSVC warning.
Summary: Per D22441, MSVC warns on our old implementation of isUInt<64>. It sees uint64_t(1) << 64 and doesn't realize that it's not going to be executed. Writing as a template specialization is ugly, but prevents the warning. Reviewers: RKSimon Subscribers: majnemer, llvm-commits Differential Revision: https://reviews.llvm.org/D22472 llvm-svn: 275909
1 parent dbf44f5 commit 4133584

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed
 

Diff for: ‎llvm/include/llvm/Support/MathExtras.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,21 @@ inline bool isShiftedInt(int64_t x) {
290290
}
291291

292292
/// isUInt - Checks if an unsigned integer fits into the given bit width.
293-
template<unsigned N>
294-
inline bool isUInt(uint64_t x) {
295-
static_assert(N > 0, "isUInt<0> doesn't make sense.");
296-
return N >= 64 || x < (UINT64_C(1)<<(N));
293+
///
294+
/// This is written as two functions rather than as simply
295+
///
296+
/// return N >= 64 || X < (UINT64_C(1) << N);
297+
///
298+
/// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting
299+
/// left too many places.
300+
template <unsigned N>
301+
inline typename std::enable_if<(N < 64), bool>::type isUInt(uint64_t X) {
302+
static_assert(N > 0, "isUInt<0> doesn't make sense");
303+
return X < (UINT64_C(1) << N);
304+
}
305+
template <unsigned N>
306+
inline typename std::enable_if<N >= 64, bool>::type isUInt(uint64_t X) {
307+
return true;
297308
}
298309

299310
// Template specializations to get better code for common cases.

0 commit comments

Comments
 (0)