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

Unified Diff: include/core/SkChecksum.h

Issue 19500020: Add SkChecksum::Murmur3. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 5 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
« no previous file with comments | « bench/ChecksumBench.cpp ('k') | tests/ChecksumTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkChecksum.h
diff --git a/include/core/SkChecksum.h b/include/core/SkChecksum.h
index 287109fef9d60386dc307a77ff72bd4276c42f45..bf3228f91db8722337e9b06db0fb23fb491d4e37 100644
--- a/include/core/SkChecksum.h
+++ b/include/core/SkChecksum.h
@@ -36,6 +36,42 @@ private:
}
public:
+
+ /**
+ * Calculate 32-bit Murmur hash (murmur3).
+ * This should take 2-3x longer than SkChecksum::Compute, but is a considerably better hash.
+ * See en.wikipedia.org/wiki/MurmurHash.
+ *
+ * @param data Memory address of the data block to be processed. Must be 32-bit aligned.
+ * @param size Size of the data block in bytes. Must be a multiple of 4.
+ * @param seed Initial hash seed. (optional)
+ * @return hash result
+ */
+ static uint32_t Murmur3(const uint32_t* data, size_t bytes, uint32_t seed=0) {
+ SkASSERT(SkIsAlign4(bytes));
+ const size_t words = bytes/4;
+
+ uint32_t hash = seed;
+ for (size_t i = 0; i < words; i++) {
+ uint32_t k = data[i];
+ k *= 0xcc9e2d51;
+ k = (k << 15) | (k >> 17);
+ k *= 0x1b873593;
+
+ hash ^= k;
+ hash = (hash << 13) | (hash >> 19);
+ hash *= 5;
+ hash += 0xe6546b64;
+ }
+ hash ^= bytes;
+ hash ^= hash >> 16;
+ hash *= 0x85ebca6b;
+ hash ^= hash >> 13;
+ hash *= 0xc2b2ae35;
+ hash ^= hash >> 16;
+ return hash;
+ }
+
/**
* Compute a 32-bit checksum for a given data block
*
« no previous file with comments | « bench/ChecksumBench.cpp ('k') | tests/ChecksumTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698