hash.cpp 11 KB

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