| 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 // This file declares some utility functions. | 10 // This file declares some utility functions. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #ifndef SUBZERO_SRC_ICEUTILS_H | 14 #ifndef SUBZERO_SRC_ICEUTILS_H |
| 15 #define SUBZERO_SRC_ICEUTILS_H | 15 #define SUBZERO_SRC_ICEUTILS_H |
| 16 | 16 |
| 17 #include <climits> | 17 #include <climits> |
| 18 | 18 |
| 19 namespace Ice { | 19 namespace Ice { |
| 20 | 20 |
| 21 // Similar to bit_cast, but allows copying from types of unrelated | 21 /// Similar to bit_cast, but allows copying from types of unrelated |
| 22 // sizes. This method was introduced to enable the strict aliasing | 22 /// sizes. This method was introduced to enable the strict aliasing |
| 23 // optimizations of GCC 4.4. Basically, GCC mindlessly relies on | 23 /// optimizations of GCC 4.4. Basically, GCC mindlessly relies on |
| 24 // obscure details in the C++ standard that make reinterpret_cast | 24 /// obscure details in the C++ standard that make reinterpret_cast |
| 25 // virtually useless. | 25 /// virtually useless. |
| 26 template <class D, class S> inline D bit_copy(const S &source) { | 26 template <class D, class S> inline D bit_copy(const S &source) { |
| 27 D destination; | 27 D destination; |
| 28 // This use of memcpy is safe: source and destination cannot overlap. | 28 // This use of memcpy is safe: source and destination cannot overlap. |
| 29 memcpy(&destination, reinterpret_cast<const void *>(&source), | 29 memcpy(&destination, reinterpret_cast<const void *>(&source), |
| 30 sizeof(destination)); | 30 sizeof(destination)); |
| 31 return destination; | 31 return destination; |
| 32 } | 32 } |
| 33 | 33 |
| 34 class Utils { | 34 class Utils { |
| 35 Utils() = delete; | 35 Utils() = delete; |
| 36 Utils(const Utils &) = delete; | 36 Utils(const Utils &) = delete; |
| 37 Utils &operator=(const Utils &) = delete; | 37 Utils &operator=(const Utils &) = delete; |
| 38 | 38 |
| 39 public: | 39 public: |
| 40 // Check whether an N-bit two's-complement representation can hold value. | 40 /// Check whether an N-bit two's-complement representation can hold value. |
| 41 template <typename T> static inline bool IsInt(int N, T value) { | 41 template <typename T> static inline bool IsInt(int N, T value) { |
| 42 assert((0 < N) && | 42 assert((0 < N) && |
| 43 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); | 43 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); |
| 44 T limit = static_cast<T>(1) << (N - 1); | 44 T limit = static_cast<T>(1) << (N - 1); |
| 45 return (-limit <= value) && (value < limit); | 45 return (-limit <= value) && (value < limit); |
| 46 } | 46 } |
| 47 | 47 |
| 48 template <typename T> static inline bool IsUint(int N, T value) { | 48 template <typename T> static inline bool IsUint(int N, T value) { |
| 49 assert((0 < N) && | 49 assert((0 < N) && |
| 50 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); | 50 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(value)))); |
| 51 T limit = static_cast<T>(1) << N; | 51 T limit = static_cast<T>(1) << N; |
| 52 return (0 <= value) && (value < limit); | 52 return (0 <= value) && (value < limit); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Check whether the magnitude of value fits in N bits, i.e., whether an | 55 /// Check whether the magnitude of value fits in N bits, i.e., whether an |
| 56 // (N+1)-bit sign-magnitude representation can hold value. | 56 /// (N+1)-bit sign-magnitude representation can hold value. |
| 57 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) { | 57 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) { |
| 58 assert((0 < N) && | 58 assert((0 < N) && |
| 59 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value)))); | 59 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value)))); |
| 60 if (Value < 0) | 60 if (Value < 0) |
| 61 Value = -Value; | 61 Value = -Value; |
| 62 return IsUint(N, Value); | 62 return IsUint(N, Value); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Return true if the addition X + Y will cause integer overflow for | 65 /// Return true if the addition X + Y will cause integer overflow for |
| 66 // integers of type T. | 66 /// integers of type T. |
| 67 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { | 67 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { |
| 68 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || | 68 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || |
| 69 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); | 69 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Return true if X is already aligned by N, where N is a power of 2. | 72 /// Return true if X is already aligned by N, where N is a power of 2. |
| 73 template <typename T> static inline bool IsAligned(T X, intptr_t N) { | 73 template <typename T> static inline bool IsAligned(T X, intptr_t N) { |
| 74 assert(llvm::isPowerOf2_64(N)); | 74 assert(llvm::isPowerOf2_64(N)); |
| 75 return (X & (N - 1)) == 0; | 75 return (X & (N - 1)) == 0; |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Return Value adjusted to the next highest multiple of Alignment. | 78 /// Return Value adjusted to the next highest multiple of Alignment. |
| 79 static inline uint32_t applyAlignment(uint32_t Value, uint32_t Alignment) { | 79 static inline uint32_t applyAlignment(uint32_t Value, uint32_t Alignment) { |
| 80 assert(llvm::isPowerOf2_32(Alignment)); | 80 assert(llvm::isPowerOf2_32(Alignment)); |
| 81 return (Value + Alignment - 1) & -Alignment; | 81 return (Value + Alignment - 1) & -Alignment; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // Return amount which must be added to adjust Pos to the next highest | 84 /// Return amount which must be added to adjust Pos to the next highest |
| 85 // multiple of Align. | 85 /// multiple of Align. |
| 86 static inline uint64_t OffsetToAlignment(uint64_t Pos, uint64_t Align) { | 86 static inline uint64_t OffsetToAlignment(uint64_t Pos, uint64_t Align) { |
| 87 assert(llvm::isPowerOf2_64(Align)); | 87 assert(llvm::isPowerOf2_64(Align)); |
| 88 uint64_t Mod = Pos & (Align - 1); | 88 uint64_t Mod = Pos & (Align - 1); |
| 89 if (Mod == 0) | 89 if (Mod == 0) |
| 90 return 0; | 90 return 0; |
| 91 return Align - Mod; | 91 return Align - Mod; |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Rotate the value bit pattern to the left by shift bits. | 94 /// Rotate the value bit pattern to the left by shift bits. |
| 95 // Precondition: 0 <= shift < 32 | 95 /// Precondition: 0 <= shift < 32 |
| 96 static inline uint32_t rotateLeft32(uint32_t value, uint32_t shift) { | 96 static inline uint32_t rotateLeft32(uint32_t value, uint32_t shift) { |
| 97 if (shift == 0) | 97 if (shift == 0) |
| 98 return value; | 98 return value; |
| 99 return (value << shift) | (value >> (32 - shift)); | 99 return (value << shift) | (value >> (32 - shift)); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Rotate the value bit pattern to the right by shift bits. | 102 /// Rotate the value bit pattern to the right by shift bits. |
| 103 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { | 103 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { |
| 104 if (shift == 0) | 104 if (shift == 0) |
| 105 return value; | 105 return value; |
| 106 return (value >> shift) | (value << (32 - shift)); | 106 return (value >> shift) | (value << (32 - shift)); |
| 107 } | 107 } |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 } // end of namespace Ice | 110 } // end of namespace Ice |
| 111 | 111 |
| 112 #endif // SUBZERO_SRC_ICEUTILS_H | 112 #endif // SUBZERO_SRC_ICEUTILS_H |
| OLD | NEW |