Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: runtime/vm/utils.cc

Issue 9209001: Move utils.h and utils.cc from runtime/vm to runtime/platform (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addresed new comments from ager@ Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/utils.h ('k') | runtime/vm/utils_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, 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 #include "vm/utils.h"
6
7 namespace dart {
8
9 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
10 // figure 3-3, page 48, where the function is called clp2.
11 uint32_t Utils::RoundUpToPowerOfTwo(uint32_t x) {
12 x = x - 1;
13 x = x | (x >> 1);
14 x = x | (x >> 2);
15 x = x | (x >> 4);
16 x = x | (x >> 8);
17 x = x | (x >> 16);
18 return x + 1;
19 }
20
21
22 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
23 // figure 5-2, page 66, where the function is called pop.
24 int Utils::CountOneBits(uint32_t x) {
25 x = x - ((x >> 1) & 0x55555555);
26 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
27 x = (x + (x >> 4)) & 0x0F0F0F0F;
28 x = x + (x >> 8);
29 x = x + (x >> 16);
30 return static_cast<int>(x & 0x0000003F);
31 }
32
33
34 uint32_t Utils::StringHash(const char* data, int length) {
35 // This implementation is based on the public domain MurmurHash
36 // version 2.0. It assumes that the underlying CPU can read from
37 // unaligned addresses. The constants M and R have been determined
38 // to work well experimentally.
39 // TODO(3158902): need to account for unaligned address access on ARM.
40 const uint32_t M = 0x5bd1e995;
41 const int R = 24;
42 int size = length;
43 uint32_t hash = size;
44
45 // Mix four bytes at a time into the hash.
46 const uint8_t* cursor = reinterpret_cast<const uint8_t*>(data);
47 while (size >= 4) {
48 uint32_t part = *reinterpret_cast<const uint32_t*>(cursor);
49 part *= M;
50 part ^= part >> R;
51 part *= M;
52 hash *= M;
53 hash ^= part;
54 cursor += 4;
55 size -= 4;
56 }
57
58 // Handle the last few bytes of the string.
59 switch (size) {
60 case 3:
61 hash ^= cursor[2] << 16;
62 case 2:
63 hash ^= cursor[1] << 8;
64 case 1:
65 hash ^= cursor[0];
66 hash *= M;
67 }
68
69 // Do a few final mixes of the hash to ensure the last few bytes are
70 // well-incorporated.
71 hash ^= hash >> 13;
72 hash *= M;
73 hash ^= hash >> 15;
74 return hash;
75 }
76
77
78 uint32_t WordHash(word key) {
79 // TODO(iposva): Need to check hash spreading.
80 // This example is from http://www.concentric.net/~Ttwang/tech/inthash.htm
81 uword a = static_cast<uword>(key);
82 a = (a + 0x7ed55d16) + (a << 12);
83 a = (a ^ 0xc761c23c) ^ (a >> 19);
84 a = (a + 0x165667b1) + (a << 5);
85 a = (a + 0xd3a2646c) ^ (a << 9);
86 a = (a + 0xfd7046c5) + (a << 3);
87 a = (a ^ 0xb55a4f09) ^ (a >> 16);
88 return static_cast<uint32_t>(a);
89 }
90
91 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/utils.h ('k') | runtime/vm/utils_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698