summaryrefslogtreecommitdiffstats
path: root/hostTools/lzma/compress/LiteralCoder.cpp
diff options
context:
space:
mode:
authorroot <root@lamia.panaceas.james.local>2015-12-19 14:18:43 +0000
committerroot <root@lamia.panaceas.james.local>2015-12-19 14:18:43 +0000
commit71478fd62d8483483abb34609cdabb7f9cbadfd6 (patch)
tree37b8eaba1ffe2d5f775227911eb0ed6fdc3c9553 /hostTools/lzma/compress/LiteralCoder.cpp
parent1a2238d1bddc823df06f67312d96ccf9de2893cc (diff)
downloadbootloader-71478fd62d8483483abb34609cdabb7f9cbadfd6.tar.gz
bootloader-71478fd62d8483483abb34609cdabb7f9cbadfd6.tar.bz2
bootloader-71478fd62d8483483abb34609cdabb7f9cbadfd6.zip
Add hostTools from https://github.com/Noltari/cfe_bcm63xx
Diffstat (limited to 'hostTools/lzma/compress/LiteralCoder.cpp')
-rw-r--r--hostTools/lzma/compress/LiteralCoder.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/hostTools/lzma/compress/LiteralCoder.cpp b/hostTools/lzma/compress/LiteralCoder.cpp
new file mode 100644
index 0000000..fdb4fe3
--- /dev/null
+++ b/hostTools/lzma/compress/LiteralCoder.cpp
@@ -0,0 +1,66 @@
+#include "LiteralCoder.h"
+
+using namespace NCompression;
+using namespace NArithmetic;
+
+namespace NLiteral {
+
+void CEncoder2::Init()
+{
+ for (int i = 0; i < 3; i++)
+ for (int j = 1; j < (1 << 8); j++)
+ m_Encoders[i][j].Init();
+}
+
+void CEncoder2::Encode(CMyRangeEncoder *aRangeEncoder,
+ bool aMatchMode, BYTE aMatchByte, BYTE aSymbol)
+{
+ UINT32 aContext = 1;
+ bool aSame = true;
+ for (int i = 7; i >= 0; i--)
+ {
+ UINT32 aBit = (aSymbol >> i) & 1;
+ unsigned aState;
+ if (aMatchMode && aSame)
+ {
+ UINT32 aMatchBit = (aMatchByte >> i) & 1;
+ aState = 1 + aMatchBit;
+ aSame = (aMatchBit == aBit);
+ }
+ else
+ aState = 0;
+ m_Encoders[aState][aContext].Encode(aRangeEncoder, aBit);
+ aContext = (aContext << 1) | aBit;
+ }
+}
+
+UINT32 CEncoder2::GetPrice(bool aMatchMode, BYTE aMatchByte, BYTE aSymbol) const
+{
+ UINT32 aPrice = 0;
+ UINT32 aContext = 1;
+ int i = 7;
+ if (aMatchMode)
+ {
+ for (; i >= 0; i--)
+ {
+ UINT32 aMatchBit = (aMatchByte >> i) & 1;
+ UINT32 aBit = (aSymbol >> i) & 1;
+ aPrice += m_Encoders[1 + aMatchBit][aContext].GetPrice(aBit);
+ aContext = (aContext << 1) | aBit;
+ if (aMatchBit != aBit)
+ {
+ i--;
+ break;
+ }
+ }
+ }
+ for (; i >= 0; i--)
+ {
+ UINT32 aBit = (aSymbol >> i) & 1;
+ aPrice += m_Encoders[0][aContext].GetPrice(aBit);
+ aContext = (aContext << 1) | aBit;
+ }
+ return aPrice;
+};
+
+}