| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkChecksum_DEFINED | 8 #ifndef SkChecksum_DEFINED |
| 9 #define SkChecksum_DEFINED | 9 #define SkChecksum_DEFINED |
| 10 | 10 |
| 11 #include "SkString.h" | 11 #include "SkString.h" |
| 12 #include "SkTLogic.h" | 12 #include "SkTLogic.h" |
| 13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 14 | 14 |
| 15 // #include "SkOpts.h" |
| 16 // It's sort of pesky to be able to include SkOpts.h here, so we'll just re-decl
are what we need. |
| 17 namespace SkOpts { |
| 18 extern uint32_t (*hash_fn)(const void*, size_t, uint32_t); |
| 19 } |
| 20 |
| 15 class SkChecksum : SkNoncopyable { | 21 class SkChecksum : SkNoncopyable { |
| 16 public: | 22 public: |
| 17 /** | 23 /** |
| 18 * uint32_t -> uint32_t hash, useful for when you're about to trucate this h
ash but you | 24 * uint32_t -> uint32_t hash, useful for when you're about to trucate this h
ash but you |
| 19 * suspect its low bits aren't well mixed. | 25 * suspect its low bits aren't well mixed. |
| 20 * | 26 * |
| 21 * This is the Murmur3 finalizer. | 27 * This is the Murmur3 finalizer. |
| 22 */ | 28 */ |
| 23 static uint32_t Mix(uint32_t hash) { | 29 static uint32_t Mix(uint32_t hash) { |
| 24 hash ^= hash >> 16; | 30 hash ^= hash >> 16; |
| 25 hash *= 0x85ebca6b; | 31 hash *= 0x85ebca6b; |
| 26 hash ^= hash >> 13; | 32 hash ^= hash >> 13; |
| 27 hash *= 0xc2b2ae35; | 33 hash *= 0xc2b2ae35; |
| 28 hash ^= hash >> 16; | 34 hash ^= hash >> 16; |
| 29 return hash; | 35 return hash; |
| 30 } | 36 } |
| 31 | 37 |
| 32 /** | 38 /** |
| 33 * uint32_t -> uint32_t hash, useful for when you're about to trucate this h
ash but you | 39 * uint32_t -> uint32_t hash, useful for when you're about to trucate this h
ash but you |
| 34 * suspect its low bits aren't well mixed. | 40 * suspect its low bits aren't well mixed. |
| 35 * | 41 * |
| 36 * This version is 2-lines cheaper than Mix, but seems to be sufficient for
the font cache. | 42 * This version is 2-lines cheaper than Mix, but seems to be sufficient for
the font cache. |
| 37 */ | 43 */ |
| 38 static uint32_t CheapMix(uint32_t hash) { | 44 static uint32_t CheapMix(uint32_t hash) { |
| 39 hash ^= hash >> 16; | 45 hash ^= hash >> 16; |
| 40 hash *= 0x85ebca6b; | 46 hash *= 0x85ebca6b; |
| 41 hash ^= hash >> 16; | 47 hash ^= hash >> 16; |
| 42 return hash; | 48 return hash; |
| 43 } | 49 } |
| 44 | |
| 45 /** | |
| 46 * Calculate 32-bit Murmur hash (murmur3). | |
| 47 * See en.wikipedia.org/wiki/MurmurHash. | |
| 48 * | |
| 49 * @param data Memory address of the data block to be processed. | |
| 50 * @param size Size of the data block in bytes. | |
| 51 * @param seed Initial hash seed. (optional) | |
| 52 * @return hash result | |
| 53 */ | |
| 54 static uint32_t Murmur3(const void* data, size_t bytes, uint32_t seed=0); | |
| 55 }; | 50 }; |
| 56 | 51 |
| 57 // SkGoodHash should usually be your first choice in hashing data. | 52 // SkGoodHash should usually be your first choice in hashing data. |
| 58 // It should be both reasonably fast and high quality. | 53 // It should be both reasonably fast and high quality. |
| 59 struct SkGoodHash { | 54 struct SkGoodHash { |
| 60 template <typename K> | 55 template <typename K> |
| 61 SK_WHEN(sizeof(K) == 4, uint32_t) operator()(const K& k) const { | 56 SK_WHEN(sizeof(K) == 4, uint32_t) operator()(const K& k) const { |
| 62 return SkChecksum::Mix(*(const uint32_t*)&k); | 57 return SkChecksum::Mix(*(const uint32_t*)&k); |
| 63 } | 58 } |
| 64 | 59 |
| 65 template <typename K> | 60 template <typename K> |
| 66 SK_WHEN(sizeof(K) != 4, uint32_t) operator()(const K& k) const { | 61 SK_WHEN(sizeof(K) != 4, uint32_t) operator()(const K& k) const { |
| 67 return SkChecksum::Murmur3(&k, sizeof(K)); | 62 return SkOpts::hash_fn(&k, sizeof(K), 0); |
| 68 } | 63 } |
| 69 | 64 |
| 70 uint32_t operator()(const SkString& k) const { | 65 uint32_t operator()(const SkString& k) const { |
| 71 return SkChecksum::Murmur3(k.c_str(), k.size()); | 66 return SkOpts::hash_fn(k.c_str(), k.size(), 0); |
| 72 } | 67 } |
| 73 }; | 68 }; |
| 74 | 69 |
| 75 #endif | 70 #endif |
| OLD | NEW |