| OLD | NEW |
| 1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// | 1 //===- subzero/src/IceUtils.h - Utility functions ---------------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return IsUint(N, Value); | 63 return IsUint(N, Value); |
| 64 } | 64 } |
| 65 | 65 |
| 66 /// Return true if the addition X + Y will cause integer overflow for | 66 /// Return true if the addition X + Y will cause integer overflow for |
| 67 /// integers of type T. | 67 /// integers of type T. |
| 68 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { | 68 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { |
| 69 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || | 69 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || |
| 70 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); | 70 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); |
| 71 } | 71 } |
| 72 | 72 |
| 73 /// Adds x to y and stores the result in sum. Returns true if the addition |
| 74 /// overflowed. |
| 75 static inline bool add_overflow(uint32_t x, uint32_t y, uint32_t *sum) { |
| 76 static_assert(std::is_same<uint32_t, unsigned>::value, "Must match type"); |
| 77 #if __has_builtin(__builtin_uadd_overflow) |
| 78 return __builtin_uadd_overflow(x, y, sum); |
| 79 #else |
| 80 *sum = x + y; |
| 81 return WouldOverflowAdd(x, y); |
| 82 #endif |
| 83 } |
| 84 |
| 73 /// Return true if X is already aligned by N, where N is a power of 2. | 85 /// Return true if X is already aligned by N, where N is a power of 2. |
| 74 template <typename T> static inline bool IsAligned(T X, intptr_t N) { | 86 template <typename T> static inline bool IsAligned(T X, intptr_t N) { |
| 75 assert(llvm::isPowerOf2_64(N)); | 87 assert(llvm::isPowerOf2_64(N)); |
| 76 return (X & (N - 1)) == 0; | 88 return (X & (N - 1)) == 0; |
| 77 } | 89 } |
| 78 | 90 |
| 79 /// Return Value adjusted to the next highest multiple of Alignment. | 91 /// Return Value adjusted to the next highest multiple of Alignment. |
| 80 static inline uint32_t applyAlignment(uint32_t Value, uint32_t Alignment) { | 92 static inline uint32_t applyAlignment(uint32_t Value, uint32_t Alignment) { |
| 81 assert(llvm::isPowerOf2_32(Alignment)); | 93 assert(llvm::isPowerOf2_32(Alignment)); |
| 82 return (Value + Alignment - 1) & -Alignment; | 94 return (Value + Alignment - 1) & -Alignment; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 104 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { | 116 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { |
| 105 if (shift == 0) | 117 if (shift == 0) |
| 106 return value; | 118 return value; |
| 107 return (value >> shift) | (value << (32 - shift)); | 119 return (value >> shift) | (value << (32 - shift)); |
| 108 } | 120 } |
| 109 }; | 121 }; |
| 110 | 122 |
| 111 } // end of namespace Ice | 123 } // end of namespace Ice |
| 112 | 124 |
| 113 #endif // SUBZERO_SRC_ICEUTILS_H | 125 #endif // SUBZERO_SRC_ICEUTILS_H |
| OLD | NEW |