michael@0: // Compress/RangeCoder/RangeCoderBit.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #include "RangeCoderBit.h" michael@0: michael@0: namespace NCompress { michael@0: namespace NRangeCoder { michael@0: michael@0: UInt32 CPriceTables::ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; michael@0: static CPriceTables g_PriceTables; michael@0: michael@0: CPriceTables::CPriceTables() { Init(); } michael@0: michael@0: void CPriceTables::Init() michael@0: { michael@0: const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits); michael@0: for(int i = kNumBits - 1; i >= 0; i--) michael@0: { michael@0: UInt32 start = 1 << (kNumBits - i - 1); michael@0: UInt32 end = 1 << (kNumBits - i); michael@0: for (UInt32 j = start; j < end; j++) michael@0: ProbPrices[j] = (i << kNumBitPriceShiftBits) + michael@0: (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1)); michael@0: } michael@0: michael@0: /* michael@0: // simplest: bad solution michael@0: for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) michael@0: ProbPrices[i] = kBitPrice; michael@0: */ michael@0: michael@0: /* michael@0: const double kDummyMultMid = (1.0 / kBitPrice) / 2; michael@0: const double kDummyMultMid = 0; michael@0: // float solution michael@0: double ln2 = log(double(2)); michael@0: double lnAll = log(double(kBitModelTotal >> kNumMoveReducingBits)); michael@0: for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) michael@0: ProbPrices[i] = UInt32((fabs(lnAll - log(double(i))) / ln2 + kDummyMultMid) * kBitPrice); michael@0: */ michael@0: michael@0: /* michael@0: // experimental, slow, solution: michael@0: for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++) michael@0: { michael@0: const int kCyclesBits = 5; michael@0: const UInt32 kCycles = (1 << kCyclesBits); michael@0: michael@0: UInt32 range = UInt32(-1); michael@0: UInt32 bitCount = 0; michael@0: for (UInt32 j = 0; j < kCycles; j++) michael@0: { michael@0: range >>= (kNumBitModelTotalBits - kNumMoveReducingBits); michael@0: range *= i; michael@0: while(range < (1 << 31)) michael@0: { michael@0: range <<= 1; michael@0: bitCount++; michael@0: } michael@0: } michael@0: bitCount <<= kNumBitPriceShiftBits; michael@0: range -= (1 << 31); michael@0: for (int k = kNumBitPriceShiftBits - 1; k >= 0; k--) michael@0: { michael@0: range <<= 1; michael@0: if (range > (1 << 31)) michael@0: { michael@0: bitCount += (1 << k); michael@0: range -= (1 << 31); michael@0: } michael@0: } michael@0: ProbPrices[i] = (bitCount michael@0: // + (1 << (kCyclesBits - 1)) michael@0: ) >> kCyclesBits; michael@0: } michael@0: */ michael@0: } michael@0: michael@0: }}