Skip to content

Commit

Permalink
zopfli: some optimizations
Browse files Browse the repository at this point in the history
This include _BitScanReverse support which I sent a PR:
google/zopfli#102 and some other minor changes.
  • Loading branch information
JayXon committed Apr 16, 2016
1 parent af32331 commit b4358c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
10 changes: 5 additions & 5 deletions lib/zopfli/squeeze.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ static double GetBestLengths(ZopfliBlockState *s,
}
}
/* Lengths. */
for (k = 3; k <= leng && i + k <= inend; k++) {
double newCost;

double kmincost = mincost + costs[j];
int kend = leng <= inend - i ? leng : inend - i;
for (k = 3; k <= kend; k++) {
/* Calling the cost model is expensive, avoid this if we are already at
the minimum possible cost that it can return. */
if (costs[j + k] - costs[j] <= mincost) continue;
if (costs[j + k] <= kmincost) continue;

newCost = costs[j] + costmodel(k, sublen[k], costcontext);
double newCost = costs[j] + costmodel(k, sublen[k], costcontext);
assert(newCost >= 0);
if (newCost < costs[j + k]) {
assert(k <= ZOPFLI_MAX_MATCH);
Expand Down
30 changes: 27 additions & 3 deletions lib/zopfli/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ Author: [email protected] (Jyrki Alakuijala)
/* __builtin_clz available beginning with GCC 3.4 */
#elif __GNUC__ * 100 + __GNUC_MINOR__ >= 304
# define HAS_BUILTIN_CLZ
/* _BitScanReverse available beginning with Visual Studio 2005 */
#elif _MSC_VER >= 1400
# include <intrin.h>
# define HAS_BITSCANREVERSE
#endif

int ZopfliGetDistExtraBits(int dist) {
#ifdef HAS_BUILTIN_CLZ
if (dist < 5) return 0;
#ifdef HAS_BUILTIN_CLZ
return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */
#elif defined HAS_BITSCANREVERSE
unsigned long l;
_BitScanReverse(&l, dist - 1);
return l - 1;
#else
if (dist < 5) return 0;
else if (dist < 9) return 1;
if (dist < 9) return 1;
else if (dist < 17) return 2;
else if (dist < 33) return 3;
else if (dist < 65) return 4;
Expand All @@ -65,6 +72,14 @@ int ZopfliGetDistExtraBitsValue(int dist) {
int l = 31 ^ __builtin_clz(dist - 1); /* log2(dist - 1) */
return (dist - (1 + (1 << l))) & ((1 << (l - 1)) - 1);
}
#elif defined HAS_BITSCANREVERSE
if (dist < 5) {
return 0;
} else {
unsigned long l;
_BitScanReverse(&l, dist - 1);
return (dist - (1 + (1 << l))) & ((1 << (l - 1)) - 1);
}
#else
if (dist < 5) return 0;
else if (dist < 9) return (dist - 5) & 1;
Expand Down Expand Up @@ -92,6 +107,15 @@ int ZopfliGetDistSymbol(int dist) {
int r = ((dist - 1) >> (l - 1)) & 1;
return l * 2 + r;
}
#elif defined HAS_BITSCANREVERSE
if (dist < 5) {
return dist - 1;
} else {
unsigned long l;
_BitScanReverse(&l, dist - 1);
int r = ((dist - 1) >> (l - 1)) & 1;
return l * 2 + r;
}
#else
if (dist < 193) {
if (dist < 13) { /* dist 0..13. */
Expand Down

0 comments on commit b4358c2

Please sign in to comment.