[llvm] r271515 - Add assertions to MathExtras max/min functions

Dylan McKay via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 2 05:55:46 PDT 2016


Author: dylanmckay
Date: Thu Jun  2 07:00:34 2016
New Revision: 271515

URL: http://llvm.org/viewvc/llvm-project?rev=271515&view=rev
Log:
Add assertions to MathExtras max/min functions

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=271515&r1=271514&r2=271515&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Thu Jun  2 07:00:34 2016
@@ -312,11 +312,25 @@ inline bool isShiftedUInt(uint64_t x) {
 }
 
 /// Gets the maximum value for a N-bit unsigned integer.
-inline uint64_t maxUIntN(uint64_t N) { return (UINT64_C(1) << N) - 1; }
+inline uint64_t maxUIntN(uint64_t N) {
+  assert(N > 0 && N <= 64 && "integer width out of range");
+
+  return (UINT64_C(1) << N) - 1;
+}
+
 /// Gets the minimum value for a N-bit signed integer.
-inline int64_t minIntN(int64_t N) { return -(INT64_C(1)<<(N-1)); }
+inline int64_t minIntN(int64_t N) {
+  assert(N > 0 && N <= 64 && "integer width out of range");
+
+  return -(INT64_C(1)<<(N-1));
+}
+
 /// Gets the maximum value for a N-bit signed integer.
-inline int64_t maxIntN(int64_t N) { return (INT64_C(1)<<(N-1)) - 1; }
+inline int64_t maxIntN(int64_t N) {
+  assert(N > 0 && N <= 64 && "integer width out of range");
+
+  return (INT64_C(1)<<(N-1)) - 1;
+}
 
 /// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
 /// bit width.




More information about the llvm-commits mailing list