From 19fcc7494e68843cb9f2de819a7287ee216e3c5e Mon Sep 17 00:00:00 2001 From: mtklein Date: Fri, 11 Jul 2014 08:42:17 -0700 Subject: [PATCH] Slim Skia down to just one murmur3 implementation. BUG=skia: Committed: https://skia.googlesource.com/skia/+/6ac0037b70410ff7d5ce5788bc89314223e1a587 Committed: https://skia.googlesource.com/skia/+/67a3271f0de9ccc32d559b042b862528272047cc Committed: https://skia.googlesource.com/skia/+/53d435990bdb4d14df78013da45a9364d0287ebe CQ_EXTRA_TRYBOTS=tryserver.skia:Perf-Mac10.6-MacMini4.1-GeForce320M-x86_64-Release-Trybot R=reed@google.com, mtklein@google.com Author: mtklein@chromium.org Review URL: https://codereview.chromium.org/376183004 --- src/core/SkChecksum.h | 23 +++++++++++++++++------ src/core/SkImageFilter.cpp | 28 ++-------------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/core/SkChecksum.h b/src/core/SkChecksum.h index fe1e958..9f2ebf4 100644 --- a/src/core/SkChecksum.h +++ b/src/core/SkChecksum.h @@ -62,12 +62,18 @@ public: * @return hash result */ static uint32_t Murmur3(const uint32_t* data, size_t bytes, uint32_t seed=0) { + // Use may_alias to remind the compiler we're intentionally violating strict aliasing, + // and so not to apply strict-aliasing-based optimizations. + typedef uint32_t SK_ATTRIBUTE(may_alias) aliased_uint32_t; + const aliased_uint32_t* safe_data = (const aliased_uint32_t*)data; + SkASSERTF(SkIsAlign4(bytes), "Expected 4-byte multiple, got %zu", bytes); const size_t words = bytes/4; + uint32_t hash = seed; for (size_t i = 0; i < words; i++) { - uint32_t k = data[i]; + uint32_t k = safe_data[i]; k *= 0xcc9e2d51; k = (k << 15) | (k >> 17); k *= 0x1b873593; @@ -95,6 +101,11 @@ public: * @return checksum result */ static uint32_t Compute(const uint32_t* data, size_t size) { + // Use may_alias to remind the compiler we're intentionally violating strict aliasing, + // and so not to apply strict-aliasing-based optimizations. + typedef uint32_t SK_ATTRIBUTE(may_alias) aliased_uint32_t; + const aliased_uint32_t* safe_data = (const aliased_uint32_t*)data; + SkASSERT(SkIsAlign4(size)); /* @@ -104,7 +115,7 @@ public: * sizeof()). */ uintptr_t result = 0; - const uintptr_t* ptr = reinterpret_cast(data); + const uintptr_t* ptr = reinterpret_cast(safe_data); /* * count the number of quad element chunks. This takes into account @@ -120,10 +131,10 @@ public: } size &= ((sizeof(uintptr_t) << 2) - 1); - data = reinterpret_cast(ptr); - const uint32_t* stop = data + (size >> 2); - while (data < stop) { - result = Mash(result, *data++); + safe_data = reinterpret_cast(ptr); + const aliased_uint32_t* stop = safe_data + (size >> 2); + while (safe_data < stop) { + result = Mash(result, *safe_data++); } /* diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp index 64603b4..8a0e734 100644 --- a/src/core/SkImageFilter.cpp +++ b/src/core/SkImageFilter.cpp @@ -8,6 +8,7 @@ #include "SkImageFilter.h" #include "SkBitmap.h" +#include "SkChecksum.h" #include "SkDevice.h" #include "SkReadBuffer.h" #include "SkWriteBuffer.h" @@ -334,31 +335,6 @@ bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy, } #endif -static uint32_t compute_hash(const uint32_t* data, int count) { - uint32_t hash = 0; - - for (int i = 0; i < count; ++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 ^= size; - hash ^= hash >> 16; - hash *= 0x85ebca6b; - hash ^= hash >> 13; - hash *= 0xc2b2ae35; - hash ^= hash >> 16; - - return hash; -} - class CacheImpl : public SkImageFilter::Cache { public: explicit CacheImpl(int minChildren) : fMinChildren(minChildren) { @@ -381,7 +357,7 @@ private: return v.fKey; } static uint32_t Hash(Key key) { - return compute_hash(reinterpret_cast(&key), sizeof(Key) / sizeof(uint32_t)); + return SkChecksum::Murmur3(reinterpret_cast(&key), sizeof(Key)); } }; SkTDynamicHash fData; -- 2.7.4