Skip to content

Commit c35d606

Browse files
committed
Code optimization
1 parent 447d378 commit c35d606

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/transform/LZCodec.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
256256
// Emit match
257257
srcInc = 0;
258258

259-
// Token: 3 bits litLen + 1 bit flag + 4 bits mLen (LLLFFMMM)
259+
// Token: 3 bits litLen + 2 bits flag + 3 bits mLen (LLLFFMMM)
260260
// LLL : <= 7 --> LLL == literal length (if 7, remainder encoded outside of token)
261261
// MMM : <= 6 --> MMMM == match length (if 6, remainder encoded outside of token)
262262
// FF : if MMM == 7
263-
// FF = 00 if dist == repd0
264-
// FF = 01 if dist == repd1
263+
// FF = x0 if dist == repd0
264+
// FF = x1 if dist == repd1
265265
// else
266-
// FF=00 => 1 byte dist
267-
// FF=01 => 2 byte dist
268-
// FF=10 => 3 byte dist
269-
// FF=11 => 3 byte dist
266+
// FF = 00 => 1 byte dist
267+
// FF = 01 => 2 byte dist
268+
// FF = 10 => 2 byte dist
269+
// FF = 11 => 3 byte dist
270270
const int dist = srcIdx - ref;
271271
int token;
272272

@@ -280,17 +280,21 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
280280
}
281281
else {
282282
// Emit distance (since not repeat)
283-
int flag = 0;
283+
int flag;
284284

285285
if (dist >= 65536 ) {
286-
_mBuf[mIdx++] = byte(dist >> 16);
286+
_mBuf[mIdx] = byte(dist >> 16);
287+
_mBuf[mIdx + 1] = byte(dist >> 8);
288+
mIdx += 2;
287289
flag = 2;
288290
}
291+
else {
292+
_mBuf[mIdx] = byte(dist >> 8);
293+
const int inc = (dist >= 256 ? 1 : 0);
294+
mIdx += inc;
295+
flag = inc;
296+
}
289297

290-
_mBuf[mIdx] = byte(dist >> 8);
291-
const int inc = (dist >= 256 ? 1 : 0);
292-
mIdx += inc;
293-
flag += inc;
294298
_mBuf[mIdx++] = byte(dist);
295299
const int mLen = bestLen - minMatch;
296300

@@ -469,11 +473,11 @@ bool LZXCodec<T>::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int
469473
else {
470474
// Read mLen remainder (if any) outside of token
471475
mLen = (mLen == 6) ? 6 + minMatch + readLength(src, mLenIdx) : mLen + minMatch;
476+
const int f2 = (token >> 4) & 1;
477+
const int f1 = ((token >> 4) | (token >> 3)) & 1;
472478
dist = int(src[mIdx++]);
473-
const int f1 = (token >> 3) & 1;
474479
dist = (dist << (8 * f1)) | (-f1 & int(src[mIdx]));
475480
mIdx += f1;
476-
const int f2 = (token >> 4) & 1;
477481
dist = (dist << (8 * f2)) | (-f2 & int(src[mIdx]));
478482
mIdx += f2;
479483
}

0 commit comments

Comments
 (0)