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" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 7 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 8 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 9 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 10 | template<typename T> |
| 11 | static inline bool IsPowerOfTwo(T x) { |
| 12 | return (x & (x - 1)) == 0; |
| 13 | } |
| 14 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 15 | template<typename T> |
| 16 | static inline bool IsAligned(T x, int n) { |
| 17 | CHECK(IsPowerOfTwo(n)); |
| 18 | return (x & (n - 1)) == 0; |
| 19 | } |
| 20 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 21 | template<typename T> |
| 22 | static inline bool IsAligned(T* x, int n) { |
| 23 | return IsAligned(reinterpret_cast<uintptr_t>(x), n); |
| 24 | } |
| 25 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 26 | // Check whether an N-bit two's-complement representation can hold value. |
| 27 | static inline bool IsInt(int N, word value) { |
| 28 | CHECK_LT(0, N); |
| 29 | CHECK_LT(N, kBitsPerWord); |
| 30 | word limit = static_cast<word>(1) << (N - 1); |
| 31 | return (-limit <= value) && (value < limit); |
| 32 | } |
| 33 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 34 | static inline bool IsUint(int N, word value) { |
| 35 | CHECK_LT(0, N); |
| 36 | CHECK_LT(N, kBitsPerWord); |
| 37 | word limit = static_cast<word>(1) << N; |
| 38 | return (0 <= value) && (value < limit); |
| 39 | } |
| 40 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 41 | static inline bool IsAbsoluteUint(int N, word value) { |
| 42 | CHECK_LT(0, N); |
| 43 | CHECK_LT(N, kBitsPerWord); |
| 44 | if (value < 0) value = -value; |
| 45 | return IsUint(N, value); |
| 46 | } |
| 47 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 48 | static inline int32_t Low16Bits(int32_t value) { |
| 49 | return static_cast<int32_t>(value & 0xffff); |
| 50 | } |
| 51 | |
| 52 | static inline int32_t High16Bits(int32_t value) { |
| 53 | return static_cast<int32_t>(value >> 16); |
| 54 | } |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 55 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 56 | static inline int32_t Low32Bits(int64_t value) { |
| 57 | return static_cast<int32_t>(value); |
| 58 | } |
| 59 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 60 | static inline int32_t High32Bits(int64_t value) { |
| 61 | return static_cast<int32_t>(value >> 32); |
| 62 | } |
| 63 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 64 | template<typename T> |
| 65 | static inline T RoundDown(T x, int n) { |
| 66 | CHECK(IsPowerOfTwo(n)); |
| 67 | return (x & -n); |
| 68 | } |
| 69 | |
| 70 | template<typename T> |
| 71 | static inline T RoundUp(T x, int n) { |
| 72 | return RoundDown(x + n - 1, n); |
| 73 | } |
| 74 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 75 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 76 | // figure 3-3, page 48, where the function is called clp2. |
| 77 | static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { |
| 78 | x = x - 1; |
| 79 | x = x | (x >> 1); |
| 80 | x = x | (x >> 2); |
| 81 | x = x | (x >> 4); |
| 82 | x = x | (x >> 8); |
| 83 | x = x | (x >> 16); |
| 84 | return x + 1; |
| 85 | } |
| 86 | |
| 87 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 88 | // figure 5-2, page 66, where the function is called pop. |
| 89 | static inline int CountOneBits(uint32_t x) { |
| 90 | x = x - ((x >> 1) & 0x55555555); |
| 91 | x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 92 | x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 93 | x = x + (x >> 8); |
| 94 | x = x + (x >> 16); |
| 95 | return static_cast<int>(x & 0x0000003F); |
| 96 | } |
| 97 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 98 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 99 | |
| 100 | #endif // ART_SRC_UTILS_H_ |