[MathExtras] Fix UB in minIntN
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 18 Jul 2016 17:03:09 +0000 (17:03 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 18 Jul 2016 17:03:09 +0000 (17:03 +0000)
We negated a value with a signed type which invited problems when that
value was the most negative signed number.  Use an unsigned type
for the value instead.  It will compute the same twos complement
result without the UB.

llvm-svn: 275815

llvm/include/llvm/Support/MathExtras.h

index cf033e3..5c816ac 100644 (file)
@@ -337,7 +337,7 @@ inline uint64_t maxUIntN(uint64_t N) {
 inline int64_t minIntN(int64_t N) {
   assert(N > 0 && N <= 64 && "integer width out of range");
 
-  return -(INT64_C(1)<<(N-1));
+  return -(UINT64_C(1)<<(N-1));
 }
 
 /// Gets the maximum value for a N-bit signed integer.