skia2/src/core/SkChecksum.h

156 lines
5.5 KiB
C
Raw Normal View History

/*
* 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
#include "SkTypes.h"
/**
* Computes a 32bit checksum from a blob of 32bit aligned data. This is meant
* to be very very fast, as it is used internally by the font cache, in
* conjuction with the entire raw key. This algorithm does not generate
* unique values as well as others (e.g. MD5) but it performs much faster.
* Skia's use cases can survive non-unique values (since the entire key is
* always available). Clients should only be used in circumstances where speed
* over uniqueness is at a premium.
*/
class SkChecksum : SkNoncopyable {
private:
/*
* Our Rotate and Mash helpers are meant to automatically do the right
* thing depending if sizeof(uintptr_t) is 4 or 8.
*/
enum {
ROTR = 17,
ROTL = sizeof(uintptr_t) * 8 - ROTR,
HALFBITS = sizeof(uintptr_t) * 4
};
static inline uintptr_t Mash(uintptr_t total, uintptr_t value) {
return ((total >> ROTR) | (total << ROTL)) ^ value;
}
public:
/**
* 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;
return hash;
}
/**
* Calculate 32-bit Murmur hash (murmur3).
* This should take 2-3x longer than SkChecksum::Compute, but is a considerably better hash.
* See en.wikipedia.org/wiki/MurmurHash.
*
* @param data Memory address of the data block to be processed. Must be 32-bit aligned.
* @param size Size of the data block in bytes. Must be a multiple of 4.
* @param seed Initial hash seed. (optional)
* @return hash result
*/
Revert of Slim Skia down to just one murmur3 implementation. (https://codereview.chromium.org/376183004/) Reason for revert: broke 10.6 compile [17:13:56.863876] [4/336] CXX obj/src/core/core.SkBBoxHierarchyRecord.o [17:13:56.863997] FAILED: c++ -MMD -MF obj/src/core/core.SkBBoxRecord.o.d -DSK_INTERNAL -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=1 -DSK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_BUILD_FOR_MAC -DSK_USE_POSIX_THREADS -DSK_RELEASE -DNDEBUG -I../../include/config -I../../include/core -I../../include/pathops -I../../include/pipe -I../../include/ports -I../../include/utils -I../../include/xml -I../../src/core -I../../src/sfnt -I../../src/image -I../../src/opts -I../../src/utils -I../../include/utils/mac -I../../include/gpu -I../../src/gpu -fasm-blocks -mpascal-strings -O3 -gdwarf-2 -Wnewline-eof -mmacosx-version-min=10.6 -arch x86_64 -mssse3 -fvisibility=hidden -fvisibility-inlines-hidden -c ../../src/core/SkBBoxRecord.cpp -o obj/src/core/core.SkBBoxRecord.o [17:13:56.864085] In file included from ../../src/core/SkPictureFlat.h:14, [17:13:56.864130] from ../../src/core/SkPictureData.h:14, [17:13:56.864173] from ../../src/core/SkPictureRecord.h:18, [17:13:56.864217] from ../../src/core/SkBBoxRecord.h:12, [17:13:56.864261] from ../../src/core/SkBBoxRecord.cpp:9: [17:13:56.864336] ../../src/core/SkChecksum.h: In static member function ‘static uint32_t SkChecksum::Compute(const uint32_t*, size_t)’: [17:13:56.864397] ../../src/core/SkChecksum.h:127: error: invalid conversion from ‘const uint32_t*’ to ‘const uint32_t*’ [17:13:56.864462] ../../src/core/SkChecksum.h:128: error: invalid conversion from ‘const uint32_t*’ to ‘const uint32_t*’ [17:13:56.864510] ../../src/core/SkChecksum.h:129: error: comparison between distinct pointer types ‘const uint32_t*’ and ‘const uint32_t*’ lacks a cast Original issue's description: > Slim Skia down to just one murmur3 implementation. > > BUG=skia: > > Committed: https://skia.googlesource.com/skia/+/6ac0037b70410ff7d5ce5788bc89314223e1a587 > > Committed: https://skia.googlesource.com/skia/+/67a3271f0de9ccc32d559b042b862528272047cc > > Committed: https://skia.googlesource.com/skia/+/53d435990bdb4d14df78013da45a9364d0287ebe R=mtklein@google.com, mtklein@chromium.org TBR=mtklein@chromium.org, mtklein@google.com NOTREECHECKS=true NOTRY=true BUG=skia: Author: reed@google.com Review URL: https://codereview.chromium.org/381253003
2014-07-10 21:29:43 +00:00
static uint32_t Murmur3(const uint32_t* data, size_t bytes, uint32_t seed=0) {
// Use may_alias to remind the compiler we're intentionally violating strict aliasing,
// and so not to apply strict-aliasing-based optimizations.
typedef uint32_t SK_ATTRIBUTE(may_alias) aliased_uint32_t;
const aliased_uint32_t* safe_data = (const aliased_uint32_t*)data;
SkASSERTF(SkIsAlign4(bytes), "Expected 4-byte multiple, got %zu", bytes);
const size_t words = bytes/4;
uint32_t hash = seed;
for (size_t i = 0; i < words; i++) {
uint32_t k = safe_data[i];
k *= 0xcc9e2d51;
k = (k << 15) | (k >> 17);
k *= 0x1b873593;
hash ^= k;
hash = (hash << 13) | (hash >> 19);
hash *= 5;
hash += 0xe6546b64;
}
hash ^= bytes;
return Mix(hash);
}
/**
* Compute a 32-bit checksum for a given data block
*
* WARNING: this algorithm is tuned for efficiency, not backward/forward
* compatibility. It may change at any time, so a checksum generated with
* one version of the Skia code may not match a checksum generated with
* a different version of the Skia code.
*
* @param data Memory address of the data block to be processed. Must be
* 32-bit aligned.
* @param size Size of the data block in bytes. Must be a multiple of 4.
* @return checksum result
*/
Revert of Slim Skia down to just one murmur3 implementation. (https://codereview.chromium.org/376183004/) Reason for revert: broke 10.6 compile [17:13:56.863876] [4/336] CXX obj/src/core/core.SkBBoxHierarchyRecord.o [17:13:56.863997] FAILED: c++ -MMD -MF obj/src/core/core.SkBBoxRecord.o.d -DSK_INTERNAL -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR_TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=1 -DSK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_BUILD_FOR_MAC -DSK_USE_POSIX_THREADS -DSK_RELEASE -DNDEBUG -I../../include/config -I../../include/core -I../../include/pathops -I../../include/pipe -I../../include/ports -I../../include/utils -I../../include/xml -I../../src/core -I../../src/sfnt -I../../src/image -I../../src/opts -I../../src/utils -I../../include/utils/mac -I../../include/gpu -I../../src/gpu -fasm-blocks -mpascal-strings -O3 -gdwarf-2 -Wnewline-eof -mmacosx-version-min=10.6 -arch x86_64 -mssse3 -fvisibility=hidden -fvisibility-inlines-hidden -c ../../src/core/SkBBoxRecord.cpp -o obj/src/core/core.SkBBoxRecord.o [17:13:56.864085] In file included from ../../src/core/SkPictureFlat.h:14, [17:13:56.864130] from ../../src/core/SkPictureData.h:14, [17:13:56.864173] from ../../src/core/SkPictureRecord.h:18, [17:13:56.864217] from ../../src/core/SkBBoxRecord.h:12, [17:13:56.864261] from ../../src/core/SkBBoxRecord.cpp:9: [17:13:56.864336] ../../src/core/SkChecksum.h: In static member function ‘static uint32_t SkChecksum::Compute(const uint32_t*, size_t)’: [17:13:56.864397] ../../src/core/SkChecksum.h:127: error: invalid conversion from ‘const uint32_t*’ to ‘const uint32_t*’ [17:13:56.864462] ../../src/core/SkChecksum.h:128: error: invalid conversion from ‘const uint32_t*’ to ‘const uint32_t*’ [17:13:56.864510] ../../src/core/SkChecksum.h:129: error: comparison between distinct pointer types ‘const uint32_t*’ and ‘const uint32_t*’ lacks a cast Original issue's description: > Slim Skia down to just one murmur3 implementation. > > BUG=skia: > > Committed: https://skia.googlesource.com/skia/+/6ac0037b70410ff7d5ce5788bc89314223e1a587 > > Committed: https://skia.googlesource.com/skia/+/67a3271f0de9ccc32d559b042b862528272047cc > > Committed: https://skia.googlesource.com/skia/+/53d435990bdb4d14df78013da45a9364d0287ebe R=mtklein@google.com, mtklein@chromium.org TBR=mtklein@chromium.org, mtklein@google.com NOTREECHECKS=true NOTRY=true BUG=skia: Author: reed@google.com Review URL: https://codereview.chromium.org/381253003
2014-07-10 21:29:43 +00:00
static uint32_t Compute(const uint32_t* data, size_t size) {
// Use may_alias to remind the compiler we're intentionally violating strict aliasing,
// and so not to apply strict-aliasing-based optimizations.
typedef uint32_t SK_ATTRIBUTE(may_alias) aliased_uint32_t;
const aliased_uint32_t* safe_data = (const aliased_uint32_t*)data;
SkASSERT(SkIsAlign4(size));
/*
* We want to let the compiler use 32bit or 64bit addressing and math
* so we use uintptr_t as our magic type. This makes the code a little
* more obscure (we can't hard-code 32 or 64 anywhere, but have to use
* sizeof()).
*/
uintptr_t result = 0;
const uintptr_t* ptr = reinterpret_cast<const uintptr_t*>(safe_data);
/*
* count the number of quad element chunks. This takes into account
* if we're on a 32bit or 64bit arch, since we use sizeof(uintptr_t)
* to compute how much to shift-down the size.
*/
size_t n4 = size / (sizeof(uintptr_t) << 2);
for (size_t i = 0; i < n4; ++i) {
result = Mash(result, *ptr++);
result = Mash(result, *ptr++);
result = Mash(result, *ptr++);
result = Mash(result, *ptr++);
}
size &= ((sizeof(uintptr_t) << 2) - 1);
safe_data = reinterpret_cast<const aliased_uint32_t*>(ptr);
const aliased_uint32_t* stop = safe_data + (size >> 2);
while (safe_data < stop) {
result = Mash(result, *safe_data++);
}
/*
* smash us down to 32bits if we were 64. Note that when uintptr_t is
* 32bits, this code-path should go away, but I still got a warning
* when I wrote
* result ^= result >> 32;
* since >>32 is undefined for 32bit ints, hence the wacky HALFBITS
* define.
*/
if (8 == sizeof(result)) {
result ^= result >> HALFBITS;
}
return static_cast<uint32_t>(result);
}
};
#endif