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

Unified Diff: runtime/bin/utils.h

Issue 9186035: Use hash map for event handler file descriptor map (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fixes 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 side-by-side diff with in-line comments
Download patch
Index: runtime/bin/utils.h
diff --git a/runtime/bin/utils.h b/runtime/bin/utils.h
new file mode 100644
index 0000000000000000000000000000000000000000..101e97656a12d0ab1c401efe25fbab6d2e157531
--- /dev/null
+++ b/runtime/bin/utils.h
@@ -0,0 +1,56 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#ifndef BIN_UTILS_H_
+#define BIN_UTILS_H_
+
+// 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
+// vm/utils.h?
+
+// ----------------------------------------------------------------------------
+// Bit functions.
+
+// Returns true iff x is a power of 2 (or zero). Cannot be used with the
+// maximally negative value of the type T (the -1 overflows).
+template <typename T>
+inline bool IsPowerOf2(T x) {
+ return (x & (x - 1)) == 0;
+}
+
+// ----------------------------------------------------------------------------
+// Hash functions.
+
+// Thomas Wang, Integer Hash Functions.
+// http://www.concentric.net/~Ttwang/tech/inthash.htm
+inline uint32_t ComputeIntegerHash(uint32_t key) {
+ uint32_t hash = key;
+ hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1;
+ hash = hash ^ (hash >> 12);
+ hash = hash + (hash << 2);
+ hash = hash ^ (hash >> 4);
+ hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11);
+ hash = hash ^ (hash >> 16);
+ return hash;
+}
+
+
+inline uint32_t ComputeLongHash(uint64_t key) {
+ uint64_t hash = key;
+ hash = ~hash + (hash << 18); // hash = (hash << 18) - hash - 1;
+ hash = hash ^ (hash >> 31);
+ hash = hash * 21; // hash = (hash + (hash << 2)) + (hash << 4);
+ hash = hash ^ (hash >> 11);
+ hash = hash + (hash << 6);
+ hash = hash ^ (hash >> 22);
+ return (uint32_t) hash;
+}
+
+
+inline uint32_t ComputePointerHash(void* ptr) {
+ return ComputeIntegerHash(
+ static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr)));
+}
+
+
+#endif // BIN_UTILS_H_

Powered by Google App Engine
This is Rietveld 408576698