Chromium Code Reviews| 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 BIN_UTILS_H_ | |
| 6 #define BIN_UTILS_H_ | |
| 7 | |
| 8 // TODO(sgjesse): Should this be moved to platform/utils.h? How about | |
|
Mads Ager (chromium)
2012/01/13 13:33:16
I think we should share this with vm/utils.h.
Pow
Søren Gjesse
2012/01/13 14:51:23
Started http://codereview.chromium.org/9209001/ fo
Søren Gjesse
2012/01/16 14:14:58
Removed bin/utils.h and used PowerOfTwo and WordHa
| |
| 9 // vm/utils.h? | |
| 10 | |
| 11 // ---------------------------------------------------------------------------- | |
| 12 // Bit functions. | |
| 13 | |
| 14 // Returns true iff x is a power of 2 (or zero). Cannot be used with the | |
| 15 // maximally negative value of the type T (the -1 overflows). | |
| 16 template <typename T> | |
| 17 inline bool IsPowerOf2(T x) { | |
| 18 return (x & (x - 1)) == 0; | |
| 19 } | |
| 20 | |
| 21 // ---------------------------------------------------------------------------- | |
| 22 // Hash functions. | |
| 23 | |
| 24 // Thomas Wang, Integer Hash Functions. | |
| 25 // http://www.concentric.net/~Ttwang/tech/inthash.htm | |
| 26 inline uint32_t ComputeIntegerHash(uint32_t key) { | |
| 27 uint32_t hash = key; | |
| 28 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; | |
| 29 hash = hash ^ (hash >> 12); | |
| 30 hash = hash + (hash << 2); | |
| 31 hash = hash ^ (hash >> 4); | |
| 32 hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11); | |
| 33 hash = hash ^ (hash >> 16); | |
| 34 return hash; | |
| 35 } | |
| 36 | |
| 37 | |
| 38 inline uint32_t ComputeLongHash(uint64_t key) { | |
| 39 uint64_t hash = key; | |
| 40 hash = ~hash + (hash << 18); // hash = (hash << 18) - hash - 1; | |
| 41 hash = hash ^ (hash >> 31); | |
| 42 hash = hash * 21; // hash = (hash + (hash << 2)) + (hash << 4); | |
| 43 hash = hash ^ (hash >> 11); | |
| 44 hash = hash + (hash << 6); | |
| 45 hash = hash ^ (hash >> 22); | |
| 46 return (uint32_t) hash; | |
| 47 } | |
| 48 | |
| 49 | |
| 50 inline uint32_t ComputePointerHash(void* ptr) { | |
| 51 return ComputeIntegerHash( | |
| 52 static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr))); | |
| 53 } | |
| 54 | |
| 55 | |
| 56 #endif // BIN_UTILS_H_ | |
| OLD | NEW |