diff options
author | mtklein <mtklein@chromium.org> | 2014-07-09 08:49:08 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-07-09 08:49:08 -0700 |
commit | 6ac0037b70410ff7d5ce5788bc89314223e1a587 (patch) | |
tree | 173336caf543fa67acef505c3dfde9a4c72a267f /src/core | |
parent | 40b32be3718f0f2e01c4a21bb0004b7f93670c42 (diff) |
Slim Skia down to just one murmur3 implementation.
BUG=skia:
R=reed@google.com, mtklein@google.com
Author: mtklein@chromium.org
Review URL: https://codereview.chromium.org/376183004
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkChecksum.h | 23 | ||||
-rw-r--r-- | src/core/SkImageFilter.cpp | 28 | ||||
-rw-r--r-- | src/core/SkScaledImageCache.cpp | 29 |
3 files changed, 20 insertions, 60 deletions
diff --git a/src/core/SkChecksum.h b/src/core/SkChecksum.h index bf3228f91d..fe1e9584a2 100644 --- a/src/core/SkChecksum.h +++ b/src/core/SkChecksum.h @@ -36,6 +36,20 @@ private: } public: + /** + * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you + * suspect its low bits aren't well mixed. + * + * This is the Murmur3 finalizer. + */ + static uint32_t Mix(uint32_t hash) { + hash ^= hash >> 16; + hash *= 0x85ebca6b; + hash ^= hash >> 13; + hash *= 0xc2b2ae35; + hash ^= hash >> 16; + return hash; + } /** * Calculate 32-bit Murmur hash (murmur3). @@ -48,7 +62,7 @@ public: * @return hash result */ static uint32_t Murmur3(const uint32_t* data, size_t bytes, uint32_t seed=0) { - SkASSERT(SkIsAlign4(bytes)); + SkASSERTF(SkIsAlign4(bytes), "Expected 4-byte multiple, got %zu", bytes); const size_t words = bytes/4; uint32_t hash = seed; @@ -64,12 +78,7 @@ public: hash += 0xe6546b64; } hash ^= bytes; - hash ^= hash >> 16; - hash *= 0x85ebca6b; - hash ^= hash >> 13; - hash *= 0xc2b2ae35; - hash ^= hash >> 16; - return hash; + return Mix(hash); } /** diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp index 64603b4cb5..8a0e734b24 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<const uint32_t*>(&key), sizeof(Key) / sizeof(uint32_t)); + return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); } }; SkTDynamicHash<Value, Key> fData; diff --git a/src/core/SkScaledImageCache.cpp b/src/core/SkScaledImageCache.cpp index eda4871711..1e7f51dd9e 100644 --- a/src/core/SkScaledImageCache.cpp +++ b/src/core/SkScaledImageCache.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "SkChecksum.h" #include "SkScaledImageCache.h" #include "SkMipMap.h" #include "SkPixelRef.h" @@ -29,32 +30,6 @@ static inline SkScaledImageCache::Rec* id_to_rec(SkScaledImageCache::ID* id) { return reinterpret_cast<SkScaledImageCache::Rec*>(id); } - // Implemented from en.wikipedia.org/wiki/MurmurHash. -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; -} - struct SkScaledImageCache::Key { Key(uint32_t genID, SkScalar scaleX, @@ -64,7 +39,7 @@ struct SkScaledImageCache::Key { , fScaleX(scaleX) , fScaleY(scaleY) , fBounds(bounds) { - fHash = compute_hash(&fGenID, 7); + fHash = SkChecksum::Murmur3(&fGenID, 28); } bool operator<(const Key& other) const { |