seqenc.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2019+ Klaus Post. All rights reserved.
  2. // License information can be found in the LICENSE file.
  3. // Based on work by Yann Collet, released under BSD License.
  4. package zstd
  5. import "math/bits"
  6. type seqCoders struct {
  7. llEnc, ofEnc, mlEnc *fseEncoder
  8. llPrev, ofPrev, mlPrev *fseEncoder
  9. }
  10. // swap coders with another (block).
  11. func (s *seqCoders) swap(other *seqCoders) {
  12. *s, *other = *other, *s
  13. }
  14. // setPrev will update the previous encoders to the actually used ones
  15. // and make sure a fresh one is in the main slot.
  16. func (s *seqCoders) setPrev(ll, ml, of *fseEncoder) {
  17. compareSwap := func(used *fseEncoder, current, prev **fseEncoder) {
  18. // We used the new one, more current to history and reuse the previous history
  19. if *current == used {
  20. *prev, *current = *current, *prev
  21. c := *current
  22. p := *prev
  23. c.reUsed = false
  24. p.reUsed = true
  25. return
  26. }
  27. if used == *prev {
  28. return
  29. }
  30. // Ensure we cannot reuse by accident
  31. prevEnc := *prev
  32. prevEnc.symbolLen = 0
  33. }
  34. compareSwap(ll, &s.llEnc, &s.llPrev)
  35. compareSwap(ml, &s.mlEnc, &s.mlPrev)
  36. compareSwap(of, &s.ofEnc, &s.ofPrev)
  37. }
  38. func highBit(val uint32) (n uint32) {
  39. return uint32(bits.Len32(val) - 1)
  40. }
  41. var llCodeTable = [64]byte{0, 1, 2, 3, 4, 5, 6, 7,
  42. 8, 9, 10, 11, 12, 13, 14, 15,
  43. 16, 16, 17, 17, 18, 18, 19, 19,
  44. 20, 20, 20, 20, 21, 21, 21, 21,
  45. 22, 22, 22, 22, 22, 22, 22, 22,
  46. 23, 23, 23, 23, 23, 23, 23, 23,
  47. 24, 24, 24, 24, 24, 24, 24, 24,
  48. 24, 24, 24, 24, 24, 24, 24, 24}
  49. // Up to 6 bits
  50. const maxLLCode = 35
  51. // llBitsTable translates from ll code to number of bits.
  52. var llBitsTable = [maxLLCode + 1]byte{
  53. 0, 0, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0,
  55. 1, 1, 1, 1, 2, 2, 3, 3,
  56. 4, 6, 7, 8, 9, 10, 11, 12,
  57. 13, 14, 15, 16}
  58. // llCode returns the code that represents the literal length requested.
  59. func llCode(litLength uint32) uint8 {
  60. const llDeltaCode = 19
  61. if litLength <= 63 {
  62. // Compiler insists on bounds check (Go 1.12)
  63. return llCodeTable[litLength&63]
  64. }
  65. return uint8(highBit(litLength)) + llDeltaCode
  66. }
  67. var mlCodeTable = [128]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  68. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  69. 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
  70. 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
  71. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  72. 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
  73. 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
  74. 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42}
  75. // Up to 6 bits
  76. const maxMLCode = 52
  77. // mlBitsTable translates from ml code to number of bits.
  78. var mlBitsTable = [maxMLCode + 1]byte{
  79. 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0, 0,
  83. 1, 1, 1, 1, 2, 2, 3, 3,
  84. 4, 4, 5, 7, 8, 9, 10, 11,
  85. 12, 13, 14, 15, 16}
  86. // note : mlBase = matchLength - MINMATCH;
  87. // because it's the format it's stored in seqStore->sequences
  88. func mlCode(mlBase uint32) uint8 {
  89. const mlDeltaCode = 36
  90. if mlBase <= 127 {
  91. // Compiler insists on bounds check (Go 1.12)
  92. return mlCodeTable[mlBase&127]
  93. }
  94. return uint8(highBit(mlBase)) + mlDeltaCode
  95. }
  96. func ofCode(offset uint32) uint8 {
  97. // A valid offset will always be > 0.
  98. return uint8(bits.Len32(offset) - 1)
  99. }