From 1d421aacf2af5dded0d817d5ca49b66e42441e45 Mon Sep 17 00:00:00 2001 From: Martin Haye Date: Wed, 28 Dec 2016 08:00:28 -0800 Subject: [PATCH] More experimentation. --- .../src/org/badvision/A2PackPartitions.groovy | 9 ++- .../src/org/badvision/Lx47Algorithm.java | 60 +++++++------------ 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy b/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy index b32b444c..ed017fcd 100644 --- a/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy +++ b/Platform/Apple/tools/PackPartitions/src/org/badvision/A2PackPartitions.groovy @@ -1212,6 +1212,8 @@ class A2PackPartitions fonts[name] = [num:num, buf:readBinary(path)] } + static int lx47Uncomp = 0 + static int lx47Comp = 0 static int lx47Savings = 0 // Transform the LZ4 format to something we call "LZ4M", where the small offsets are stored @@ -1292,8 +1294,11 @@ class A2PackPartitions lx47.decompress(outputData, uncomp) assert uncomp == inputData def savings = inLen - outputData.length + lx47Uncomp += uncompLen + lx47Comp += (uncompLen - outputData.length) lx47Savings += savings - println String.format("lz47 savings=%d total=%d", savings, lx47Savings) + println String.format("lz47 savings=%d total_uncomp=%d total_comp=%d total_savings=%d", + savings, lx47Uncomp, lx47Comp, lx47Savings) } // Transform the LZ4 format to something we call "LZ4M", where the small offsets are stored @@ -1411,8 +1416,8 @@ class A2PackPartitions assert compressedLen > 0 // Then recompress to LZ4M (pretty much always smaller) - testLx47(compressedData, compressedLen, uncompressedData, uncompressedLen) def recompressedLen = recompress(compressedData, compressedLen, uncompressedData, uncompressedLen) + testLx47(compressedData, recompressedLen, uncompressedData, uncompressedLen) // If we saved at least 20 bytes, take the compressed version. if ((uncompressedLen - recompressedLen) >= 20) { diff --git a/Platform/Apple/tools/PackPartitions/src/org/badvision/Lx47Algorithm.java b/Platform/Apple/tools/PackPartitions/src/org/badvision/Lx47Algorithm.java index ea0bd62c..bfbfe30e 100644 --- a/Platform/Apple/tools/PackPartitions/src/org/badvision/Lx47Algorithm.java +++ b/Platform/Apple/tools/PackPartitions/src/org/badvision/Lx47Algorithm.java @@ -36,6 +36,10 @@ public class Lx47Algorithm } return bits; } + + int elias_exp_gamma_bits(int value, int exp) { + return elias_gamma_bits((value >> exp) + 1) + exp; + } int count_bits(int offset, int len) { return 1 + (offset > 128 ? 12 : 8) + elias_gamma_bits(len-1); @@ -108,8 +112,6 @@ public class Lx47Algorithm return optimal; } - static int nBigLits = 0; - static int nBigMatches = 0; static int nOffsets = 0; static int nPrevOffsets = 0; static int nPrev2Offsets = 0; @@ -153,25 +155,14 @@ public class Lx47Algorithm void writeEliasExpGamma(int value, int exp) { assert value > 0; - writeEliasGamma(((value-1) >> exp) + 1); - for (int i=exp-1; i>=0; i--) + writeEliasGamma((value >> exp) + 1); + for (int i=exp-1; i>=0; i--) { writeBit(value & (1< 255) { - nBigLits++; - value -= 255; } } void writeMatchLen(int value) { - writeEliasExpGamma(value, 1); - while (value > 255) { - nBigMatches++; - value -= 255; - } + writeEliasGamma(value); } void write2byte(int offset) { @@ -187,21 +178,8 @@ public class Lx47Algorithm } } - int prevOff = -1; - int prevOff2 = -1; - void writeOffset(int offset) { write2byte(offset); - if (offset >= 126) { - ++nOffsets; - if (offset == prevOff) - ++nPrevOffsets; - else if (offset == prevOff2) - ++nPrev2Offsets; - prevOff2 = prevOff; - prevOff = offset; - } - //writeEliasExpGamma(offset, 7) // same; other values worse } } @@ -248,11 +226,10 @@ public class Lx47Algorithm w.writeBit(1); /* sequence length */ - w.writeEliasGamma(optimal[input_index].len-1); + w.writeMatchLen(optimal[input_index].len-1); /* sequence offset */ - offset1 = optimal[input_index].offset-1; - w.writeOffset(offset1); + w.writeOffset(optimal[input_index].offset-1); } } @@ -309,9 +286,18 @@ public class Lx47Algorithm out = (out << 1) | readBit(); return out; } - - int readLiteralLen() { - return readEliasGamma(); + + int readEliasExpGamma(int exp) { + int val = readEliasGamma(); + if (val < 0) + return val; + val = (val-1) << exp; + for (int i=exp-1; i>=0; i--) { + int bit = readBit(); + if (bit > 0) + val |= (1< 0) { output_data[outPos] = output_data[outPos - off];