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