fse_decoder_generic.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. symbolNext[i] = 1
  20. } else {
  21. symbolNext[i] = uint16(v)
  22. }
  23. }
  24. }
  25. // Spread symbols
  26. {
  27. tableMask := tableSize - 1
  28. step := tableStep(tableSize)
  29. position := uint32(0)
  30. for ss, v := range s.norm[:s.symbolLen] {
  31. for i := 0; i < int(v); i++ {
  32. s.dt[position].setAddBits(uint8(ss))
  33. position = (position + step) & tableMask
  34. for position > highThreshold {
  35. // lowprob area
  36. position = (position + step) & tableMask
  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. }