fse_decoder_generic.go 1.8 KB

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