2019-03-07 01:55:38 +03:00
|
|
|
package brotli
|
|
|
|
|
2019-03-09 06:45:16 +03:00
|
|
|
import "math"
|
|
|
|
|
2019-03-07 01:55:38 +03:00
|
|
|
/* The distance symbols effectively used by "Large Window Brotli" (32-bit). */
|
2019-03-16 03:24:40 +03:00
|
|
|
const numHistogramDistanceSymbols = 544
|
2019-03-07 01:55:38 +03:00
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
type histogramLiteral struct {
|
2019-03-15 22:05:31 +03:00
|
|
|
data_ [numLiteralSymbols]uint32
|
2019-03-07 01:55:38 +03:00
|
|
|
total_count_ uint
|
|
|
|
bit_cost_ float64
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramClearLiteral(self *histogramLiteral) {
|
2019-03-15 22:05:31 +03:00
|
|
|
self.data_ = [numLiteralSymbols]uint32{}
|
2019-03-07 01:55:38 +03:00
|
|
|
self.total_count_ = 0
|
2019-03-09 06:45:16 +03:00
|
|
|
self.bit_cost_ = math.MaxFloat64
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func clearHistogramsLiteral(array []histogramLiteral, length uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
var i uint
|
|
|
|
for i = 0; i < length; i++ {
|
2019-03-16 03:24:40 +03:00
|
|
|
histogramClearLiteral(&array[i:][0])
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddLiteral(self *histogramLiteral, val uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.data_[val]++
|
|
|
|
self.total_count_++
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddVectorLiteral(self *histogramLiteral, p []byte, n uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.total_count_ += n
|
|
|
|
n += 1
|
|
|
|
for {
|
|
|
|
n--
|
|
|
|
if n == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
self.data_[p[0]]++
|
|
|
|
p = p[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddHistogramLiteral(self *histogramLiteral, v *histogramLiteral) {
|
2019-03-07 01:55:38 +03:00
|
|
|
var i uint
|
|
|
|
self.total_count_ += v.total_count_
|
2019-03-15 22:05:31 +03:00
|
|
|
for i = 0; i < numLiteralSymbols; i++ {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.data_[i] += v.data_[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramDataSizeLiteral() uint {
|
2019-03-15 22:05:31 +03:00
|
|
|
return numLiteralSymbols
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
type histogramCommand struct {
|
2019-03-15 22:05:31 +03:00
|
|
|
data_ [numCommandSymbols]uint32
|
2019-03-07 01:55:38 +03:00
|
|
|
total_count_ uint
|
|
|
|
bit_cost_ float64
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramClearCommand(self *histogramCommand) {
|
2019-03-15 22:05:31 +03:00
|
|
|
self.data_ = [numCommandSymbols]uint32{}
|
2019-03-07 01:55:38 +03:00
|
|
|
self.total_count_ = 0
|
2019-03-09 06:45:16 +03:00
|
|
|
self.bit_cost_ = math.MaxFloat64
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func clearHistogramsCommand(array []histogramCommand, length uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
var i uint
|
|
|
|
for i = 0; i < length; i++ {
|
2019-03-16 03:24:40 +03:00
|
|
|
histogramClearCommand(&array[i:][0])
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddCommand(self *histogramCommand, val uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.data_[val]++
|
|
|
|
self.total_count_++
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddVectorCommand(self *histogramCommand, p []uint16, n uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.total_count_ += n
|
|
|
|
n += 1
|
|
|
|
for {
|
|
|
|
n--
|
|
|
|
if n == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
self.data_[p[0]]++
|
|
|
|
p = p[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddHistogramCommand(self *histogramCommand, v *histogramCommand) {
|
2019-03-07 01:55:38 +03:00
|
|
|
var i uint
|
|
|
|
self.total_count_ += v.total_count_
|
2019-03-15 22:05:31 +03:00
|
|
|
for i = 0; i < numCommandSymbols; i++ {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.data_[i] += v.data_[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramDataSizeCommand() uint {
|
2019-03-15 22:05:31 +03:00
|
|
|
return numCommandSymbols
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
type histogramDistance struct {
|
2019-03-15 22:05:31 +03:00
|
|
|
data_ [numDistanceSymbols]uint32
|
2019-03-07 01:55:38 +03:00
|
|
|
total_count_ uint
|
|
|
|
bit_cost_ float64
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramClearDistance(self *histogramDistance) {
|
2019-03-15 22:05:31 +03:00
|
|
|
self.data_ = [numDistanceSymbols]uint32{}
|
2019-03-07 01:55:38 +03:00
|
|
|
self.total_count_ = 0
|
2019-03-09 06:45:16 +03:00
|
|
|
self.bit_cost_ = math.MaxFloat64
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func clearHistogramsDistance(array []histogramDistance, length uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
var i uint
|
|
|
|
for i = 0; i < length; i++ {
|
2019-03-16 03:24:40 +03:00
|
|
|
histogramClearDistance(&array[i:][0])
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddDistance(self *histogramDistance, val uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.data_[val]++
|
|
|
|
self.total_count_++
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddVectorDistance(self *histogramDistance, p []uint16, n uint) {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.total_count_ += n
|
|
|
|
n += 1
|
|
|
|
for {
|
|
|
|
n--
|
|
|
|
if n == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
self.data_[p[0]]++
|
|
|
|
p = p[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramAddHistogramDistance(self *histogramDistance, v *histogramDistance) {
|
2019-03-07 01:55:38 +03:00
|
|
|
var i uint
|
|
|
|
self.total_count_ += v.total_count_
|
2019-03-15 22:05:31 +03:00
|
|
|
for i = 0; i < numDistanceSymbols; i++ {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.data_[i] += v.data_[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func histogramDataSizeDistance() uint {
|
2019-03-15 22:05:31 +03:00
|
|
|
return numDistanceSymbols
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
type blockSplitIterator struct {
|
2019-03-15 22:05:31 +03:00
|
|
|
split_ *blockSplit
|
2019-03-07 01:55:38 +03:00
|
|
|
idx_ uint
|
|
|
|
type_ uint
|
|
|
|
length_ uint
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func initBlockSplitIterator(self *blockSplitIterator, split *blockSplit) {
|
2019-03-07 01:55:38 +03:00
|
|
|
self.split_ = split
|
|
|
|
self.idx_ = 0
|
|
|
|
self.type_ = 0
|
|
|
|
if split.lengths != nil {
|
|
|
|
self.length_ = uint(split.lengths[0])
|
|
|
|
} else {
|
|
|
|
self.length_ = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
func blockSplitIteratorNext(self *blockSplitIterator) {
|
2019-03-07 01:55:38 +03:00
|
|
|
if self.length_ == 0 {
|
|
|
|
self.idx_++
|
|
|
|
self.type_ = uint(self.split_.types[self.idx_])
|
|
|
|
self.length_ = uint(self.split_.lengths[self.idx_])
|
|
|
|
}
|
|
|
|
|
|
|
|
self.length_--
|
|
|
|
}
|
|
|
|
|
2020-05-09 02:48:16 +03:00
|
|
|
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) {
|
2019-03-07 01:55:38 +03:00
|
|
|
var pos uint = start_pos
|
2019-03-16 03:24:40 +03:00
|
|
|
var literal_it blockSplitIterator
|
|
|
|
var insert_and_copy_it blockSplitIterator
|
|
|
|
var dist_it blockSplitIterator
|
2019-03-07 01:55:38 +03:00
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
initBlockSplitIterator(&literal_it, literal_split)
|
|
|
|
initBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split)
|
|
|
|
initBlockSplitIterator(&dist_it, dist_split)
|
2020-05-09 02:48:16 +03:00
|
|
|
for i := range cmds {
|
2019-03-15 22:05:31 +03:00
|
|
|
var cmd *command = &cmds[i]
|
2019-03-07 01:55:38 +03:00
|
|
|
var j uint
|
2019-03-16 03:24:40 +03:00
|
|
|
blockSplitIteratorNext(&insert_and_copy_it)
|
|
|
|
histogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_], uint(cmd.cmd_prefix_))
|
2019-03-07 01:55:38 +03:00
|
|
|
|
|
|
|
/* TODO: unwrap iterator blocks. */
|
|
|
|
for j = uint(cmd.insert_len_); j != 0; j-- {
|
|
|
|
var context uint
|
2019-03-16 03:24:40 +03:00
|
|
|
blockSplitIteratorNext(&literal_it)
|
2019-03-07 01:55:38 +03:00
|
|
|
context = literal_it.type_
|
|
|
|
if context_modes != nil {
|
2019-03-16 03:24:40 +03:00
|
|
|
var lut contextLUT = getContextLUT(context_modes[context])
|
|
|
|
context = (context << literalContextBits) + uint(getContext(prev_byte, prev_byte2, lut))
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 03:24:40 +03:00
|
|
|
histogramAddLiteral(&literal_histograms[context], uint(ringbuffer[pos&mask]))
|
2019-03-07 01:55:38 +03:00
|
|
|
prev_byte2 = prev_byte
|
|
|
|
prev_byte = ringbuffer[pos&mask]
|
|
|
|
pos++
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:05:31 +03:00
|
|
|
pos += uint(commandCopyLen(cmd))
|
|
|
|
if commandCopyLen(cmd) != 0 {
|
2019-03-07 01:55:38 +03:00
|
|
|
prev_byte2 = ringbuffer[(pos-2)&mask]
|
|
|
|
prev_byte = ringbuffer[(pos-1)&mask]
|
|
|
|
if cmd.cmd_prefix_ >= 128 {
|
|
|
|
var context uint
|
2019-03-16 03:24:40 +03:00
|
|
|
blockSplitIteratorNext(&dist_it)
|
|
|
|
context = uint(uint32(dist_it.type_<<distanceContextBits) + commandDistanceContext(cmd))
|
|
|
|
histogramAddDistance(©_dist_histograms[context], uint(cmd.dist_prefix_)&0x3FF)
|
2019-03-07 01:55:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|