Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_UTILS_H_ |
| 4 | #define ART_SRC_UTILS_H_ |
| 5 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "globals.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame^] | 7 | #include "logging.h" |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 8 | #include "stringprintf.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 9 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 10 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 11 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 12 | template<typename T> |
| 13 | static inline bool IsPowerOfTwo(T x) { |
| 14 | return (x & (x - 1)) == 0; |
| 15 | } |
| 16 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 17 | template<typename T> |
| 18 | static inline bool IsAligned(T x, int n) { |
| 19 | CHECK(IsPowerOfTwo(n)); |
| 20 | return (x & (n - 1)) == 0; |
| 21 | } |
| 22 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 23 | template<typename T> |
| 24 | static inline bool IsAligned(T* x, int n) { |
| 25 | return IsAligned(reinterpret_cast<uintptr_t>(x), n); |
| 26 | } |
| 27 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 28 | // Check whether an N-bit two's-complement representation can hold value. |
| 29 | static inline bool IsInt(int N, word value) { |
| 30 | CHECK_LT(0, N); |
| 31 | CHECK_LT(N, kBitsPerWord); |
| 32 | word limit = static_cast<word>(1) << (N - 1); |
| 33 | return (-limit <= value) && (value < limit); |
| 34 | } |
| 35 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 36 | static inline bool IsUint(int N, word value) { |
| 37 | CHECK_LT(0, N); |
| 38 | CHECK_LT(N, kBitsPerWord); |
| 39 | word limit = static_cast<word>(1) << N; |
| 40 | return (0 <= value) && (value < limit); |
| 41 | } |
| 42 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 43 | static inline bool IsAbsoluteUint(int N, word value) { |
| 44 | CHECK_LT(0, N); |
| 45 | CHECK_LT(N, kBitsPerWord); |
| 46 | if (value < 0) value = -value; |
| 47 | return IsUint(N, value); |
| 48 | } |
| 49 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 50 | static inline int32_t Low16Bits(int32_t value) { |
| 51 | return static_cast<int32_t>(value & 0xffff); |
| 52 | } |
| 53 | |
| 54 | static inline int32_t High16Bits(int32_t value) { |
| 55 | return static_cast<int32_t>(value >> 16); |
| 56 | } |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 57 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 58 | static inline int32_t Low32Bits(int64_t value) { |
| 59 | return static_cast<int32_t>(value); |
| 60 | } |
| 61 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 62 | static inline int32_t High32Bits(int64_t value) { |
| 63 | return static_cast<int32_t>(value >> 32); |
| 64 | } |
| 65 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 66 | template<typename T> |
| 67 | static inline T RoundDown(T x, int n) { |
| 68 | CHECK(IsPowerOfTwo(n)); |
| 69 | return (x & -n); |
| 70 | } |
| 71 | |
| 72 | template<typename T> |
| 73 | static inline T RoundUp(T x, int n) { |
| 74 | return RoundDown(x + n - 1, n); |
| 75 | } |
| 76 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 77 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 78 | // figure 3-3, page 48, where the function is called clp2. |
| 79 | static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { |
| 80 | x = x - 1; |
| 81 | x = x | (x >> 1); |
| 82 | x = x | (x >> 2); |
| 83 | x = x | (x >> 4); |
| 84 | x = x | (x >> 8); |
| 85 | x = x | (x >> 16); |
| 86 | return x + 1; |
| 87 | } |
| 88 | |
| 89 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 90 | // figure 5-2, page 66, where the function is called pop. |
| 91 | static inline int CountOneBits(uint32_t x) { |
| 92 | x = x - ((x >> 1) & 0x55555555); |
| 93 | x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 94 | x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 95 | x = x + (x >> 8); |
| 96 | x = x + (x >> 16); |
| 97 | return static_cast<int>(x & 0x0000003F); |
| 98 | } |
| 99 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame^] | 100 | #define CLZ(x) __builtin_clz(x) |
| 101 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 102 | static inline bool NeedsEscaping(uint16_t ch) { |
| 103 | return (ch < ' ' || ch > '~'); |
| 104 | } |
| 105 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 106 | static inline std::string PrintableChar(uint16_t ch) { |
| 107 | std::string result; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 108 | result += '\''; |
| 109 | if (NeedsEscaping(ch)) { |
| 110 | StringAppendF(&result, "\\u%04x", ch); |
| 111 | } else { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 112 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 113 | } |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 114 | result += '\''; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 115 | return result; |
| 116 | } |
| 117 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 118 | // TODO: assume the content is UTF-8, and show code point escapes? |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 119 | template<typename StringT> |
| 120 | static inline std::string PrintableString(const StringT& s) { |
| 121 | std::string result; |
| 122 | result += '"'; |
| 123 | for (typename StringT::iterator it = s.begin(); it != s.end(); ++it) { |
| 124 | char ch = *it; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 125 | if (NeedsEscaping(ch)) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 126 | StringAppendF(&result, "\\x%02x", ch & 0xff); |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 127 | } else { |
| 128 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | result += '"'; |
| 132 | return result; |
| 133 | } |
| 134 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 135 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 136 | |
| 137 | #endif // ART_SRC_UTILS_H_ |