1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/7zip/Compress/RangeCoder/RangeCoderBit.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +// Compress/RangeCoder/RangeCoderBit.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#include "RangeCoderBit.h" 1.9 + 1.10 +namespace NCompress { 1.11 +namespace NRangeCoder { 1.12 + 1.13 +UInt32 CPriceTables::ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; 1.14 +static CPriceTables g_PriceTables; 1.15 + 1.16 +CPriceTables::CPriceTables() { Init(); } 1.17 + 1.18 +void CPriceTables::Init() 1.19 +{ 1.20 + const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits); 1.21 + for(int i = kNumBits - 1; i >= 0; i--) 1.22 + { 1.23 + UInt32 start = 1 << (kNumBits - i - 1); 1.24 + UInt32 end = 1 << (kNumBits - i); 1.25 + for (UInt32 j = start; j < end; j++) 1.26 + ProbPrices[j] = (i << kNumBitPriceShiftBits) + 1.27 + (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1)); 1.28 + } 1.29 + 1.30 + /* 1.31 + // simplest: bad solution 1.32 + for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) 1.33 + ProbPrices[i] = kBitPrice; 1.34 + */ 1.35 + 1.36 + /* 1.37 + const double kDummyMultMid = (1.0 / kBitPrice) / 2; 1.38 + const double kDummyMultMid = 0; 1.39 + // float solution 1.40 + double ln2 = log(double(2)); 1.41 + double lnAll = log(double(kBitModelTotal >> kNumMoveReducingBits)); 1.42 + for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) 1.43 + ProbPrices[i] = UInt32((fabs(lnAll - log(double(i))) / ln2 + kDummyMultMid) * kBitPrice); 1.44 + */ 1.45 + 1.46 + /* 1.47 + // experimental, slow, solution: 1.48 + for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) 1.49 + { 1.50 + const int kCyclesBits = 5; 1.51 + const UInt32 kCycles = (1 << kCyclesBits); 1.52 + 1.53 + UInt32 range = UInt32(-1); 1.54 + UInt32 bitCount = 0; 1.55 + for (UInt32 j = 0; j < kCycles; j++) 1.56 + { 1.57 + range >>= (kNumBitModelTotalBits - kNumMoveReducingBits); 1.58 + range *= i; 1.59 + while(range < (1 << 31)) 1.60 + { 1.61 + range <<= 1; 1.62 + bitCount++; 1.63 + } 1.64 + } 1.65 + bitCount <<= kNumBitPriceShiftBits; 1.66 + range -= (1 << 31); 1.67 + for (int k = kNumBitPriceShiftBits - 1; k >= 0; k--) 1.68 + { 1.69 + range <<= 1; 1.70 + if (range > (1 << 31)) 1.71 + { 1.72 + bitCount += (1 << k); 1.73 + range -= (1 << 31); 1.74 + } 1.75 + } 1.76 + ProbPrices[i] = (bitCount 1.77 + // + (1 << (kCyclesBits - 1)) 1.78 + ) >> kCyclesBits; 1.79 + } 1.80 + */ 1.81 +} 1.82 + 1.83 +}}