Skip to content

Commit

Permalink
Optimization and simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Nov 30, 2023
1 parent ca46356 commit ef62e12
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions v2/entropy/BinaryEntropyCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (this *BinaryEntropyEncoder) EncodeBit(bit byte, pred int) {
this.predictor.Update(bit)

// Write unchanged first 32 bits to bitstream
for (this.low^this.high)>>24 == 0 {
for (this.low ^ this.high) < (1 << 24) {
this.flush()
}
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (this *BinaryEntropyDecoder) DecodeBit(pred int) byte {
}

// Read 32 bits from bitstream
for (this.low^this.high)>>24 == 0 {
for (this.low ^ this.high) < (1 << 24) {
this.read()
}

Expand Down
32 changes: 15 additions & 17 deletions v2/entropy/FPAQCodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
const (
_FPAQ_PSCALE = 1 << 16
_FPAQ_DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024
_FPAQ_ENTROPY_TOP = uint64(0x00FFFFFFFFFFFFFF)
)

// FPAQEncoder entropy encoder derived from fpaq0r by Matt Mahoney & Alexander Ratushnyak.
Expand All @@ -39,7 +40,6 @@ type FPAQEncoder struct {
buffer []byte
index int
probs [4][]int // probability of bit=1
p []int // pointer to current prob
ctxIdx byte // previous bits
}

Expand All @@ -52,12 +52,11 @@ func NewFPAQEncoder(bs kanzi.OutputBitStream) (*FPAQEncoder, error) {

this := &FPAQEncoder{}
this.low = 0
this.high = _BINARY_ENTROPY_TOP
this.high = _FPAQ_ENTROPY_TOP
this.bitstream = bs
this.buffer = make([]byte, 0)
this.index = 0
this.ctxIdx = 1
this.p = this.probs[0]

for i := 0; i < 4; i++ {
this.probs[i] = make([]int, 256)
Expand All @@ -78,12 +77,11 @@ func NewFPAQEncoderWithCtx(bs kanzi.OutputBitStream, ctx *map[string]any) (*FPAQ

this := &FPAQEncoder{}
this.low = 0
this.high = _BINARY_ENTROPY_TOP
this.high = _FPAQ_ENTROPY_TOP
this.bitstream = bs
this.buffer = make([]byte, 0)
this.index = 0
this.ctxIdx = 1
this.p = this.probs[0]

for i := 0; i < 4; i++ {
this.probs[i] = make([]int, 256)
Expand Down Expand Up @@ -111,7 +109,7 @@ func (this *FPAQEncoder) encodeBit(bit byte, p *int) {
}

// Write unchanged first 32 bits to bitstream
for (this.low^this.high)>>24 == 0 {
for (this.low ^ this.high) < (1 << 24) {
this.flush()
}
}
Expand Down Expand Up @@ -143,19 +141,19 @@ func (this *FPAQEncoder) Write(block []byte) (int, error) {

this.index = 0
buf := block[startChunk : startChunk+chunkSize]
this.p = this.probs[0]
p := this.probs[0]

for _, val := range buf {
bits := int(val) + 256
this.encodeBit(val&0x80, &this.p[1])
this.encodeBit(val&0x40, &this.p[bits>>7])
this.encodeBit(val&0x20, &this.p[bits>>6])
this.encodeBit(val&0x10, &this.p[bits>>5])
this.encodeBit(val&0x08, &this.p[bits>>4])
this.encodeBit(val&0x04, &this.p[bits>>3])
this.encodeBit(val&0x02, &this.p[bits>>2])
this.encodeBit(val&0x01, &this.p[bits>>1])
this.p = this.probs[val>>6]
this.encodeBit(val&0x80, &p[1])
this.encodeBit(val&0x40, &p[bits>>7])
this.encodeBit(val&0x20, &p[bits>>6])
this.encodeBit(val&0x10, &p[bits>>5])
this.encodeBit(val&0x08, &p[bits>>4])
this.encodeBit(val&0x04, &p[bits>>3])
this.encodeBit(val&0x02, &p[bits>>2])
this.encodeBit(val&0x01, &p[bits>>1])
p = this.probs[val>>6]
}

WriteVarInt(this.bitstream, uint32(this.index))
Expand Down Expand Up @@ -309,7 +307,7 @@ func (this *FPAQDecoder) decodeBitV2(pred int) byte {
}

// Read 32 bits from bitstream
for (this.low^this.high)>>24 == 0 {
for (this.low ^ this.high) < (1 << 24) {
this.read()
}

Expand Down

0 comments on commit ef62e12

Please sign in to comment.