bitwriter.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2018 Klaus Post. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Based on work Copyright (c) 2013, Yann Collet, released under BSD License.
  5. package zstd
  6. import "fmt"
  7. // bitWriter will write bits.
  8. // First bit will be LSB of the first byte of output.
  9. type bitWriter struct {
  10. bitContainer uint64
  11. nBits uint8
  12. out []byte
  13. }
  14. // bitMask16 is bitmasks. Has extra to avoid bounds check.
  15. var bitMask16 = [32]uint16{
  16. 0, 1, 3, 7, 0xF, 0x1F,
  17. 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
  18. 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0xFFFF,
  19. 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
  20. 0xFFFF, 0xFFFF} /* up to 16 bits */
  21. var bitMask32 = [32]uint32{
  22. 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF,
  23. 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,
  24. 0x1ffff, 0x3ffff, 0x7FFFF, 0xfFFFF, 0x1fFFFF, 0x3fFFFF, 0x7fFFFF, 0xffFFFF,
  25. 0x1ffFFFF, 0x3ffFFFF, 0x7ffFFFF, 0xfffFFFF, 0x1fffFFFF, 0x3fffFFFF, 0x7fffFFFF,
  26. } // up to 32 bits
  27. // addBits16NC will add up to 16 bits.
  28. // It will not check if there is space for them,
  29. // so the caller must ensure that it has flushed recently.
  30. func (b *bitWriter) addBits16NC(value uint16, bits uint8) {
  31. b.bitContainer |= uint64(value&bitMask16[bits&31]) << (b.nBits & 63)
  32. b.nBits += bits
  33. }
  34. // addBits32NC will add up to 31 bits.
  35. // It will not check if there is space for them,
  36. // so the caller must ensure that it has flushed recently.
  37. func (b *bitWriter) addBits32NC(value uint32, bits uint8) {
  38. b.bitContainer |= uint64(value&bitMask32[bits&31]) << (b.nBits & 63)
  39. b.nBits += bits
  40. }
  41. // addBits64NC will add up to 64 bits.
  42. // There must be space for 32 bits.
  43. func (b *bitWriter) addBits64NC(value uint64, bits uint8) {
  44. if bits <= 31 {
  45. b.addBits32Clean(uint32(value), bits)
  46. return
  47. }
  48. b.addBits32Clean(uint32(value), 32)
  49. b.flush32()
  50. b.addBits32Clean(uint32(value>>32), bits-32)
  51. }
  52. // addBits32Clean will add up to 32 bits.
  53. // It will not check if there is space for them.
  54. // The input must not contain more bits than specified.
  55. func (b *bitWriter) addBits32Clean(value uint32, bits uint8) {
  56. b.bitContainer |= uint64(value) << (b.nBits & 63)
  57. b.nBits += bits
  58. }
  59. // addBits16Clean will add up to 16 bits. value may not contain more set bits than indicated.
  60. // It will not check if there is space for them, so the caller must ensure that it has flushed recently.
  61. func (b *bitWriter) addBits16Clean(value uint16, bits uint8) {
  62. b.bitContainer |= uint64(value) << (b.nBits & 63)
  63. b.nBits += bits
  64. }
  65. // flush will flush all pending full bytes.
  66. // There will be at least 56 bits available for writing when this has been called.
  67. // Using flush32 is faster, but leaves less space for writing.
  68. func (b *bitWriter) flush() {
  69. v := b.nBits >> 3
  70. switch v {
  71. case 0:
  72. case 1:
  73. b.out = append(b.out,
  74. byte(b.bitContainer),
  75. )
  76. case 2:
  77. b.out = append(b.out,
  78. byte(b.bitContainer),
  79. byte(b.bitContainer>>8),
  80. )
  81. case 3:
  82. b.out = append(b.out,
  83. byte(b.bitContainer),
  84. byte(b.bitContainer>>8),
  85. byte(b.bitContainer>>16),
  86. )
  87. case 4:
  88. b.out = append(b.out,
  89. byte(b.bitContainer),
  90. byte(b.bitContainer>>8),
  91. byte(b.bitContainer>>16),
  92. byte(b.bitContainer>>24),
  93. )
  94. case 5:
  95. b.out = append(b.out,
  96. byte(b.bitContainer),
  97. byte(b.bitContainer>>8),
  98. byte(b.bitContainer>>16),
  99. byte(b.bitContainer>>24),
  100. byte(b.bitContainer>>32),
  101. )
  102. case 6:
  103. b.out = append(b.out,
  104. byte(b.bitContainer),
  105. byte(b.bitContainer>>8),
  106. byte(b.bitContainer>>16),
  107. byte(b.bitContainer>>24),
  108. byte(b.bitContainer>>32),
  109. byte(b.bitContainer>>40),
  110. )
  111. case 7:
  112. b.out = append(b.out,
  113. byte(b.bitContainer),
  114. byte(b.bitContainer>>8),
  115. byte(b.bitContainer>>16),
  116. byte(b.bitContainer>>24),
  117. byte(b.bitContainer>>32),
  118. byte(b.bitContainer>>40),
  119. byte(b.bitContainer>>48),
  120. )
  121. case 8:
  122. b.out = append(b.out,
  123. byte(b.bitContainer),
  124. byte(b.bitContainer>>8),
  125. byte(b.bitContainer>>16),
  126. byte(b.bitContainer>>24),
  127. byte(b.bitContainer>>32),
  128. byte(b.bitContainer>>40),
  129. byte(b.bitContainer>>48),
  130. byte(b.bitContainer>>56),
  131. )
  132. default:
  133. panic(fmt.Errorf("bits (%d) > 64", b.nBits))
  134. }
  135. b.bitContainer >>= v << 3
  136. b.nBits &= 7
  137. }
  138. // flush32 will flush out, so there are at least 32 bits available for writing.
  139. func (b *bitWriter) flush32() {
  140. if b.nBits < 32 {
  141. return
  142. }
  143. b.out = append(b.out,
  144. byte(b.bitContainer),
  145. byte(b.bitContainer>>8),
  146. byte(b.bitContainer>>16),
  147. byte(b.bitContainer>>24))
  148. b.nBits -= 32
  149. b.bitContainer >>= 32
  150. }
  151. // flushAlign will flush remaining full bytes and align to next byte boundary.
  152. func (b *bitWriter) flushAlign() {
  153. nbBytes := (b.nBits + 7) >> 3
  154. for i := uint8(0); i < nbBytes; i++ {
  155. b.out = append(b.out, byte(b.bitContainer>>(i*8)))
  156. }
  157. b.nBits = 0
  158. b.bitContainer = 0
  159. }
  160. // close will write the alignment bit and write the final byte(s)
  161. // to the output.
  162. func (b *bitWriter) close() error {
  163. // End mark
  164. b.addBits16Clean(1, 1)
  165. // flush until next byte.
  166. b.flushAlign()
  167. return nil
  168. }
  169. // reset and continue writing by appending to out.
  170. func (b *bitWriter) reset(out []byte) {
  171. b.bitContainer = 0
  172. b.nBits = 0
  173. b.out = out
  174. }