github.com/bir3/gocompiler@v0.9.2202/extra/compress/zstd/fse_decoder_generic.go (about)

     1  //go:build !amd64 || appengine || !gc || noasm
     2  // +build !amd64 appengine !gc noasm
     3  
     4  package zstd
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  )
    10  
    11  // buildDtable will build the decoding table.
    12  func (s *fseDecoder) buildDtable() error {
    13  	tableSize := uint32(1 << s.actualTableLog)
    14  	highThreshold := tableSize - 1
    15  	symbolNext := s.stateTable[:256]
    16  
    17  	// Init, lay down lowprob symbols
    18  	{
    19  		for i, v := range s.norm[:s.symbolLen] {
    20  			if v == -1 {
    21  				s.dt[highThreshold].setAddBits(uint8(i))
    22  				highThreshold--
    23  				symbolNext[i] = 1
    24  			} else {
    25  				symbolNext[i] = uint16(v)
    26  			}
    27  		}
    28  	}
    29  
    30  	// Spread symbols
    31  	{
    32  		tableMask := tableSize - 1
    33  		step := tableStep(tableSize)
    34  		position := uint32(0)
    35  		for ss, v := range s.norm[:s.symbolLen] {
    36  			for i := 0; i < int(v); i++ {
    37  				s.dt[position].setAddBits(uint8(ss))
    38  				position = (position + step) & tableMask
    39  				for position > highThreshold {
    40  					// lowprob area
    41  					position = (position + step) & tableMask
    42  				}
    43  			}
    44  		}
    45  		if position != 0 {
    46  			// position must reach all cells once, otherwise normalizedCounter is incorrect
    47  			return errors.New("corrupted input (position != 0)")
    48  		}
    49  	}
    50  
    51  	// Build Decoding table
    52  	{
    53  		tableSize := uint16(1 << s.actualTableLog)
    54  		for u, v := range s.dt[:tableSize] {
    55  			symbol := v.addBits()
    56  			nextState := symbolNext[symbol]
    57  			symbolNext[symbol] = nextState + 1
    58  			nBits := s.actualTableLog - byte(highBits(uint32(nextState)))
    59  			s.dt[u&maxTableMask].setNBits(nBits)
    60  			newState := (nextState << nBits) - tableSize
    61  			if newState > tableSize {
    62  				return fmt.Errorf("newState (%d) outside table size (%d)", newState, tableSize)
    63  			}
    64  			if newState == uint16(u) && nBits == 0 {
    65  				// Seems weird that this is possible with nbits > 0.
    66  				return fmt.Errorf("newState (%d) == oldState (%d) and no bits", newState, u)
    67  			}
    68  			s.dt[u&maxTableMask].setNewState(newState)
    69  		}
    70  	}
    71  	return nil
    72  }