seqenc.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. return
  34. }
  35. compareSwap(ll, &s.llEnc, &s.llPrev)
  36. compareSwap(ml, &s.mlEnc, &s.mlPrev)
  37. compareSwap(of, &s.ofEnc, &s.ofPrev)
  38. }
  39. func highBit(val uint32) (n uint32) {
  40. return uint32(bits.Len32(val) - 1)
  41. }
  42. var llCodeTable = [64]byte{0, 1, 2, 3, 4, 5, 6, 7,
  43. 8, 9, 10, 11, 12, 13, 14, 15,
  44. 16, 16, 17, 17, 18, 18, 19, 19,
  45. 20, 20, 20, 20, 21, 21, 21, 21,
  46. 22, 22, 22, 22, 22, 22, 22, 22,
  47. 23, 23, 23, 23, 23, 23, 23, 23,
  48. 24, 24, 24, 24, 24, 24, 24, 24,
  49. 24, 24, 24, 24, 24, 24, 24, 24}
  50. // Up to 6 bits
  51. const maxLLCode = 35
  52. // llBitsTable translates from ll code to number of bits.
  53. var llBitsTable = [maxLLCode + 1]byte{
  54. 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0,
  56. 1, 1, 1, 1, 2, 2, 3, 3,
  57. 4, 6, 7, 8, 9, 10, 11, 12,
  58. 13, 14, 15, 16}
  59. // llCode returns the code that represents the literal length requested.
  60. func llCode(litLength uint32) uint8 {
  61. const llDeltaCode = 19
  62. if litLength <= 63 {
  63. // Compiler insists on bounds check (Go 1.12)
  64. return llCodeTable[litLength&63]
  65. }
  66. return uint8(highBit(litLength)) + llDeltaCode
  67. }
  68. var mlCodeTable = [128]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  69. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  70. 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37,
  71. 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39,
  72. 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  73. 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
  74. 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42,
  75. 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42}
  76. // Up to 6 bits
  77. const maxMLCode = 52
  78. // mlBitsTable translates from ml code to number of bits.
  79. var mlBitsTable = [maxMLCode + 1]byte{
  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. 0, 0, 0, 0, 0, 0, 0, 0,
  84. 1, 1, 1, 1, 2, 2, 3, 3,
  85. 4, 4, 5, 7, 8, 9, 10, 11,
  86. 12, 13, 14, 15, 16}
  87. // note : mlBase = matchLength - MINMATCH;
  88. // because it's the format it's stored in seqStore->sequences
  89. func mlCode(mlBase uint32) uint8 {
  90. const mlDeltaCode = 36
  91. if mlBase <= 127 {
  92. // Compiler insists on bounds check (Go 1.12)
  93. return mlCodeTable[mlBase&127]
  94. }
  95. return uint8(highBit(mlBase)) + mlDeltaCode
  96. }
  97. func ofCode(offset uint32) uint8 {
  98. // A valid offset will always be > 0.
  99. return uint8(bits.Len32(offset) - 1)
  100. }