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