1ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org/*
2ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org * Copyright 2012 Google Inc.
3ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org *
4ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org * Use of this source code is governed by a BSD-style license that can be
5ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org * found in the LICENSE file.
6ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org */
7ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org
8ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org#ifndef SkChecksum_DEFINED
9ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org#define SkChecksum_DEFINED
10ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org
11ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org#include "SkTypes.h"
12ef76060cbf36032a5bef9cd8d18138704349c3aejunov@chromium.org
13d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com/**
14d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com *  Computes a 32bit checksum from a blob of 32bit aligned data. This is meant
15d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com *  to be very very fast, as it is used internally by the font cache, in
16d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com *  conjuction with the entire raw key. This algorithm does not generate
17d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com *  unique values as well as others (e.g. MD5) but it performs much faster.
18d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com *  Skia's use cases can survive non-unique values (since the entire key is
19d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com *  always available). Clients should only be used in circumstances where speed
20d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com *  over uniqueness is at a premium.
21d42f7587e5972d7e787b5cf2a043c33fbe96091freed@google.com */
2288db9ef0cd8646171f35a3325706291070b5dc55reed@google.comclass SkChecksum : SkNoncopyable {
2388db9ef0cd8646171f35a3325706291070b5dc55reed@google.comprivate:
2488db9ef0cd8646171f35a3325706291070b5dc55reed@google.com    /*
2588db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *  Our Rotate and Mash helpers are meant to automatically do the right
2688db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *  thing depending if sizeof(uintptr_t) is 4 or 8.
2788db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     */
2888db9ef0cd8646171f35a3325706291070b5dc55reed@google.com    enum {
2988db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        ROTR = 17,
3088db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        ROTL = sizeof(uintptr_t) * 8 - ROTR,
3188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        HALFBITS = sizeof(uintptr_t) * 4
3288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com    };
33fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
3488db9ef0cd8646171f35a3325706291070b5dc55reed@google.com    static inline uintptr_t Mash(uintptr_t total, uintptr_t value) {
3588db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        return ((total >> ROTR) | (total << ROTL)) ^ value;
3688db9ef0cd8646171f35a3325706291070b5dc55reed@google.com    }
37fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
3888db9ef0cd8646171f35a3325706291070b5dc55reed@google.compublic:
3967a3271f0de9ccc32d559b042b862528272047ccmtklein    /**
4067a3271f0de9ccc32d559b042b862528272047ccmtklein     * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
4167a3271f0de9ccc32d559b042b862528272047ccmtklein     * suspect its low bits aren't well mixed.
4267a3271f0de9ccc32d559b042b862528272047ccmtklein     *
4367a3271f0de9ccc32d559b042b862528272047ccmtklein     * This is the Murmur3 finalizer.
4467a3271f0de9ccc32d559b042b862528272047ccmtklein     */
4567a3271f0de9ccc32d559b042b862528272047ccmtklein    static uint32_t Mix(uint32_t hash) {
4667a3271f0de9ccc32d559b042b862528272047ccmtklein        hash ^= hash >> 16;
4767a3271f0de9ccc32d559b042b862528272047ccmtklein        hash *= 0x85ebca6b;
4867a3271f0de9ccc32d559b042b862528272047ccmtklein        hash ^= hash >> 13;
4967a3271f0de9ccc32d559b042b862528272047ccmtklein        hash *= 0xc2b2ae35;
5067a3271f0de9ccc32d559b042b862528272047ccmtklein        hash ^= hash >> 16;
5167a3271f0de9ccc32d559b042b862528272047ccmtklein        return hash;
5267a3271f0de9ccc32d559b042b862528272047ccmtklein    }
5370d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org
5470d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org    /**
5570d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     * Calculate 32-bit Murmur hash (murmur3).
5670d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     * This should take 2-3x longer than SkChecksum::Compute, but is a considerably better hash.
5770d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     * See en.wikipedia.org/wiki/MurmurHash.
5870d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     *
5970d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     *  @param data Memory address of the data block to be processed. Must be 32-bit aligned.
6070d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     *  @param size Size of the data block in bytes. Must be a multiple of 4.
6170d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     *  @param seed Initial hash seed. (optional)
6270d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     *  @return hash result
6370d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org     */
64bbf9f6d1cbb94d3486072228781f6e3b6e2252e4reed    static uint32_t Murmur3(const uint32_t* data, size_t bytes, uint32_t seed=0) {
6519fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        // Use may_alias to remind the compiler we're intentionally violating strict aliasing,
6619fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        // and so not to apply strict-aliasing-based optimizations.
6719fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        typedef uint32_t SK_ATTRIBUTE(may_alias) aliased_uint32_t;
6819fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        const aliased_uint32_t* safe_data = (const aliased_uint32_t*)data;
6919fcc7494e68843cb9f2de819a7287ee216e3c5emtklein
7067a3271f0de9ccc32d559b042b862528272047ccmtklein        SkASSERTF(SkIsAlign4(bytes), "Expected 4-byte multiple, got %zu", bytes);
7170d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org        const size_t words = bytes/4;
7270d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org
7319fcc7494e68843cb9f2de819a7287ee216e3c5emtklein
7470d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org        uint32_t hash = seed;
7570d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org        for (size_t i = 0; i < words; i++) {
7619fcc7494e68843cb9f2de819a7287ee216e3c5emtklein            uint32_t k = safe_data[i];
7770d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org            k *= 0xcc9e2d51;
7870d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org            k = (k << 15) | (k >> 17);
7970d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org            k *= 0x1b873593;
8070d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org
8170d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org            hash ^= k;
8270d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org            hash = (hash << 13) | (hash >> 19);
8370d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org            hash *= 5;
8470d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org            hash += 0xe6546b64;
8570d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org        }
8670d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org        hash ^= bytes;
8767a3271f0de9ccc32d559b042b862528272047ccmtklein        return Mix(hash);
8870d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org    }
8970d75ca764e16e15f016e423b85a0fa2a29fb8c7commit-bot@chromium.org
9088db9ef0cd8646171f35a3325706291070b5dc55reed@google.com    /**
9188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *  Compute a 32-bit checksum for a given data block
9288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *
9341d0d2f9059bb72d8f4fab3b69bb15a17ea5dcb0epoger@google.com     *  WARNING: this algorithm is tuned for efficiency, not backward/forward
9441d0d2f9059bb72d8f4fab3b69bb15a17ea5dcb0epoger@google.com     *  compatibility.  It may change at any time, so a checksum generated with
9541d0d2f9059bb72d8f4fab3b69bb15a17ea5dcb0epoger@google.com     *  one version of the Skia code may not match a checksum generated with
9641d0d2f9059bb72d8f4fab3b69bb15a17ea5dcb0epoger@google.com     *  a different version of the Skia code.
9741d0d2f9059bb72d8f4fab3b69bb15a17ea5dcb0epoger@google.com     *
9888db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *  @param data Memory address of the data block to be processed. Must be
9988db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *              32-bit aligned.
10088db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *  @param size Size of the data block in bytes. Must be a multiple of 4.
10188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     *  @return checksum result
10288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com     */
103bbf9f6d1cbb94d3486072228781f6e3b6e2252e4reed    static uint32_t Compute(const uint32_t* data, size_t size) {
10419fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        // Use may_alias to remind the compiler we're intentionally violating strict aliasing,
10519fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        // and so not to apply strict-aliasing-based optimizations.
10619fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        typedef uint32_t SK_ATTRIBUTE(may_alias) aliased_uint32_t;
10719fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        const aliased_uint32_t* safe_data = (const aliased_uint32_t*)data;
10819fcc7494e68843cb9f2de819a7287ee216e3c5emtklein
10988db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        SkASSERT(SkIsAlign4(size));
110fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
11188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        /*
11288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  We want to let the compiler use 32bit or 64bit addressing and math
11388db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  so we use uintptr_t as our magic type. This makes the code a little
11488db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  more obscure (we can't hard-code 32 or 64 anywhere, but have to use
11588db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  sizeof()).
11688db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         */
11788db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        uintptr_t result = 0;
11819fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        const uintptr_t* ptr = reinterpret_cast<const uintptr_t*>(safe_data);
119fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
12088db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        /*
12188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  count the number of quad element chunks. This takes into account
12288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  if we're on a 32bit or 64bit arch, since we use sizeof(uintptr_t)
12388db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  to compute how much to shift-down the size.
12488db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         */
125b158a82bc18b5535224e3ca315ee6d80c7ad899breed@google.com        size_t n4 = size / (sizeof(uintptr_t) << 2);
126b158a82bc18b5535224e3ca315ee6d80c7ad899breed@google.com        for (size_t i = 0; i < n4; ++i) {
12788db9ef0cd8646171f35a3325706291070b5dc55reed@google.com            result = Mash(result, *ptr++);
12888db9ef0cd8646171f35a3325706291070b5dc55reed@google.com            result = Mash(result, *ptr++);
12988db9ef0cd8646171f35a3325706291070b5dc55reed@google.com            result = Mash(result, *ptr++);
13088db9ef0cd8646171f35a3325706291070b5dc55reed@google.com            result = Mash(result, *ptr++);
13188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        }
13288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        size &= ((sizeof(uintptr_t) << 2) - 1);
133fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
13419fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        safe_data = reinterpret_cast<const aliased_uint32_t*>(ptr);
13519fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        const aliased_uint32_t* stop = safe_data + (size >> 2);
13619fcc7494e68843cb9f2de819a7287ee216e3c5emtklein        while (safe_data < stop) {
13719fcc7494e68843cb9f2de819a7287ee216e3c5emtklein            result = Mash(result, *safe_data++);
13888db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        }
139fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
14088db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        /*
14188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  smash us down to 32bits if we were 64. Note that when uintptr_t is
14288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  32bits, this code-path should go away, but I still got a warning
14388db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  when I wrote
14488db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *      result ^= result >> 32;
14588db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  since >>32 is undefined for 32bit ints, hence the wacky HALFBITS
14688db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         *  define.
14788db9ef0cd8646171f35a3325706291070b5dc55reed@google.com         */
14888db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        if (8 == sizeof(result)) {
14988db9ef0cd8646171f35a3325706291070b5dc55reed@google.com            result ^= result >> HALFBITS;
15088db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        }
15188db9ef0cd8646171f35a3325706291070b5dc55reed@google.com        return static_cast<uint32_t>(result);
15288db9ef0cd8646171f35a3325706291070b5dc55reed@google.com    }
15388db9ef0cd8646171f35a3325706291070b5dc55reed@google.com};
15488db9ef0cd8646171f35a3325706291070b5dc55reed@google.com
155fffc8d01b5fca2b2cfc40661c1ff4fdc7a0f9aecrobertphillips@google.com#endif
156