blob: fceb86bc300f1550a6b4be6f0d2fcfe7e7ac09a3 [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
Ben Wagnerd5148e32018-07-16 17:44:06 -040011#include "../private/SkNoncopyable.h"
mtklein02f46cf2015-03-20 13:48:42 -070012#include "SkString.h"
13#include "SkTLogic.h"
junov@chromium.orgef760602012-06-27 20:03:16 +000014#include "SkTypes.h"
15
mtklein4e976072016-08-08 09:06:27 -070016// #include "SkOpts.h"
17// It's sort of pesky to be able to include SkOpts.h here, so we'll just re-declare what we need.
18namespace SkOpts {
19 extern uint32_t (*hash_fn)(const void*, size_t, uint32_t);
20}
21
reed@google.com88db9ef2012-07-03 19:44:20 +000022class SkChecksum : SkNoncopyable {
reed@google.com88db9ef2012-07-03 19:44:20 +000023public:
mtklein67a32712014-07-10 06:03:46 -070024 /**
25 * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
26 * suspect its low bits aren't well mixed.
27 *
28 * This is the Murmur3 finalizer.
29 */
30 static uint32_t Mix(uint32_t hash) {
31 hash ^= hash >> 16;
32 hash *= 0x85ebca6b;
33 hash ^= hash >> 13;
34 hash *= 0xc2b2ae35;
35 hash ^= hash >> 16;
36 return hash;
37 }
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000038
39 /**
reed40dab982015-01-28 13:28:53 -080040 * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
41 * suspect its low bits aren't well mixed.
42 *
43 * This version is 2-lines cheaper than Mix, but seems to be sufficient for the font cache.
44 */
45 static uint32_t CheapMix(uint32_t hash) {
46 hash ^= hash >> 16;
47 hash *= 0x85ebca6b;
48 hash ^= hash >> 16;
49 return hash;
50 }
reed@google.com88db9ef2012-07-03 19:44:20 +000051};
52
mtklein02f46cf2015-03-20 13:48:42 -070053// SkGoodHash should usually be your first choice in hashing data.
54// It should be both reasonably fast and high quality.
mtkleinc8d1dd42015-10-15 12:23:01 -070055struct SkGoodHash {
56 template <typename K>
57 SK_WHEN(sizeof(K) == 4, uint32_t) operator()(const K& k) const {
mtklein02f46cf2015-03-20 13:48:42 -070058 return SkChecksum::Mix(*(const uint32_t*)&k);
59 }
mtklein02f46cf2015-03-20 13:48:42 -070060
mtkleinc8d1dd42015-10-15 12:23:01 -070061 template <typename K>
62 SK_WHEN(sizeof(K) != 4, uint32_t) operator()(const K& k) const {
mtklein4e976072016-08-08 09:06:27 -070063 return SkOpts::hash_fn(&k, sizeof(K), 0);
mtkleinc8d1dd42015-10-15 12:23:01 -070064 }
65
66 uint32_t operator()(const SkString& k) const {
mtklein4e976072016-08-08 09:06:27 -070067 return SkOpts::hash_fn(k.c_str(), k.size(), 0);
mtkleinc8d1dd42015-10-15 12:23:01 -070068 }
69};
mtklein02f46cf2015-03-20 13:48:42 -070070
robertphillips@google.comfffc8d02012-06-28 00:29:23 +000071#endif