Debian Codegraph @

zstd/fse_decoder_generic.go

raw
1 //go:build (!amd64 && !arm64) || appengine || !gc || noasm
2
3 package zstd
4
5 import (
6 "errors"
7 "fmt"
8 )
9
10 // buildDtable will build the decoding table.
11 func (s *fseDecoder) buildDtable() error {
12 tableSize := uint32(1 << s.actualTableLog)
13 highThreshold := tableSize - 1
14 symbolNext := s.stateTable[:256]
15
16 // Init, lay down lowprob symbols
17 {
18 for i, v := range s.norm[:s.symbolLen] {
19 if v == -1 {
20 s.dt[highThreshold].setAddBits(uint8(i))
21 highThreshold--
22 v = 1
23 }
24 symbolNext[i] = uint16(v)
25 }
26 }
27
28 // Spread symbols
29 {
30 tableMask := tableSize - 1
31 step := tableStep(tableSize)
32 position := uint32(0)
33 for ss, v := range s.norm[:s.symbolLen] {
34 for i := 0; i < int(v); i++ {
35 s.dt[position].setAddBits(uint8(ss))
36 for {
37 // lowprob area
38 position = (position + step) & tableMask
39 if position <= highThreshold {
40 break
41 }
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 }