OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_UTILS_H_ | 5 #ifndef V8_UTILS_H_ |
6 #define V8_UTILS_H_ | 6 #define V8_UTILS_H_ |
7 | 7 |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 template<class T, int shift, int size> | 233 template<class T, int shift, int size> |
234 class BitField : public BitFieldBase<T, shift, size, uint32_t> { }; | 234 class BitField : public BitFieldBase<T, shift, size, uint32_t> { }; |
235 | 235 |
236 | 236 |
237 template<class T, int shift, int size> | 237 template<class T, int shift, int size> |
238 class BitField64 : public BitFieldBase<T, shift, size, uint64_t> { }; | 238 class BitField64 : public BitFieldBase<T, shift, size, uint64_t> { }; |
239 | 239 |
240 | 240 |
241 // ---------------------------------------------------------------------------- | 241 // ---------------------------------------------------------------------------- |
| 242 // BitSetComputer is a help template for encoding and decoding information for |
| 243 // a variable number of items in an array. |
| 244 // |
| 245 // To encode boolean data in a smi array you would use: |
| 246 // typedef BitSetComputer<bool, 1, kSmiValueSize, uint32_t> BoolComputer; |
| 247 // |
| 248 template <class T, int kBitsPerItem, int kBitsPerWord, class U> |
| 249 class BitSetComputer { |
| 250 public: |
| 251 static const int kItemsPerWord = kBitsPerWord / kBitsPerItem; |
| 252 static const int kMask = (1 << kBitsPerItem) - 1; |
| 253 |
| 254 // The number of array elements required to embed T information for each item. |
| 255 static int word_count(int items) { |
| 256 if (items == 0) return 0; |
| 257 return (items - 1) / kItemsPerWord + 1; |
| 258 } |
| 259 |
| 260 // The array index to look at for item. |
| 261 static int index(int base_index, int item) { |
| 262 return base_index + item / kItemsPerWord; |
| 263 } |
| 264 |
| 265 // Extract T data for a given item from data. |
| 266 static T decode(U data, int item) { |
| 267 return static_cast<T>((data >> shift(item)) & kMask); |
| 268 } |
| 269 |
| 270 // Return the encoding for a store of value for item in previous. |
| 271 static U encode(U previous, int item, T value) { |
| 272 int set_bits = (static_cast<int>(value) << shift(item)); |
| 273 return (previous & ~(kMask << shift(item))) | set_bits; |
| 274 } |
| 275 |
| 276 static int shift(int item) { return (item % kItemsPerWord) * kBitsPerItem; } |
| 277 }; |
| 278 |
| 279 |
| 280 // ---------------------------------------------------------------------------- |
242 // Hash function. | 281 // Hash function. |
243 | 282 |
244 static const uint32_t kZeroHashSeed = 0; | 283 static const uint32_t kZeroHashSeed = 0; |
245 | 284 |
246 // Thomas Wang, Integer Hash Functions. | 285 // Thomas Wang, Integer Hash Functions. |
247 // http://www.concentric.net/~Ttwang/tech/inthash.htm | 286 // http://www.concentric.net/~Ttwang/tech/inthash.htm |
248 inline uint32_t ComputeIntegerHash(uint32_t key, uint32_t seed) { | 287 inline uint32_t ComputeIntegerHash(uint32_t key, uint32_t seed) { |
249 uint32_t hash = key; | 288 uint32_t hash = key; |
250 hash = hash ^ seed; | 289 hash = hash ^ seed; |
251 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; | 290 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; |
(...skipping 1237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1489 // Takes the address of the limit variable in order to find out where | 1528 // Takes the address of the limit variable in order to find out where |
1490 // the top of stack is right now. | 1529 // the top of stack is right now. |
1491 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); | 1530 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); |
1492 return limit; | 1531 return limit; |
1493 } | 1532 } |
1494 | 1533 |
1495 } // namespace internal | 1534 } // namespace internal |
1496 } // namespace v8 | 1535 } // namespace v8 |
1497 | 1536 |
1498 #endif // V8_UTILS_H_ | 1537 #endif // V8_UTILS_H_ |
OLD | NEW |