Skip to content

Commit b4358c2

Browse files
committed
zopfli: some optimizations
This include _BitScanReverse support which I sent a PR: google/zopfli#102 and some other minor changes.
1 parent af32331 commit b4358c2

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

lib/zopfli/squeeze.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ static double GetBestLengths(ZopfliBlockState *s,
282282
}
283283
}
284284
/* Lengths. */
285-
for (k = 3; k <= leng && i + k <= inend; k++) {
286-
double newCost;
287-
285+
double kmincost = mincost + costs[j];
286+
int kend = leng <= inend - i ? leng : inend - i;
287+
for (k = 3; k <= kend; k++) {
288288
/* Calling the cost model is expensive, avoid this if we are already at
289289
the minimum possible cost that it can return. */
290-
if (costs[j + k] - costs[j] <= mincost) continue;
290+
if (costs[j + k] <= kmincost) continue;
291291

292-
newCost = costs[j] + costmodel(k, sublen[k], costcontext);
292+
double newCost = costs[j] + costmodel(k, sublen[k], costcontext);
293293
assert(newCost >= 0);
294294
if (newCost < costs[j + k]) {
295295
assert(k <= ZOPFLI_MAX_MATCH);

lib/zopfli/util.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,22 @@ Author: [email protected] (Jyrki Alakuijala)
3333
/* __builtin_clz available beginning with GCC 3.4 */
3434
#elif __GNUC__ * 100 + __GNUC_MINOR__ >= 304
3535
# define HAS_BUILTIN_CLZ
36+
/* _BitScanReverse available beginning with Visual Studio 2005 */
37+
#elif _MSC_VER >= 1400
38+
# include <intrin.h>
39+
# define HAS_BITSCANREVERSE
3640
#endif
3741

3842
int ZopfliGetDistExtraBits(int dist) {
39-
#ifdef HAS_BUILTIN_CLZ
4043
if (dist < 5) return 0;
44+
#ifdef HAS_BUILTIN_CLZ
4145
return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */
46+
#elif defined HAS_BITSCANREVERSE
47+
unsigned long l;
48+
_BitScanReverse(&l, dist - 1);
49+
return l - 1;
4250
#else
43-
if (dist < 5) return 0;
44-
else if (dist < 9) return 1;
51+
if (dist < 9) return 1;
4552
else if (dist < 17) return 2;
4653
else if (dist < 33) return 3;
4754
else if (dist < 65) return 4;
@@ -65,6 +72,14 @@ int ZopfliGetDistExtraBitsValue(int dist) {
6572
int l = 31 ^ __builtin_clz(dist - 1); /* log2(dist - 1) */
6673
return (dist - (1 + (1 << l))) & ((1 << (l - 1)) - 1);
6774
}
75+
#elif defined HAS_BITSCANREVERSE
76+
if (dist < 5) {
77+
return 0;
78+
} else {
79+
unsigned long l;
80+
_BitScanReverse(&l, dist - 1);
81+
return (dist - (1 + (1 << l))) & ((1 << (l - 1)) - 1);
82+
}
6883
#else
6984
if (dist < 5) return 0;
7085
else if (dist < 9) return (dist - 5) & 1;
@@ -92,6 +107,15 @@ int ZopfliGetDistSymbol(int dist) {
92107
int r = ((dist - 1) >> (l - 1)) & 1;
93108
return l * 2 + r;
94109
}
110+
#elif defined HAS_BITSCANREVERSE
111+
if (dist < 5) {
112+
return dist - 1;
113+
} else {
114+
unsigned long l;
115+
_BitScanReverse(&l, dist - 1);
116+
int r = ((dist - 1) >> (l - 1)) & 1;
117+
return l * 2 + r;
118+
}
95119
#else
96120
if (dist < 193) {
97121
if (dist < 13) { /* dist 0..13. */

0 commit comments

Comments
 (0)