junov@chromium.org | ef76060 | 2012-06-27 20:03:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "SkTypes.h" |
| 12 | |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 13 | class SkChecksum : SkNoncopyable { |
| 14 | private: |
| 15 | /* |
| 16 | * Our Rotate and Mash helpers are meant to automatically do the right |
| 17 | * thing depending if sizeof(uintptr_t) is 4 or 8. |
| 18 | */ |
| 19 | enum { |
| 20 | ROTR = 17, |
| 21 | ROTL = sizeof(uintptr_t) * 8 - ROTR, |
| 22 | HALFBITS = sizeof(uintptr_t) * 4 |
| 23 | }; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 24 | |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 25 | static inline uintptr_t Mash(uintptr_t total, uintptr_t value) { |
| 26 | return ((total >> ROTR) | (total << ROTL)) ^ value; |
| 27 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 28 | |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 29 | public: |
| 30 | /** |
| 31 | * Compute a 32-bit checksum for a given data block |
| 32 | * |
| 33 | * @param data Memory address of the data block to be processed. Must be |
| 34 | * 32-bit aligned. |
| 35 | * @param size Size of the data block in bytes. Must be a multiple of 4. |
| 36 | * @return checksum result |
| 37 | */ |
| 38 | static uint32_t Compute(const uint32_t* data, size_t size) { |
| 39 | SkASSERT(SkIsAlign4(size)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 40 | |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 41 | /* |
| 42 | * We want to let the compiler use 32bit or 64bit addressing and math |
| 43 | * so we use uintptr_t as our magic type. This makes the code a little |
| 44 | * more obscure (we can't hard-code 32 or 64 anywhere, but have to use |
| 45 | * sizeof()). |
| 46 | */ |
| 47 | uintptr_t result = 0; |
| 48 | const uintptr_t* ptr = reinterpret_cast<const uintptr_t*>(data); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 49 | |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 50 | /* |
| 51 | * count the number of quad element chunks. This takes into account |
| 52 | * if we're on a 32bit or 64bit arch, since we use sizeof(uintptr_t) |
| 53 | * to compute how much to shift-down the size. |
| 54 | */ |
reed@google.com | b158a82 | 2012-07-09 13:13:23 +0000 | [diff] [blame] | 55 | size_t n4 = size / (sizeof(uintptr_t) << 2); |
| 56 | for (size_t i = 0; i < n4; ++i) { |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 57 | result = Mash(result, *ptr++); |
| 58 | result = Mash(result, *ptr++); |
| 59 | result = Mash(result, *ptr++); |
| 60 | result = Mash(result, *ptr++); |
| 61 | } |
| 62 | size &= ((sizeof(uintptr_t) << 2) - 1); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 63 | |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 64 | data = reinterpret_cast<const uint32_t*>(ptr); |
| 65 | const uint32_t* stop = data + (size >> 2); |
| 66 | while (data < stop) { |
| 67 | result = Mash(result, *data++); |
| 68 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 69 | |
reed@google.com | 88db9ef | 2012-07-03 19:44:20 +0000 | [diff] [blame] | 70 | /* |
| 71 | * smash us down to 32bits if we were 64. Note that when uintptr_t is |
| 72 | * 32bits, this code-path should go away, but I still got a warning |
| 73 | * when I wrote |
| 74 | * result ^= result >> 32; |
| 75 | * since >>32 is undefined for 32bit ints, hence the wacky HALFBITS |
| 76 | * define. |
| 77 | */ |
| 78 | if (8 == sizeof(result)) { |
| 79 | result ^= result >> HALFBITS; |
| 80 | } |
| 81 | return static_cast<uint32_t>(result); |
| 82 | } |
| 83 | }; |
| 84 | |
robertphillips@google.com | fffc8d0 | 2012-06-28 00:29:23 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |