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
|
|
|
|
|
2018-07-16 21:44:06 +00:00
|
|
|
#include "../private/SkNoncopyable.h"
|
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"
|
|
|
|
|
2016-08-08 16:06:27 +00:00
|
|
|
// #include "SkOpts.h"
|
|
|
|
// It's sort of pesky to be able to include SkOpts.h here, so we'll just re-declare what we need.
|
|
|
|
namespace SkOpts {
|
|
|
|
extern uint32_t (*hash_fn)(const void*, size_t, uint32_t);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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 {
|
2016-08-08 16:06:27 +00:00
|
|
|
return SkOpts::hash_fn(&k, sizeof(K), 0);
|
2015-10-15 19:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t operator()(const SkString& k) const {
|
2016-08-08 16:06:27 +00:00
|
|
|
return SkOpts::hash_fn(k.c_str(), k.size(), 0);
|
2015-10-15 19:23:01 +00:00
|
|
|
}
|
|
|
|
};
|
2015-03-20 20:48:42 +00:00
|
|
|
|
2012-06-28 00:29:23 +00:00
|
|
|
#endif
|