/* LzFind.c -- Match finder for LZ algorithms\r
-2017-06-10 : Igor Pavlov : Public domain */\r
+2018-07-08 : Igor Pavlov : Public domain */\r
\r
#include "Precomp.h"\r
\r
\r
void MatchFinder_Construct(CMatchFinder *p)\r
{\r
- UInt32 i;\r
+ unsigned i;\r
p->bufferBase = NULL;\r
p->directInput = 0;\r
p->hash = NULL;\r
\r
for (i = 0; i < 256; i++)\r
{\r
- UInt32 r = i;\r
+ UInt32 r = (UInt32)i;\r
unsigned j;\r
for (j = 0; j < 8; j++)\r
r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1)));\r
MatchFinder_ReduceOffsets(p, subValue);\r
}\r
\r
+\r
+MY_NO_INLINE\r
static void MatchFinder_CheckLimits(CMatchFinder *p)\r
{\r
if (p->pos == kMaxValForNormalize)\r
MatchFinder_SetLimits(p);\r
}\r
\r
-static UInt32 * Hc_GetMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
+\r
+/*\r
+ (lenLimit > maxLen)\r
+*/\r
+MY_FORCE_INLINE\r
+static UInt32 * Hc_GetMatchesSpec(unsigned lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,\r
- UInt32 *distances, UInt32 maxLen)\r
+ UInt32 *distances, unsigned maxLen)\r
{\r
+ /*\r
son[_cyclicBufferPos] = curMatch;\r
for (;;)\r
{\r
break;\r
if (maxLen < len)\r
{\r
- *distances++ = maxLen = len;\r
+ maxLen = len;\r
+ *distances++ = len;\r
*distances++ = delta - 1;\r
if (len == lenLimit)\r
return distances;\r
}\r
}\r
}\r
+ */\r
+\r
+ const Byte *lim = cur + lenLimit;\r
+ son[_cyclicBufferPos] = curMatch;\r
+ do\r
+ {\r
+ UInt32 delta = pos - curMatch;\r
+ if (delta >= _cyclicBufferSize)\r
+ break;\r
+ {\r
+ ptrdiff_t diff;\r
+ curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)];\r
+ diff = (ptrdiff_t)0 - delta;\r
+ if (cur[maxLen] == cur[maxLen + diff])\r
+ {\r
+ const Byte *c = cur;\r
+ while (*c == c[diff])\r
+ {\r
+ if (++c == lim)\r
+ {\r
+ distances[0] = (UInt32)(lim - cur);\r
+ distances[1] = delta - 1;\r
+ return distances + 2;\r
+ }\r
+ }\r
+ {\r
+ unsigned len = (unsigned)(c - cur);\r
+ if (maxLen < len)\r
+ {\r
+ maxLen = len;\r
+ distances[0] = (UInt32)len;\r
+ distances[1] = delta - 1;\r
+ distances += 2;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ while (--cutValue);\r
+\r
+ return distances;\r
}\r
\r
+\r
+MY_FORCE_INLINE\r
UInt32 * GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue,\r
UInt32 *distances, UInt32 maxLen)\r
{\r
- CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;\r
- CLzRef *ptr1 = son + (_cyclicBufferPos << 1);\r
- UInt32 len0 = 0, len1 = 0;\r
+ CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;\r
+ CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);\r
+ unsigned len0 = 0, len1 = 0;\r
for (;;)\r
{\r
UInt32 delta = pos - curMatch;\r
return distances;\r
}\r
{\r
- CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
+ CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
const Byte *pb = cur - delta;\r
- UInt32 len = (len0 < len1 ? len0 : len1);\r
+ unsigned len = (len0 < len1 ? len0 : len1);\r
+ UInt32 pair0 = pair[0];\r
if (pb[len] == cur[len])\r
{\r
if (++len != lenLimit && pb[len] == cur[len])\r
break;\r
if (maxLen < len)\r
{\r
- *distances++ = maxLen = len;\r
+ maxLen = (UInt32)len;\r
+ *distances++ = (UInt32)len;\r
*distances++ = delta - 1;\r
if (len == lenLimit)\r
{\r
- *ptr1 = pair[0];\r
+ *ptr1 = pair0;\r
*ptr0 = pair[1];\r
return distances;\r
}\r
static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte *cur, CLzRef *son,\r
UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue)\r
{\r
- CLzRef *ptr0 = son + (_cyclicBufferPos << 1) + 1;\r
- CLzRef *ptr1 = son + (_cyclicBufferPos << 1);\r
- UInt32 len0 = 0, len1 = 0;\r
+ CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;\r
+ CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);\r
+ unsigned len0 = 0, len1 = 0;\r
for (;;)\r
{\r
UInt32 delta = pos - curMatch;\r
return;\r
}\r
{\r
- CLzRef *pair = son + ((_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
+ CLzRef *pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1);\r
const Byte *pb = cur - delta;\r
- UInt32 len = (len0 < len1 ? len0 : len1);\r
+ unsigned len = (len0 < len1 ? len0 : len1);\r
if (pb[len] == cur[len])\r
{\r
while (++len != lenLimit)\r
p->buffer++; \\r
if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p);\r
\r
-#define MOVE_POS_RET MOVE_POS return offset;\r
+#define MOVE_POS_RET MOVE_POS return (UInt32)offset;\r
\r
static void MatchFinder_MovePos(CMatchFinder *p) { MOVE_POS; }\r
\r
#define GET_MATCHES_HEADER2(minLen, ret_op) \\r
- UInt32 lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \\r
- lenLimit = p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \\r
+ unsigned lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \\r
+ lenLimit = (unsigned)p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \\r
cur = p->buffer;\r
\r
#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0)\r
#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue\r
\r
#define GET_MATCHES_FOOTER(offset, maxLen) \\r
- offset = (UInt32)(GetMatchesSpec1(lenLimit, curMatch, MF_PARAMS(p), \\r
- distances + offset, maxLen) - distances); MOVE_POS_RET;\r
+ offset = (unsigned)(GetMatchesSpec1((UInt32)lenLimit, curMatch, MF_PARAMS(p), \\r
+ distances + offset, (UInt32)maxLen) - distances); MOVE_POS_RET;\r
\r
#define SKIP_FOOTER \\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;\r
+ SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS;\r
\r
#define UPDATE_maxLen { \\r
ptrdiff_t diff = (ptrdiff_t)0 - d2; \\r
const Byte *c = cur + maxLen; \\r
const Byte *lim = cur + lenLimit; \\r
for (; c != lim; c++) if (*(c + diff) != *c) break; \\r
- maxLen = (UInt32)(c - cur); }\r
+ maxLen = (unsigned)(c - cur); }\r
\r
static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 offset;\r
+ unsigned offset;\r
GET_MATCHES_HEADER(2)\r
HASH2_CALC;\r
curMatch = p->hash[hv];\r
\r
UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 offset;\r
+ unsigned offset;\r
GET_MATCHES_HEADER(3)\r
HASH_ZIP_CALC;\r
curMatch = p->hash[hv];\r
\r
static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 h2, d2, maxLen, offset, pos;\r
+ UInt32 h2, d2, pos;\r
+ unsigned maxLen, offset;\r
UInt32 *hash;\r
GET_MATCHES_HEADER(3)\r
\r
if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
{\r
UPDATE_maxLen\r
- distances[0] = maxLen;\r
+ distances[0] = (UInt32)maxLen;\r
distances[1] = d2 - 1;\r
offset = 2;\r
if (maxLen == lenLimit)\r
{\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
+ SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));\r
MOVE_POS_RET;\r
}\r
}\r
\r
static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
+ UInt32 h2, h3, d2, d3, pos;\r
+ unsigned maxLen, offset;\r
UInt32 *hash;\r
GET_MATCHES_HEADER(4)\r
\r
\r
if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
{\r
- distances[0] = maxLen = 2;\r
+ maxLen = 2;\r
+ distances[0] = 2;\r
distances[1] = d2 - 1;\r
offset = 2;\r
}\r
if (offset != 0)\r
{\r
UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
+ distances[(size_t)offset - 2] = (UInt32)maxLen;\r
if (maxLen == lenLimit)\r
{\r
- SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p));\r
+ SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p));\r
MOVE_POS_RET;\r
}\r
}\r
\r
static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 h2, h3, d2, d3, maxLen, offset, pos;\r
+ UInt32 h2, h3, d2, d3, pos;\r
+ unsigned maxLen, offset;\r
UInt32 *hash;\r
GET_MATCHES_HEADER(4)\r
\r
\r
if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur)\r
{\r
- distances[0] = maxLen = 2;\r
+ maxLen = 2;\r
+ distances[0] = 2;\r
distances[1] = d2 - 1;\r
offset = 2;\r
}\r
if (offset != 0)\r
{\r
UPDATE_maxLen\r
- distances[(size_t)offset - 2] = maxLen;\r
+ distances[(size_t)offset - 2] = (UInt32)maxLen;\r
if (maxLen == lenLimit)\r
{\r
p->son[p->cyclicBufferPos] = curMatch;\r
if (maxLen < 3)\r
maxLen = 3;\r
\r
- offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
+ offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
distances + offset, maxLen) - (distances));\r
MOVE_POS_RET\r
}\r
\r
UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances)\r
{\r
- UInt32 offset;\r
+ unsigned offset;\r
GET_MATCHES_HEADER(3)\r
HASH_ZIP_CALC;\r
curMatch = p->hash[hv];\r
p->hash[hv] = p->pos;\r
- offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
+ offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p),\r
distances, 2) - (distances));\r
MOVE_POS_RET\r
}\r