Skip to content

Commit 087e420

Browse files
committed
Small optimziations
1 parent a7f8fc5 commit 087e420

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/transform/ZRLT.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ bool ZRLT::forward(SliceArray<byte>& input, SliceArray<byte>& output, int length
4040
uint srcIdx = 0;
4141
uint dstIdx = 0;
4242
const uint srcEnd = length;
43-
const uint dstEnd = length; // do not expand
43+
const uint dstEnd = length - 16; // do not expand
44+
const uint srcEnd4 = length - 4;
4445
bool res = true;
4546
byte zeros[4] = { byte(0) };
4647

4748
while (srcIdx < srcEnd) {
4849
if (src[srcIdx] == byte(0)) {
4950
uint runLength = 1;
5051

51-
while ((srcIdx + runLength + 4 < srcEnd) && (memcmp(&src[srcIdx + runLength], &zeros[0], 4) == 0))
52+
while ((srcIdx + runLength < srcEnd4) && (memcmp(&src[srcIdx + runLength], &zeros[0], 4) == 0))
5253
runLength += 4;
5354

5455
while ((srcIdx + runLength < srcEnd) && src[srcIdx + runLength] == byte(0))
@@ -58,14 +59,26 @@ bool ZRLT::forward(SliceArray<byte>& input, SliceArray<byte>& output, int length
5859

5960
// Encode length
6061
runLength++;
61-
int log = Global::_log2(uint32(runLength));
6262

63-
if (dstIdx >= dstEnd - log) {
63+
if (dstIdx >= dstEnd) {
6464
res = false;
6565
break;
6666
}
6767

68+
int log = Global::_log2(uint32(runLength));
69+
6870
// Write every bit as a byte except the most significant one
71+
while (log > 3) {
72+
log--;
73+
dst[dstIdx++] = byte((runLength >> log) & 1);
74+
log--;
75+
dst[dstIdx++] = byte((runLength >> log) & 1);
76+
log--;
77+
dst[dstIdx++] = byte((runLength >> log) & 1);
78+
log--;
79+
dst[dstIdx++] = byte((runLength >> log) & 1);
80+
}
81+
6982
while (log > 0) {
7083
log--;
7184
dst[dstIdx++] = byte((runLength >> log) & 1);
@@ -74,24 +87,19 @@ bool ZRLT::forward(SliceArray<byte>& input, SliceArray<byte>& output, int length
7487
continue;
7588
}
7689

90+
if (dstIdx >= dstEnd) {
91+
res = false;
92+
break;
93+
}
94+
7795
const int val = int(src[srcIdx]);
7896

7997
if (val >= 0xFE) {
80-
if (dstIdx >= dstEnd - 1) {
81-
res = false;
82-
break;
83-
}
84-
8598
dst[dstIdx] = byte(0xFF);
99+
dst[dstIdx + 1] = byte(val - 0xFE);
86100
dstIdx++;
87-
dst[dstIdx] = byte(val - 0xFE);
88101
}
89102
else {
90-
if (dstIdx >= dstEnd) {
91-
res = false;
92-
break;
93-
}
94-
95103
dst[dstIdx] = byte(val + 1);
96104
}
97105

0 commit comments

Comments
 (0)