hash.cpp 11 KB

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