| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return 1; | 136 return 1; |
| 137 } | 137 } |
| 138 | 138 |
| 139 | 139 |
| 140 template <typename T> | 140 template <typename T> |
| 141 static int PointerValueCompare(const T* a, const T* b) { | 141 static int PointerValueCompare(const T* a, const T* b) { |
| 142 return Compare<T>(*a, *b); | 142 return Compare<T>(*a, *b); |
| 143 } | 143 } |
| 144 | 144 |
| 145 | 145 |
| 146 // Compare function to compare the object pointer value of two |
| 147 // handlified objects. The handles are passed as pointers to the |
| 148 // handles. |
| 149 template<typename T> class Handle; // Forward declaration. |
| 150 template <typename T> |
| 151 static int HandleObjectPointerCompare(const Handle<T>* a, const Handle<T>* b) { |
| 152 return Compare<T*>(*(*a), *(*b)); |
| 153 } |
| 154 |
| 155 |
| 146 // Returns the smallest power of two which is >= x. If you pass in a | 156 // Returns the smallest power of two which is >= x. If you pass in a |
| 147 // number that is already a power of two, it is returned as is. | 157 // number that is already a power of two, it is returned as is. |
| 148 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | 158 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
| 149 // figure 3-3, page 48, where the function is called clp2. | 159 // figure 3-3, page 48, where the function is called clp2. |
| 150 static inline uint32_t RoundUpToPowerOf2(uint32_t x) { | 160 static inline uint32_t RoundUpToPowerOf2(uint32_t x) { |
| 151 ASSERT(x <= 0x80000000u); | 161 ASSERT(x <= 0x80000000u); |
| 152 x = x - 1; | 162 x = x - 1; |
| 153 x = x | (x >> 1); | 163 x = x | (x >> 1); |
| 154 x = x | (x >> 2); | 164 x = x | (x >> 2); |
| 155 x = x | (x >> 4); | 165 x = x | (x >> 4); |
| 156 x = x | (x >> 8); | 166 x = x | (x >> 8); |
| 157 x = x | (x >> 16); | 167 x = x | (x >> 16); |
| 158 return x + 1; | 168 return x + 1; |
| 159 } | 169 } |
| 160 | 170 |
| 161 | 171 |
| 162 static inline uint32_t RoundDownToPowerOf2(uint32_t x) { | 172 static inline uint32_t RoundDownToPowerOf2(uint32_t x) { |
| 163 uint32_t rounded_up = RoundUpToPowerOf2(x); | 173 uint32_t rounded_up = RoundUpToPowerOf2(x); |
| 164 if (rounded_up > x) return rounded_up >> 1; | 174 if (rounded_up > x) return rounded_up >> 1; |
| 165 return rounded_up; | 175 return rounded_up; |
| 166 } | 176 } |
| 167 | 177 |
| 168 | 178 |
| 169 template <typename T, typename U> | 179 template <typename T, typename U> |
| 170 static inline bool IsAligned(T value, U alignment) { | 180 static inline bool IsAligned(T value, U alignment) { |
| 171 ASSERT(IsPowerOf2(alignment)); | |
| 172 return (value & (alignment - 1)) == 0; | 181 return (value & (alignment - 1)) == 0; |
| 173 } | 182 } |
| 174 | 183 |
| 175 | 184 |
| 176 // Returns true if (addr + offset) is aligned. | 185 // Returns true if (addr + offset) is aligned. |
| 177 static inline bool IsAddressAligned(Address addr, | 186 static inline bool IsAddressAligned(Address addr, |
| 178 intptr_t alignment, | 187 intptr_t alignment, |
| 179 int offset = 0) { | 188 int offset = 0) { |
| 180 intptr_t offs = OffsetFrom(addr + offset); | 189 intptr_t offs = OffsetFrom(addr + offset); |
| 181 return IsAligned(offs, alignment); | 190 return IsAligned(offs, alignment); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; | 259 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; |
| 251 hash = hash ^ (hash >> 12); | 260 hash = hash ^ (hash >> 12); |
| 252 hash = hash + (hash << 2); | 261 hash = hash + (hash << 2); |
| 253 hash = hash ^ (hash >> 4); | 262 hash = hash ^ (hash >> 4); |
| 254 hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11); | 263 hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11); |
| 255 hash = hash ^ (hash >> 16); | 264 hash = hash ^ (hash >> 16); |
| 256 return hash; | 265 return hash; |
| 257 } | 266 } |
| 258 | 267 |
| 259 | 268 |
| 269 static inline uint32_t ComputeLongHash(uint64_t key) { |
| 270 uint64_t hash = key; |
| 271 hash = ~hash + (hash << 18); // hash = (hash << 18) - hash - 1; |
| 272 hash = hash ^ (hash >> 31); |
| 273 hash = hash * 21; // hash = (hash + (hash << 2)) + (hash << 4); |
| 274 hash = hash ^ (hash >> 11); |
| 275 hash = hash + (hash << 6); |
| 276 hash = hash ^ (hash >> 22); |
| 277 return (uint32_t) hash; |
| 278 } |
| 279 |
| 280 |
| 260 static inline uint32_t ComputePointerHash(void* ptr) { | 281 static inline uint32_t ComputePointerHash(void* ptr) { |
| 261 return ComputeIntegerHash( | 282 return ComputeIntegerHash( |
| 262 static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr))); | 283 static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr))); |
| 263 } | 284 } |
| 264 | 285 |
| 265 | 286 |
| 266 // ---------------------------------------------------------------------------- | 287 // ---------------------------------------------------------------------------- |
| 267 // Miscellaneous | 288 // Miscellaneous |
| 268 | 289 |
| 269 // A static resource holds a static instance that can be reserved in | 290 // A static resource holds a static instance that can be reserved in |
| (...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); | 938 ASSERT(element < static_cast<int>(sizeof(T) * CHAR_BIT)); |
| 918 return 1 << element; | 939 return 1 << element; |
| 919 } | 940 } |
| 920 | 941 |
| 921 T bits_; | 942 T bits_; |
| 922 }; | 943 }; |
| 923 | 944 |
| 924 } } // namespace v8::internal | 945 } } // namespace v8::internal |
| 925 | 946 |
| 926 #endif // V8_UTILS_H_ | 947 #endif // V8_UTILS_H_ |
| OLD | NEW |