[llvm-commits] [llvm] r100517 - /llvm/trunk/include/llvm/Support/MathExtras.h

Jakob Stoklund Olesen stoklund at 2pi.dk
Mon Apr 5 20:12:43 PDT 2010


Author: stoklund
Date: Mon Apr  5 22:12:43 2010
New Revision: 100517

URL: http://llvm.org/viewvc/llvm-project?rev=100517&view=rev
Log:
Avoid overflowing a signed integer which triggers undefined behaviour.
Overflowing an unsigned integer is fine and behaves as you would expect.
Also fix a pasto, allowing SignExtend64 to take a 64-bit argument.

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=100517&r1=100516&r2=100517&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Mon Apr  5 22:12:43 2010
@@ -459,14 +459,14 @@
 
 /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
 /// Usage int32_t r = SignExtend32<5>(x);
-template <unsigned B> inline int32_t SignExtend32(int32_t x) {
-  return (x << (32 - B)) >> (32 - B);
+template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
+  return int32_t(x << (32 - B)) >> (32 - B);
 }
 
 /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
 /// Usage int64_t r = SignExtend64<5>(x);
-template <unsigned B> inline int64_t SignExtend64(int32_t x) {
-  return (x << (64 - B)) >> (64 - B);
+template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
+  return int64_t(x << (64 - B)) >> (64 - B);
 }
 
 } // End llvm namespace





More information about the llvm-commits mailing list