| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/utils.h" | 5 #include "platform/utils.h" |
| 6 | 6 |
| 7 namespace dart { | 7 namespace dart { |
| 8 | 8 |
| 9 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | 9 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
| 10 // figure 3-3, page 48, where the function is called clp2. | 10 // figure 3-3, page 48, where the function is called clp2. |
| 11 uintptr_t Utils::RoundUpToPowerOfTwo(uintptr_t x) { | 11 uintptr_t Utils::RoundUpToPowerOfTwo(uintptr_t x) { |
| 12 x = x - 1; | 12 x = x - 1; |
| 13 x = x | (x >> 1); | 13 x = x | (x >> 1); |
| 14 x = x | (x >> 2); | 14 x = x | (x >> 2); |
| 15 x = x | (x >> 4); | 15 x = x | (x >> 4); |
| 16 x = x | (x >> 8); | 16 x = x | (x >> 8); |
| 17 x = x | (x >> 16); | 17 x = x | (x >> 16); |
| 18 #if defined(ARCH_IS_64_BIT) | 18 #if defined(ARCH_IS_64_BIT) |
| 19 x = x | (x >> 32); | 19 x = x | (x >> 32); |
| 20 #endif // defined(ARCH_IS_64_BIT) | 20 #endif // defined(ARCH_IS_64_BIT) |
| 21 return x + 1; | 21 return x + 1; |
| 22 } | 22 } |
| 23 | 23 |
| 24 | |
| 25 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | 24 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
| 26 // figure 5-2, page 66, where the function is called pop. | 25 // figure 5-2, page 66, where the function is called pop. |
| 27 int Utils::CountOneBits(uint32_t x) { | 26 int Utils::CountOneBits(uint32_t x) { |
| 28 x = x - ((x >> 1) & 0x55555555); | 27 x = x - ((x >> 1) & 0x55555555); |
| 29 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); | 28 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 30 x = (x + (x >> 4)) & 0x0F0F0F0F; | 29 x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 31 x = x + (x >> 8); | 30 x = x + (x >> 8); |
| 32 x = x + (x >> 16); | 31 x = x + (x >> 16); |
| 33 return static_cast<int>(x & 0x0000003F); | 32 return static_cast<int>(x & 0x0000003F); |
| 34 } | 33 } |
| 35 | 34 |
| 36 | |
| 37 // TODO(koda): Compare to flsll call/intrinsic. | 35 // TODO(koda): Compare to flsll call/intrinsic. |
| 38 int Utils::HighestBit(int64_t v) { | 36 int Utils::HighestBit(int64_t v) { |
| 39 uint64_t x = static_cast<uint64_t>((v > 0) ? v : -v); | 37 uint64_t x = static_cast<uint64_t>((v > 0) ? v : -v); |
| 40 uint64_t t; | 38 uint64_t t; |
| 41 int r = 0; | 39 int r = 0; |
| 42 if ((t = x >> 32) != 0) { | 40 if ((t = x >> 32) != 0) { |
| 43 x = t; | 41 x = t; |
| 44 r += 32; | 42 r += 32; |
| 45 } | 43 } |
| 46 if ((t = x >> 16) != 0) { | 44 if ((t = x >> 16) != 0) { |
| 47 x = t; | 45 x = t; |
| 48 r += 16; | 46 r += 16; |
| 49 } | 47 } |
| 50 if ((t = x >> 8) != 0) { | 48 if ((t = x >> 8) != 0) { |
| 51 x = t; | 49 x = t; |
| 52 r += 8; | 50 r += 8; |
| 53 } | 51 } |
| 54 if ((t = x >> 4) != 0) { | 52 if ((t = x >> 4) != 0) { |
| 55 x = t; | 53 x = t; |
| 56 r += 4; | 54 r += 4; |
| 57 } | 55 } |
| 58 if ((t = x >> 2) != 0) { | 56 if ((t = x >> 2) != 0) { |
| 59 x = t; | 57 x = t; |
| 60 r += 2; | 58 r += 2; |
| 61 } | 59 } |
| 62 if (x > 1) r += 1; | 60 if (x > 1) r += 1; |
| 63 return r; | 61 return r; |
| 64 } | 62 } |
| 65 | 63 |
| 66 | |
| 67 uint32_t Utils::StringHash(const char* data, int length) { | 64 uint32_t Utils::StringHash(const char* data, int length) { |
| 68 // This implementation is based on the public domain MurmurHash | 65 // This implementation is based on the public domain MurmurHash |
| 69 // version 2.0. It assumes that the underlying CPU can read from | 66 // version 2.0. It assumes that the underlying CPU can read from |
| 70 // unaligned addresses. The constants M and R have been determined | 67 // unaligned addresses. The constants M and R have been determined |
| 71 // to work well experimentally. | 68 // to work well experimentally. |
| 72 // TODO(3158902): need to account for unaligned address access on ARM. | 69 // TODO(3158902): need to account for unaligned address access on ARM. |
| 73 const uint32_t M = 0x5bd1e995; | 70 const uint32_t M = 0x5bd1e995; |
| 74 const int R = 24; | 71 const int R = 24; |
| 75 int size = length; | 72 int size = length; |
| 76 uint32_t hash = size; | 73 uint32_t hash = size; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 100 } | 97 } |
| 101 | 98 |
| 102 // Do a few final mixes of the hash to ensure the last few bytes are | 99 // Do a few final mixes of the hash to ensure the last few bytes are |
| 103 // well-incorporated. | 100 // well-incorporated. |
| 104 hash ^= hash >> 13; | 101 hash ^= hash >> 13; |
| 105 hash *= M; | 102 hash *= M; |
| 106 hash ^= hash >> 15; | 103 hash ^= hash >> 15; |
| 107 return hash; | 104 return hash; |
| 108 } | 105 } |
| 109 | 106 |
| 110 | |
| 111 uint32_t Utils::WordHash(intptr_t key) { | 107 uint32_t Utils::WordHash(intptr_t key) { |
| 112 // TODO(iposva): Need to check hash spreading. | 108 // TODO(iposva): Need to check hash spreading. |
| 113 // This example is from http://www.concentric.net/~Ttwang/tech/inthash.htm | 109 // This example is from http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 114 uword a = static_cast<uword>(key); | 110 uword a = static_cast<uword>(key); |
| 115 a = (a + 0x7ed55d16) + (a << 12); | 111 a = (a + 0x7ed55d16) + (a << 12); |
| 116 a = (a ^ 0xc761c23c) ^ (a >> 19); | 112 a = (a ^ 0xc761c23c) ^ (a >> 19); |
| 117 a = (a + 0x165667b1) + (a << 5); | 113 a = (a + 0x165667b1) + (a << 5); |
| 118 a = (a + 0xd3a2646c) ^ (a << 9); | 114 a = (a + 0xd3a2646c) ^ (a << 9); |
| 119 a = (a + 0xfd7046c5) + (a << 3); | 115 a = (a + 0xfd7046c5) + (a << 3); |
| 120 a = (a ^ 0xb55a4f09) ^ (a >> 16); | 116 a = (a ^ 0xb55a4f09) ^ (a >> 16); |
| 121 return static_cast<uint32_t>(a); | 117 return static_cast<uint32_t>(a); |
| 122 } | 118 } |
| 123 | 119 |
| 124 | |
| 125 } // namespace dart | 120 } // namespace dart |
| OLD | NEW |