lzham_match_accel.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // File: lzham_match_accel.cpp
  2. // See Copyright Notice and license at the end of include/lzham.h
  3. #include "lzham_core.h"
  4. #include "lzham_match_accel.h"
  5. #include "lzham_timer.h"
  6. namespace lzham
  7. {
  8. static inline uint32 hash2_to_12(uint c0, uint c1)
  9. {
  10. return c0 ^ (c1 << 4);
  11. }
  12. static inline uint32 hash3_to_16(uint c0, uint c1, uint c2)
  13. {
  14. return (c0 | (c1 << 8)) ^ (c2 << 4);
  15. }
  16. search_accelerator::search_accelerator() :
  17. m_pLZBase(NULL),
  18. m_pTask_pool(NULL),
  19. m_max_helper_threads(0),
  20. m_max_dict_size(0),
  21. m_max_dict_size_mask(0),
  22. m_lookahead_pos(0),
  23. m_lookahead_size(0),
  24. m_cur_dict_size(0),
  25. m_fill_lookahead_pos(0),
  26. m_fill_lookahead_size(0),
  27. m_fill_dict_size(0),
  28. m_max_probes(0),
  29. m_max_matches(0),
  30. m_all_matches(false),
  31. m_next_match_ref(0),
  32. m_num_completed_helper_threads(0)
  33. {
  34. }
  35. bool search_accelerator::init(CLZBase* pLZBase, task_pool* pPool, uint max_helper_threads, uint max_dict_size, uint max_matches, bool all_matches, uint max_probes)
  36. {
  37. LZHAM_ASSERT(pLZBase);
  38. LZHAM_ASSERT(max_dict_size && math::is_power_of_2(max_dict_size));
  39. LZHAM_ASSERT(max_probes);
  40. m_max_probes = LZHAM_MIN(cMatchAccelMaxSupportedProbes, max_probes);
  41. m_pLZBase = pLZBase;
  42. m_pTask_pool = max_helper_threads ? pPool : NULL;
  43. m_max_helper_threads = m_pTask_pool ? max_helper_threads : 0;
  44. m_max_matches = LZHAM_MIN(m_max_probes, max_matches);
  45. m_all_matches = all_matches;
  46. m_max_dict_size = max_dict_size;
  47. m_max_dict_size_mask = m_max_dict_size - 1;
  48. m_cur_dict_size = 0;
  49. m_lookahead_size = 0;
  50. m_lookahead_pos = 0;
  51. m_fill_lookahead_pos = 0;
  52. m_fill_lookahead_size = 0;
  53. m_fill_dict_size = 0;
  54. m_num_completed_helper_threads = 0;
  55. if (!m_dict.try_resize_no_construct(max_dict_size + LZHAM_MIN(m_max_dict_size, static_cast<uint>(CLZBase::cMaxHugeMatchLen))))
  56. return false;
  57. if (!m_hash.try_resize_no_construct(cHashSize))
  58. return false;
  59. if (!m_nodes.try_resize_no_construct(max_dict_size))
  60. return false;
  61. memset(m_hash.get_ptr(), 0, m_hash.size_in_bytes());
  62. return true;
  63. }
  64. void search_accelerator::reset()
  65. {
  66. m_cur_dict_size = 0;
  67. m_lookahead_size = 0;
  68. m_lookahead_pos = 0;
  69. m_fill_lookahead_pos = 0;
  70. m_fill_lookahead_size = 0;
  71. m_fill_dict_size = 0;
  72. m_num_completed_helper_threads = 0;
  73. // Clearing the hash tables is only necessary for determinism (otherwise, it's possible the matches returned after a reset will depend on the data processes before the reset).
  74. if (m_hash.size())
  75. memset(m_hash.get_ptr(), 0, m_hash.size_in_bytes());
  76. if (m_digram_hash.size())
  77. memset(m_digram_hash.get_ptr(), 0, m_digram_hash.size_in_bytes());
  78. }
  79. void search_accelerator::flush()
  80. {
  81. m_cur_dict_size = 0;
  82. }
  83. uint search_accelerator::get_max_add_bytes() const
  84. {
  85. uint add_pos = static_cast<uint>(m_lookahead_pos & (m_max_dict_size - 1));
  86. return m_max_dict_size - add_pos;
  87. }
  88. static uint8 g_hamming_dist[256] =
  89. {
  90. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  91. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  92. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  93. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  94. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  95. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  96. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  97. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  98. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  99. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  100. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  101. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  102. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  103. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  104. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  105. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  106. };
  107. void search_accelerator::find_all_matches_callback(uint64 data, void* pData_ptr)
  108. {
  109. scoped_perf_section find_all_matches_timer("find_all_matches_callback");
  110. LZHAM_NOTE_UNUSED(pData_ptr);
  111. const uint thread_index = (uint)data;
  112. dict_match temp_matches[cMatchAccelMaxSupportedProbes * 2];
  113. uint fill_lookahead_pos = m_fill_lookahead_pos;
  114. uint fill_dict_size = m_fill_dict_size;
  115. uint fill_lookahead_size = m_fill_lookahead_size;
  116. uint c0 = 0, c1 = 0;
  117. if (fill_lookahead_size >= 2)
  118. {
  119. c0 = m_dict[fill_lookahead_pos & m_max_dict_size_mask];
  120. c1 = m_dict[(fill_lookahead_pos & m_max_dict_size_mask) + 1];
  121. }
  122. const uint8* pDict = m_dict.get_ptr();
  123. while (fill_lookahead_size >= 3)
  124. {
  125. uint insert_pos = fill_lookahead_pos & m_max_dict_size_mask;
  126. uint c2 = pDict[insert_pos + 2];
  127. uint h = hash3_to_16(c0, c1, c2);
  128. c0 = c1;
  129. c1 = c2;
  130. LZHAM_ASSERT(!m_hash_thread_index.size() || (m_hash_thread_index[h] != UINT8_MAX));
  131. // Only process those strings that this worker thread was assigned to - this allows us to manipulate multiple trees in parallel with no worries about synchronization.
  132. if (m_hash_thread_index.size() && (m_hash_thread_index[h] != thread_index))
  133. {
  134. fill_lookahead_pos++;
  135. fill_lookahead_size--;
  136. fill_dict_size++;
  137. continue;
  138. }
  139. dict_match* pDstMatch = temp_matches;
  140. uint cur_pos = m_hash[h];
  141. m_hash[h] = static_cast<uint>(fill_lookahead_pos);
  142. uint *pLeft = &m_nodes[insert_pos].m_left;
  143. uint *pRight = &m_nodes[insert_pos].m_right;
  144. const uint max_match_len = LZHAM_MIN(static_cast<uint>(CLZBase::cMaxMatchLen), fill_lookahead_size);
  145. uint best_match_len = 2;
  146. const uint8* pIns = &pDict[insert_pos];
  147. uint n = m_max_probes;
  148. for ( ; ; )
  149. {
  150. uint delta_pos = fill_lookahead_pos - cur_pos;
  151. if ((n-- == 0) || (!delta_pos) || (delta_pos >= fill_dict_size))
  152. {
  153. *pLeft = 0;
  154. *pRight = 0;
  155. break;
  156. }
  157. uint pos = cur_pos & m_max_dict_size_mask;
  158. node *pNode = &m_nodes[pos];
  159. // Unfortunately, the initial compare match_len must be 0 because of the way we hash and truncate matches at the end of each block.
  160. uint match_len = 0;
  161. const uint8* pComp = &pDict[pos];
  162. #if LZHAM_PLATFORM_X360 || (LZHAM_USE_UNALIGNED_INT_LOADS == 0)
  163. for ( ; match_len < max_match_len; match_len++)
  164. if (pComp[match_len] != pIns[match_len])
  165. break;
  166. #else
  167. // Compare a qword at a time for a bit more efficiency.
  168. const uint64* pComp_end = reinterpret_cast<const uint64*>(pComp + max_match_len - 7);
  169. const uint64* pComp_cur = reinterpret_cast<const uint64*>(pComp);
  170. const uint64* pIns_cur = reinterpret_cast<const uint64*>(pIns);
  171. while (pComp_cur < pComp_end)
  172. {
  173. if (*pComp_cur != *pIns_cur)
  174. break;
  175. pComp_cur++;
  176. pIns_cur++;
  177. }
  178. uint alt_match_len = static_cast<uint>(reinterpret_cast<const uint8*>(pComp_cur) - reinterpret_cast<const uint8*>(pComp));
  179. for ( ; alt_match_len < max_match_len; alt_match_len++)
  180. if (pComp[alt_match_len] != pIns[alt_match_len])
  181. break;
  182. #ifdef LZVERIFY
  183. for ( ; match_len < max_match_len; match_len++)
  184. if (pComp[match_len] != pIns[match_len])
  185. break;
  186. LZHAM_VERIFY(alt_match_len == match_len);
  187. #endif
  188. match_len = alt_match_len;
  189. #endif
  190. if (match_len > best_match_len)
  191. {
  192. pDstMatch->m_len = static_cast<uint16>(match_len - CLZBase::cMinMatchLen);
  193. pDstMatch->m_dist = delta_pos;
  194. pDstMatch++;
  195. best_match_len = match_len;
  196. if (match_len == max_match_len)
  197. {
  198. *pLeft = pNode->m_left;
  199. *pRight = pNode->m_right;
  200. break;
  201. }
  202. }
  203. else if (m_all_matches)
  204. {
  205. pDstMatch->m_len = static_cast<uint16>(match_len - CLZBase::cMinMatchLen);
  206. pDstMatch->m_dist = delta_pos;
  207. pDstMatch++;
  208. }
  209. else if ((best_match_len > 2) && (best_match_len == match_len))
  210. {
  211. uint bestMatchDist = pDstMatch[-1].m_dist;
  212. uint compMatchDist = delta_pos;
  213. uint bestMatchSlot, bestMatchSlotOfs;
  214. m_pLZBase->compute_lzx_position_slot(bestMatchDist, bestMatchSlot, bestMatchSlotOfs);
  215. uint compMatchSlot, compMatchOfs;
  216. m_pLZBase->compute_lzx_position_slot(compMatchDist, compMatchSlot, compMatchOfs);
  217. // If both matches uses the same match slot, choose the one with the offset containing the lowest nibble as these bits separately entropy coded.
  218. // This could choose a match which is further away in the absolute sense, but closer in a coding sense.
  219. if ( (compMatchSlot < bestMatchSlot) ||
  220. ((compMatchSlot >= 8) && (compMatchSlot == bestMatchSlot) && ((compMatchOfs & 15) < (bestMatchSlotOfs & 15))) )
  221. {
  222. LZHAM_ASSERT((pDstMatch[-1].m_len + (uint)CLZBase::cMinMatchLen) == best_match_len);
  223. pDstMatch[-1].m_dist = delta_pos;
  224. }
  225. else if ((match_len < max_match_len) && (compMatchSlot <= bestMatchSlot))
  226. {
  227. // Choose the match which has lowest hamming distance in the mismatch byte for a tiny win on binary files.
  228. // TODO: This competes against the prev. optimization.
  229. uint desired_mismatch_byte = pIns[match_len];
  230. uint cur_mismatch_byte = pDict[(insert_pos - bestMatchDist + match_len) & m_max_dict_size_mask];
  231. uint cur_mismatch_dist = g_hamming_dist[cur_mismatch_byte ^ desired_mismatch_byte];
  232. uint new_mismatch_byte = pComp[match_len];
  233. uint new_mismatch_dist = g_hamming_dist[new_mismatch_byte ^ desired_mismatch_byte];
  234. if (new_mismatch_dist < cur_mismatch_dist)
  235. {
  236. LZHAM_ASSERT((pDstMatch[-1].m_len + (uint)CLZBase::cMinMatchLen) == best_match_len);
  237. pDstMatch[-1].m_dist = delta_pos;
  238. }
  239. }
  240. }
  241. uint new_pos;
  242. if (pComp[match_len] < pIns[match_len])
  243. {
  244. *pLeft = cur_pos;
  245. pLeft = &pNode->m_right;
  246. new_pos = pNode->m_right;
  247. }
  248. else
  249. {
  250. *pRight = cur_pos;
  251. pRight = &pNode->m_left;
  252. new_pos = pNode->m_left;
  253. }
  254. if (new_pos == cur_pos)
  255. break;
  256. cur_pos = new_pos;
  257. }
  258. const uint num_matches = (uint)(pDstMatch - temp_matches);
  259. if (num_matches)
  260. {
  261. pDstMatch[-1].m_dist |= 0x80000000;
  262. const uint num_matches_to_write = LZHAM_MIN(num_matches, m_max_matches);
  263. const uint match_ref_ofs = static_cast<uint>(atomic_exchange_add(&m_next_match_ref, num_matches_to_write));
  264. memcpy(&m_matches[match_ref_ofs],
  265. temp_matches + (num_matches - num_matches_to_write),
  266. sizeof(temp_matches[0]) * num_matches_to_write);
  267. // FIXME: This is going to really hurt on platforms requiring export barriers.
  268. LZHAM_MEMORY_EXPORT_BARRIER
  269. atomic_exchange32((atomic32_t*)&m_match_refs[static_cast<uint>(fill_lookahead_pos - m_fill_lookahead_pos)], match_ref_ofs);
  270. }
  271. else
  272. {
  273. atomic_exchange32((atomic32_t*)&m_match_refs[static_cast<uint>(fill_lookahead_pos - m_fill_lookahead_pos)], -2);
  274. }
  275. fill_lookahead_pos++;
  276. fill_lookahead_size--;
  277. fill_dict_size++;
  278. }
  279. while (fill_lookahead_size)
  280. {
  281. uint insert_pos = fill_lookahead_pos & m_max_dict_size_mask;
  282. m_nodes[insert_pos].m_left = 0;
  283. m_nodes[insert_pos].m_right = 0;
  284. atomic_exchange32((atomic32_t*)&m_match_refs[static_cast<uint>(fill_lookahead_pos - m_fill_lookahead_pos)], -2);
  285. fill_lookahead_pos++;
  286. fill_lookahead_size--;
  287. fill_dict_size++;
  288. }
  289. atomic_increment32(&m_num_completed_helper_threads);
  290. }
  291. bool search_accelerator::find_len2_matches()
  292. {
  293. if (!m_digram_hash.size())
  294. {
  295. if (!m_digram_hash.try_resize(cDigramHashSize))
  296. return false;
  297. }
  298. if (m_digram_next.size() < m_lookahead_size)
  299. {
  300. if (!m_digram_next.try_resize(m_lookahead_size))
  301. return false;
  302. }
  303. uint lookahead_dict_pos = m_lookahead_pos & m_max_dict_size_mask;
  304. for (int lookahead_ofs = 0; lookahead_ofs < ((int)m_lookahead_size - 1); ++lookahead_ofs, ++lookahead_dict_pos)
  305. {
  306. uint c0 = m_dict[lookahead_dict_pos];
  307. uint c1 = m_dict[lookahead_dict_pos + 1];
  308. uint h = hash2_to_12(c0, c1) & (cDigramHashSize - 1);
  309. m_digram_next[lookahead_ofs] = m_digram_hash[h];
  310. m_digram_hash[h] = m_lookahead_pos + lookahead_ofs;
  311. }
  312. m_digram_next[m_lookahead_size - 1] = 0;
  313. return true;
  314. }
  315. uint search_accelerator::get_len2_match(uint lookahead_ofs)
  316. {
  317. if ((m_fill_lookahead_size - lookahead_ofs) < 2)
  318. return 0;
  319. uint cur_pos = m_lookahead_pos + lookahead_ofs;
  320. uint next_match_pos = m_digram_next[cur_pos - m_fill_lookahead_pos];
  321. uint match_dist = cur_pos - next_match_pos;
  322. if ((!match_dist) || (match_dist > CLZBase::cMaxLen2MatchDist) || (match_dist > (m_cur_dict_size + lookahead_ofs)))
  323. return 0;
  324. const uint8* pCur = &m_dict[cur_pos & m_max_dict_size_mask];
  325. const uint8* pMatch = &m_dict[next_match_pos & m_max_dict_size_mask];
  326. if ((pCur[0] == pMatch[0]) && (pCur[1] == pMatch[1]))
  327. return match_dist;
  328. return 0;
  329. }
  330. bool search_accelerator::find_all_matches(uint num_bytes)
  331. {
  332. if (!m_matches.try_resize_no_construct(m_max_probes * num_bytes))
  333. return false;
  334. if (!m_match_refs.try_resize_no_construct(num_bytes))
  335. return false;
  336. memset(m_match_refs.get_ptr(), 0xFF, m_match_refs.size_in_bytes());
  337. m_fill_lookahead_pos = m_lookahead_pos;
  338. m_fill_lookahead_size = num_bytes;
  339. m_fill_dict_size = m_cur_dict_size;
  340. m_next_match_ref = 0;
  341. if (!m_pTask_pool)
  342. {
  343. find_all_matches_callback(0, NULL);
  344. m_num_completed_helper_threads = 0;
  345. }
  346. else
  347. {
  348. if (!m_hash_thread_index.try_resize_no_construct(0x10000))
  349. return false;
  350. memset(m_hash_thread_index.get_ptr(), 0xFF, m_hash_thread_index.size_in_bytes());
  351. uint next_thread_index = 0;
  352. const uint8* pDict = &m_dict[m_lookahead_pos & m_max_dict_size_mask];
  353. uint num_unique_trigrams = 0;
  354. if (num_bytes >= 3)
  355. {
  356. uint c0 = pDict[0];
  357. uint c1 = pDict[1];
  358. const int limit = ((int)num_bytes - 2);
  359. for (int i = 0; i < limit; i++)
  360. {
  361. uint c2 = pDict[2];
  362. uint t = hash3_to_16(c0, c1, c2);
  363. c0 = c1;
  364. c1 = c2;
  365. pDict++;
  366. if (m_hash_thread_index[t] == UINT8_MAX)
  367. {
  368. num_unique_trigrams++;
  369. m_hash_thread_index[t] = static_cast<uint8>(next_thread_index);
  370. if (++next_thread_index == m_max_helper_threads)
  371. next_thread_index = 0;
  372. }
  373. }
  374. }
  375. m_num_completed_helper_threads = 0;
  376. if (!m_pTask_pool->queue_multiple_object_tasks(this, &search_accelerator::find_all_matches_callback, 0, m_max_helper_threads))
  377. return false;
  378. }
  379. return find_len2_matches();
  380. }
  381. bool search_accelerator::add_bytes_begin(uint num_bytes, const uint8* pBytes)
  382. {
  383. LZHAM_ASSERT(num_bytes <= m_max_dict_size);
  384. LZHAM_ASSERT(!m_lookahead_size);
  385. uint add_pos = m_lookahead_pos & m_max_dict_size_mask;
  386. LZHAM_ASSERT((add_pos + num_bytes) <= m_max_dict_size);
  387. memcpy(&m_dict[add_pos], pBytes, num_bytes);
  388. uint dict_bytes_to_mirror = LZHAM_MIN(static_cast<uint>(CLZBase::cMaxHugeMatchLen), m_max_dict_size);
  389. if (add_pos < dict_bytes_to_mirror)
  390. memcpy(&m_dict[m_max_dict_size], &m_dict[0], dict_bytes_to_mirror);
  391. m_lookahead_size = num_bytes;
  392. uint max_possible_dict_size = m_max_dict_size - num_bytes;
  393. m_cur_dict_size = LZHAM_MIN(m_cur_dict_size, max_possible_dict_size);
  394. m_next_match_ref = 0;
  395. return find_all_matches(num_bytes);
  396. }
  397. void search_accelerator::add_bytes_end()
  398. {
  399. if (m_pTask_pool)
  400. {
  401. m_pTask_pool->join();
  402. }
  403. LZHAM_ASSERT((uint)m_next_match_ref <= m_matches.size());
  404. }
  405. dict_match* search_accelerator::find_matches(uint lookahead_ofs, bool spin)
  406. {
  407. LZHAM_ASSERT(lookahead_ofs < m_lookahead_size);
  408. const uint match_ref_ofs = static_cast<uint>(m_lookahead_pos - m_fill_lookahead_pos + lookahead_ofs);
  409. int match_ref;
  410. uint spin_count = 0;
  411. // This may spin until the match finder job(s) catch up to the caller's lookahead position.
  412. for ( ; ; )
  413. {
  414. match_ref = static_cast<int>(m_match_refs[match_ref_ofs]);
  415. if (match_ref == -2)
  416. return NULL;
  417. else if (match_ref != -1)
  418. break;
  419. spin_count++;
  420. const uint cMaxSpinCount = 1000;
  421. if ((spin) && (spin_count < cMaxSpinCount))
  422. {
  423. lzham_yield_processor();
  424. lzham_yield_processor();
  425. lzham_yield_processor();
  426. lzham_yield_processor();
  427. lzham_yield_processor();
  428. lzham_yield_processor();
  429. lzham_yield_processor();
  430. lzham_yield_processor();
  431. LZHAM_MEMORY_IMPORT_BARRIER
  432. }
  433. else
  434. {
  435. spin_count = cMaxSpinCount;
  436. lzham_sleep(1);
  437. }
  438. }
  439. LZHAM_MEMORY_IMPORT_BARRIER
  440. return &m_matches[match_ref];
  441. }
  442. void search_accelerator::advance_bytes(uint num_bytes)
  443. {
  444. LZHAM_ASSERT(num_bytes <= m_lookahead_size);
  445. m_lookahead_pos += num_bytes;
  446. m_lookahead_size -= num_bytes;
  447. m_cur_dict_size += num_bytes;
  448. LZHAM_ASSERT(m_cur_dict_size <= m_max_dict_size);
  449. }
  450. }