blob: 6289a444ae78c3993f89a50e23cb14ec3f3d7b85 [file] [log] [blame]
junov@chromium.orgef760602012-06-27 20:03:16 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkChecksum_DEFINED
9#define SkChecksum_DEFINED
10
mtklein02f46cf2015-03-20 13:48:42 -070011#include "SkString.h"
12#include "SkTLogic.h"
junov@chromium.orgef760602012-06-27 20:03:16 +000013#include "SkTypes.h"
14
reed@google.com88db9ef2012-07-03 19:44:20 +000015class SkChecksum : SkNoncopyable {
reed@google.com88db9ef2012-07-03 19:44:20 +000016public:
mtklein67a32712014-07-10 06:03:46 -070017 /**
18 * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
19 * suspect its low bits aren't well mixed.
20 *
21 * This is the Murmur3 finalizer.
22 */
23 static uint32_t Mix(uint32_t hash) {
24 hash ^= hash >> 16;
25 hash *= 0x85ebca6b;
26 hash ^= hash >> 13;
27 hash *= 0xc2b2ae35;
28 hash ^= hash >> 16;
29 return hash;
30 }
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000031
32 /**
reed40dab982015-01-28 13:28:53 -080033 * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
34 * suspect its low bits aren't well mixed.
35 *
36 * This version is 2-lines cheaper than Mix, but seems to be sufficient for the font cache.
37 */
38 static uint32_t CheapMix(uint32_t hash) {
39 hash ^= hash >> 16;
40 hash *= 0x85ebca6b;
41 hash ^= hash >> 16;
42 return hash;
43 }
44
45 /**
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000046 * Calculate 32-bit Murmur hash (murmur3).
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000047 * See en.wikipedia.org/wiki/MurmurHash.
48 *
mtklein02f46cf2015-03-20 13:48:42 -070049 * @param data Memory address of the data block to be processed.
50 * @param size Size of the data block in bytes.
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000051 * @param seed Initial hash seed. (optional)
52 * @return hash result
53 */
mtkleine71000f2015-10-21 11:53:27 -070054 static uint32_t Murmur3(const void* data, size_t bytes, uint32_t seed=0);
reed@google.com88db9ef2012-07-03 19:44:20 +000055};
56
mtklein02f46cf2015-03-20 13:48:42 -070057// SkGoodHash should usually be your first choice in hashing data.
58// It should be both reasonably fast and high quality.
mtkleinc8d1dd42015-10-15 12:23:01 -070059struct SkGoodHash {
60 template <typename K>
61 SK_WHEN(sizeof(K) == 4, uint32_t) operator()(const K& k) const {
mtklein02f46cf2015-03-20 13:48:42 -070062 return SkChecksum::Mix(*(const uint32_t*)&k);
63 }
mtklein02f46cf2015-03-20 13:48:42 -070064
mtkleinc8d1dd42015-10-15 12:23:01 -070065 template <typename K>
66 SK_WHEN(sizeof(K) != 4, uint32_t) operator()(const K& k) const {
67 return SkChecksum::Murmur3(&k, sizeof(K));
68 }
69
70 uint32_t operator()(const SkString& k) const {
71 return SkChecksum::Murmur3(k.c_str(), k.size());
72 }
73};
mtklein02f46cf2015-03-20 13:48:42 -070074
robertphillips@google.comfffc8d02012-06-28 00:29:23 +000075#endif