]>
Commit | Line | Data |
---|---|---|
30fdf114 | 1 | /* LzFindMt.c -- multithreaded Match finder for LZ algorithms\r |
5ec5a236 | 2 | 2017-06-10 : Igor Pavlov : Public domain */\r |
c4ab09ef LG |
3 | \r |
4 | #include "Precomp.h"\r | |
30fdf114 LG |
5 | \r |
6 | #include "LzHash.h"\r | |
7 | \r | |
8 | #include "LzFindMt.h"\r | |
9 | \r | |
c4ab09ef | 10 | static void MtSync_Construct(CMtSync *p)\r |
30fdf114 LG |
11 | {\r |
12 | p->wasCreated = False;\r | |
13 | p->csWasInitialized = False;\r | |
14 | p->csWasEntered = False;\r | |
15 | Thread_Construct(&p->thread);\r | |
16 | Event_Construct(&p->canStart);\r | |
17 | Event_Construct(&p->wasStarted);\r | |
18 | Event_Construct(&p->wasStopped);\r | |
19 | Semaphore_Construct(&p->freeSemaphore);\r | |
20 | Semaphore_Construct(&p->filledSemaphore);\r | |
21 | }\r | |
22 | \r | |
c4ab09ef | 23 | static void MtSync_GetNextBlock(CMtSync *p)\r |
30fdf114 LG |
24 | {\r |
25 | if (p->needStart)\r | |
26 | {\r | |
27 | p->numProcessedBlocks = 1;\r | |
28 | p->needStart = False;\r | |
29 | p->stopWriting = False;\r | |
30 | p->exit = False;\r | |
31 | Event_Reset(&p->wasStarted);\r | |
32 | Event_Reset(&p->wasStopped);\r | |
33 | \r | |
34 | Event_Set(&p->canStart);\r | |
35 | Event_Wait(&p->wasStarted);\r | |
5ec5a236 LG |
36 | \r |
37 | // if (mt) MatchFinder_Init_LowHash(mt->MatchFinder);\r | |
30fdf114 LG |
38 | }\r |
39 | else\r | |
40 | {\r | |
41 | CriticalSection_Leave(&p->cs);\r | |
42 | p->csWasEntered = False;\r | |
43 | p->numProcessedBlocks++;\r | |
44 | Semaphore_Release1(&p->freeSemaphore);\r | |
45 | }\r | |
46 | Semaphore_Wait(&p->filledSemaphore);\r | |
47 | CriticalSection_Enter(&p->cs);\r | |
48 | p->csWasEntered = True;\r | |
49 | }\r | |
50 | \r | |
51 | /* MtSync_StopWriting must be called if Writing was started */\r | |
52 | \r | |
c4ab09ef | 53 | static void MtSync_StopWriting(CMtSync *p)\r |
30fdf114 LG |
54 | {\r |
55 | UInt32 myNumBlocks = p->numProcessedBlocks;\r | |
56 | if (!Thread_WasCreated(&p->thread) || p->needStart)\r | |
57 | return;\r | |
58 | p->stopWriting = True;\r | |
59 | if (p->csWasEntered)\r | |
60 | {\r | |
61 | CriticalSection_Leave(&p->cs);\r | |
62 | p->csWasEntered = False;\r | |
63 | }\r | |
64 | Semaphore_Release1(&p->freeSemaphore);\r | |
65 | \r | |
66 | Event_Wait(&p->wasStopped);\r | |
67 | \r | |
68 | while (myNumBlocks++ != p->numProcessedBlocks)\r | |
69 | {\r | |
70 | Semaphore_Wait(&p->filledSemaphore);\r | |
71 | Semaphore_Release1(&p->freeSemaphore);\r | |
72 | }\r | |
73 | p->needStart = True;\r | |
74 | }\r | |
75 | \r | |
c4ab09ef | 76 | static void MtSync_Destruct(CMtSync *p)\r |
30fdf114 LG |
77 | {\r |
78 | if (Thread_WasCreated(&p->thread))\r | |
79 | {\r | |
80 | MtSync_StopWriting(p);\r | |
81 | p->exit = True;\r | |
82 | if (p->needStart)\r | |
83 | Event_Set(&p->canStart);\r | |
84 | Thread_Wait(&p->thread);\r | |
85 | Thread_Close(&p->thread);\r | |
86 | }\r | |
87 | if (p->csWasInitialized)\r | |
88 | {\r | |
89 | CriticalSection_Delete(&p->cs);\r | |
90 | p->csWasInitialized = False;\r | |
91 | }\r | |
92 | \r | |
93 | Event_Close(&p->canStart);\r | |
94 | Event_Close(&p->wasStarted);\r | |
95 | Event_Close(&p->wasStopped);\r | |
96 | Semaphore_Close(&p->freeSemaphore);\r | |
97 | Semaphore_Close(&p->filledSemaphore);\r | |
98 | \r | |
99 | p->wasCreated = False;\r | |
100 | }\r | |
101 | \r | |
102 | #define RINOK_THREAD(x) { if ((x) != 0) return SZ_ERROR_THREAD; }\r | |
103 | \r | |
c4ab09ef | 104 | static SRes MtSync_Create2(CMtSync *p, THREAD_FUNC_TYPE startAddress, void *obj, UInt32 numBlocks)\r |
30fdf114 LG |
105 | {\r |
106 | if (p->wasCreated)\r | |
107 | return SZ_OK;\r | |
108 | \r | |
109 | RINOK_THREAD(CriticalSection_Init(&p->cs));\r | |
110 | p->csWasInitialized = True;\r | |
111 | \r | |
112 | RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->canStart));\r | |
113 | RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStarted));\r | |
114 | RINOK_THREAD(AutoResetEvent_CreateNotSignaled(&p->wasStopped));\r | |
115 | \r | |
116 | RINOK_THREAD(Semaphore_Create(&p->freeSemaphore, numBlocks, numBlocks));\r | |
117 | RINOK_THREAD(Semaphore_Create(&p->filledSemaphore, 0, numBlocks));\r | |
118 | \r | |
119 | p->needStart = True;\r | |
120 | \r | |
121 | RINOK_THREAD(Thread_Create(&p->thread, startAddress, obj));\r | |
122 | p->wasCreated = True;\r | |
123 | return SZ_OK;\r | |
124 | }\r | |
125 | \r | |
c4ab09ef | 126 | static SRes MtSync_Create(CMtSync *p, THREAD_FUNC_TYPE startAddress, void *obj, UInt32 numBlocks)\r |
30fdf114 LG |
127 | {\r |
128 | SRes res = MtSync_Create2(p, startAddress, obj, numBlocks);\r | |
129 | if (res != SZ_OK)\r | |
130 | MtSync_Destruct(p);\r | |
131 | return res;\r | |
132 | }\r | |
133 | \r | |
134 | void MtSync_Init(CMtSync *p) { p->needStart = True; }\r | |
135 | \r | |
136 | #define kMtMaxValForNormalize 0xFFFFFFFF\r | |
137 | \r | |
138 | #define DEF_GetHeads2(name, v, action) \\r | |
c4ab09ef LG |
139 | static void GetHeads ## name(const Byte *p, UInt32 pos, \\r |
140 | UInt32 *hash, UInt32 hashMask, UInt32 *heads, UInt32 numHeads, const UInt32 *crc) \\r | |
141 | { action; for (; numHeads != 0; numHeads--) { \\r | |
142 | const UInt32 value = (v); p++; *heads++ = pos - hash[value]; hash[value] = pos++; } }\r | |
30fdf114 LG |
143 | \r |
144 | #define DEF_GetHeads(name, v) DEF_GetHeads2(name, v, ;)\r | |
145 | \r | |
c4ab09ef | 146 | DEF_GetHeads2(2, (p[0] | ((UInt32)p[1] << 8)), UNUSED_VAR(hashMask); UNUSED_VAR(crc); )\r |
30fdf114 LG |
147 | DEF_GetHeads(3, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8)) & hashMask)\r |
148 | DEF_GetHeads(4, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ (crc[p[3]] << 5)) & hashMask)\r | |
149 | DEF_GetHeads(4b, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ ((UInt32)p[3] << 16)) & hashMask)\r | |
c4ab09ef | 150 | /* DEF_GetHeads(5, (crc[p[0]] ^ p[1] ^ ((UInt32)p[2] << 8) ^ (crc[p[3]] << 5) ^ (crc[p[4]] << 3)) & hashMask) */\r |
30fdf114 | 151 | \r |
c4ab09ef | 152 | static void HashThreadFunc(CMatchFinderMt *mt)\r |
30fdf114 LG |
153 | {\r |
154 | CMtSync *p = &mt->hashSync;\r | |
155 | for (;;)\r | |
156 | {\r | |
157 | UInt32 numProcessedBlocks = 0;\r | |
158 | Event_Wait(&p->canStart);\r | |
159 | Event_Set(&p->wasStarted);\r | |
5ec5a236 LG |
160 | \r |
161 | MatchFinder_Init_HighHash(mt->MatchFinder);\r | |
162 | \r | |
30fdf114 LG |
163 | for (;;)\r |
164 | {\r | |
165 | if (p->exit)\r | |
166 | return;\r | |
167 | if (p->stopWriting)\r | |
168 | {\r | |
169 | p->numProcessedBlocks = numProcessedBlocks;\r | |
170 | Event_Set(&p->wasStopped);\r | |
171 | break;\r | |
172 | }\r | |
173 | \r | |
174 | {\r | |
175 | CMatchFinder *mf = mt->MatchFinder;\r | |
176 | if (MatchFinder_NeedMove(mf))\r | |
177 | {\r | |
178 | CriticalSection_Enter(&mt->btSync.cs);\r | |
179 | CriticalSection_Enter(&mt->hashSync.cs);\r | |
180 | {\r | |
c4ab09ef LG |
181 | const Byte *beforePtr = Inline_MatchFinder_GetPointerToCurrentPos(mf);\r |
182 | ptrdiff_t offset;\r | |
30fdf114 | 183 | MatchFinder_MoveBlock(mf);\r |
c4ab09ef LG |
184 | offset = beforePtr - Inline_MatchFinder_GetPointerToCurrentPos(mf);\r |
185 | mt->pointerToCurPos -= offset;\r | |
186 | mt->buffer -= offset;\r | |
30fdf114 LG |
187 | }\r |
188 | CriticalSection_Leave(&mt->btSync.cs);\r | |
189 | CriticalSection_Leave(&mt->hashSync.cs);\r | |
190 | continue;\r | |
191 | }\r | |
192 | \r | |
193 | Semaphore_Wait(&p->freeSemaphore);\r | |
194 | \r | |
195 | MatchFinder_ReadIfRequired(mf);\r | |
196 | if (mf->pos > (kMtMaxValForNormalize - kMtHashBlockSize))\r | |
197 | {\r | |
198 | UInt32 subValue = (mf->pos - mf->historySize - 1);\r | |
199 | MatchFinder_ReduceOffsets(mf, subValue);\r | |
c4ab09ef | 200 | MatchFinder_Normalize3(subValue, mf->hash + mf->fixedHashSize, (size_t)mf->hashMask + 1);\r |
30fdf114 LG |
201 | }\r |
202 | {\r | |
203 | UInt32 *heads = mt->hashBuf + ((numProcessedBlocks++) & kMtHashNumBlocksMask) * kMtHashBlockSize;\r | |
204 | UInt32 num = mf->streamPos - mf->pos;\r | |
205 | heads[0] = 2;\r | |
206 | heads[1] = num;\r | |
207 | if (num >= mf->numHashBytes)\r | |
208 | {\r | |
209 | num = num - mf->numHashBytes + 1;\r | |
210 | if (num > kMtHashBlockSize - 2)\r | |
211 | num = kMtHashBlockSize - 2;\r | |
212 | mt->GetHeadsFunc(mf->buffer, mf->pos, mf->hash + mf->fixedHashSize, mf->hashMask, heads + 2, num, mf->crc);\r | |
5ec5a236 | 213 | heads[0] = 2 + num;\r |
30fdf114 LG |
214 | }\r |
215 | mf->pos += num;\r | |
216 | mf->buffer += num;\r | |
217 | }\r | |
218 | }\r | |
219 | \r | |
220 | Semaphore_Release1(&p->filledSemaphore);\r | |
221 | }\r | |
222 | }\r | |
223 | }\r | |
224 | \r | |
c4ab09ef | 225 | static void MatchFinderMt_GetNextBlock_Hash(CMatchFinderMt *p)\r |
30fdf114 LG |
226 | {\r |
227 | MtSync_GetNextBlock(&p->hashSync);\r | |
228 | p->hashBufPosLimit = p->hashBufPos = ((p->hashSync.numProcessedBlocks - 1) & kMtHashNumBlocksMask) * kMtHashBlockSize;\r | |
229 | p->hashBufPosLimit += p->hashBuf[p->hashBufPos++];\r | |
230 | p->hashNumAvail = p->hashBuf[p->hashBufPos++];\r | |
231 | }\r | |
232 | \r | |
233 | #define kEmptyHashValue 0\r | |
234 | \r | |
235 | /* #define MFMT_GM_INLINE */\r | |
236 | \r | |
237 | #ifdef MFMT_GM_INLINE\r | |
238 | \r | |
239 | #define NO_INLINE MY_FAST_CALL\r | |
240 | \r | |
c4ab09ef | 241 | static Int32 NO_INLINE GetMatchesSpecN(UInt32 lenLimit, UInt32 pos, const Byte *cur, CLzRef *son,\r |
30fdf114 LG |
242 | UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue,\r |
243 | UInt32 *_distances, UInt32 _maxLen, const UInt32 *hash, Int32 limit, UInt32 size, UInt32 *posRes)\r | |
244 | {\r | |
245 | do\r | |
246 | {\r | |
247 | UInt32 *distances = _distances + 1;\r | |
248 | UInt32 curMatch = pos - *hash++;\r | |
249 | \r | |
250 | CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;\r | |
251 | CLzRef *ptr1 = son + (_cyclicBufferPos << 1);\r | |
252 | UInt32 len0 = 0, len1 = 0;\r | |
253 | UInt32 cutValue = _cutValue;\r | |
254 | UInt32 maxLen = _maxLen;\r | |
255 | for (;;)\r | |
256 | {\r | |
257 | UInt32 delta = pos - curMatch;\r | |
258 | if (cutValue-- == 0 || delta >= _cyclicBufferSize)\r | |
259 | {\r | |
260 | *ptr0 = *ptr1 = kEmptyHashValue;\r | |
261 | break;\r | |
262 | }\r | |
263 | {\r | |
264 | CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r | |
265 | const Byte *pb = cur - delta;\r | |
266 | UInt32 len = (len0 < len1 ? len0 : len1);\r | |
267 | if (pb[len] == cur[len])\r | |
268 | {\r | |
269 | if (++len != lenLimit && pb[len] == cur[len])\r | |
270 | while (++len != lenLimit)\r | |
271 | if (pb[len] != cur[len])\r | |
272 | break;\r | |
273 | if (maxLen < len)\r | |
274 | {\r | |
275 | *distances++ = maxLen = len;\r | |
276 | *distances++ = delta - 1;\r | |
277 | if (len == lenLimit)\r | |
278 | {\r | |
279 | *ptr1 = pair[0];\r | |
280 | *ptr0 = pair[1];\r | |
281 | break;\r | |
282 | }\r | |
283 | }\r | |
284 | }\r | |
285 | if (pb[len] < cur[len])\r | |
286 | {\r | |
287 | *ptr1 = curMatch;\r | |
288 | ptr1 = pair + 1;\r | |
289 | curMatch = *ptr1;\r | |
290 | len1 = len;\r | |
291 | }\r | |
292 | else\r | |
293 | {\r | |
294 | *ptr0 = curMatch;\r | |
295 | ptr0 = pair;\r | |
296 | curMatch = *ptr0;\r | |
297 | len0 = len;\r | |
298 | }\r | |
299 | }\r | |
300 | }\r | |
301 | pos++;\r | |
302 | _cyclicBufferPos++;\r | |
303 | cur++;\r | |
304 | {\r | |
305 | UInt32 num = (UInt32)(distances - _distances);\r | |
306 | *_distances = num - 1;\r | |
307 | _distances += num;\r | |
308 | limit -= num;\r | |
309 | }\r | |
310 | }\r | |
311 | while (limit > 0 && --size != 0);\r | |
312 | *posRes = pos;\r | |
313 | return limit;\r | |
314 | }\r | |
315 | \r | |
316 | #endif\r | |
317 | \r | |
c4ab09ef | 318 | static void BtGetMatches(CMatchFinderMt *p, UInt32 *distances)\r |
30fdf114 LG |
319 | {\r |
320 | UInt32 numProcessed = 0;\r | |
321 | UInt32 curPos = 2;\r | |
322 | UInt32 limit = kMtBtBlockSize - (p->matchMaxLen * 2);\r | |
c4ab09ef | 323 | \r |
30fdf114 | 324 | distances[1] = p->hashNumAvail;\r |
c4ab09ef | 325 | \r |
30fdf114 LG |
326 | while (curPos < limit)\r |
327 | {\r | |
328 | if (p->hashBufPos == p->hashBufPosLimit)\r | |
329 | {\r | |
330 | MatchFinderMt_GetNextBlock_Hash(p);\r | |
331 | distances[1] = numProcessed + p->hashNumAvail;\r | |
332 | if (p->hashNumAvail >= p->numHashBytes)\r | |
333 | continue;\r | |
c4ab09ef LG |
334 | distances[0] = curPos + p->hashNumAvail;\r |
335 | distances += curPos;\r | |
30fdf114 | 336 | for (; p->hashNumAvail != 0; p->hashNumAvail--)\r |
c4ab09ef LG |
337 | *distances++ = 0;\r |
338 | return;\r | |
30fdf114 LG |
339 | }\r |
340 | {\r | |
341 | UInt32 size = p->hashBufPosLimit - p->hashBufPos;\r | |
342 | UInt32 lenLimit = p->matchMaxLen;\r | |
343 | UInt32 pos = p->pos;\r | |
344 | UInt32 cyclicBufferPos = p->cyclicBufferPos;\r | |
345 | if (lenLimit >= p->hashNumAvail)\r | |
346 | lenLimit = p->hashNumAvail;\r | |
347 | {\r | |
348 | UInt32 size2 = p->hashNumAvail - lenLimit + 1;\r | |
349 | if (size2 < size)\r | |
350 | size = size2;\r | |
351 | size2 = p->cyclicBufferSize - cyclicBufferPos;\r | |
352 | if (size2 < size)\r | |
353 | size = size2;\r | |
354 | }\r | |
c4ab09ef | 355 | \r |
30fdf114 LG |
356 | #ifndef MFMT_GM_INLINE\r |
357 | while (curPos < limit && size-- != 0)\r | |
358 | {\r | |
359 | UInt32 *startDistances = distances + curPos;\r | |
360 | UInt32 num = (UInt32)(GetMatchesSpec1(lenLimit, pos - p->hashBuf[p->hashBufPos++],\r | |
c4ab09ef LG |
361 | pos, p->buffer, p->son, cyclicBufferPos, p->cyclicBufferSize, p->cutValue,\r |
362 | startDistances + 1, p->numHashBytes - 1) - startDistances);\r | |
30fdf114 LG |
363 | *startDistances = num - 1;\r |
364 | curPos += num;\r | |
365 | cyclicBufferPos++;\r | |
366 | pos++;\r | |
367 | p->buffer++;\r | |
368 | }\r | |
369 | #else\r | |
370 | {\r | |
371 | UInt32 posRes;\r | |
372 | curPos = limit - GetMatchesSpecN(lenLimit, pos, p->buffer, p->son, cyclicBufferPos, p->cyclicBufferSize, p->cutValue,\r | |
c4ab09ef | 373 | distances + curPos, p->numHashBytes - 1, p->hashBuf + p->hashBufPos, (Int32)(limit - curPos), size, &posRes);\r |
30fdf114 LG |
374 | p->hashBufPos += posRes - pos;\r |
375 | cyclicBufferPos += posRes - pos;\r | |
376 | p->buffer += posRes - pos;\r | |
377 | pos = posRes;\r | |
378 | }\r | |
379 | #endif\r | |
380 | \r | |
381 | numProcessed += pos - p->pos;\r | |
382 | p->hashNumAvail -= pos - p->pos;\r | |
383 | p->pos = pos;\r | |
384 | if (cyclicBufferPos == p->cyclicBufferSize)\r | |
385 | cyclicBufferPos = 0;\r | |
386 | p->cyclicBufferPos = cyclicBufferPos;\r | |
387 | }\r | |
388 | }\r | |
c4ab09ef | 389 | \r |
30fdf114 LG |
390 | distances[0] = curPos;\r |
391 | }\r | |
392 | \r | |
c4ab09ef | 393 | static void BtFillBlock(CMatchFinderMt *p, UInt32 globalBlockIndex)\r |
30fdf114 LG |
394 | {\r |
395 | CMtSync *sync = &p->hashSync;\r | |
396 | if (!sync->needStart)\r | |
397 | {\r | |
398 | CriticalSection_Enter(&sync->cs);\r | |
399 | sync->csWasEntered = True;\r | |
400 | }\r | |
401 | \r | |
402 | BtGetMatches(p, p->btBuf + (globalBlockIndex & kMtBtNumBlocksMask) * kMtBtBlockSize);\r | |
403 | \r | |
404 | if (p->pos > kMtMaxValForNormalize - kMtBtBlockSize)\r | |
405 | {\r | |
406 | UInt32 subValue = p->pos - p->cyclicBufferSize;\r | |
c4ab09ef | 407 | MatchFinder_Normalize3(subValue, p->son, (size_t)p->cyclicBufferSize * 2);\r |
30fdf114 LG |
408 | p->pos -= subValue;\r |
409 | }\r | |
410 | \r | |
411 | if (!sync->needStart)\r | |
412 | {\r | |
413 | CriticalSection_Leave(&sync->cs);\r | |
414 | sync->csWasEntered = False;\r | |
415 | }\r | |
416 | }\r | |
417 | \r | |
418 | void BtThreadFunc(CMatchFinderMt *mt)\r | |
419 | {\r | |
420 | CMtSync *p = &mt->btSync;\r | |
421 | for (;;)\r | |
422 | {\r | |
423 | UInt32 blockIndex = 0;\r | |
424 | Event_Wait(&p->canStart);\r | |
425 | Event_Set(&p->wasStarted);\r | |
426 | for (;;)\r | |
427 | {\r | |
428 | if (p->exit)\r | |
429 | return;\r | |
430 | if (p->stopWriting)\r | |
431 | {\r | |
432 | p->numProcessedBlocks = blockIndex;\r | |
433 | MtSync_StopWriting(&mt->hashSync);\r | |
434 | Event_Set(&p->wasStopped);\r | |
435 | break;\r | |
436 | }\r | |
437 | Semaphore_Wait(&p->freeSemaphore);\r | |
438 | BtFillBlock(mt, blockIndex++);\r | |
439 | Semaphore_Release1(&p->filledSemaphore);\r | |
440 | }\r | |
441 | }\r | |
442 | }\r | |
443 | \r | |
444 | void MatchFinderMt_Construct(CMatchFinderMt *p)\r | |
445 | {\r | |
c4ab09ef | 446 | p->hashBuf = NULL;\r |
30fdf114 LG |
447 | MtSync_Construct(&p->hashSync);\r |
448 | MtSync_Construct(&p->btSync);\r | |
449 | }\r | |
450 | \r | |
5ec5a236 | 451 | static void MatchFinderMt_FreeMem(CMatchFinderMt *p, ISzAllocPtr alloc)\r |
30fdf114 | 452 | {\r |
5ec5a236 | 453 | ISzAlloc_Free(alloc, p->hashBuf);\r |
c4ab09ef | 454 | p->hashBuf = NULL;\r |
30fdf114 LG |
455 | }\r |
456 | \r | |
5ec5a236 | 457 | void MatchFinderMt_Destruct(CMatchFinderMt *p, ISzAllocPtr alloc)\r |
30fdf114 LG |
458 | {\r |
459 | MtSync_Destruct(&p->hashSync);\r | |
460 | MtSync_Destruct(&p->btSync);\r | |
461 | MatchFinderMt_FreeMem(p, alloc);\r | |
462 | }\r | |
463 | \r | |
464 | #define kHashBufferSize (kMtHashBlockSize * kMtHashNumBlocks)\r | |
465 | #define kBtBufferSize (kMtBtBlockSize * kMtBtNumBlocks)\r | |
466 | \r | |
c4ab09ef LG |
467 | static THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE HashThreadFunc2(void *p) { HashThreadFunc((CMatchFinderMt *)p); return 0; }\r |
468 | static THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE BtThreadFunc2(void *p)\r | |
30fdf114 LG |
469 | {\r |
470 | Byte allocaDummy[0x180];\r | |
c4ab09ef | 471 | unsigned i = 0;\r |
30fdf114 | 472 | for (i = 0; i < 16; i++)\r |
c4ab09ef LG |
473 | allocaDummy[i] = (Byte)0;\r |
474 | if (allocaDummy[0] == 0)\r | |
475 | BtThreadFunc((CMatchFinderMt *)p);\r | |
30fdf114 LG |
476 | return 0;\r |
477 | }\r | |
478 | \r | |
479 | SRes MatchFinderMt_Create(CMatchFinderMt *p, UInt32 historySize, UInt32 keepAddBufferBefore,\r | |
5ec5a236 | 480 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter, ISzAllocPtr alloc)\r |
30fdf114 LG |
481 | {\r |
482 | CMatchFinder *mf = p->MatchFinder;\r | |
483 | p->historySize = historySize;\r | |
484 | if (kMtBtBlockSize <= matchMaxLen * 4)\r | |
485 | return SZ_ERROR_PARAM;\r | |
c4ab09ef | 486 | if (!p->hashBuf)\r |
30fdf114 | 487 | {\r |
5ec5a236 | 488 | p->hashBuf = (UInt32 *)ISzAlloc_Alloc(alloc, (kHashBufferSize + kBtBufferSize) * sizeof(UInt32));\r |
c4ab09ef | 489 | if (!p->hashBuf)\r |
30fdf114 LG |
490 | return SZ_ERROR_MEM;\r |
491 | p->btBuf = p->hashBuf + kHashBufferSize;\r | |
492 | }\r | |
493 | keepAddBufferBefore += (kHashBufferSize + kBtBufferSize);\r | |
494 | keepAddBufferAfter += kMtHashBlockSize;\r | |
495 | if (!MatchFinder_Create(mf, historySize, keepAddBufferBefore, matchMaxLen, keepAddBufferAfter, alloc))\r | |
496 | return SZ_ERROR_MEM;\r | |
497 | \r | |
498 | RINOK(MtSync_Create(&p->hashSync, HashThreadFunc2, p, kMtHashNumBlocks));\r | |
499 | RINOK(MtSync_Create(&p->btSync, BtThreadFunc2, p, kMtBtNumBlocks));\r | |
500 | return SZ_OK;\r | |
501 | }\r | |
502 | \r | |
503 | /* Call it after ReleaseStream / SetStream */\r | |
5ec5a236 | 504 | static void MatchFinderMt_Init(CMatchFinderMt *p)\r |
30fdf114 LG |
505 | {\r |
506 | CMatchFinder *mf = p->MatchFinder;\r | |
5ec5a236 LG |
507 | \r |
508 | p->btBufPos =\r | |
509 | p->btBufPosLimit = 0;\r | |
510 | p->hashBufPos =\r | |
511 | p->hashBufPosLimit = 0;\r | |
c4ab09ef LG |
512 | \r |
513 | /* Init without data reading. We don't want to read data in this thread */\r | |
5ec5a236 LG |
514 | MatchFinder_Init_3(mf, False);\r |
515 | MatchFinder_Init_LowHash(mf);\r | |
c4ab09ef LG |
516 | \r |
517 | p->pointerToCurPos = Inline_MatchFinder_GetPointerToCurrentPos(mf);\r | |
30fdf114 LG |
518 | p->btNumAvailBytes = 0;\r |
519 | p->lzPos = p->historySize + 1;\r | |
520 | \r | |
521 | p->hash = mf->hash;\r | |
522 | p->fixedHashSize = mf->fixedHashSize;\r | |
523 | p->crc = mf->crc;\r | |
524 | \r | |
525 | p->son = mf->son;\r | |
526 | p->matchMaxLen = mf->matchMaxLen;\r | |
527 | p->numHashBytes = mf->numHashBytes;\r | |
528 | p->pos = mf->pos;\r | |
529 | p->buffer = mf->buffer;\r | |
530 | p->cyclicBufferPos = mf->cyclicBufferPos;\r | |
531 | p->cyclicBufferSize = mf->cyclicBufferSize;\r | |
532 | p->cutValue = mf->cutValue;\r | |
533 | }\r | |
534 | \r | |
535 | /* ReleaseStream is required to finish multithreading */\r | |
536 | void MatchFinderMt_ReleaseStream(CMatchFinderMt *p)\r | |
537 | {\r | |
538 | MtSync_StopWriting(&p->btSync);\r | |
539 | /* p->MatchFinder->ReleaseStream(); */\r | |
540 | }\r | |
541 | \r | |
c4ab09ef | 542 | static void MatchFinderMt_Normalize(CMatchFinderMt *p)\r |
30fdf114 LG |
543 | {\r |
544 | MatchFinder_Normalize3(p->lzPos - p->historySize - 1, p->hash, p->fixedHashSize);\r | |
545 | p->lzPos = p->historySize + 1;\r | |
546 | }\r | |
547 | \r | |
c4ab09ef | 548 | static void MatchFinderMt_GetNextBlock_Bt(CMatchFinderMt *p)\r |
30fdf114 LG |
549 | {\r |
550 | UInt32 blockIndex;\r | |
551 | MtSync_GetNextBlock(&p->btSync);\r | |
552 | blockIndex = ((p->btSync.numProcessedBlocks - 1) & kMtBtNumBlocksMask);\r | |
553 | p->btBufPosLimit = p->btBufPos = blockIndex * kMtBtBlockSize;\r | |
554 | p->btBufPosLimit += p->btBuf[p->btBufPos++];\r | |
555 | p->btNumAvailBytes = p->btBuf[p->btBufPos++];\r | |
556 | if (p->lzPos >= kMtMaxValForNormalize - kMtBtBlockSize)\r | |
557 | MatchFinderMt_Normalize(p);\r | |
558 | }\r | |
559 | \r | |
c4ab09ef | 560 | static const Byte * MatchFinderMt_GetPointerToCurrentPos(CMatchFinderMt *p)\r |
30fdf114 LG |
561 | {\r |
562 | return p->pointerToCurPos;\r | |
563 | }\r | |
564 | \r | |
565 | #define GET_NEXT_BLOCK_IF_REQUIRED if (p->btBufPos == p->btBufPosLimit) MatchFinderMt_GetNextBlock_Bt(p);\r | |
566 | \r | |
c4ab09ef | 567 | static UInt32 MatchFinderMt_GetNumAvailableBytes(CMatchFinderMt *p)\r |
30fdf114 LG |
568 | {\r |
569 | GET_NEXT_BLOCK_IF_REQUIRED;\r | |
570 | return p->btNumAvailBytes;\r | |
571 | }\r | |
572 | \r | |
c4ab09ef | 573 | static UInt32 * MixMatches2(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances)\r |
30fdf114 | 574 | {\r |
c4ab09ef | 575 | UInt32 h2, curMatch2;\r |
30fdf114 LG |
576 | UInt32 *hash = p->hash;\r |
577 | const Byte *cur = p->pointerToCurPos;\r | |
578 | UInt32 lzPos = p->lzPos;\r | |
579 | MT_HASH2_CALC\r | |
580 | \r | |
c4ab09ef LG |
581 | curMatch2 = hash[h2];\r |
582 | hash[h2] = lzPos;\r | |
30fdf114 LG |
583 | \r |
584 | if (curMatch2 >= matchMinPos)\r | |
585 | if (cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0])\r | |
586 | {\r | |
587 | *distances++ = 2;\r | |
588 | *distances++ = lzPos - curMatch2 - 1;\r | |
589 | }\r | |
c4ab09ef | 590 | \r |
30fdf114 LG |
591 | return distances;\r |
592 | }\r | |
593 | \r | |
c4ab09ef | 594 | static UInt32 * MixMatches3(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances)\r |
30fdf114 | 595 | {\r |
c4ab09ef | 596 | UInt32 h2, h3, curMatch2, curMatch3;\r |
30fdf114 LG |
597 | UInt32 *hash = p->hash;\r |
598 | const Byte *cur = p->pointerToCurPos;\r | |
599 | UInt32 lzPos = p->lzPos;\r | |
600 | MT_HASH3_CALC\r | |
601 | \r | |
c4ab09ef | 602 | curMatch2 = hash[ h2];\r |
5ec5a236 | 603 | curMatch3 = (hash + kFix3HashSize)[h3];\r |
30fdf114 | 604 | \r |
c4ab09ef | 605 | hash[ h2] = lzPos;\r |
5ec5a236 | 606 | (hash + kFix3HashSize)[h3] = lzPos;\r |
30fdf114 LG |
607 | \r |
608 | if (curMatch2 >= matchMinPos && cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0])\r | |
609 | {\r | |
610 | distances[1] = lzPos - curMatch2 - 1;\r | |
611 | if (cur[(ptrdiff_t)curMatch2 - lzPos + 2] == cur[2])\r | |
612 | {\r | |
613 | distances[0] = 3;\r | |
614 | return distances + 2;\r | |
615 | }\r | |
616 | distances[0] = 2;\r | |
617 | distances += 2;\r | |
618 | }\r | |
c4ab09ef | 619 | \r |
30fdf114 LG |
620 | if (curMatch3 >= matchMinPos && cur[(ptrdiff_t)curMatch3 - lzPos] == cur[0])\r |
621 | {\r | |
622 | *distances++ = 3;\r | |
623 | *distances++ = lzPos - curMatch3 - 1;\r | |
624 | }\r | |
c4ab09ef | 625 | \r |
30fdf114 LG |
626 | return distances;\r |
627 | }\r | |
628 | \r | |
629 | /*\r | |
c4ab09ef | 630 | static UInt32 *MixMatches4(CMatchFinderMt *p, UInt32 matchMinPos, UInt32 *distances)\r |
30fdf114 | 631 | {\r |
c4ab09ef | 632 | UInt32 h2, h3, h4, curMatch2, curMatch3, curMatch4;\r |
30fdf114 LG |
633 | UInt32 *hash = p->hash;\r |
634 | const Byte *cur = p->pointerToCurPos;\r | |
635 | UInt32 lzPos = p->lzPos;\r | |
636 | MT_HASH4_CALC\r | |
637 | \r | |
c4ab09ef | 638 | curMatch2 = hash[ h2];\r |
5ec5a236 LG |
639 | curMatch3 = (hash + kFix3HashSize)[h3];\r |
640 | curMatch4 = (hash + kFix4HashSize)[h4];\r | |
30fdf114 | 641 | \r |
c4ab09ef | 642 | hash[ h2] = lzPos;\r |
5ec5a236 LG |
643 | (hash + kFix3HashSize)[h3] = lzPos;\r |
644 | (hash + kFix4HashSize)[h4] = lzPos;\r | |
30fdf114 LG |
645 | \r |
646 | if (curMatch2 >= matchMinPos && cur[(ptrdiff_t)curMatch2 - lzPos] == cur[0])\r | |
647 | {\r | |
648 | distances[1] = lzPos - curMatch2 - 1;\r | |
649 | if (cur[(ptrdiff_t)curMatch2 - lzPos + 2] == cur[2])\r | |
650 | {\r | |
c4ab09ef | 651 | distances[0] = (cur[(ptrdiff_t)curMatch2 - lzPos + 3] == cur[3]) ? 4 : 3;\r |
30fdf114 LG |
652 | return distances + 2;\r |
653 | }\r | |
654 | distances[0] = 2;\r | |
655 | distances += 2;\r | |
656 | }\r | |
c4ab09ef | 657 | \r |
30fdf114 LG |
658 | if (curMatch3 >= matchMinPos && cur[(ptrdiff_t)curMatch3 - lzPos] == cur[0])\r |
659 | {\r | |
660 | distances[1] = lzPos - curMatch3 - 1;\r | |
661 | if (cur[(ptrdiff_t)curMatch3 - lzPos + 3] == cur[3])\r | |
662 | {\r | |
663 | distances[0] = 4;\r | |
664 | return distances + 2;\r | |
665 | }\r | |
666 | distances[0] = 3;\r | |
667 | distances += 2;\r | |
668 | }\r | |
669 | \r | |
670 | if (curMatch4 >= matchMinPos)\r | |
671 | if (\r | |
672 | cur[(ptrdiff_t)curMatch4 - lzPos] == cur[0] &&\r | |
673 | cur[(ptrdiff_t)curMatch4 - lzPos + 3] == cur[3]\r | |
674 | )\r | |
675 | {\r | |
676 | *distances++ = 4;\r | |
677 | *distances++ = lzPos - curMatch4 - 1;\r | |
678 | }\r | |
c4ab09ef | 679 | \r |
30fdf114 LG |
680 | return distances;\r |
681 | }\r | |
682 | */\r | |
683 | \r | |
684 | #define INCREASE_LZ_POS p->lzPos++; p->pointerToCurPos++;\r | |
685 | \r | |
c4ab09ef | 686 | static UInt32 MatchFinderMt2_GetMatches(CMatchFinderMt *p, UInt32 *distances)\r |
30fdf114 LG |
687 | {\r |
688 | const UInt32 *btBuf = p->btBuf + p->btBufPos;\r | |
689 | UInt32 len = *btBuf++;\r | |
690 | p->btBufPos += 1 + len;\r | |
691 | p->btNumAvailBytes--;\r | |
692 | {\r | |
693 | UInt32 i;\r | |
694 | for (i = 0; i < len; i += 2)\r | |
695 | {\r | |
5ec5a236 LG |
696 | UInt32 v0 = btBuf[0];\r |
697 | UInt32 v1 = btBuf[1];\r | |
698 | btBuf += 2;\r | |
699 | distances[0] = v0;\r | |
700 | distances[1] = v1;\r | |
701 | distances += 2;\r | |
30fdf114 LG |
702 | }\r |
703 | }\r | |
704 | INCREASE_LZ_POS\r | |
705 | return len;\r | |
706 | }\r | |
707 | \r | |
c4ab09ef | 708 | static UInt32 MatchFinderMt_GetMatches(CMatchFinderMt *p, UInt32 *distances)\r |
30fdf114 LG |
709 | {\r |
710 | const UInt32 *btBuf = p->btBuf + p->btBufPos;\r | |
711 | UInt32 len = *btBuf++;\r | |
712 | p->btBufPos += 1 + len;\r | |
713 | \r | |
714 | if (len == 0)\r | |
715 | {\r | |
c4ab09ef | 716 | /* change for bt5 ! */\r |
30fdf114 LG |
717 | if (p->btNumAvailBytes-- >= 4)\r |
718 | len = (UInt32)(p->MixMatchesFunc(p, p->lzPos - p->historySize, distances) - (distances));\r | |
719 | }\r | |
720 | else\r | |
721 | {\r | |
722 | /* Condition: there are matches in btBuf with length < p->numHashBytes */\r | |
723 | UInt32 *distances2;\r | |
724 | p->btNumAvailBytes--;\r | |
725 | distances2 = p->MixMatchesFunc(p, p->lzPos - btBuf[1], distances);\r | |
726 | do\r | |
727 | {\r | |
5ec5a236 LG |
728 | UInt32 v0 = btBuf[0];\r |
729 | UInt32 v1 = btBuf[1];\r | |
730 | btBuf += 2;\r | |
731 | distances2[0] = v0;\r | |
732 | distances2[1] = v1;\r | |
733 | distances2 += 2;\r | |
30fdf114 LG |
734 | }\r |
735 | while ((len -= 2) != 0);\r | |
c4ab09ef | 736 | len = (UInt32)(distances2 - (distances));\r |
30fdf114 LG |
737 | }\r |
738 | INCREASE_LZ_POS\r | |
739 | return len;\r | |
740 | }\r | |
741 | \r | |
c4ab09ef LG |
742 | #define SKIP_HEADER2_MT do { GET_NEXT_BLOCK_IF_REQUIRED\r |
743 | #define SKIP_HEADER_MT(n) SKIP_HEADER2_MT if (p->btNumAvailBytes-- >= (n)) { const Byte *cur = p->pointerToCurPos; UInt32 *hash = p->hash;\r | |
744 | #define SKIP_FOOTER_MT } INCREASE_LZ_POS p->btBufPos += p->btBuf[p->btBufPos] + 1; } while (--num != 0);\r | |
30fdf114 | 745 | \r |
c4ab09ef | 746 | static void MatchFinderMt0_Skip(CMatchFinderMt *p, UInt32 num)\r |
30fdf114 | 747 | {\r |
c4ab09ef LG |
748 | SKIP_HEADER2_MT { p->btNumAvailBytes--;\r |
749 | SKIP_FOOTER_MT\r | |
30fdf114 LG |
750 | }\r |
751 | \r | |
c4ab09ef | 752 | static void MatchFinderMt2_Skip(CMatchFinderMt *p, UInt32 num)\r |
30fdf114 | 753 | {\r |
c4ab09ef LG |
754 | SKIP_HEADER_MT(2)\r |
755 | UInt32 h2;\r | |
30fdf114 | 756 | MT_HASH2_CALC\r |
c4ab09ef LG |
757 | hash[h2] = p->lzPos;\r |
758 | SKIP_FOOTER_MT\r | |
30fdf114 LG |
759 | }\r |
760 | \r | |
c4ab09ef | 761 | static void MatchFinderMt3_Skip(CMatchFinderMt *p, UInt32 num)\r |
30fdf114 | 762 | {\r |
c4ab09ef LG |
763 | SKIP_HEADER_MT(3)\r |
764 | UInt32 h2, h3;\r | |
30fdf114 | 765 | MT_HASH3_CALC\r |
5ec5a236 | 766 | (hash + kFix3HashSize)[h3] =\r |
c4ab09ef | 767 | hash[ h2] =\r |
30fdf114 | 768 | p->lzPos;\r |
c4ab09ef | 769 | SKIP_FOOTER_MT\r |
30fdf114 LG |
770 | }\r |
771 | \r | |
772 | /*\r | |
c4ab09ef | 773 | static void MatchFinderMt4_Skip(CMatchFinderMt *p, UInt32 num)\r |
30fdf114 | 774 | {\r |
c4ab09ef LG |
775 | SKIP_HEADER_MT(4)\r |
776 | UInt32 h2, h3, h4;\r | |
30fdf114 | 777 | MT_HASH4_CALC\r |
5ec5a236 LG |
778 | (hash + kFix4HashSize)[h4] =\r |
779 | (hash + kFix3HashSize)[h3] =\r | |
c4ab09ef | 780 | hash[ h2] =\r |
30fdf114 | 781 | p->lzPos;\r |
c4ab09ef | 782 | SKIP_FOOTER_MT\r |
30fdf114 LG |
783 | }\r |
784 | */\r | |
785 | \r | |
786 | void MatchFinderMt_CreateVTable(CMatchFinderMt *p, IMatchFinder *vTable)\r | |
787 | {\r | |
788 | vTable->Init = (Mf_Init_Func)MatchFinderMt_Init;\r | |
30fdf114 LG |
789 | vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinderMt_GetNumAvailableBytes;\r |
790 | vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinderMt_GetPointerToCurrentPos;\r | |
791 | vTable->GetMatches = (Mf_GetMatches_Func)MatchFinderMt_GetMatches;\r | |
c4ab09ef LG |
792 | \r |
793 | switch (p->MatchFinder->numHashBytes)\r | |
30fdf114 LG |
794 | {\r |
795 | case 2:\r | |
796 | p->GetHeadsFunc = GetHeads2;\r | |
5ec5a236 | 797 | p->MixMatchesFunc = (Mf_Mix_Matches)NULL;\r |
30fdf114 LG |
798 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt0_Skip;\r |
799 | vTable->GetMatches = (Mf_GetMatches_Func)MatchFinderMt2_GetMatches;\r | |
800 | break;\r | |
801 | case 3:\r | |
802 | p->GetHeadsFunc = GetHeads3;\r | |
803 | p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches2;\r | |
804 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt2_Skip;\r | |
805 | break;\r | |
806 | default:\r | |
807 | /* case 4: */\r | |
808 | p->GetHeadsFunc = p->MatchFinder->bigHash ? GetHeads4b : GetHeads4;\r | |
30fdf114 LG |
809 | p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches3;\r |
810 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt3_Skip;\r | |
811 | break;\r | |
812 | /*\r | |
813 | default:\r | |
814 | p->GetHeadsFunc = GetHeads5;\r | |
815 | p->MixMatchesFunc = (Mf_Mix_Matches)MixMatches4;\r | |
816 | vTable->Skip = (Mf_Skip_Func)MatchFinderMt4_Skip;\r | |
817 | break;\r | |
818 | */\r | |
819 | }\r | |
820 | }\r |