hash.cpp 11 KB

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