histogram.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package brotli
  2. import "math"
  3. /* The distance symbols effectively used by "Large Window Brotli" (32-bit). */
  4. const numHistogramDistanceSymbols = 544
  5. type histogramLiteral struct {
  6. data_ [numLiteralSymbols]uint32
  7. total_count_ uint
  8. bit_cost_ float64
  9. }
  10. func histogramClearLiteral(self *histogramLiteral) {
  11. self.data_ = [numLiteralSymbols]uint32{}
  12. self.total_count_ = 0
  13. self.bit_cost_ = math.MaxFloat64
  14. }
  15. func clearHistogramsLiteral(array []histogramLiteral, length uint) {
  16. var i uint
  17. for i = 0; i < length; i++ {
  18. histogramClearLiteral(&array[i:][0])
  19. }
  20. }
  21. func histogramAddLiteral(self *histogramLiteral, val uint) {
  22. self.data_[val]++
  23. self.total_count_++
  24. }
  25. func histogramAddVectorLiteral(self *histogramLiteral, p []byte, n uint) {
  26. self.total_count_ += n
  27. n += 1
  28. for {
  29. n--
  30. if n == 0 {
  31. break
  32. }
  33. self.data_[p[0]]++
  34. p = p[1:]
  35. }
  36. }
  37. func histogramAddHistogramLiteral(self *histogramLiteral, v *histogramLiteral) {
  38. var i uint
  39. self.total_count_ += v.total_count_
  40. for i = 0; i < numLiteralSymbols; i++ {
  41. self.data_[i] += v.data_[i]
  42. }
  43. }
  44. func histogramDataSizeLiteral() uint {
  45. return numLiteralSymbols
  46. }
  47. type histogramCommand struct {
  48. data_ [numCommandSymbols]uint32
  49. total_count_ uint
  50. bit_cost_ float64
  51. }
  52. func histogramClearCommand(self *histogramCommand) {
  53. self.data_ = [numCommandSymbols]uint32{}
  54. self.total_count_ = 0
  55. self.bit_cost_ = math.MaxFloat64
  56. }
  57. func clearHistogramsCommand(array []histogramCommand, length uint) {
  58. var i uint
  59. for i = 0; i < length; i++ {
  60. histogramClearCommand(&array[i:][0])
  61. }
  62. }
  63. func histogramAddCommand(self *histogramCommand, val uint) {
  64. self.data_[val]++
  65. self.total_count_++
  66. }
  67. func histogramAddVectorCommand(self *histogramCommand, p []uint16, n uint) {
  68. self.total_count_ += n
  69. n += 1
  70. for {
  71. n--
  72. if n == 0 {
  73. break
  74. }
  75. self.data_[p[0]]++
  76. p = p[1:]
  77. }
  78. }
  79. func histogramAddHistogramCommand(self *histogramCommand, v *histogramCommand) {
  80. var i uint
  81. self.total_count_ += v.total_count_
  82. for i = 0; i < numCommandSymbols; i++ {
  83. self.data_[i] += v.data_[i]
  84. }
  85. }
  86. func histogramDataSizeCommand() uint {
  87. return numCommandSymbols
  88. }
  89. type histogramDistance struct {
  90. data_ [numDistanceSymbols]uint32
  91. total_count_ uint
  92. bit_cost_ float64
  93. }
  94. func histogramClearDistance(self *histogramDistance) {
  95. self.data_ = [numDistanceSymbols]uint32{}
  96. self.total_count_ = 0
  97. self.bit_cost_ = math.MaxFloat64
  98. }
  99. func clearHistogramsDistance(array []histogramDistance, length uint) {
  100. var i uint
  101. for i = 0; i < length; i++ {
  102. histogramClearDistance(&array[i:][0])
  103. }
  104. }
  105. func histogramAddDistance(self *histogramDistance, val uint) {
  106. self.data_[val]++
  107. self.total_count_++
  108. }
  109. func histogramAddVectorDistance(self *histogramDistance, p []uint16, n uint) {
  110. self.total_count_ += n
  111. n += 1
  112. for {
  113. n--
  114. if n == 0 {
  115. break
  116. }
  117. self.data_[p[0]]++
  118. p = p[1:]
  119. }
  120. }
  121. func histogramAddHistogramDistance(self *histogramDistance, v *histogramDistance) {
  122. var i uint
  123. self.total_count_ += v.total_count_
  124. for i = 0; i < numDistanceSymbols; i++ {
  125. self.data_[i] += v.data_[i]
  126. }
  127. }
  128. func histogramDataSizeDistance() uint {
  129. return numDistanceSymbols
  130. }
  131. type blockSplitIterator struct {
  132. split_ *blockSplit
  133. idx_ uint
  134. type_ uint
  135. length_ uint
  136. }
  137. func initBlockSplitIterator(self *blockSplitIterator, split *blockSplit) {
  138. self.split_ = split
  139. self.idx_ = 0
  140. self.type_ = 0
  141. if len(split.lengths) > 0 {
  142. self.length_ = uint(split.lengths[0])
  143. } else {
  144. self.length_ = 0
  145. }
  146. }
  147. func blockSplitIteratorNext(self *blockSplitIterator) {
  148. if self.length_ == 0 {
  149. self.idx_++
  150. self.type_ = uint(self.split_.types[self.idx_])
  151. self.length_ = uint(self.split_.lengths[self.idx_])
  152. }
  153. self.length_--
  154. }
  155. func buildHistogramsWithContext(cmds []command, literal_split *blockSplit, insert_and_copy_split *blockSplit, dist_split *blockSplit, ringbuffer []byte, start_pos uint, mask uint, prev_byte byte, prev_byte2 byte, context_modes []int, literal_histograms []histogramLiteral, insert_and_copy_histograms []histogramCommand, copy_dist_histograms []histogramDistance) {
  156. var pos uint = start_pos
  157. var literal_it blockSplitIterator
  158. var insert_and_copy_it blockSplitIterator
  159. var dist_it blockSplitIterator
  160. initBlockSplitIterator(&literal_it, literal_split)
  161. initBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split)
  162. initBlockSplitIterator(&dist_it, dist_split)
  163. for i := range cmds {
  164. var cmd *command = &cmds[i]
  165. var j uint
  166. blockSplitIteratorNext(&insert_and_copy_it)
  167. histogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_], uint(cmd.cmd_prefix_))
  168. /* TODO: unwrap iterator blocks. */
  169. for j = uint(cmd.insert_len_); j != 0; j-- {
  170. var context uint
  171. blockSplitIteratorNext(&literal_it)
  172. context = literal_it.type_
  173. if context_modes != nil {
  174. var lut contextLUT = getContextLUT(context_modes[context])
  175. context = (context << literalContextBits) + uint(getContext(prev_byte, prev_byte2, lut))
  176. }
  177. histogramAddLiteral(&literal_histograms[context], uint(ringbuffer[pos&mask]))
  178. prev_byte2 = prev_byte
  179. prev_byte = ringbuffer[pos&mask]
  180. pos++
  181. }
  182. pos += uint(commandCopyLen(cmd))
  183. if commandCopyLen(cmd) != 0 {
  184. prev_byte2 = ringbuffer[(pos-2)&mask]
  185. prev_byte = ringbuffer[(pos-1)&mask]
  186. if cmd.cmd_prefix_ >= 128 {
  187. var context uint
  188. blockSplitIteratorNext(&dist_it)
  189. context = uint(uint32(dist_it.type_<<distanceContextBits) + commandDistanceContext(cmd))
  190. histogramAddDistance(&copy_dist_histograms[context], uint(cmd.dist_prefix_)&0x3FF)
  191. }
  192. }
  193. }
  194. }