| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Hash.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <bit> |
| 8 | #include <cstring> |
| 9 | |
| 10 | #include <zlib.h> |
| 11 | |
| 12 | #include "Common/CPUDetect.h" |
| 13 | #include "Common/Intrinsics.h" |
| 14 | |
| 15 | #ifdef _M_ARM_64 |
| 16 | #ifdef _MSC_VER |
| 17 | #include <intrin.h> |
| 18 | #else |
| 19 | #include <arm_acle.h> |
| 20 | #endif |
| 21 | #endif |
| 22 | |
| 23 | namespace Common |
| 24 | { |
| 25 | u32 HashAdler32(const u8* data, size_t len) |
| 26 | { |
| 27 | // Use fast implementation from zlib-ng |
| 28 | return adler32_z(1, data, len); |
| 29 | } |
| 30 | |
| 31 | // Stupid hash - but can't go back now :) |
| 32 | // Don't use for new things. At least it's reasonably fast. |
| 33 | u32 HashEctor(const u8* data, size_t len) |
| 34 | { |
| 35 | u32 crc = 0; |
| 36 | |
| 37 | for (size_t i = 0; i < len; i++) |
| 38 | { |
| 39 | crc ^= data[i]; |
| 40 | crc = (crc << 3) | (crc >> 29); |
| 41 | } |
| 42 | |
| 43 | return crc; |
| 44 | } |
| 45 | |
| 46 | #ifdef _ARCH_64 |
| 47 | |
| 48 | //----------------------------------------------------------------------------- |
| 49 | // Block read - if your platform needs to do endian-swapping or can only |
| 50 | // handle aligned reads, do the conversion here |
| 51 | |
| 52 | static u64 getblock(const u64* p, int i) |
| 53 | { |
| 54 | return p[i]; |
| 55 | } |
| 56 | |
| 57 | //---------- |
| 58 | // Block mix - combine the key bits with the hash bits and scramble everything |
| 59 | |
| 60 | static void bmix64(u64& h1, u64& h2, u64& k1, u64& k2, u64& c1, u64& c2) |
| 61 | { |
| 62 | k1 *= c1; |
| 63 | k1 = std::rotl(k1, 23); |
| 64 | k1 *= c2; |
| 65 | h1 ^= k1; |
| 66 | h1 += h2; |
| 67 | |
| 68 | h2 = std::rotl(h2, 41); |
| 69 | |
| 70 | k2 *= c2; |
| 71 | k2 = std::rotl(k2, 23); |
| 72 | k2 *= c1; |
| 73 | h2 ^= k2; |
| 74 | h2 += h1; |
| 75 | |
| 76 | h1 = h1 * 3 + 0x52dce729; |
| 77 | h2 = h2 * 3 + 0x38495ab5; |
| 78 | |
| 79 | c1 = c1 * 5 + 0x7b7d159c; |
| 80 | c2 = c2 * 5 + 0x6bce6396; |
| 81 | } |
| 82 | |
| 83 | //---------- |
| 84 | // Finalization mix - avalanches all bits to within 0.05% bias |
| 85 | |
| 86 | static u64 fmix64(u64 k) |
| 87 | { |
| 88 | k ^= k >> 33; |
| 89 | k *= 0xff51afd7ed558ccd; |
| 90 | k ^= k >> 33; |
| 91 | k *= 0xc4ceb9fe1a85ec53; |
| 92 | k ^= k >> 33; |
| 93 | |
| 94 | return k; |
| 95 | } |
| 96 | |
| 97 | static u64 GetMurmurHash3(const u8* src, u32 len, u32 samples) |
| 98 | { |
| 99 | const u8* data = (const u8*)src; |
| 100 | const int nblocks = len / 16; |
| 101 | u32 Step = (len / 8); |
| 102 | if (samples == 0) |
| 103 | samples = std::max(Step, 1u); |
| 104 | Step = Step / samples; |
| 105 | if (Step < 1) |
| 106 | Step = 1; |
| 107 | |
| 108 | u64 h1 = 0x9368e53c2f6af274; |
| 109 | u64 h2 = 0x586dcd208f7cd3fd; |
| 110 | |
| 111 | u64 c1 = 0x87c37b91114253d5; |
| 112 | u64 c2 = 0x4cf5ad432745937f; |
| 113 | |
| 114 | //---------- |
| 115 | // body |
| 116 | |
| 117 | const u64* blocks = (const u64*)(data); |
| 118 | |
| 119 | for (int i = 0; i < nblocks; i += Step) |
| 120 | { |
| 121 | u64 k1 = getblock(blocks, i * 2 + 0); |
| 122 | u64 k2 = getblock(blocks, i * 2 + 1); |
| 123 | |
| 124 | bmix64(h1, h2, k1, k2, c1, c2); |
| 125 | } |
| 126 | |
| 127 | //---------- |
| 128 | // tail |
| 129 | |
| 130 | const u8* tail = (const u8*)(data + nblocks * 16); |
| 131 | |
| 132 | u64 k1 = 0; |
| 133 | u64 k2 = 0; |
| 134 | |
| 135 | switch (len & 15) |
| 136 | { |
| 137 | case 15: |
| 138 | k2 ^= u64(tail[14]) << 48; |
| 139 | case 14: |
| 140 | k2 ^= u64(tail[13]) << 40; |
| 141 | case 13: |
| 142 | k2 ^= u64(tail[12]) << 32; |
| 143 | case 12: |
| 144 | k2 ^= u64(tail[11]) << 24; |
| 145 | case 11: |
| 146 | k2 ^= u64(tail[10]) << 16; |
| 147 | case 10: |
| 148 | k2 ^= u64(tail[9]) << 8; |
| 149 | case 9: |
| 150 | k2 ^= u64(tail[8]) << 0; |
| 151 | |
| 152 | case 8: |
| 153 | k1 ^= u64(tail[7]) << 56; |
| 154 | case 7: |
| 155 | k1 ^= u64(tail[6]) << 48; |
| 156 | case 6: |
| 157 | k1 ^= u64(tail[5]) << 40; |
| 158 | case 5: |
| 159 | k1 ^= u64(tail[4]) << 32; |
| 160 | case 4: |
| 161 | k1 ^= u64(tail[3]) << 24; |
| 162 | case 3: |
| 163 | k1 ^= u64(tail[2]) << 16; |
| 164 | case 2: |
| 165 | k1 ^= u64(tail[1]) << 8; |
| 166 | case 1: |
| 167 | k1 ^= u64(tail[0]) << 0; |
| 168 | bmix64(h1, h2, k1, k2, c1, c2); |
| 169 | }; |
| 170 | |
| 171 | //---------- |
| 172 | // finalization |
| 173 | |
| 174 | h2 ^= len; |
| 175 | |
| 176 | h1 += h2; |
| 177 | h2 += h1; |
| 178 | |
| 179 | h1 = fmix64(h1); |
| 180 | h2 = fmix64(h2); |
| 181 | |
| 182 | h1 += h2; |
| 183 | |
| 184 | return h1; |
| 185 | } |
| 186 | |
| 187 | #else |
| 188 | |
| 189 | //----------------------------------------------------------------------------- |
| 190 | // Block read - if your platform needs to do endian-swapping or can only |
| 191 | // handle aligned reads, do the conversion here |
| 192 | |
| 193 | static u32 getblock(const u32* p, int i) |
| 194 | { |
| 195 | return p[i]; |
| 196 | } |
| 197 | |
| 198 | //---------- |
| 199 | // Finalization mix - force all bits of a hash block to avalanche |
| 200 | |
| 201 | // avalanches all bits to within 0.25% bias |
| 202 | |
| 203 | static u32 fmix32(u32 h) |
| 204 | { |
| 205 | h ^= h >> 16; |
| 206 | h *= 0x85ebca6b; |
| 207 | h ^= h >> 13; |
| 208 | h *= 0xc2b2ae35; |
| 209 | h ^= h >> 16; |
| 210 | |
| 211 | return h; |
| 212 | } |
| 213 | |
| 214 | static void bmix32(u32& h1, u32& h2, u32& k1, u32& k2, u32& c1, u32& c2) |
| 215 | { |
| 216 | k1 *= c1; |
| 217 | k1 = std::rotl(k1, 11); |
| 218 | k1 *= c2; |
| 219 | h1 ^= k1; |
| 220 | h1 += h2; |
| 221 | |
| 222 | h2 = std::rotl(h2, 17); |
| 223 | |
| 224 | k2 *= c2; |
| 225 | k2 = std::rotl(k2, 11); |
| 226 | k2 *= c1; |
| 227 | h2 ^= k2; |
| 228 | h2 += h1; |
| 229 | |
| 230 | h1 = h1 * 3 + 0x52dce729; |
| 231 | h2 = h2 * 3 + 0x38495ab5; |
| 232 | |
| 233 | c1 = c1 * 5 + 0x7b7d159c; |
| 234 | c2 = c2 * 5 + 0x6bce6396; |
| 235 | } |
| 236 | |
| 237 | //---------- |
| 238 | |
| 239 | static u64 GetMurmurHash3(const u8* src, u32 len, u32 samples) |
| 240 | { |
| 241 | const u8* data = (const u8*)src; |
| 242 | u32 out[2]; |
| 243 | const int nblocks = len / 8; |
| 244 | u32 Step = (len / 4); |
| 245 | if (samples == 0) |
| 246 | samples = std::max(Step, 1u); |
| 247 | Step = Step / samples; |
| 248 | if (Step < 1) |
| 249 | Step = 1; |
| 250 | |
| 251 | u32 h1 = 0x8de1c3ac; |
| 252 | u32 h2 = 0xbab98226; |
| 253 | |
| 254 | u32 c1 = 0x95543787; |
| 255 | u32 c2 = 0x2ad7eb25; |
| 256 | |
| 257 | //---------- |
| 258 | // body |
| 259 | |
| 260 | const u32* blocks = (const u32*)(data + nblocks * 8); |
| 261 | |
| 262 | for (int i = -nblocks; i < 0; i += Step) |
| 263 | { |
| 264 | u32 k1 = getblock(blocks, i * 2 + 0); |
| 265 | u32 k2 = getblock(blocks, i * 2 + 1); |
| 266 | |
| 267 | bmix32(h1, h2, k1, k2, c1, c2); |
| 268 | } |
| 269 | |
| 270 | //---------- |
| 271 | // tail |
| 272 | |
| 273 | const u8* tail = (const u8*)(data + nblocks * 8); |
| 274 | |
| 275 | u32 k1 = 0; |
| 276 | u32 k2 = 0; |
| 277 | |
| 278 | switch (len & 7) |
| 279 | { |
| 280 | case 7: |
| 281 | k2 ^= tail[6] << 16; |
| 282 | case 6: |
| 283 | k2 ^= tail[5] << 8; |
| 284 | case 5: |
| 285 | k2 ^= tail[4] << 0; |
| 286 | case 4: |
| 287 | k1 ^= tail[3] << 24; |
| 288 | case 3: |
| 289 | k1 ^= tail[2] << 16; |
| 290 | case 2: |
| 291 | k1 ^= tail[1] << 8; |
| 292 | case 1: |
| 293 | k1 ^= tail[0] << 0; |
| 294 | bmix32(h1, h2, k1, k2, c1, c2); |
| 295 | }; |
| 296 | |
| 297 | //---------- |
| 298 | // finalization |
| 299 | |
| 300 | h2 ^= len; |
| 301 | |
| 302 | h1 += h2; |
| 303 | h2 += h1; |
| 304 | |
| 305 | h1 = fmix32(h1); |
| 306 | h2 = fmix32(h2); |
| 307 | |
| 308 | h1 += h2; |
| 309 | h2 += h1; |
| 310 | |
| 311 | out[0] = h1; |
| 312 | out[1] = h2; |
| 313 | |
| 314 | return *((u64*)&out); |
| 315 | } |
| 316 | |
| 317 | #endif |
| 318 | |
| 319 | #if defined(_M_X86_64) |
| 320 | |
| 321 | FUNCTION_TARGET_SSE42 |
| 322 | static u64 GetHash64_SSE42_CRC32(const u8* src, u32 len, u32 samples) |
| 323 | { |
| 324 | u64 h[4] = {len, 0, 0, 0}; |
| 325 | u32 Step = (len / 8); |
| 326 | const u64* data = (const u64*)src; |
| 327 | const u64* end = data + Step; |
| 328 | if (samples == 0) |
| 329 | samples = std::max(Step, 1u); |
| 330 | Step = Step / samples; |
| 331 | if (Step < 1) |
| 332 | Step = 1; |
| 333 | |
| 334 | while (data < end - Step * 3) |
| 335 | { |
| 336 | h[0] = _mm_crc32_u64(h[0], data[Step * 0]); |
| 337 | h[1] = _mm_crc32_u64(h[1], data[Step * 1]); |
| 338 | h[2] = _mm_crc32_u64(h[2], data[Step * 2]); |
| 339 | h[3] = _mm_crc32_u64(h[3], data[Step * 3]); |
| 340 | data += Step * 4; |
| 341 | } |
| 342 | if (data < end - Step * 0) |
| 343 | h[0] = _mm_crc32_u64(h[0], data[Step * 0]); |
| 344 | if (data < end - Step * 1) |
| 345 | h[1] = _mm_crc32_u64(h[1], data[Step * 1]); |
| 346 | if (data < end - Step * 2) |
| 347 | h[2] = _mm_crc32_u64(h[2], data[Step * 2]); |
| 348 | |
| 349 | if (len & 7) |
| 350 | { |
| 351 | u64 temp = 0; |
| 352 | memcpy(&temp, end, len & 7); |
| 353 | h[0] = _mm_crc32_u64(h[0], temp); |
| 354 | } |
| 355 | |
| 356 | // FIXME: is there a better way to combine these partial hashes? |
| 357 | return h[0] + (h[1] << 10) + (h[2] << 21) + (h[3] << 32); |
| 358 | } |
| 359 | |
| 360 | #elif defined(_M_ARM_64) |
| 361 | |
| 362 | static u64 GetHash64_ARMv8_CRC32(const u8* src, u32 len, u32 samples) |
| 363 | { |
| 364 | u64 h[4] = {len, 0, 0, 0}; |
| 365 | u32 Step = (len / 8); |
| 366 | const u64* data = (const u64*)src; |
| 367 | const u64* end = data + Step; |
| 368 | if (samples == 0) |
| 369 | samples = std::max(Step, 1u); |
| 370 | Step = Step / samples; |
| 371 | if (Step < 1) |
| 372 | Step = 1; |
| 373 | |
| 374 | while (data < end - Step * 3) |
| 375 | { |
| 376 | h[0] = __crc32d(h[0], data[Step * 0]); |
| 377 | h[1] = __crc32d(h[1], data[Step * 1]); |
| 378 | h[2] = __crc32d(h[2], data[Step * 2]); |
| 379 | h[3] = __crc32d(h[3], data[Step * 3]); |
| 380 | data += Step * 4; |
| 381 | } |
| 382 | if (data < end - Step * 0) |
| 383 | h[0] = __crc32d(h[0], data[Step * 0]); |
| 384 | if (data < end - Step * 1) |
| 385 | h[1] = __crc32d(h[1], data[Step * 1]); |
| 386 | if (data < end - Step * 2) |
| 387 | h[2] = __crc32d(h[2], data[Step * 2]); |
| 388 | |
| 389 | if (len & 7) |
| 390 | { |
| 391 | u64 temp = 0; |
| 392 | memcpy(&temp, end, len & 7); |
| 393 | h[0] = __crc32d(h[0], temp); |
| 394 | } |
| 395 | |
| 396 | // FIXME: is there a better way to combine these partial hashes? |
| 397 | return h[0] + (h[1] << 10) + (h[2] << 21) + (h[3] << 32); |
| 398 | } |
| 399 | |
| 400 | #endif |
| 401 | |
| 402 | using TextureHashFunction = u64 (*)(const u8* src, u32 len, u32 samples); |
| 403 | static u64 SetHash64Function(const u8* src, u32 len, u32 samples); |
| 404 | static TextureHashFunction s_texture_hash_func = SetHash64Function; |
| 405 | |
| 406 | static u64 SetHash64Function(const u8* src, u32 len, u32 samples) |
| 407 | { |
| 408 | if (cpu_info.bCRC32) |
| 409 | { |
| 410 | #if defined(_M_X86_64) |
| 411 | s_texture_hash_func = &GetHash64_SSE42_CRC32; |
| 412 | #elif defined(_M_ARM_64) |
| 413 | s_texture_hash_func = &GetHash64_ARMv8_CRC32; |
| 414 | #endif |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | s_texture_hash_func = &GetMurmurHash3; |
| 419 | } |
| 420 | return s_texture_hash_func(src, len, samples); |
| 421 | } |
| 422 | |
| 423 | u64 GetHash64(const u8* src, u32 len, u32 samples) |
| 424 | { |
| 425 | return s_texture_hash_func(src, len, samples); |
| 426 | } |
| 427 | |
| 428 | u32 StartCRC32() |
| 429 | { |
| 430 | return crc32_z(0L, Z_NULL, 0); |
| 431 | } |
| 432 | |
| 433 | u32 UpdateCRC32(u32 crc, const u8* data, size_t len) |
| 434 | { |
| 435 | return crc32_z(crc, data, len); |
| 436 | } |
| 437 | |
| 438 | u32 ComputeCRC32(const u8* data, size_t len) |
| 439 | { |
| 440 | return UpdateCRC32(StartCRC32(), data, len); |
| 441 | } |
| 442 | |
| 443 | u32 ComputeCRC32(std::string_view data) |
| 444 | { |
| 445 | return ComputeCRC32(reinterpret_cast<const u8*>(data.data()), data.size()); |
| 446 | } |
| 447 | } // namespace Common |