Debian Codegraph @

src/core/SkChecksum.cpp

raw
1 /*
2 * Copyright 2023 Google LLC
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 #include "src/core/SkChecksum.h"
8
9 #include "include/private/base/SkAssert.h"
10
11 #include <cstring>
12
13 // wyhash, a fast and good hash function, from https://github.com/wangyi-fudan/wyhash
14
15 // 128bit multiply function
16 static inline void _wymum(uint64_t* A, uint64_t* B) {
17 #if defined(__SIZEOF_INT128__)
18 __uint128_t r = *A;
19 r *= *B;
20 *A = (uint64_t)r;
21 *B = (uint64_t)(r >> 64);
22 #elif defined(_MSC_VER) && defined(_M_X64)
23 *A = _umul128(*A, *B, B);
24 #else
25 uint64_t ha = *A >> 32, hb = *B >> 32, la = (uint32_t)*A, lb = (uint32_t)*B, hi, lo;
26 uint64_t rh = ha * hb, rm0 = ha * lb, rm1 = hb * la, rl = la * lb, t = rl + (rm0 << 32),
27 c = t < rl;
28 lo = t + (rm1 << 32);
29 c += lo < t;
30 hi = rh + (rm0 >> 32) + (rm1 >> 32) + c;
31 *A = lo;
32 *B = hi;
33 #endif
34 }
35
36 // multiply and xor mix function, aka MUM
37 static inline uint64_t _wymix(uint64_t A, uint64_t B) {
38 _wymum(&A, &B);
39 return A ^ B;
40 }
41
42 // read functions
43 static inline uint64_t _wyr8(const uint8_t* p) {
44 uint64_t v;
45 memcpy(&v, p, 8);
46 return v;
47 }
48
49 static inline uint64_t _wyr4(const uint8_t* p) {
50 uint32_t v;
51 memcpy(&v, p, 4);
52 return v;
53 }
54
55 static inline uint64_t _wyr3(const uint8_t* p, size_t k) {
56 return (((uint64_t)p[0]) << 16) | (((uint64_t)p[k >> 1]) << 8) | p[k - 1];
57 }
58
59 // wyhash main function
60 static inline uint64_t wyhash(const void* key, size_t len, uint64_t seed, const uint64_t* secret) {
61 const uint8_t* p = (const uint8_t*)key;
62 seed ^= _wymix(seed ^ secret[0], secret[1]);
63 uint64_t a, b;
64 if (len <= 16) SK_LIKELY {
65 if (len >= 4) SK_LIKELY {
66 a = (_wyr4(p) << 32) | _wyr4(p + ((len >> 3) << 2));
67 b = (_wyr4(p + len - 4) << 32) | _wyr4(p + len - 4 - ((len >> 3) << 2));
68 } else if (len > 0) SK_LIKELY {
69 a = _wyr3(p, len);
70 b = 0;
71 } else
72 a = b = 0;
73 } else {
74 size_t i = len;
75 if (i > 48) SK_UNLIKELY {
76 uint64_t see1 = seed, see2 = seed;
77 do {
78 seed = _wymix(_wyr8(p) ^ secret[1], _wyr8(p + 8) ^ seed);
79 see1 = _wymix(_wyr8(p + 16) ^ secret[2], _wyr8(p + 24) ^ see1);
80 see2 = _wymix(_wyr8(p + 32) ^ secret[3], _wyr8(p + 40) ^ see2);
81 p += 48;
82 i -= 48;
83 } while (i > 48) SK_LIKELY;
84 seed ^= see1 ^ see2;
85 }
86 while (i > 16) SK_UNLIKELY {
87 seed = _wymix(_wyr8(p) ^ secret[1], _wyr8(p + 8) ^ seed);
88 i -= 16;
89 p += 16;
90 }
91 a = _wyr8(p + i - 16);
92 b = _wyr8(p + i - 8);
93 }
94 a ^= secret[1];
95 b ^= seed;
96 _wymum(&a, &b);
97 return _wymix(a ^ secret[0] ^ len, b ^ secret[1]);
98 }
99
100 // the default secret parameters
101 static const uint64_t _wyp[4] = {
102 0xa0761d6478bd642full, 0xe7037ed1a0b428dbull, 0x8ebc6af09c88c6e3ull, 0x589965cc75374cc3ull};
103
104 namespace SkChecksum {
105
106 uint32_t Hash32(const void *data, size_t bytes, uint32_t seed) {
107 return static_cast<uint32_t>(wyhash(data, bytes, seed, _wyp));
108 }
109
110 uint64_t Hash64(const void *data, size_t bytes, uint64_t seed) {
111 return wyhash(data, bytes, seed, _wyp);
112 }
113
114 } // namespace SkChecksum