| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #ifndef VM_UTILS_H_ | |
| 6 #define VM_UTILS_H_ | |
| 7 | |
| 8 #include "platform/assert.h" | |
| 9 #include "vm/globals.h" | |
| 10 | |
| 11 namespace dart { | |
| 12 | |
| 13 class Utils { | |
| 14 public: | |
| 15 template<typename T> | |
| 16 static inline T Minimum(T x, T y) { | |
| 17 return x < y ? x : y; | |
| 18 } | |
| 19 | |
| 20 template<typename T> | |
| 21 static inline T Maximum(T x, T y) { | |
| 22 return x > y ? x : y; | |
| 23 } | |
| 24 | |
| 25 template<typename T> | |
| 26 static inline T Abs(T x) { | |
| 27 if (x < 0) return -x; | |
| 28 return x; | |
| 29 } | |
| 30 | |
| 31 template<typename T> | |
| 32 static inline bool IsPowerOfTwo(T x) { | |
| 33 return (x & (x - 1)) == 0; | |
| 34 } | |
| 35 | |
| 36 template<typename T> | |
| 37 static inline int ShiftForPowerOfTwo(T x) { | |
| 38 ASSERT(IsPowerOfTwo(x)); | |
| 39 int num_shifts = 0; | |
| 40 while (x > 1) { | |
| 41 num_shifts++; | |
| 42 x = x >> 1; | |
| 43 } | |
| 44 return num_shifts; | |
| 45 } | |
| 46 | |
| 47 template<typename T> | |
| 48 static inline bool IsAligned(T x, int n) { | |
| 49 ASSERT(IsPowerOfTwo(n)); | |
| 50 return (x & (n - 1)) == 0; | |
| 51 } | |
| 52 | |
| 53 template<typename T> | |
| 54 static inline bool IsAligned(T* x, int n) { | |
| 55 return IsAligned(reinterpret_cast<uword>(x), n); | |
| 56 } | |
| 57 | |
| 58 template<typename T> | |
| 59 static inline T RoundDown(T x, int n) { | |
| 60 ASSERT(IsPowerOfTwo(n)); | |
| 61 return (x & -n); | |
| 62 } | |
| 63 | |
| 64 template<typename T> | |
| 65 static inline T* RoundDown(T* x, int n) { | |
| 66 return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uword>(x), n)); | |
| 67 } | |
| 68 | |
| 69 template<typename T> | |
| 70 static inline T RoundUp(T x, int n) { | |
| 71 return RoundDown(x + n - 1, n); | |
| 72 } | |
| 73 | |
| 74 template<typename T> | |
| 75 static inline T* RoundUp(T* x, int n) { | |
| 76 return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uword>(x), n)); | |
| 77 } | |
| 78 | |
| 79 static uint32_t RoundUpToPowerOfTwo(uint32_t x); | |
| 80 static int CountOneBits(uint32_t x); | |
| 81 | |
| 82 // Computes a hash value for the given string. | |
| 83 static uint32_t StringHash(const char* data, int length); | |
| 84 | |
| 85 // Computes a hash value for the given word. | |
| 86 static uint32_t WordHash(word key); | |
| 87 | |
| 88 // Check whether an N-bit two's-complement representation can hold value. | |
| 89 static inline bool IsInt(int N, word value) { | |
| 90 ASSERT((0 < N) && (N < kBitsPerWord)); | |
| 91 word limit = static_cast<word>(1) << (N - 1); | |
| 92 return (-limit <= value) && (value < limit); | |
| 93 } | |
| 94 | |
| 95 static inline bool IsUint(int N, word value) { | |
| 96 ASSERT((0 < N) && (N < kBitsPerWord)); | |
| 97 word limit = static_cast<word>(1) << N; | |
| 98 return (0 <= value) && (value < limit); | |
| 99 } | |
| 100 | |
| 101 // Check whether the magnitude of value fits in N bits, i.e., whether an | |
| 102 // (N+1)-bit sign-magnitude representation can hold value. | |
| 103 static inline bool IsAbsoluteUint(int N, word value) { | |
| 104 ASSERT((0 < N) && (N < kBitsPerWord)); | |
| 105 if (value < 0) value = -value; | |
| 106 return IsUint(N, value); | |
| 107 } | |
| 108 | |
| 109 static inline int32_t Low16Bits(int32_t value) { | |
| 110 return static_cast<int32_t>(value & 0xffff); | |
| 111 } | |
| 112 | |
| 113 static inline int32_t High16Bits(int32_t value) { | |
| 114 return static_cast<int32_t>(value >> 16); | |
| 115 } | |
| 116 | |
| 117 static inline int32_t Low32Bits(int64_t value) { | |
| 118 return static_cast<int32_t>(value); | |
| 119 } | |
| 120 | |
| 121 static inline int32_t High32Bits(int64_t value) { | |
| 122 return static_cast<int32_t>(value >> 32); | |
| 123 } | |
| 124 | |
| 125 static inline int64_t LowHighTo64Bits(uint32_t low, int32_t high) { | |
| 126 return (static_cast<int64_t>(high) << 32) | (low & 0x0ffffffffLL); | |
| 127 } | |
| 128 | |
| 129 static bool IsDecimalDigit(char c) { | |
| 130 return ('0' <= c) && (c <= '9'); | |
| 131 } | |
| 132 | |
| 133 static bool IsHexDigit(char c) { | |
| 134 return IsDecimalDigit(c) | |
| 135 || (('A' <= c) && (c <= 'F')) | |
| 136 || (('a' <= c) && (c <= 'f')); | |
| 137 } | |
| 138 | |
| 139 static int HexDigitToInt(char c) { | |
| 140 ASSERT(IsHexDigit(c)); | |
| 141 if (IsDecimalDigit(c)) return c - '0'; | |
| 142 if (('A' <= c) && (c <= 'F')) return 10 + (c - 'A'); | |
| 143 return 10 + (c - 'a'); | |
| 144 } | |
| 145 | |
| 146 static char IntToHexDigit(int i) { | |
| 147 ASSERT(0 <= i && i < 16); | |
| 148 if (i < 10) return static_cast<char>('0' + i); | |
| 149 return static_cast<char>('A' + (i - 10)); | |
| 150 } | |
| 151 }; | |
| 152 | |
| 153 } // namespace dart | |
| 154 | |
| 155 #endif // VM_UTILS_H_ | |
| OLD | NEW |