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 |
11 /// This file declares some utility functions. | 11 /// This file declares some utility functions. |
12 /// | 12 /// |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #ifndef SUBZERO_SRC_ICEUTILS_H | 15 #ifndef SUBZERO_SRC_ICEUTILS_H |
16 #define SUBZERO_SRC_ICEUTILS_H | 16 #define SUBZERO_SRC_ICEUTILS_H |
17 | 17 |
18 #include <climits> | 18 #include <climits> |
19 | 19 |
20 namespace Ice { | 20 namespace Ice { |
21 | 21 |
22 /// Similar to bit_cast, but allows copying from types of unrelated | 22 /// Similar to bit_cast, but allows copying from types of unrelated sizes. This |
23 /// sizes. This method was introduced to enable the strict aliasing | 23 /// method was introduced to enable the strict aliasing optimizations of GCC |
24 /// optimizations of GCC 4.4. Basically, GCC mindlessly relies on | 24 /// 4.4. Basically, GCC mindlessly relies on obscure details in the C++ standard |
25 /// obscure details in the C++ standard that make reinterpret_cast | 25 /// that make reinterpret_cast virtually useless. |
26 /// virtually useless. | |
27 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) { |
28 D destination; | 27 D destination; |
29 // This use of memcpy is safe: source and destination cannot overlap. | 28 // This use of memcpy is safe: source and destination cannot overlap. |
30 memcpy(&destination, reinterpret_cast<const void *>(&source), | 29 memcpy(&destination, reinterpret_cast<const void *>(&source), |
31 sizeof(destination)); | 30 sizeof(destination)); |
32 return destination; | 31 return destination; |
33 } | 32 } |
34 | 33 |
35 class Utils { | 34 class Utils { |
36 Utils() = delete; | 35 Utils() = delete; |
(...skipping 19 matching lines...) Expand all Loading... |
56 /// 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 |
57 /// (N+1)-bit sign-magnitude representation can hold value. | 56 /// (N+1)-bit sign-magnitude representation can hold value. |
58 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) { | 57 template <typename T> static inline bool IsAbsoluteUint(int N, T Value) { |
59 assert((0 < N) && | 58 assert((0 < N) && |
60 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value)))); | 59 (static_cast<unsigned int>(N) < (CHAR_BIT * sizeof(Value)))); |
61 if (Value < 0) | 60 if (Value < 0) |
62 Value = -Value; | 61 Value = -Value; |
63 return IsUint(N, Value); | 62 return IsUint(N, Value); |
64 } | 63 } |
65 | 64 |
66 /// 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 integers |
67 /// integers of type T. | 66 /// of type T. |
68 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { | 67 template <typename T> static inline bool WouldOverflowAdd(T X, T Y) { |
69 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)) || |
70 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); | 69 (X < 0 && Y < 0 && (X < std::numeric_limits<T>::min() - Y))); |
71 } | 70 } |
72 | 71 |
73 /// Adds x to y and stores the result in sum. Returns true if the addition | 72 /// Adds x to y and stores the result in sum. Returns true if the addition |
74 /// overflowed. | 73 /// overflowed. |
75 static inline bool add_overflow(uint32_t x, uint32_t y, uint32_t *sum) { | 74 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"); | 75 static_assert(std::is_same<uint32_t, unsigned>::value, "Must match type"); |
77 #if __has_builtin(__builtin_uadd_overflow) | 76 #if __has_builtin(__builtin_uadd_overflow) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { | 115 static inline uint32_t rotateRight32(uint32_t value, uint32_t shift) { |
117 if (shift == 0) | 116 if (shift == 0) |
118 return value; | 117 return value; |
119 return (value >> shift) | (value << (32 - shift)); | 118 return (value >> shift) | (value << (32 - shift)); |
120 } | 119 } |
121 }; | 120 }; |
122 | 121 |
123 } // end of namespace Ice | 122 } // end of namespace Ice |
124 | 123 |
125 #endif // SUBZERO_SRC_ICEUTILS_H | 124 #endif // SUBZERO_SRC_ICEUTILS_H |
OLD | NEW |