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