blob: e5cc3d101f5b3ee3391d38aed0a60f2f9f613400 [file] [log] [blame]
junov@chromium.orgef760602012-06-27 20:03:16 +00001/*
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.com88db9ef2012-07-03 19:44:20 +000013class SkChecksum : SkNoncopyable {
14private:
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.comfbfcd562012-08-23 18:09:54 +000024
reed@google.com88db9ef2012-07-03 19:44:20 +000025 static inline uintptr_t Mash(uintptr_t total, uintptr_t value) {
26 return ((total >> ROTR) | (total << ROTL)) ^ value;
27 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028
reed@google.com88db9ef2012-07-03 19:44:20 +000029public:
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.comfbfcd562012-08-23 18:09:54 +000040
reed@google.com88db9ef2012-07-03 19:44:20 +000041 /*
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.comfbfcd562012-08-23 18:09:54 +000049
reed@google.com88db9ef2012-07-03 19:44:20 +000050 /*
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.comb158a822012-07-09 13:13:23 +000055 size_t n4 = size / (sizeof(uintptr_t) << 2);
56 for (size_t i = 0; i < n4; ++i) {
reed@google.com88db9ef2012-07-03 19:44:20 +000057 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.comfbfcd562012-08-23 18:09:54 +000063
reed@google.com88db9ef2012-07-03 19:44:20 +000064 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.comfbfcd562012-08-23 18:09:54 +000069
reed@google.com88db9ef2012-07-03 19:44:20 +000070 /*
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.comfffc8d02012-06-28 00:29:23 +000085#endif
86