hash.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include "common/common_funcs.h" // For rotl
  6. #include "common/hash.h"
  7. #include "common/platform.h"
  8. #if _M_SSE >= 0x402
  9. #include "common/cpu_detect.h"
  10. #include <nmmintrin.h>
  11. #endif
  12. static u64 (*ptrHashFunction)(const u8 *src, int len, u32 samples) = &GetMurmurHash3;
  13. // uint32_t
  14. // WARNING - may read one more byte!
  15. // Implementation from Wikipedia.
  16. u32 HashFletcher(const u8* data_u8, size_t length)
  17. {
  18. const u16* data = (const u16*)data_u8; /* Pointer to the data to be summed */
  19. size_t len = (length + 1) / 2; /* Length in 16-bit words */
  20. u32 sum1 = 0xffff, sum2 = 0xffff;
  21. while (len)
  22. {
  23. size_t tlen = len > 360 ? 360 : len;
  24. len -= tlen;
  25. do {
  26. sum1 += *data++;
  27. sum2 += sum1;
  28. }
  29. while (--tlen);
  30. sum1 = (sum1 & 0xffff) + (sum1 >> 16);
  31. sum2 = (sum2 & 0xffff) + (sum2 >> 16);
  32. }
  33. // Second reduction step to reduce sums to 16 bits
  34. sum1 = (sum1 & 0xffff) + (sum1 >> 16);
  35. sum2 = (sum2 & 0xffff) + (sum2 >> 16);
  36. return(sum2 << 16 | sum1);
  37. }
  38. // Implementation from Wikipedia
  39. // Slightly slower than Fletcher above, but slightly more reliable.
  40. #define MOD_ADLER 65521
  41. // data: Pointer to the data to be summed; len is in bytes
  42. u32 HashAdler32(const u8* data, size_t len)
  43. {
  44. u32 a = 1, b = 0;
  45. while (len)
  46. {
  47. size_t tlen = len > 5550 ? 5550 : len;
  48. len -= tlen;
  49. do
  50. {
  51. a += *data++;
  52. b += a;
  53. }
  54. while (--tlen);
  55. a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
  56. b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
  57. }
  58. // It can be shown that a <= 0x1013a here, so a single subtract will do.
  59. if (a >= MOD_ADLER)
  60. {
  61. a -= MOD_ADLER;
  62. }
  63. // It can be shown that b can reach 0xfff87 here.
  64. b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
  65. if (b >= MOD_ADLER)
  66. {
  67. b -= MOD_ADLER;
  68. }
  69. return((b << 16) | a);
  70. }
  71. // Stupid hash - but can't go back now :)
  72. // Don't use for new things. At least it's reasonably fast.
  73. u32 HashEctor(const u8* ptr, int length)
  74. {
  75. u32 crc = 0;
  76. for (int i = 0; i < length; i++)
  77. {
  78. crc ^= ptr[i];
  79. crc = (crc << 3) | (crc >> 29);
  80. }
  81. return(crc);
  82. }
  83. #ifdef _M_X64
  84. //-----------------------------------------------------------------------------
  85. // Block read - if your platform needs to do endian-swapping or can only
  86. // handle aligned reads, do the conversion here
  87. inline u64 getblock(const u64 * p, int i)
  88. {
  89. return p[i];
  90. }
  91. //----------
  92. // Block mix - combine the key bits with the hash bits and scramble everything
  93. inline void bmix64(u64 & h1, u64 & h2, u64 & k1, u64 & k2, u64 & c1, u64 & c2)
  94. {
  95. k1 *= c1;
  96. k1 = _rotl64(k1,23);
  97. k1 *= c2;
  98. h1 ^= k1;
  99. h1 += h2;
  100. h2 = _rotl64(h2,41);
  101. k2 *= c2;
  102. k2 = _rotl64(k2,23);
  103. k2 *= c1;
  104. h2 ^= k2;
  105. h2 += h1;
  106. h1 = h1*3+0x52dce729;
  107. h2 = h2*3+0x38495ab5;
  108. c1 = c1*5+0x7b7d159c;
  109. c2 = c2*5+0x6bce6396;
  110. }
  111. //----------
  112. // Finalization mix - avalanches all bits to within 0.05% bias
  113. inline u64 fmix64(u64 k)
  114. {
  115. k ^= k >> 33;
  116. k *= 0xff51afd7ed558ccd;
  117. k ^= k >> 33;
  118. k *= 0xc4ceb9fe1a85ec53;
  119. k ^= k >> 33;
  120. return k;
  121. }
  122. u64 GetMurmurHash3(const u8 *src, int len, u32 samples)
  123. {
  124. const u8 * data = (const u8*)src;
  125. const int nblocks = len / 16;
  126. u32 Step = (len / 8);
  127. if(samples == 0) samples = std::max(Step, 1u);
  128. Step = Step / samples;
  129. if(Step < 1) Step = 1;
  130. u64 h1 = 0x9368e53c2f6af274;
  131. u64 h2 = 0x586dcd208f7cd3fd;
  132. u64 c1 = 0x87c37b91114253d5;
  133. u64 c2 = 0x4cf5ad432745937f;
  134. //----------
  135. // body
  136. const u64 * blocks = (const u64 *)(data);
  137. for(int i = 0; i < nblocks; i+=Step)
  138. {
  139. u64 k1 = getblock(blocks,i*2+0);
  140. u64 k2 = getblock(blocks,i*2+1);
  141. bmix64(h1,h2,k1,k2,c1,c2);
  142. }
  143. //----------
  144. // tail
  145. const u8 * tail = (const u8*)(data + nblocks*16);
  146. u64 k1 = 0;
  147. u64 k2 = 0;
  148. switch(len & 15)
  149. {
  150. case 15: k2 ^= u64(tail[14]) << 48;
  151. case 14: k2 ^= u64(tail[13]) << 40;
  152. case 13: k2 ^= u64(tail[12]) << 32;
  153. case 12: k2 ^= u64(tail[11]) << 24;
  154. case 11: k2 ^= u64(tail[10]) << 16;
  155. case 10: k2 ^= u64(tail[ 9]) << 8;
  156. case 9: k2 ^= u64(tail[ 8]) << 0;
  157. case 8: k1 ^= u64(tail[ 7]) << 56;
  158. case 7: k1 ^= u64(tail[ 6]) << 48;
  159. case 6: k1 ^= u64(tail[ 5]) << 40;
  160. case 5: k1 ^= u64(tail[ 4]) << 32;
  161. case 4: k1 ^= u64(tail[ 3]) << 24;
  162. case 3: k1 ^= u64(tail[ 2]) << 16;
  163. case 2: k1 ^= u64(tail[ 1]) << 8;
  164. case 1: k1 ^= u64(tail[ 0]) << 0;
  165. bmix64(h1,h2,k1,k2,c1,c2);
  166. };
  167. //----------
  168. // finalization
  169. h2 ^= len;
  170. h1 += h2;
  171. h2 += h1;
  172. h1 = fmix64(h1);
  173. h2 = fmix64(h2);
  174. h1 += h2;
  175. return h1;
  176. }
  177. // CRC32 hash using the SSE4.2 instruction
  178. u64 GetCRC32(const u8 *src, int len, u32 samples)
  179. {
  180. #if _M_SSE >= 0x402
  181. u64 h = len;
  182. u32 Step = (len / 8);
  183. const u64 *data = (const u64 *)src;
  184. const u64 *end = data + Step;
  185. if(samples == 0) samples = std::max(Step, 1u);
  186. Step = Step / samples;
  187. if(Step < 1) Step = 1;
  188. while(data < end)
  189. {
  190. h = _mm_crc32_u64(h, data[0]);
  191. data += Step;
  192. }
  193. const u8 *data2 = (const u8*)end;
  194. return _mm_crc32_u64(h, u64(data2[0]));
  195. #else
  196. return 0;
  197. #endif
  198. }
  199. /*
  200. * NOTE: This hash function is used for custom texture loading/dumping, so
  201. * it should not be changed, which would require all custom textures to be
  202. * recalculated for their new hash values. If the hashing function is
  203. * changed, make sure this one is still used when the legacy parameter is
  204. * true.
  205. */
  206. u64 GetHashHiresTexture(const u8 *src, int len, u32 samples)
  207. {
  208. const u64 m = 0xc6a4a7935bd1e995;
  209. u64 h = len * m;
  210. const int r = 47;
  211. u32 Step = (len / 8);
  212. const u64 *data = (const u64 *)src;
  213. const u64 *end = data + Step;
  214. if(samples == 0) samples = std::max(Step, 1u);
  215. Step = Step / samples;
  216. if(Step < 1) Step = 1;
  217. while(data < end)
  218. {
  219. u64 k = data[0];
  220. data+=Step;
  221. k *= m;
  222. k ^= k >> r;
  223. k *= m;
  224. h ^= k;
  225. h *= m;
  226. }
  227. const u8 * data2 = (const u8*)end;
  228. switch(len & 7)
  229. {
  230. case 7: h ^= u64(data2[6]) << 48;
  231. case 6: h ^= u64(data2[5]) << 40;
  232. case 5: h ^= u64(data2[4]) << 32;
  233. case 4: h ^= u64(data2[3]) << 24;
  234. case 3: h ^= u64(data2[2]) << 16;
  235. case 2: h ^= u64(data2[1]) << 8;
  236. case 1: h ^= u64(data2[0]);
  237. h *= m;
  238. };
  239. h ^= h >> r;
  240. h *= m;
  241. h ^= h >> r;
  242. return h;
  243. }
  244. #else
  245. // CRC32 hash using the SSE4.2 instruction
  246. u64 GetCRC32(const u8 *src, int len, u32 samples)
  247. {
  248. #if _M_SSE >= 0x402
  249. u32 h = len;
  250. u32 Step = (len/4);
  251. const u32 *data = (const u32 *)src;
  252. const u32 *end = data + Step;
  253. if(samples == 0) samples = std::max(Step, 1u);
  254. Step = Step / samples;
  255. if(Step < 1) Step = 1;
  256. while(data < end)
  257. {
  258. h = _mm_crc32_u32(h, data[0]);
  259. data += Step;
  260. }
  261. const u8 *data2 = (const u8*)end;
  262. return (u64)_mm_crc32_u32(h, u32(data2[0]));
  263. #else
  264. return 0;
  265. #endif
  266. }
  267. //-----------------------------------------------------------------------------
  268. // Block read - if your platform needs to do endian-swapping or can only
  269. // handle aligned reads, do the conversion here
  270. inline u32 getblock(const u32 * p, int i)
  271. {
  272. return p[i];
  273. }
  274. //----------
  275. // Finalization mix - force all bits of a hash block to avalanche
  276. // avalanches all bits to within 0.25% bias
  277. inline u32 fmix32(u32 h)
  278. {
  279. h ^= h >> 16;
  280. h *= 0x85ebca6b;
  281. h ^= h >> 13;
  282. h *= 0xc2b2ae35;
  283. h ^= h >> 16;
  284. return h;
  285. }
  286. inline void bmix32(u32 & h1, u32 & h2, u32 & k1, u32 & k2, u32 & c1, u32 & c2)
  287. {
  288. k1 *= c1;
  289. k1 = _rotl(k1,11);
  290. k1 *= c2;
  291. h1 ^= k1;
  292. h1 += h2;
  293. h2 = _rotl(h2,17);
  294. k2 *= c2;
  295. k2 = _rotl(k2,11);
  296. k2 *= c1;
  297. h2 ^= k2;
  298. h2 += h1;
  299. h1 = h1*3+0x52dce729;
  300. h2 = h2*3+0x38495ab5;
  301. c1 = c1*5+0x7b7d159c;
  302. c2 = c2*5+0x6bce6396;
  303. }
  304. //----------
  305. u64 GetMurmurHash3(const u8* src, int len, u32 samples)
  306. {
  307. const u8 * data = (const u8*)src;
  308. u32 out[2];
  309. const int nblocks = len / 8;
  310. u32 Step = (len / 4);
  311. if(samples == 0) samples = std::max(Step, 1u);
  312. Step = Step / samples;
  313. if(Step < 1) Step = 1;
  314. u32 h1 = 0x8de1c3ac;
  315. u32 h2 = 0xbab98226;
  316. u32 c1 = 0x95543787;
  317. u32 c2 = 0x2ad7eb25;
  318. //----------
  319. // body
  320. const u32 * blocks = (const u32 *)(data + nblocks*8);
  321. for(int i = -nblocks; i < 0; i+=Step)
  322. {
  323. u32 k1 = getblock(blocks,i*2+0);
  324. u32 k2 = getblock(blocks,i*2+1);
  325. bmix32(h1,h2,k1,k2,c1,c2);
  326. }
  327. //----------
  328. // tail
  329. const u8 * tail = (const u8*)(data + nblocks*8);
  330. u32 k1 = 0;
  331. u32 k2 = 0;
  332. switch(len & 7)
  333. {
  334. case 7: k2 ^= tail[6] << 16;
  335. case 6: k2 ^= tail[5] << 8;
  336. case 5: k2 ^= tail[4] << 0;
  337. case 4: k1 ^= tail[3] << 24;
  338. case 3: k1 ^= tail[2] << 16;
  339. case 2: k1 ^= tail[1] << 8;
  340. case 1: k1 ^= tail[0] << 0;
  341. bmix32(h1,h2,k1,k2,c1,c2);
  342. };
  343. //----------
  344. // finalization
  345. h2 ^= len;
  346. h1 += h2;
  347. h2 += h1;
  348. h1 = fmix32(h1);
  349. h2 = fmix32(h2);
  350. h1 += h2;
  351. h2 += h1;
  352. out[0] = h1;
  353. out[1] = h2;
  354. return *((u64 *)&out);
  355. }
  356. /*
  357. * FIXME: The old 32-bit version of this hash made different hashes than the
  358. * 64-bit version. Until someone can make a new version of the 32-bit one that
  359. * makes identical hashes, this is just a c/p of the 64-bit one.
  360. */
  361. u64 GetHashHiresTexture(const u8 *src, int len, u32 samples)
  362. {
  363. const u64 m = 0xc6a4a7935bd1e995ULL;
  364. u64 h = len * m;
  365. const int r = 47;
  366. u32 Step = (len / 8);
  367. const u64 *data = (const u64 *)src;
  368. const u64 *end = data + Step;
  369. if(samples == 0) samples = std::max(Step, 1u);
  370. Step = Step / samples;
  371. if(Step < 1) Step = 1;
  372. while(data < end)
  373. {
  374. u64 k = data[0];
  375. data+=Step;
  376. k *= m;
  377. k ^= k >> r;
  378. k *= m;
  379. h ^= k;
  380. h *= m;
  381. }
  382. const u8 * data2 = (const u8*)end;
  383. switch(len & 7)
  384. {
  385. case 7: h ^= u64(data2[6]) << 48;
  386. case 6: h ^= u64(data2[5]) << 40;
  387. case 5: h ^= u64(data2[4]) << 32;
  388. case 4: h ^= u64(data2[3]) << 24;
  389. case 3: h ^= u64(data2[2]) << 16;
  390. case 2: h ^= u64(data2[1]) << 8;
  391. case 1: h ^= u64(data2[0]);
  392. h *= m;
  393. };
  394. h ^= h >> r;
  395. h *= m;
  396. h ^= h >> r;
  397. return h;
  398. }
  399. #endif
  400. u64 GetHash64(const u8 *src, int len, u32 samples)
  401. {
  402. return ptrHashFunction(src, len, samples);
  403. }
  404. // sets the hash function used for the texture cache
  405. void SetHash64Function(bool useHiresTextures)
  406. {
  407. if (useHiresTextures)
  408. {
  409. ptrHashFunction = &GetHashHiresTexture;
  410. }
  411. #if _M_SSE >= 0x402
  412. else if (cpu_info.bSSE4_2 && !useHiresTextures) // sse crc32 version
  413. {
  414. ptrHashFunction = &GetCRC32;
  415. }
  416. #endif
  417. else
  418. {
  419. ptrHashFunction = &GetMurmurHash3;
  420. }
  421. }