2012-06-27 20:03:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkChecksum_DEFINED
|
|
|
|
#define SkChecksum_DEFINED
|
|
|
|
|
2015-03-20 20:48:42 +00:00
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkTLogic.h"
|
2012-06-27 20:03:16 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2012-07-03 19:44:20 +00:00
|
|
|
class SkChecksum : SkNoncopyable {
|
|
|
|
public:
|
2014-07-10 13:03:46 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2015-01-28 21:28:53 +00:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 version is 2-lines cheaper than Mix, but seems to be sufficient for the font cache.
|
|
|
|
*/
|
|
|
|
static uint32_t CheapMix(uint32_t hash) {
|
|
|
|
hash ^= hash >> 16;
|
|
|
|
hash *= 0x85ebca6b;
|
|
|
|
hash ^= hash >> 16;
|
2014-07-10 13:03:46 +00:00
|
|
|
return hash;
|
|
|
|
}
|
2013-07-23 20:25:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate 32-bit Murmur hash (murmur3).
|
|
|
|
* See en.wikipedia.org/wiki/MurmurHash.
|
|
|
|
*
|
2015-03-20 20:48:42 +00:00
|
|
|
* @param data Memory address of the data block to be processed.
|
|
|
|
* @param size Size of the data block in bytes.
|
2013-07-23 20:25:34 +00:00
|
|
|
* @param seed Initial hash seed. (optional)
|
|
|
|
* @return hash result
|
|
|
|
*/
|
2015-10-21 18:53:27 +00:00
|
|
|
static uint32_t Murmur3(const void* data, size_t bytes, uint32_t seed=0);
|
2012-07-03 19:44:20 +00:00
|
|
|
};
|
|
|
|
|
2015-03-20 20:48:42 +00:00
|
|
|
// SkGoodHash should usually be your first choice in hashing data.
|
|
|
|
// It should be both reasonably fast and high quality.
|
2015-10-15 19:23:01 +00:00
|
|
|
struct SkGoodHash {
|
|
|
|
template <typename K>
|
|
|
|
SK_WHEN(sizeof(K) == 4, uint32_t) operator()(const K& k) const {
|
2015-03-20 20:48:42 +00:00
|
|
|
return SkChecksum::Mix(*(const uint32_t*)&k);
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:23:01 +00:00
|
|
|
template <typename K>
|
|
|
|
SK_WHEN(sizeof(K) != 4, uint32_t) operator()(const K& k) const {
|
|
|
|
return SkChecksum::Murmur3(&k, sizeof(K));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t operator()(const SkString& k) const {
|
|
|
|
return SkChecksum::Murmur3(k.c_str(), k.size());
|
|
|
|
}
|
|
|
|
};
|
2015-03-20 20:48:42 +00:00
|
|
|
|
2012-06-28 00:29:23 +00:00
|
|
|
#endif
|