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. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 // Check whether the magnitude of value fits in N bits, i.e., whether an | 54 // Check whether the magnitude of value fits in N bits, i.e., whether an |
55 // (N+1)-bit sign-magnitude representation can hold value. | 55 // (N+1)-bit sign-magnitude representation can hold value. |
56 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) { | 56 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) { |
57 assert((0 < N) && | 57 assert((0 < N) && |
58 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value)))); | 58 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value)))); |
59 if (Value < 0) | 59 if (Value < 0) |
60 Value = -Value; | 60 Value = -Value; |
61 return IsUint(N, Value); | 61 return IsUint(N, Value); |
62 } | 62 } |
63 | 63 |
| 64 // Return true if the addition X + Y will cause integer overflow for |
| 65 // integers of type T. |
64 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { | 66 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { |
65 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || | 67 return ((X > 0 && Y > 0 && (X > std::numeric_limits<T>::max() - Y)) || |
66 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); | 68 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); |
67 } | 69 } |
68 | 70 |
| 71 // Return true if X is already aligned by N, where N is a power of 2. |
69 template <typename T> static inline bool IsAligned(T X, intptr_t N) { | 72 template <typename T> static inline bool IsAligned(T X, intptr_t N) { |
70 assert(llvm::isPowerOf2_64(N)); | 73 assert(llvm::isPowerOf2_64(N)); |
71 return (X & (N - 1)) == 0; | 74 return (X & (N - 1)) == 0; |
72 } | 75 } |
73 | 76 |
| 77 // Return Value adjusted to the next highest multiple of Alignment. |
| 78 static inline uint32_t applyAlignment(uint32_t Value, uint32_t Alignment) { |
| 79 assert(llvm::isPowerOf2_32(Alignment)); |
| 80 return (Value + Alignment - 1) & -Alignment; |
| 81 } |
| 82 |
| 83 // Return amount which must be added to adjust Pos to the next highest |
| 84 // multiple of Align. |
74 static inline uint64_t OffsetToAlignment(uint64_t Pos, uint64_t Align) { | 85 static inline uint64_t OffsetToAlignment(uint64_t Pos, uint64_t Align) { |
75 assert(llvm::isPowerOf2_64(Align)); | 86 assert(llvm::isPowerOf2_64(Align)); |
76 uint64_t Mod = Pos & (Align - 1); | 87 uint64_t Mod = Pos & (Align - 1); |
77 if (Mod == 0) | 88 if (Mod == 0) |
78 return 0; | 89 return 0; |
79 return Align - Mod; | 90 return Align - Mod; |
80 } | 91 } |
81 | 92 |
| 93 // Rotate the value bit pattern to the left by shift bits. |
82 // Precondition: 0 <= shift < 32 | 94 // Precondition: 0 <= shift < 32 |
83 static inline uint32_t rotateLeft32(uint32_t value, uint32_t shift) { | 95 static inline uint32_t rotateLeft32(uint32_t value, uint32_t shift) { |
84 if (shift == 0) | 96 if (shift == 0) |
85 return value; | 97 return value; |
86 return (value << shift) | (value >> (32 - shift)); | 98 return (value << shift) | (value >> (32 - shift)); |
87 } | 99 } |
88 | 100 |
| 101 // Rotate the value bit pattern to the right by shift bits. |
89 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { | 102 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { |
90 if (shift == 0) | 103 if (shift == 0) |
91 return value; | 104 return value; |
92 return (value >> shift) | (value << (32 - shift)); | 105 return (value >> shift) | (value << (32 - shift)); |
93 } | 106 } |
94 }; | 107 }; |
95 | 108 |
96 } // end of namespace Ice | 109 } // end of namespace Ice |
97 | 110 |
98 #endif // SUBZERO_SRC_ICEUTILS_H | 111 #endif // SUBZERO_SRC_ICEUTILS_H |
OLD | NEW |