fse_decoder_generic.go 1.7 KB

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