diff --git a/backward_references.go b/backward_references.go index f027189..784e6f9 100644 --- a/backward_references.go +++ b/backward_references.go @@ -39,12 +39,12 @@ func computeDistanceCode(distance uint, max_distance uint, dist_cache []int) uin } } - return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1 + return distance + numDistanceShortCodes - 1 } -func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) { +func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) - var orig_commands []Command = commands + var orig_commands []command = commands var insert_length uint = *last_insert_len var pos_end uint = position + num_bytes var store_end uint @@ -121,7 +121,7 @@ func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, hasher.PrepareDistanceCache(dist_cache) } - InitCommand(&commands[0], ¶ms.dist, insert_length, sr.len, sr.len_code_delta, distance_code) + initCommand(&commands[0], ¶ms.dist, insert_length, sr.len, sr.len_code_delta, distance_code) commands = commands[1:] } diff --git a/backward_references_hq.go b/backward_references_hq.go index c83c503..9833fce 100644 --- a/backward_references_hq.go +++ b/backward_references_hq.go @@ -60,7 +60,7 @@ func zopfliNodeCopyDistance(self *zopfliNode) uint32 { func zopfliNodeDistanceCode(self *zopfliNode) uint32 { var short_code uint32 = self.dcode_insert_length >> 27 if short_code == 0 { - return zopfliNodeCopyDistance(self) + BROTLI_NUM_DISTANCE_SHORT_CODES - 1 + return zopfliNodeCopyDistance(self) + numDistanceShortCodes - 1 } else { return short_code - 1 } @@ -72,7 +72,7 @@ func zopfliNodeCommandLength(self *zopfliNode) uint32 { /* Histogram based cost model for zopflification. */ type zopfliCostModel struct { - cost_cmd_ [BROTLI_NUM_COMMAND_SYMBOLS]float32 + cost_cmd_ [numCommandSymbols]float32 cost_dist_ []float32 distance_histogram_size uint32 literal_costs_ []float32 @@ -134,24 +134,24 @@ func setCost(histogram []uint32, histogram_size uint, literal_histogram bool, co } } -func zopfliCostModelSetFromCommands(self *zopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint, commands []Command, num_commands uint, last_insert_len uint) { - var histogram_literal [BROTLI_NUM_LITERAL_SYMBOLS]uint32 - var histogram_cmd [BROTLI_NUM_COMMAND_SYMBOLS]uint32 +func zopfliCostModelSetFromCommands(self *zopfliCostModel, position uint, ringbuffer []byte, ringbuffer_mask uint, commands []command, num_commands uint, last_insert_len uint) { + var histogram_literal [numLiteralSymbols]uint32 + var histogram_cmd [numCommandSymbols]uint32 var histogram_dist [maxEffectiveDistanceAlphabetSize]uint32 - var cost_literal [BROTLI_NUM_LITERAL_SYMBOLS]float32 + var cost_literal [numLiteralSymbols]float32 var pos uint = position - last_insert_len var min_cost_cmd float32 = kInfinity var i uint var cost_cmd []float32 = self.cost_cmd_[:] var literal_costs []float32 - histogram_literal = [BROTLI_NUM_LITERAL_SYMBOLS]uint32{} - histogram_cmd = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{} + histogram_literal = [numLiteralSymbols]uint32{} + histogram_cmd = [numCommandSymbols]uint32{} histogram_dist = [maxEffectiveDistanceAlphabetSize]uint32{} for i = 0; i < num_commands; i++ { var inslength uint = uint(commands[i].insert_len_) - var copylength uint = uint(CommandCopyLen(&commands[i])) + var copylength uint = uint(commandCopyLen(&commands[i])) var distcode uint = uint(commands[i].dist_prefix_) & 0x3FF var cmdcode uint = uint(commands[i].cmd_prefix_) var j uint @@ -168,11 +168,11 @@ func zopfliCostModelSetFromCommands(self *zopfliCostModel, position uint, ringbu pos += inslength + copylength } - setCost(histogram_literal[:], BROTLI_NUM_LITERAL_SYMBOLS, true, cost_literal[:]) - setCost(histogram_cmd[:], BROTLI_NUM_COMMAND_SYMBOLS, false, cost_cmd) + setCost(histogram_literal[:], numLiteralSymbols, true, cost_literal[:]) + setCost(histogram_cmd[:], numCommandSymbols, false, cost_cmd) setCost(histogram_dist[:], uint(self.distance_histogram_size), false, self.cost_dist_) - for i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; i++ { + for i = 0; i < numCommandSymbols; i++ { min_cost_cmd = brotli_min_float(min_cost_cmd, cost_cmd[i]) } @@ -205,7 +205,7 @@ func zopfliCostModelSetFromLiteralCosts(self *zopfliCostModel, position uint, ri literal_carry -= literal_costs[i+1] - literal_costs[i] } - for i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; i++ { + for i = 0; i < numCommandSymbols; i++ { cost_cmd[i] = float32(FastLog2(uint(11 + uint32(i)))) } @@ -406,14 +406,14 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, for k = 0; k < max_iters && k < startPosQueueSize(queue); k++ { var posdata *posData = startPosQueueAt(queue, k) var start uint = posdata.pos - var inscode uint16 = GetInsertLengthCode(pos - start) + var inscode uint16 = getInsertLengthCode(pos - start) var start_costdiff float32 = posdata.costdiff - var base_cost float32 = start_costdiff + float32(GetInsertExtra(inscode)) + zopfliCostModelGetLiteralCosts(model, 0, pos) + var base_cost float32 = start_costdiff + float32(getInsertExtra(inscode)) + zopfliCostModelGetLiteralCosts(model, 0, pos) var best_len uint = min_len - 1 var j uint = 0 /* Look for last distance matches using the distance cache from this starting position. */ - for ; j < BROTLI_NUM_DISTANCE_SHORT_CODES && best_len < max_len; j++ { + for ; j < numDistanceShortCodes && best_len < max_len; j++ { var idx uint = uint(kDistanceCacheIndex[j]) var backward uint = uint(posdata.distance_cache[idx] + kDistanceCacheOffset[j]) var prev_ix uint = cur_ix - backward @@ -447,15 +447,15 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, var dist_cost float32 = base_cost + zopfliCostModelGetDistanceCost(model, j) var l uint for l = best_len + 1; l <= len; l++ { - var copycode uint16 = GetCopyLengthCode(l) - var cmdcode uint16 = CombineLengthCodes(inscode, copycode, j == 0) + var copycode uint16 = getCopyLengthCode(l) + var cmdcode uint16 = combineLengthCodes(inscode, copycode, j == 0) var tmp float32 if cmdcode < 128 { tmp = base_cost } else { tmp = dist_cost } - var cost float32 = tmp + float32(GetCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode) + var cost float32 = tmp + float32(getCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode) if cost < nodes[pos+l].u.cost { updateZopfliNode(nodes, pos, start, l, l, backward, j+1, cost) result = brotli_max_size_t(result, l) @@ -479,7 +479,7 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, var match BackwardMatch = matches[j] var dist uint = uint(match.distance) var is_dictionary_match bool = (dist > max_distance+gap) - var dist_code uint = dist + BROTLI_NUM_DISTANCE_SHORT_CODES - 1 + var dist_code uint = dist + numDistanceShortCodes - 1 var dist_symbol uint16 var distextra uint32 var distnumextra uint32 @@ -508,9 +508,9 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, } else { len_code = len } - var copycode uint16 = GetCopyLengthCode(len_code) - var cmdcode uint16 = CombineLengthCodes(inscode, copycode, false) - var cost float32 = dist_cost + float32(GetCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode) + var copycode uint16 = getCopyLengthCode(len_code) + var cmdcode uint16 = combineLengthCodes(inscode, copycode, false) + var cost float32 = dist_cost + float32(getCopyExtra(copycode)) + zopfliCostModelGetCommandCost(model, cmdcode) if cost < nodes[pos+len].u.cost { updateZopfliNode(nodes, pos, start, uint(len), len_code, dist, 0, cost) result = brotli_max_size_t(result, uint(len)) @@ -541,7 +541,7 @@ func computeShortestPathFromNodes(num_bytes uint, nodes []zopfliNode) uint { } /* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */ -func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, dist_cache []int, last_insert_len *uint, params *BrotliEncoderParams, commands []Command, num_literals *uint) { +func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, dist_cache []int, last_insert_len *uint, params *BrotliEncoderParams, commands []command, num_literals *uint) { var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) var pos uint = 0 var offset uint32 = nodes[0].u.next @@ -563,7 +563,7 @@ func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, var max_distance uint = brotli_min_size_t(block_start+pos, max_backward_limit) var is_dictionary bool = (distance > max_distance+gap) var dist_code uint = uint(zopfliNodeDistanceCode(next)) - InitCommand(&commands[i], ¶ms.dist, insert_length, copy_length, int(len_code)-int(copy_length), dist_code) + initCommand(&commands[i], ¶ms.dist, insert_length, copy_length, int(len_code)-int(copy_length), dist_code) if !is_dictionary && dist_code > 0 { dist_cache[3] = dist_cache[2] @@ -676,7 +676,7 @@ func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, return computeShortestPathFromNodes(num_bytes, nodes) } -func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher *H10, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) { +func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher *H10, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { var nodes []zopfliNode nodes = make([]zopfliNode, (num_bytes + 1)) initZopfliNodes(nodes, num_bytes+1) @@ -685,7 +685,7 @@ func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer [] nodes = nil } -func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []Command, num_commands *uint, num_literals *uint) { +func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) var num_matches []uint32 = make([]uint32, num_bytes) var matches_size uint = 4 * num_bytes diff --git a/bit_cost.go b/bit_cost.go index 6088c00..0a82585 100644 --- a/bit_cost.go +++ b/bit_cost.go @@ -111,7 +111,7 @@ func populationCostLiteral(histogram *HistogramLiteral) float64 { } { var max_depth uint = 1 - var depth_histo = [BROTLI_CODE_LENGTH_CODES]uint32{0} + var depth_histo = [codeLengthCodes]uint32{0} /* In this loop we compute the entropy of the histogram and simultaneously build a simplified histogram of the code length codes where we use the zero repeat code 17, but we don't use the non-zero repeat code 16. */ @@ -159,7 +159,7 @@ func populationCostLiteral(histogram *HistogramLiteral) float64 { } else { reps -= 2 for reps > 0 { - depth_histo[BROTLI_REPEAT_ZERO_CODE_LENGTH]++ + depth_histo[repeatZeroCodeLength]++ /* Add the 3 extra bits for the 17 code length code. */ bits += 3 @@ -174,7 +174,7 @@ func populationCostLiteral(histogram *HistogramLiteral) float64 { bits += float64(18 + 2*max_depth) /* Add the entropy of the code length code histogram. */ - bits += bitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES) + bits += bitsEntropy(depth_histo[:], codeLengthCodes) } return bits @@ -242,7 +242,7 @@ func populationCostCommand(histogram *HistogramCommand) float64 { } { var max_depth uint = 1 - var depth_histo = [BROTLI_CODE_LENGTH_CODES]uint32{0} + var depth_histo = [codeLengthCodes]uint32{0} /* In this loop we compute the entropy of the histogram and simultaneously build a simplified histogram of the code length codes where we use the zero repeat code 17, but we don't use the non-zero repeat code 16. */ @@ -290,7 +290,7 @@ func populationCostCommand(histogram *HistogramCommand) float64 { } else { reps -= 2 for reps > 0 { - depth_histo[BROTLI_REPEAT_ZERO_CODE_LENGTH]++ + depth_histo[repeatZeroCodeLength]++ /* Add the 3 extra bits for the 17 code length code. */ bits += 3 @@ -305,7 +305,7 @@ func populationCostCommand(histogram *HistogramCommand) float64 { bits += float64(18 + 2*max_depth) /* Add the entropy of the code length code histogram. */ - bits += bitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES) + bits += bitsEntropy(depth_histo[:], codeLengthCodes) } return bits @@ -373,7 +373,7 @@ func populationCostDistance(histogram *HistogramDistance) float64 { } { var max_depth uint = 1 - var depth_histo = [BROTLI_CODE_LENGTH_CODES]uint32{0} + var depth_histo = [codeLengthCodes]uint32{0} /* In this loop we compute the entropy of the histogram and simultaneously build a simplified histogram of the code length codes where we use the zero repeat code 17, but we don't use the non-zero repeat code 16. */ @@ -421,7 +421,7 @@ func populationCostDistance(histogram *HistogramDistance) float64 { } else { reps -= 2 for reps > 0 { - depth_histo[BROTLI_REPEAT_ZERO_CODE_LENGTH]++ + depth_histo[repeatZeroCodeLength]++ /* Add the 3 extra bits for the 17 code length code. */ bits += 3 @@ -436,7 +436,7 @@ func populationCostDistance(histogram *HistogramDistance) float64 { bits += float64(18 + 2*max_depth) /* Add the entropy of the code length code histogram. */ - bits += bitsEntropy(depth_histo[:], BROTLI_CODE_LENGTH_CODES) + bits += bitsEntropy(depth_histo[:], codeLengthCodes) } return bits diff --git a/bit_reader.go b/bit_reader.go index d7d25c3..58e783a 100644 --- a/bit_reader.go +++ b/bit_reader.go @@ -221,7 +221,7 @@ func safeReadBits(br *bitReader, n_bits uint32, val *uint32) bool { /* Advances the bit reader position to the next byte boundary and verifies that any skipped bits are set to zero. */ -func jumpToByteBoundary(br *bitReader) bool { +func bitReaderJumpToByteBoundary(br *bitReader) bool { var pad_bits_count uint32 = getAvailableBits(br) & 0x7 var pad_bits uint32 = 0 if pad_bits_count != 0 { diff --git a/block_splitter.go b/block_splitter.go index 3426291..8fb306c 100644 --- a/block_splitter.go +++ b/block_splitter.go @@ -21,7 +21,7 @@ package brotli */ /* Block split point selection utilities. */ -type BlockSplit struct { +type blockSplit struct { num_types uint num_blocks uint types []byte @@ -56,7 +56,7 @@ var kIterMulForRefining uint = 2 var kMinItersForRefining uint = 100 -func CountLiterals(cmds []Command, num_commands uint) uint { +func countLiterals(cmds []command, num_commands uint) uint { var total_length uint = 0 /* Count how many we have. */ @@ -68,7 +68,7 @@ func CountLiterals(cmds []Command, num_commands uint) uint { return total_length } -func CopyLiteralsToByteArray(cmds []Command, num_commands uint, data []byte, offset uint, mask uint, literals []byte) { +func copyLiteralsToByteArray(cmds []command, num_commands uint, data []byte, offset uint, mask uint, literals []byte) { var pos uint = 0 var from_pos uint = offset & mask var i uint @@ -87,18 +87,18 @@ func CopyLiteralsToByteArray(cmds []Command, num_commands uint, data []byte, off pos += insert_len } - from_pos = uint((uint32(from_pos+insert_len) + CommandCopyLen(&cmds[i])) & uint32(mask)) + from_pos = uint((uint32(from_pos+insert_len) + commandCopyLen(&cmds[i])) & uint32(mask)) } } -func MyRand(seed *uint32) uint32 { +func myRand(seed *uint32) uint32 { /* Initial seed should be 7. In this case, loop length is (1 << 29). */ *seed *= 16807 return *seed } -func BitCost(count uint) float64 { +func bitCost(count uint) float64 { if count == 0 { return -2.0 } else { @@ -106,11 +106,11 @@ func BitCost(count uint) float64 { } } -const HISTOGRAMS_PER_BATCH = 64 +const histogramsPerBatch = 64 -const CLUSTERS_PER_BATCH = 16 +const clustersPerBatch = 16 -func BrotliInitBlockSplit(self *BlockSplit) { +func initBlockSplit(self *blockSplit) { self.num_types = 0 self.num_blocks = 0 self.types = nil @@ -119,22 +119,22 @@ func BrotliInitBlockSplit(self *BlockSplit) { self.lengths_alloc_size = 0 } -func BrotliDestroyBlockSplit(self *BlockSplit) { +func destroyBlockSplit(self *blockSplit) { self.types = nil self.lengths = nil } -func BrotliSplitBlock(cmds []Command, num_commands uint, data []byte, pos uint, mask uint, params *BrotliEncoderParams, literal_split *BlockSplit, insert_and_copy_split *BlockSplit, dist_split *BlockSplit) { +func splitBlock(cmds []command, num_commands uint, data []byte, pos uint, mask uint, params *BrotliEncoderParams, literal_split *blockSplit, insert_and_copy_split *blockSplit, dist_split *blockSplit) { { - var literals_count uint = CountLiterals(cmds, num_commands) + var literals_count uint = countLiterals(cmds, num_commands) var literals []byte = make([]byte, literals_count) /* Create a continuous array of literals. */ - CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals) + copyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals) /* Create the block split on the array of literals. Literal histograms have alphabet size 256. */ - SplitByteVectorLiteral(literals, literals_count, kSymbolsPerLiteralHistogram, kMaxLiteralHistograms, kLiteralStrideLength, kLiteralBlockSwitchCost, params, literal_split) + splitByteVectorLiteral(literals, literals_count, kSymbolsPerLiteralHistogram, kMaxLiteralHistograms, kLiteralStrideLength, kLiteralBlockSwitchCost, params, literal_split) literals = nil } @@ -148,7 +148,7 @@ func BrotliSplitBlock(cmds []Command, num_commands uint, data []byte, pos uint, } /* Create the block split on the array of command prefixes. */ - SplitByteVectorCommand(insert_and_copy_codes, num_commands, kSymbolsPerCommandHistogram, kMaxCommandHistograms, kCommandStrideLength, kCommandBlockSwitchCost, params, insert_and_copy_split) + splitByteVectorCommand(insert_and_copy_codes, num_commands, kSymbolsPerCommandHistogram, kMaxCommandHistograms, kCommandStrideLength, kCommandBlockSwitchCost, params, insert_and_copy_split) /* TODO: reuse for distances? */ @@ -161,15 +161,15 @@ func BrotliSplitBlock(cmds []Command, num_commands uint, data []byte, pos uint, var i uint for i = 0; i < num_commands; i++ { - var cmd *Command = &cmds[i] - if CommandCopyLen(cmd) != 0 && cmd.cmd_prefix_ >= 128 { + var cmd *command = &cmds[i] + if commandCopyLen(cmd) != 0 && cmd.cmd_prefix_ >= 128 { distance_prefixes[j] = cmd.dist_prefix_ & 0x3FF j++ } } /* Create the block split on the array of distance prefixes. */ - SplitByteVectorDistance(distance_prefixes, j, kSymbolsPerDistanceHistogram, kMaxCommandHistograms, kCommandStrideLength, kDistanceBlockSwitchCost, params, dist_split) + splitByteVectorDistance(distance_prefixes, j, kSymbolsPerDistanceHistogram, kMaxCommandHistograms, kCommandStrideLength, kDistanceBlockSwitchCost, params, dist_split) distance_prefixes = nil } diff --git a/block_splitter_command.go b/block_splitter_command.go index d63299e..6aa6844 100644 --- a/block_splitter_command.go +++ b/block_splitter_command.go @@ -6,7 +6,7 @@ package brotli Distributed under MIT license. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -func InitialEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramCommand) { +func initialEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramCommand) { var seed uint32 = 7 var block_length uint = length / num_histograms var i uint @@ -14,7 +14,7 @@ func InitialEntropyCodesCommand(data []uint16, length uint, stride uint, num_his for i = 0; i < num_histograms; i++ { var pos uint = length * i / num_histograms if i != 0 { - pos += uint(MyRand(&seed) % uint32(block_length)) + pos += uint(myRand(&seed) % uint32(block_length)) } if pos+stride >= length { @@ -25,18 +25,18 @@ func InitialEntropyCodesCommand(data []uint16, length uint, stride uint, num_his } } -func RandomSampleCommand(seed *uint32, data []uint16, length uint, stride uint, sample *HistogramCommand) { +func randomSampleCommand(seed *uint32, data []uint16, length uint, stride uint, sample *HistogramCommand) { var pos uint = 0 if stride >= length { stride = length } else { - pos = uint(MyRand(seed) % uint32(length-stride+1)) + pos = uint(myRand(seed) % uint32(length-stride+1)) } HistogramAddVectorCommand(sample, data[pos:], stride) } -func RefineEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramCommand) { +func refineEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramCommand) { var iters uint = kIterMulForRefining*length/stride + kMinItersForRefining var seed uint32 = 7 var iter uint @@ -44,7 +44,7 @@ func RefineEntropyCodesCommand(data []uint16, length uint, stride uint, num_hist for iter = 0; iter < iters; iter++ { var sample HistogramCommand HistogramClearCommand(&sample) - RandomSampleCommand(&seed, data, length, stride, &sample) + randomSampleCommand(&seed, data, length, stride, &sample) HistogramAddHistogramCommand(&histograms[iter%num_histograms], &sample) } } @@ -52,7 +52,7 @@ func RefineEntropyCodesCommand(data []uint16, length uint, stride uint, num_hist /* Assigns a block id from the range [0, num_histograms) to each data element in data[0..length) and fills in block_id[0..length) with the assigned values. Returns the number of blocks, i.e. one plus the number of block switches. */ -func FindBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramCommand, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { +func findBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramCommand, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { var data_size uint = HistogramDataSizeCommand() var bitmaplen uint = (num_histograms + 7) >> 3 var num_blocks uint = 1 @@ -77,7 +77,7 @@ func FindBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, for i = data_size; i != 0; { i-- for j = 0; j < num_histograms; j++ { - insert_cost[i*num_histograms+j] = insert_cost[j] - BitCost(uint(histograms[j].data_[i])) + insert_cost[i*num_histograms+j] = insert_cost[j] - bitCost(uint(histograms[j].data_[i])) } } @@ -150,18 +150,18 @@ func FindBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, return num_blocks } -var RemapBlockIdsCommand_kInvalidId uint16 = 256 +var remapBlockIdsCommand_kInvalidId uint16 = 256 -func RemapBlockIdsCommand(block_ids []byte, length uint, new_id []uint16, num_histograms uint) uint { +func remapBlockIdsCommand(block_ids []byte, length uint, new_id []uint16, num_histograms uint) uint { var next_id uint16 = 0 var i uint for i = 0; i < num_histograms; i++ { - new_id[i] = RemapBlockIdsCommand_kInvalidId + new_id[i] = remapBlockIdsCommand_kInvalidId } for i = 0; i < length; i++ { assert(uint(block_ids[i]) < num_histograms) - if new_id[block_ids[i]] == RemapBlockIdsCommand_kInvalidId { + if new_id[block_ids[i]] == remapBlockIdsCommand_kInvalidId { new_id[block_ids[i]] = next_id next_id++ } @@ -176,7 +176,7 @@ func RemapBlockIdsCommand(block_ids []byte, length uint, new_id []uint16, num_hi return uint(next_id) } -func BuildBlockHistogramsCommand(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []HistogramCommand) { +func buildBlockHistogramsCommand(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []HistogramCommand) { var i uint ClearHistogramsCommand(histograms, num_histograms) for i = 0; i < length; i++ { @@ -184,12 +184,12 @@ func BuildBlockHistogramsCommand(data []uint16, length uint, block_ids []byte, n } } -var ClusterBlocksCommand_kInvalidIndex uint32 = BROTLI_UINT32_MAX +var clusterBlocksCommand_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids []byte, split *BlockSplit) { +func clusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids []byte, split *blockSplit) { var histogram_symbols []uint32 = make([]uint32, num_blocks) var block_lengths []uint32 = make([]uint32, num_blocks) - var expected_num_clusters uint = CLUSTERS_PER_BATCH * (num_blocks + HISTOGRAMS_PER_BATCH - 1) / HISTOGRAMS_PER_BATCH + var expected_num_clusters uint = clustersPerBatch * (num_blocks + histogramsPerBatch - 1) / histogramsPerBatch var all_histograms_size uint = 0 var all_histograms_capacity uint = expected_num_clusters var all_histograms []HistogramCommand = make([]HistogramCommand, all_histograms_capacity) @@ -197,19 +197,19 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids var cluster_size_capacity uint = expected_num_clusters var cluster_size []uint32 = make([]uint32, cluster_size_capacity) var num_clusters uint = 0 - var histograms []HistogramCommand = make([]HistogramCommand, brotli_min_size_t(num_blocks, HISTOGRAMS_PER_BATCH)) - var max_num_pairs uint = HISTOGRAMS_PER_BATCH * HISTOGRAMS_PER_BATCH / 2 + var histograms []HistogramCommand = make([]HistogramCommand, brotli_min_size_t(num_blocks, histogramsPerBatch)) + var max_num_pairs uint = histogramsPerBatch * histogramsPerBatch / 2 var pairs_capacity uint = max_num_pairs + 1 - var pairs []HistogramPair = make([]HistogramPair, pairs_capacity) + var pairs []histogramPair = make([]histogramPair, pairs_capacity) var pos uint = 0 var clusters []uint32 var num_final_clusters uint var new_index []uint32 var i uint - var sizes = [HISTOGRAMS_PER_BATCH]uint32{0} - var new_clusters = [HISTOGRAMS_PER_BATCH]uint32{0} - var symbols = [HISTOGRAMS_PER_BATCH]uint32{0} - var remap = [HISTOGRAMS_PER_BATCH]uint32{0} + var sizes = [histogramsPerBatch]uint32{0} + var new_clusters = [histogramsPerBatch]uint32{0} + var symbols = [histogramsPerBatch]uint32{0} + var remap = [histogramsPerBatch]uint32{0} for i := 0; i < int(num_blocks); i++ { block_lengths[i] = 0 @@ -227,8 +227,8 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids assert(block_idx == num_blocks) } - for i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCH { - var num_to_combine uint = brotli_min_size_t(num_blocks-i, HISTOGRAMS_PER_BATCH) + for i = 0; i < num_blocks; i += histogramsPerBatch { + var num_to_combine uint = brotli_min_size_t(num_blocks-i, histogramsPerBatch) var num_new_clusters uint var j uint for j = 0; j < num_to_combine; j++ { @@ -245,7 +245,7 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids sizes[j] = 1 } - num_new_clusters = BrotliHistogramCombineCommand(histograms, sizes[:], symbols[:], new_clusters[:], []HistogramPair(pairs), num_to_combine, num_to_combine, HISTOGRAMS_PER_BATCH, max_num_pairs) + num_new_clusters = histogramCombineCommand(histograms, sizes[:], symbols[:], new_clusters[:], []histogramPair(pairs), num_to_combine, num_to_combine, histogramsPerBatch, max_num_pairs) if all_histograms_capacity < (all_histograms_size + num_new_clusters) { var _new_size uint if all_histograms_capacity == 0 { @@ -289,7 +289,7 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids max_num_pairs = brotli_min_size_t(64*num_clusters, (num_clusters/2)*num_clusters) if pairs_capacity < max_num_pairs+1 { pairs = nil - pairs = make([]HistogramPair, (max_num_pairs + 1)) + pairs = make([]histogramPair, (max_num_pairs + 1)) } clusters = make([]uint32, num_clusters) @@ -297,13 +297,13 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids clusters[i] = uint32(i) } - num_final_clusters = BrotliHistogramCombineCommand(all_histograms, cluster_size, histogram_symbols, clusters, pairs, num_clusters, num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES, max_num_pairs) + num_final_clusters = histogramCombineCommand(all_histograms, cluster_size, histogram_symbols, clusters, pairs, num_clusters, num_blocks, maxNumberOfBlockTypes, max_num_pairs) pairs = nil cluster_size = nil new_index = make([]uint32, num_clusters) for i = 0; i < num_clusters; i++ { - new_index[i] = ClusterBlocksCommand_kInvalidIndex + new_index[i] = clusterBlocksCommand_kInvalidIndex } pos = 0 { @@ -324,9 +324,9 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids } else { best_out = histogram_symbols[i-1] } - best_bits = BrotliHistogramBitCostDistanceCommand(&histo, &all_histograms[best_out]) + best_bits = histogramBitCostDistanceCommand(&histo, &all_histograms[best_out]) for j = 0; j < num_final_clusters; j++ { - var cur_bits float64 = BrotliHistogramBitCostDistanceCommand(&histo, &all_histograms[clusters[j]]) + var cur_bits float64 = histogramBitCostDistanceCommand(&histo, &all_histograms[clusters[j]]) if cur_bits < best_bits { best_bits = cur_bits best_out = clusters[j] @@ -334,7 +334,7 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids } histogram_symbols[i] = best_out - if new_index[best_out] == ClusterBlocksCommand_kInvalidIndex { + if new_index[best_out] == clusterBlocksCommand_kInvalidIndex { new_index[best_out] = next_index next_index++ } @@ -370,7 +370,7 @@ func ClusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids histogram_symbols = nil } -func SplitByteVectorCommand(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *BlockSplit) { +func splitByteVectorCommand(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *blockSplit) { var data_size uint = HistogramDataSizeCommand() var num_histograms uint = length/literals_per_histogram + 1 var histograms []HistogramCommand @@ -394,9 +394,9 @@ func SplitByteVectorCommand(data []uint16, length uint, literals_per_histogram u histograms = make([]HistogramCommand, num_histograms) /* Find good entropy codes. */ - InitialEntropyCodesCommand(data, length, sampling_stride_length, num_histograms, histograms) + initialEntropyCodesCommand(data, length, sampling_stride_length, num_histograms, histograms) - RefineEntropyCodesCommand(data, length, sampling_stride_length, num_histograms, histograms) + refineEntropyCodesCommand(data, length, sampling_stride_length, num_histograms, histograms) { var block_ids []byte = make([]byte, length) var num_blocks uint = 0 @@ -415,9 +415,9 @@ func SplitByteVectorCommand(data []uint16, length uint, literals_per_histogram u var i uint for i = 0; i < iters; i++ { - num_blocks = FindBlocksCommand(data, length, block_switch_cost, num_histograms, histograms, insert_cost, cost, switch_signal, block_ids) - num_histograms = RemapBlockIdsCommand(block_ids, length, new_id, num_histograms) - BuildBlockHistogramsCommand(data, length, block_ids, num_histograms, histograms) + num_blocks = findBlocksCommand(data, length, block_switch_cost, num_histograms, histograms, insert_cost, cost, switch_signal, block_ids) + num_histograms = remapBlockIdsCommand(block_ids, length, new_id, num_histograms) + buildBlockHistogramsCommand(data, length, block_ids, num_histograms, histograms) } insert_cost = nil @@ -425,7 +425,7 @@ func SplitByteVectorCommand(data []uint16, length uint, literals_per_histogram u switch_signal = nil new_id = nil histograms = nil - ClusterBlocksCommand(data, length, num_blocks, block_ids, split) + clusterBlocksCommand(data, length, num_blocks, block_ids, split) block_ids = nil } } diff --git a/block_splitter_distance.go b/block_splitter_distance.go index 32e3942..dbcd27c 100644 --- a/block_splitter_distance.go +++ b/block_splitter_distance.go @@ -6,7 +6,7 @@ package brotli Distributed under MIT license. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -func InitialEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramDistance) { +func initialEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramDistance) { var seed uint32 = 7 var block_length uint = length / num_histograms var i uint @@ -14,7 +14,7 @@ func InitialEntropyCodesDistance(data []uint16, length uint, stride uint, num_hi for i = 0; i < num_histograms; i++ { var pos uint = length * i / num_histograms if i != 0 { - pos += uint(MyRand(&seed) % uint32(block_length)) + pos += uint(myRand(&seed) % uint32(block_length)) } if pos+stride >= length { @@ -25,18 +25,18 @@ func InitialEntropyCodesDistance(data []uint16, length uint, stride uint, num_hi } } -func RandomSampleDistance(seed *uint32, data []uint16, length uint, stride uint, sample *HistogramDistance) { +func randomSampleDistance(seed *uint32, data []uint16, length uint, stride uint, sample *HistogramDistance) { var pos uint = 0 if stride >= length { stride = length } else { - pos = uint(MyRand(seed) % uint32(length-stride+1)) + pos = uint(myRand(seed) % uint32(length-stride+1)) } HistogramAddVectorDistance(sample, data[pos:], stride) } -func RefineEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramDistance) { +func refineEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramDistance) { var iters uint = kIterMulForRefining*length/stride + kMinItersForRefining var seed uint32 = 7 var iter uint @@ -44,7 +44,7 @@ func RefineEntropyCodesDistance(data []uint16, length uint, stride uint, num_his for iter = 0; iter < iters; iter++ { var sample HistogramDistance HistogramClearDistance(&sample) - RandomSampleDistance(&seed, data, length, stride, &sample) + randomSampleDistance(&seed, data, length, stride, &sample) HistogramAddHistogramDistance(&histograms[iter%num_histograms], &sample) } } @@ -52,7 +52,7 @@ func RefineEntropyCodesDistance(data []uint16, length uint, stride uint, num_his /* Assigns a block id from the range [0, num_histograms) to each data element in data[0..length) and fills in block_id[0..length) with the assigned values. Returns the number of blocks, i.e. one plus the number of block switches. */ -func FindBlocksDistance(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramDistance, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { +func findBlocksDistance(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramDistance, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { var data_size uint = HistogramDataSizeDistance() var bitmaplen uint = (num_histograms + 7) >> 3 var num_blocks uint = 1 @@ -77,7 +77,7 @@ func FindBlocksDistance(data []uint16, length uint, block_switch_bitcost float64 for i = data_size; i != 0; { i-- for j = 0; j < num_histograms; j++ { - insert_cost[i*num_histograms+j] = insert_cost[j] - BitCost(uint(histograms[j].data_[i])) + insert_cost[i*num_histograms+j] = insert_cost[j] - bitCost(uint(histograms[j].data_[i])) } } @@ -150,18 +150,18 @@ func FindBlocksDistance(data []uint16, length uint, block_switch_bitcost float64 return num_blocks } -var RemapBlockIdsDistance_kInvalidId uint16 = 256 +var remapBlockIdsDistance_kInvalidId uint16 = 256 -func RemapBlockIdsDistance(block_ids []byte, length uint, new_id []uint16, num_histograms uint) uint { +func remapBlockIdsDistance(block_ids []byte, length uint, new_id []uint16, num_histograms uint) uint { var next_id uint16 = 0 var i uint for i = 0; i < num_histograms; i++ { - new_id[i] = RemapBlockIdsDistance_kInvalidId + new_id[i] = remapBlockIdsDistance_kInvalidId } for i = 0; i < length; i++ { assert(uint(block_ids[i]) < num_histograms) - if new_id[block_ids[i]] == RemapBlockIdsDistance_kInvalidId { + if new_id[block_ids[i]] == remapBlockIdsDistance_kInvalidId { new_id[block_ids[i]] = next_id next_id++ } @@ -176,7 +176,7 @@ func RemapBlockIdsDistance(block_ids []byte, length uint, new_id []uint16, num_h return uint(next_id) } -func BuildBlockHistogramsDistance(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []HistogramDistance) { +func buildBlockHistogramsDistance(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []HistogramDistance) { var i uint ClearHistogramsDistance(histograms, num_histograms) for i = 0; i < length; i++ { @@ -184,12 +184,12 @@ func BuildBlockHistogramsDistance(data []uint16, length uint, block_ids []byte, } } -var ClusterBlocksDistance_kInvalidIndex uint32 = BROTLI_UINT32_MAX +var clusterBlocksDistance_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_ids []byte, split *BlockSplit) { +func clusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_ids []byte, split *blockSplit) { var histogram_symbols []uint32 = make([]uint32, num_blocks) var block_lengths []uint32 = make([]uint32, num_blocks) - var expected_num_clusters uint = CLUSTERS_PER_BATCH * (num_blocks + HISTOGRAMS_PER_BATCH - 1) / HISTOGRAMS_PER_BATCH + var expected_num_clusters uint = clustersPerBatch * (num_blocks + histogramsPerBatch - 1) / histogramsPerBatch var all_histograms_size uint = 0 var all_histograms_capacity uint = expected_num_clusters var all_histograms []HistogramDistance = make([]HistogramDistance, all_histograms_capacity) @@ -197,19 +197,19 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id var cluster_size_capacity uint = expected_num_clusters var cluster_size []uint32 = make([]uint32, cluster_size_capacity) var num_clusters uint = 0 - var histograms []HistogramDistance = make([]HistogramDistance, brotli_min_size_t(num_blocks, HISTOGRAMS_PER_BATCH)) - var max_num_pairs uint = HISTOGRAMS_PER_BATCH * HISTOGRAMS_PER_BATCH / 2 + var histograms []HistogramDistance = make([]HistogramDistance, brotli_min_size_t(num_blocks, histogramsPerBatch)) + var max_num_pairs uint = histogramsPerBatch * histogramsPerBatch / 2 var pairs_capacity uint = max_num_pairs + 1 - var pairs []HistogramPair = make([]HistogramPair, pairs_capacity) + var pairs []histogramPair = make([]histogramPair, pairs_capacity) var pos uint = 0 var clusters []uint32 var num_final_clusters uint var new_index []uint32 var i uint - var sizes = [HISTOGRAMS_PER_BATCH]uint32{0} - var new_clusters = [HISTOGRAMS_PER_BATCH]uint32{0} - var symbols = [HISTOGRAMS_PER_BATCH]uint32{0} - var remap = [HISTOGRAMS_PER_BATCH]uint32{0} + var sizes = [histogramsPerBatch]uint32{0} + var new_clusters = [histogramsPerBatch]uint32{0} + var symbols = [histogramsPerBatch]uint32{0} + var remap = [histogramsPerBatch]uint32{0} for i := 0; i < int(num_blocks); i++ { block_lengths[i] = 0 @@ -227,8 +227,8 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id assert(block_idx == num_blocks) } - for i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCH { - var num_to_combine uint = brotli_min_size_t(num_blocks-i, HISTOGRAMS_PER_BATCH) + for i = 0; i < num_blocks; i += histogramsPerBatch { + var num_to_combine uint = brotli_min_size_t(num_blocks-i, histogramsPerBatch) var num_new_clusters uint var j uint for j = 0; j < num_to_combine; j++ { @@ -245,7 +245,7 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id sizes[j] = 1 } - num_new_clusters = BrotliHistogramCombineDistance(histograms, sizes[:], symbols[:], new_clusters[:], []HistogramPair(pairs), num_to_combine, num_to_combine, HISTOGRAMS_PER_BATCH, max_num_pairs) + num_new_clusters = histogramCombineDistance(histograms, sizes[:], symbols[:], new_clusters[:], []histogramPair(pairs), num_to_combine, num_to_combine, histogramsPerBatch, max_num_pairs) if all_histograms_capacity < (all_histograms_size + num_new_clusters) { var _new_size uint if all_histograms_capacity == 0 { @@ -289,7 +289,7 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id max_num_pairs = brotli_min_size_t(64*num_clusters, (num_clusters/2)*num_clusters) if pairs_capacity < max_num_pairs+1 { pairs = nil - pairs = make([]HistogramPair, (max_num_pairs + 1)) + pairs = make([]histogramPair, (max_num_pairs + 1)) } clusters = make([]uint32, num_clusters) @@ -297,13 +297,13 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id clusters[i] = uint32(i) } - num_final_clusters = BrotliHistogramCombineDistance(all_histograms, cluster_size, histogram_symbols, clusters, pairs, num_clusters, num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES, max_num_pairs) + num_final_clusters = histogramCombineDistance(all_histograms, cluster_size, histogram_symbols, clusters, pairs, num_clusters, num_blocks, maxNumberOfBlockTypes, max_num_pairs) pairs = nil cluster_size = nil new_index = make([]uint32, num_clusters) for i = 0; i < num_clusters; i++ { - new_index[i] = ClusterBlocksDistance_kInvalidIndex + new_index[i] = clusterBlocksDistance_kInvalidIndex } pos = 0 { @@ -324,9 +324,9 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id } else { best_out = histogram_symbols[i-1] } - best_bits = BrotliHistogramBitCostDistanceDistance(&histo, &all_histograms[best_out]) + best_bits = histogramBitCostDistanceDistance(&histo, &all_histograms[best_out]) for j = 0; j < num_final_clusters; j++ { - var cur_bits float64 = BrotliHistogramBitCostDistanceDistance(&histo, &all_histograms[clusters[j]]) + var cur_bits float64 = histogramBitCostDistanceDistance(&histo, &all_histograms[clusters[j]]) if cur_bits < best_bits { best_bits = cur_bits best_out = clusters[j] @@ -334,7 +334,7 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id } histogram_symbols[i] = best_out - if new_index[best_out] == ClusterBlocksDistance_kInvalidIndex { + if new_index[best_out] == clusterBlocksDistance_kInvalidIndex { new_index[best_out] = next_index next_index++ } @@ -370,7 +370,7 @@ func ClusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id histogram_symbols = nil } -func SplitByteVectorDistance(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *BlockSplit) { +func splitByteVectorDistance(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *blockSplit) { var data_size uint = HistogramDataSizeDistance() var num_histograms uint = length/literals_per_histogram + 1 var histograms []HistogramDistance @@ -394,9 +394,9 @@ func SplitByteVectorDistance(data []uint16, length uint, literals_per_histogram histograms = make([]HistogramDistance, num_histograms) /* Find good entropy codes. */ - InitialEntropyCodesDistance(data, length, sampling_stride_length, num_histograms, histograms) + initialEntropyCodesDistance(data, length, sampling_stride_length, num_histograms, histograms) - RefineEntropyCodesDistance(data, length, sampling_stride_length, num_histograms, histograms) + refineEntropyCodesDistance(data, length, sampling_stride_length, num_histograms, histograms) { var block_ids []byte = make([]byte, length) var num_blocks uint = 0 @@ -415,9 +415,9 @@ func SplitByteVectorDistance(data []uint16, length uint, literals_per_histogram var i uint for i = 0; i < iters; i++ { - num_blocks = FindBlocksDistance(data, length, block_switch_cost, num_histograms, histograms, insert_cost, cost, switch_signal, block_ids) - num_histograms = RemapBlockIdsDistance(block_ids, length, new_id, num_histograms) - BuildBlockHistogramsDistance(data, length, block_ids, num_histograms, histograms) + num_blocks = findBlocksDistance(data, length, block_switch_cost, num_histograms, histograms, insert_cost, cost, switch_signal, block_ids) + num_histograms = remapBlockIdsDistance(block_ids, length, new_id, num_histograms) + buildBlockHistogramsDistance(data, length, block_ids, num_histograms, histograms) } insert_cost = nil @@ -425,7 +425,7 @@ func SplitByteVectorDistance(data []uint16, length uint, literals_per_histogram switch_signal = nil new_id = nil histograms = nil - ClusterBlocksDistance(data, length, num_blocks, block_ids, split) + clusterBlocksDistance(data, length, num_blocks, block_ids, split) block_ids = nil } } diff --git a/block_splitter_literal.go b/block_splitter_literal.go index 9a7d62e..bda9ad6 100644 --- a/block_splitter_literal.go +++ b/block_splitter_literal.go @@ -6,7 +6,7 @@ package brotli Distributed under MIT license. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -func InitialEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []HistogramLiteral) { +func initialEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []HistogramLiteral) { var seed uint32 = 7 var block_length uint = length / num_histograms var i uint @@ -14,7 +14,7 @@ func InitialEntropyCodesLiteral(data []byte, length uint, stride uint, num_histo for i = 0; i < num_histograms; i++ { var pos uint = length * i / num_histograms if i != 0 { - pos += uint(MyRand(&seed) % uint32(block_length)) + pos += uint(myRand(&seed) % uint32(block_length)) } if pos+stride >= length { @@ -25,18 +25,18 @@ func InitialEntropyCodesLiteral(data []byte, length uint, stride uint, num_histo } } -func RandomSampleLiteral(seed *uint32, data []byte, length uint, stride uint, sample *HistogramLiteral) { +func randomSampleLiteral(seed *uint32, data []byte, length uint, stride uint, sample *HistogramLiteral) { var pos uint = 0 if stride >= length { stride = length } else { - pos = uint(MyRand(seed) % uint32(length-stride+1)) + pos = uint(myRand(seed) % uint32(length-stride+1)) } HistogramAddVectorLiteral(sample, data[pos:], stride) } -func RefineEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []HistogramLiteral) { +func refineEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []HistogramLiteral) { var iters uint = kIterMulForRefining*length/stride + kMinItersForRefining var seed uint32 = 7 var iter uint @@ -44,7 +44,7 @@ func RefineEntropyCodesLiteral(data []byte, length uint, stride uint, num_histog for iter = 0; iter < iters; iter++ { var sample HistogramLiteral HistogramClearLiteral(&sample) - RandomSampleLiteral(&seed, data, length, stride, &sample) + randomSampleLiteral(&seed, data, length, stride, &sample) HistogramAddHistogramLiteral(&histograms[iter%num_histograms], &sample) } } @@ -52,7 +52,7 @@ func RefineEntropyCodesLiteral(data []byte, length uint, stride uint, num_histog /* Assigns a block id from the range [0, num_histograms) to each data element in data[0..length) and fills in block_id[0..length) with the assigned values. Returns the number of blocks, i.e. one plus the number of block switches. */ -func FindBlocksLiteral(data []byte, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramLiteral, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { +func findBlocksLiteral(data []byte, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramLiteral, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { var data_size uint = HistogramDataSizeLiteral() var bitmaplen uint = (num_histograms + 7) >> 3 var num_blocks uint = 1 @@ -77,7 +77,7 @@ func FindBlocksLiteral(data []byte, length uint, block_switch_bitcost float64, n for i = data_size; i != 0; { i-- for j = 0; j < num_histograms; j++ { - insert_cost[i*num_histograms+j] = insert_cost[j] - BitCost(uint(histograms[j].data_[i])) + insert_cost[i*num_histograms+j] = insert_cost[j] - bitCost(uint(histograms[j].data_[i])) } } @@ -150,18 +150,18 @@ func FindBlocksLiteral(data []byte, length uint, block_switch_bitcost float64, n return num_blocks } -var RemapBlockIdsLiteral_kInvalidId uint16 = 256 +var remapBlockIdsLiteral_kInvalidId uint16 = 256 -func RemapBlockIdsLiteral(block_ids []byte, length uint, new_id []uint16, num_histograms uint) uint { +func remapBlockIdsLiteral(block_ids []byte, length uint, new_id []uint16, num_histograms uint) uint { var next_id uint16 = 0 var i uint for i = 0; i < num_histograms; i++ { - new_id[i] = RemapBlockIdsLiteral_kInvalidId + new_id[i] = remapBlockIdsLiteral_kInvalidId } for i = 0; i < length; i++ { assert(uint(block_ids[i]) < num_histograms) - if new_id[block_ids[i]] == RemapBlockIdsLiteral_kInvalidId { + if new_id[block_ids[i]] == remapBlockIdsLiteral_kInvalidId { new_id[block_ids[i]] = next_id next_id++ } @@ -176,7 +176,7 @@ func RemapBlockIdsLiteral(block_ids []byte, length uint, new_id []uint16, num_hi return uint(next_id) } -func BuildBlockHistogramsLiteral(data []byte, length uint, block_ids []byte, num_histograms uint, histograms []HistogramLiteral) { +func buildBlockHistogramsLiteral(data []byte, length uint, block_ids []byte, num_histograms uint, histograms []HistogramLiteral) { var i uint ClearHistogramsLiteral(histograms, num_histograms) for i = 0; i < length; i++ { @@ -184,12 +184,12 @@ func BuildBlockHistogramsLiteral(data []byte, length uint, block_ids []byte, num } } -var ClusterBlocksLiteral_kInvalidIndex uint32 = BROTLI_UINT32_MAX +var clusterBlocksLiteral_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids []byte, split *BlockSplit) { +func clusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids []byte, split *blockSplit) { var histogram_symbols []uint32 = make([]uint32, num_blocks) var block_lengths []uint32 = make([]uint32, num_blocks) - var expected_num_clusters uint = CLUSTERS_PER_BATCH * (num_blocks + HISTOGRAMS_PER_BATCH - 1) / HISTOGRAMS_PER_BATCH + var expected_num_clusters uint = clustersPerBatch * (num_blocks + histogramsPerBatch - 1) / histogramsPerBatch var all_histograms_size uint = 0 var all_histograms_capacity uint = expected_num_clusters var all_histograms []HistogramLiteral = make([]HistogramLiteral, all_histograms_capacity) @@ -197,19 +197,19 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ var cluster_size_capacity uint = expected_num_clusters var cluster_size []uint32 = make([]uint32, cluster_size_capacity) var num_clusters uint = 0 - var histograms []HistogramLiteral = make([]HistogramLiteral, brotli_min_size_t(num_blocks, HISTOGRAMS_PER_BATCH)) - var max_num_pairs uint = HISTOGRAMS_PER_BATCH * HISTOGRAMS_PER_BATCH / 2 + var histograms []HistogramLiteral = make([]HistogramLiteral, brotli_min_size_t(num_blocks, histogramsPerBatch)) + var max_num_pairs uint = histogramsPerBatch * histogramsPerBatch / 2 var pairs_capacity uint = max_num_pairs + 1 - var pairs []HistogramPair = make([]HistogramPair, pairs_capacity) + var pairs []histogramPair = make([]histogramPair, pairs_capacity) var pos uint = 0 var clusters []uint32 var num_final_clusters uint var new_index []uint32 var i uint - var sizes = [HISTOGRAMS_PER_BATCH]uint32{0} - var new_clusters = [HISTOGRAMS_PER_BATCH]uint32{0} - var symbols = [HISTOGRAMS_PER_BATCH]uint32{0} - var remap = [HISTOGRAMS_PER_BATCH]uint32{0} + var sizes = [histogramsPerBatch]uint32{0} + var new_clusters = [histogramsPerBatch]uint32{0} + var symbols = [histogramsPerBatch]uint32{0} + var remap = [histogramsPerBatch]uint32{0} for i := 0; i < int(num_blocks); i++ { block_lengths[i] = 0 @@ -227,8 +227,8 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ assert(block_idx == num_blocks) } - for i = 0; i < num_blocks; i += HISTOGRAMS_PER_BATCH { - var num_to_combine uint = brotli_min_size_t(num_blocks-i, HISTOGRAMS_PER_BATCH) + for i = 0; i < num_blocks; i += histogramsPerBatch { + var num_to_combine uint = brotli_min_size_t(num_blocks-i, histogramsPerBatch) var num_new_clusters uint var j uint for j = 0; j < num_to_combine; j++ { @@ -245,7 +245,7 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ sizes[j] = 1 } - num_new_clusters = BrotliHistogramCombineLiteral(histograms, sizes[:], symbols[:], new_clusters[:], []HistogramPair(pairs), num_to_combine, num_to_combine, HISTOGRAMS_PER_BATCH, max_num_pairs) + num_new_clusters = histogramCombineLiteral(histograms, sizes[:], symbols[:], new_clusters[:], []histogramPair(pairs), num_to_combine, num_to_combine, histogramsPerBatch, max_num_pairs) if all_histograms_capacity < (all_histograms_size + num_new_clusters) { var _new_size uint if all_histograms_capacity == 0 { @@ -289,7 +289,7 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ max_num_pairs = brotli_min_size_t(64*num_clusters, (num_clusters/2)*num_clusters) if pairs_capacity < max_num_pairs+1 { pairs = nil - pairs = make([]HistogramPair, (max_num_pairs + 1)) + pairs = make([]histogramPair, (max_num_pairs + 1)) } clusters = make([]uint32, num_clusters) @@ -297,13 +297,13 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ clusters[i] = uint32(i) } - num_final_clusters = BrotliHistogramCombineLiteral(all_histograms, cluster_size, histogram_symbols, clusters, pairs, num_clusters, num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES, max_num_pairs) + num_final_clusters = histogramCombineLiteral(all_histograms, cluster_size, histogram_symbols, clusters, pairs, num_clusters, num_blocks, maxNumberOfBlockTypes, max_num_pairs) pairs = nil cluster_size = nil new_index = make([]uint32, num_clusters) for i = 0; i < num_clusters; i++ { - new_index[i] = ClusterBlocksLiteral_kInvalidIndex + new_index[i] = clusterBlocksLiteral_kInvalidIndex } pos = 0 { @@ -324,9 +324,9 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ } else { best_out = histogram_symbols[i-1] } - best_bits = BrotliHistogramBitCostDistanceLiteral(&histo, &all_histograms[best_out]) + best_bits = histogramBitCostDistanceLiteral(&histo, &all_histograms[best_out]) for j = 0; j < num_final_clusters; j++ { - var cur_bits float64 = BrotliHistogramBitCostDistanceLiteral(&histo, &all_histograms[clusters[j]]) + var cur_bits float64 = histogramBitCostDistanceLiteral(&histo, &all_histograms[clusters[j]]) if cur_bits < best_bits { best_bits = cur_bits best_out = clusters[j] @@ -334,7 +334,7 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ } histogram_symbols[i] = best_out - if new_index[best_out] == ClusterBlocksLiteral_kInvalidIndex { + if new_index[best_out] == clusterBlocksLiteral_kInvalidIndex { new_index[best_out] = next_index next_index++ } @@ -370,7 +370,7 @@ func ClusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ histogram_symbols = nil } -func SplitByteVectorLiteral(data []byte, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *BlockSplit) { +func splitByteVectorLiteral(data []byte, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *blockSplit) { var data_size uint = HistogramDataSizeLiteral() var num_histograms uint = length/literals_per_histogram + 1 var histograms []HistogramLiteral @@ -394,9 +394,9 @@ func SplitByteVectorLiteral(data []byte, length uint, literals_per_histogram uin histograms = make([]HistogramLiteral, num_histograms) /* Find good entropy codes. */ - InitialEntropyCodesLiteral(data, length, sampling_stride_length, num_histograms, histograms) + initialEntropyCodesLiteral(data, length, sampling_stride_length, num_histograms, histograms) - RefineEntropyCodesLiteral(data, length, sampling_stride_length, num_histograms, histograms) + refineEntropyCodesLiteral(data, length, sampling_stride_length, num_histograms, histograms) { var block_ids []byte = make([]byte, length) var num_blocks uint = 0 @@ -415,9 +415,9 @@ func SplitByteVectorLiteral(data []byte, length uint, literals_per_histogram uin var i uint for i = 0; i < iters; i++ { - num_blocks = FindBlocksLiteral(data, length, block_switch_cost, num_histograms, histograms, insert_cost, cost, switch_signal, block_ids) - num_histograms = RemapBlockIdsLiteral(block_ids, length, new_id, num_histograms) - BuildBlockHistogramsLiteral(data, length, block_ids, num_histograms, histograms) + num_blocks = findBlocksLiteral(data, length, block_switch_cost, num_histograms, histograms, insert_cost, cost, switch_signal, block_ids) + num_histograms = remapBlockIdsLiteral(block_ids, length, new_id, num_histograms) + buildBlockHistogramsLiteral(data, length, block_ids, num_histograms, histograms) } insert_cost = nil @@ -425,7 +425,7 @@ func SplitByteVectorLiteral(data []byte, length uint, literals_per_histogram uin switch_signal = nil new_id = nil histograms = nil - ClusterBlocksLiteral(data, length, num_blocks, block_ids, split) + clusterBlocksLiteral(data, length, num_blocks, block_ids, split) block_ids = nil } } diff --git a/brotli_bit_stream.go b/brotli_bit_stream.go index f6a29c2..d4f1f6b 100644 --- a/brotli_bit_stream.go +++ b/brotli_bit_stream.go @@ -1,50 +1,48 @@ package brotli -const MAX_HUFFMAN_TREE_SIZE = (2*BROTLI_NUM_COMMAND_SYMBOLS + 1) +const maxHuffmanTreeSize = (2*numCommandSymbols + 1) /* The maximum size of Huffman dictionary for distances assuming that NPOSTFIX = 0 and NDIRECT = 0. */ -const MAX_SIMPLE_DISTANCE_ALPHABET_SIZE = 140 - -/* MAX_SIMPLE_DISTANCE_ALPHABET_SIZE == 140 */ +const maxSimpleDistanceAlphabetSize = 140 /* Represents the range of values belonging to a prefix code: [offset, offset + 2^nbits) */ -type PrefixCodeRange struct { +type prefixCodeRange struct { offset uint32 nbits uint32 } -var kBlockLengthPrefixCode = [BROTLI_NUM_BLOCK_LEN_SYMBOLS]PrefixCodeRange{ - PrefixCodeRange{1, 2}, - PrefixCodeRange{5, 2}, - PrefixCodeRange{9, 2}, - PrefixCodeRange{13, 2}, - PrefixCodeRange{17, 3}, - PrefixCodeRange{25, 3}, - PrefixCodeRange{33, 3}, - PrefixCodeRange{41, 3}, - PrefixCodeRange{49, 4}, - PrefixCodeRange{65, 4}, - PrefixCodeRange{81, 4}, - PrefixCodeRange{97, 4}, - PrefixCodeRange{113, 5}, - PrefixCodeRange{145, 5}, - PrefixCodeRange{177, 5}, - PrefixCodeRange{209, 5}, - PrefixCodeRange{241, 6}, - PrefixCodeRange{305, 6}, - PrefixCodeRange{369, 7}, - PrefixCodeRange{497, 8}, - PrefixCodeRange{753, 9}, - PrefixCodeRange{1265, 10}, - PrefixCodeRange{2289, 11}, - PrefixCodeRange{4337, 12}, - PrefixCodeRange{8433, 13}, - PrefixCodeRange{16625, 24}, +var kBlockLengthPrefixCode = [numBlockLenSymbols]prefixCodeRange{ + prefixCodeRange{1, 2}, + prefixCodeRange{5, 2}, + prefixCodeRange{9, 2}, + prefixCodeRange{13, 2}, + prefixCodeRange{17, 3}, + prefixCodeRange{25, 3}, + prefixCodeRange{33, 3}, + prefixCodeRange{41, 3}, + prefixCodeRange{49, 4}, + prefixCodeRange{65, 4}, + prefixCodeRange{81, 4}, + prefixCodeRange{97, 4}, + prefixCodeRange{113, 5}, + prefixCodeRange{145, 5}, + prefixCodeRange{177, 5}, + prefixCodeRange{209, 5}, + prefixCodeRange{241, 6}, + prefixCodeRange{305, 6}, + prefixCodeRange{369, 7}, + prefixCodeRange{497, 8}, + prefixCodeRange{753, 9}, + prefixCodeRange{1265, 10}, + prefixCodeRange{2289, 11}, + prefixCodeRange{4337, 12}, + prefixCodeRange{8433, 13}, + prefixCodeRange{16625, 24}, } -func BlockLengthPrefixCode(len uint32) uint32 { +func blockLengthPrefixCode(len uint32) uint32 { var code uint32 if len >= 177 { if len >= 753 { @@ -57,29 +55,29 @@ func BlockLengthPrefixCode(len uint32) uint32 { } else { code = 0 } - for code < (BROTLI_NUM_BLOCK_LEN_SYMBOLS-1) && len >= kBlockLengthPrefixCode[code+1].offset { + for code < (numBlockLenSymbols-1) && len >= kBlockLengthPrefixCode[code+1].offset { code++ } return code } -func GetBlockLengthPrefixCode(len uint32, code *uint, n_extra *uint32, extra *uint32) { - *code = uint(BlockLengthPrefixCode(uint32(len))) +func getBlockLengthPrefixCode(len uint32, code *uint, n_extra *uint32, extra *uint32) { + *code = uint(blockLengthPrefixCode(uint32(len))) *n_extra = kBlockLengthPrefixCode[*code].nbits *extra = len - kBlockLengthPrefixCode[*code].offset } -type BlockTypeCodeCalculator struct { +type blockTypeCodeCalculator struct { last_type uint second_last_type uint } -func InitBlockTypeCodeCalculator(self *BlockTypeCodeCalculator) { +func initBlockTypeCodeCalculator(self *blockTypeCodeCalculator) { self.last_type = 1 self.second_last_type = 0 } -func NextBlockTypeCode(calculator *BlockTypeCodeCalculator, type_ byte) uint { +func nextBlockTypeCode(calculator *blockTypeCodeCalculator, type_ byte) uint { var type_code uint if uint(type_) == calculator.last_type+1 { type_code = 1 @@ -96,7 +94,7 @@ func NextBlockTypeCode(calculator *BlockTypeCodeCalculator, type_ byte) uint { /* |nibblesbits| represents the 2 bits to encode MNIBBLES (0-3) REQUIRES: length > 0 REQUIRES: length <= (1 << 24) */ -func BrotliEncodeMlen(length uint, bits *uint64, numbits *uint, nibblesbits *uint64) { +func encodeMlen(length uint, bits *uint64, numbits *uint, nibblesbits *uint64) { var lg uint if length == 1 { lg = 1 @@ -118,29 +116,29 @@ func BrotliEncodeMlen(length uint, bits *uint64, numbits *uint, nibblesbits *uin *bits = uint64(length) - 1 } -func StoreCommandExtra(cmd *Command, storage_ix *uint, storage []byte) { - var copylen_code uint32 = CommandCopyLenCode(cmd) - var inscode uint16 = GetInsertLengthCode(uint(cmd.insert_len_)) - var copycode uint16 = GetCopyLengthCode(uint(copylen_code)) - var insnumextra uint32 = GetInsertExtra(inscode) - var insextraval uint64 = uint64(cmd.insert_len_) - uint64(GetInsertBase(inscode)) - var copyextraval uint64 = uint64(copylen_code) - uint64(GetCopyBase(copycode)) +func storeCommandExtra(cmd *command, storage_ix *uint, storage []byte) { + var copylen_code uint32 = commandCopyLenCode(cmd) + var inscode uint16 = getInsertLengthCode(uint(cmd.insert_len_)) + var copycode uint16 = getCopyLengthCode(uint(copylen_code)) + var insnumextra uint32 = getInsertExtra(inscode) + var insextraval uint64 = uint64(cmd.insert_len_) - uint64(getInsertBase(inscode)) + var copyextraval uint64 = uint64(copylen_code) - uint64(getCopyBase(copycode)) var bits uint64 = copyextraval< 0 REQUIRES: length <= (1 << 24) */ -func StoreCompressedMetaBlockHeader(is_final_block bool, length uint, storage_ix *uint, storage []byte) { +func storeCompressedMetaBlockHeader(is_final_block bool, length uint, storage_ix *uint, storage []byte) { var lenbits uint64 var nlenbits uint var nibblesbits uint64 @@ -173,7 +171,7 @@ func StoreCompressedMetaBlockHeader(is_final_block bool, length uint, storage_ix BrotliWriteBits(1, 0, storage_ix, storage) } - BrotliEncodeMlen(length, &lenbits, &nlenbits, &nibblesbits) + encodeMlen(length, &lenbits, &nlenbits, &nibblesbits) BrotliWriteBits(2, nibblesbits, storage_ix, storage) BrotliWriteBits(nlenbits, lenbits, storage_ix, storage) @@ -186,7 +184,7 @@ func StoreCompressedMetaBlockHeader(is_final_block bool, length uint, storage_ix /* Stores the uncompressed meta-block header. REQUIRES: length > 0 REQUIRES: length <= (1 << 24) */ -func BrotliStoreUncompressedMetaBlockHeader(length uint, storage_ix *uint, storage []byte) { +func storeUncompressedMetaBlockHeader(length uint, storage_ix *uint, storage []byte) { var lenbits uint64 var nlenbits uint var nibblesbits uint64 @@ -195,7 +193,7 @@ func BrotliStoreUncompressedMetaBlockHeader(length uint, storage_ix *uint, stora Uncompressed block cannot be the last one, so set to 0. */ BrotliWriteBits(1, 0, storage_ix, storage) - BrotliEncodeMlen(length, &lenbits, &nlenbits, &nibblesbits) + encodeMlen(length, &lenbits, &nlenbits, &nibblesbits) BrotliWriteBits(2, nibblesbits, storage_ix, storage) BrotliWriteBits(nlenbits, lenbits, storage_ix, storage) @@ -203,14 +201,14 @@ func BrotliStoreUncompressedMetaBlockHeader(length uint, storage_ix *uint, stora BrotliWriteBits(1, 1, storage_ix, storage) } -var BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder = [BROTLI_CODE_LENGTH_CODES]byte{1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15} +var storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder = [codeLengthCodes]byte{1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15} -var BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeSymbols = [6]byte{0, 7, 3, 2, 1, 15} -var BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeBitLengths = [6]byte{2, 4, 3, 2, 2, 4} +var storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeSymbols = [6]byte{0, 7, 3, 2, 1, 15} +var storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeBitLengths = [6]byte{2, 4, 3, 2, 2, 4} -func BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(num_codes int, code_length_bitdepth []byte, storage_ix *uint, storage []byte) { +func storeHuffmanTreeOfHuffmanTreeToBitMask(num_codes int, code_length_bitdepth []byte, storage_ix *uint, storage []byte) { var skip_some uint = 0 - var codes_to_store uint = BROTLI_CODE_LENGTH_CODES + var codes_to_store uint = codeLengthCodes /* The bit lengths of the Huffman code over the code length alphabet are compressed with the following static Huffman code: Symbol Code @@ -225,15 +223,15 @@ func BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(num_codes int, code_length_bit /* Throw away trailing zeros: */ if num_codes > 1 { for ; codes_to_store > 0; codes_to_store-- { - if code_length_bitdepth[BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[codes_to_store-1]] != 0 { + if code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[codes_to_store-1]] != 0 { break } } } - if code_length_bitdepth[BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[0]] == 0 && code_length_bitdepth[BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[1]] == 0 { + if code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[0]] == 0 && code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[1]] == 0 { skip_some = 2 /* skips two. */ - if code_length_bitdepth[BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[2]] == 0 { + if code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[2]] == 0 { skip_some = 3 /* skips three. */ } } @@ -242,13 +240,13 @@ func BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(num_codes int, code_length_bit { var i uint for i = skip_some; i < codes_to_store; i++ { - var l uint = uint(code_length_bitdepth[BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[i]]) - BrotliWriteBits(uint(BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeBitLengths[l]), uint64(BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeSymbols[l]), storage_ix, storage) + var l uint = uint(code_length_bitdepth[storeHuffmanTreeOfHuffmanTreeToBitMask_kStorageOrder[i]]) + BrotliWriteBits(uint(storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeBitLengths[l]), uint64(storeHuffmanTreeOfHuffmanTreeToBitMask_kHuffmanBitLengthHuffmanCodeSymbols[l]), storage_ix, storage) } } } -func BrotliStoreHuffmanTreeToBitMask(huffman_tree_size uint, huffman_tree []byte, huffman_tree_extra_bits []byte, code_length_bitdepth []byte, code_length_bitdepth_symbols []uint16, storage_ix *uint, storage []byte) { +func storeHuffmanTreeToBitMask(huffman_tree_size uint, huffman_tree []byte, huffman_tree_extra_bits []byte, code_length_bitdepth []byte, code_length_bitdepth_symbols []uint16, storage_ix *uint, storage []byte) { var i uint for i = 0; i < huffman_tree_size; i++ { var ix uint = uint(huffman_tree[i]) @@ -256,16 +254,16 @@ func BrotliStoreHuffmanTreeToBitMask(huffman_tree_size uint, huffman_tree []byte /* Extra bits */ switch ix { - case BROTLI_REPEAT_PREVIOUS_CODE_LENGTH: + case repeatPreviousCodeLength: BrotliWriteBits(2, uint64(huffman_tree_extra_bits[i]), storage_ix, storage) - case BROTLI_REPEAT_ZERO_CODE_LENGTH: + case repeatZeroCodeLength: BrotliWriteBits(3, uint64(huffman_tree_extra_bits[i]), storage_ix, storage) } } } -func StoreSimpleHuffmanTree(depths []byte, symbols []uint, num_symbols uint, max_bits uint, storage_ix *uint, storage []byte) { +func storeSimpleHuffmanTree(depths []byte, symbols []uint, num_symbols uint, max_bits uint, storage_ix *uint, storage []byte) { /* value of 1 indicates a simple Huffman code */ BrotliWriteBits(2, 1, storage_ix, storage) @@ -311,13 +309,13 @@ func StoreSimpleHuffmanTree(depths []byte, symbols []uint, num_symbols uint, max /* num = alphabet size depths = symbol depths */ -func BrotliStoreHuffmanTree(depths []byte, num uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { - var huffman_tree [BROTLI_NUM_COMMAND_SYMBOLS]byte - var huffman_tree_extra_bits [BROTLI_NUM_COMMAND_SYMBOLS]byte +func storeHuffmanTree(depths []byte, num uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { + var huffman_tree [numCommandSymbols]byte + var huffman_tree_extra_bits [numCommandSymbols]byte var huffman_tree_size uint = 0 - var code_length_bitdepth = [BROTLI_CODE_LENGTH_CODES]byte{0} - var code_length_bitdepth_symbols [BROTLI_CODE_LENGTH_CODES]uint16 - var huffman_tree_histogram = [BROTLI_CODE_LENGTH_CODES]uint32{0} + var code_length_bitdepth = [codeLengthCodes]byte{0} + var code_length_bitdepth_symbols [codeLengthCodes]uint16 + var huffman_tree_histogram = [codeLengthCodes]uint32{0} var i uint var num_codes int = 0 /* Write the Huffman tree into the brotli-representation. @@ -326,7 +324,7 @@ func BrotliStoreHuffmanTree(depths []byte, num uint, tree []HuffmanTree, storage var code uint = 0 - assert(num <= BROTLI_NUM_COMMAND_SYMBOLS) + assert(num <= numCommandSymbols) BrotliWriteHuffmanTree(depths, num, &huffman_tree_size, huffman_tree[:], huffman_tree_extra_bits[:]) @@ -335,7 +333,7 @@ func BrotliStoreHuffmanTree(depths []byte, num uint, tree []HuffmanTree, storage huffman_tree_histogram[huffman_tree[i]]++ } - for i = 0; i < BROTLI_CODE_LENGTH_CODES; i++ { + for i = 0; i < codeLengthCodes; i++ { if huffman_tree_histogram[i] != 0 { if num_codes == 0 { code = i @@ -349,24 +347,24 @@ func BrotliStoreHuffmanTree(depths []byte, num uint, tree []HuffmanTree, storage /* Calculate another Huffman tree to use for compressing both the earlier Huffman tree with. */ - BrotliCreateHuffmanTree(huffman_tree_histogram[:], BROTLI_CODE_LENGTH_CODES, 5, tree, code_length_bitdepth[:]) + BrotliCreateHuffmanTree(huffman_tree_histogram[:], codeLengthCodes, 5, tree, code_length_bitdepth[:]) - BrotliConvertBitDepthsToSymbols(code_length_bitdepth[:], BROTLI_CODE_LENGTH_CODES, code_length_bitdepth_symbols[:]) + BrotliConvertBitDepthsToSymbols(code_length_bitdepth[:], codeLengthCodes, code_length_bitdepth_symbols[:]) /* Now, we have all the data, let's start storing it */ - BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(num_codes, code_length_bitdepth[:], storage_ix, storage) + storeHuffmanTreeOfHuffmanTreeToBitMask(num_codes, code_length_bitdepth[:], storage_ix, storage) if num_codes == 1 { code_length_bitdepth[code] = 0 } /* Store the real Huffman tree now. */ - BrotliStoreHuffmanTreeToBitMask(huffman_tree_size, huffman_tree[:], huffman_tree_extra_bits[:], code_length_bitdepth[:], code_length_bitdepth_symbols[:], storage_ix, storage) + storeHuffmanTreeToBitMask(huffman_tree_size, huffman_tree[:], huffman_tree_extra_bits[:], code_length_bitdepth[:], code_length_bitdepth_symbols[:], storage_ix, storage) } /* Builds a Huffman tree from histogram[0:length] into depth[0:length] and bits[0:length] and stores the encoded tree to the bit stream. */ -func BuildAndStoreHuffmanTree(histogram []uint32, histogram_length uint, alphabet_size uint, tree []HuffmanTree, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { +func buildAndStoreHuffmanTree(histogram []uint32, histogram_length uint, alphabet_size uint, tree []HuffmanTree, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { var count uint = 0 var s4 = [4]uint{0} var i uint @@ -405,17 +403,17 @@ func BuildAndStoreHuffmanTree(histogram []uint32, histogram_length uint, alphabe BrotliConvertBitDepthsToSymbols(depth, histogram_length, bits) if count <= 4 { - StoreSimpleHuffmanTree(depth, s4[:], count, max_bits, storage_ix, storage) + storeSimpleHuffmanTree(depth, s4[:], count, max_bits, storage_ix, storage) } else { - BrotliStoreHuffmanTree(depth, histogram_length, tree, storage_ix, storage) + storeHuffmanTree(depth, histogram_length, tree, storage_ix, storage) } } -func SortHuffmanTree1(v0 *HuffmanTree, v1 *HuffmanTree) bool { +func sortHuffmanTree1(v0 *HuffmanTree, v1 *HuffmanTree) bool { return v0.total_count_ < v1.total_count_ } -func BrotliBuildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint, max_bits uint, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { +func buildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint, max_bits uint, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { var count uint = 0 var symbols = [4]uint{0} var length uint = 0 @@ -471,7 +469,7 @@ func BrotliBuildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint var j int = n + 1 var k int - SortHuffmanTreeItems(tree, uint(n), HuffmanTreeComparator(SortHuffmanTree1)) + SortHuffmanTreeItems(tree, uint(n), HuffmanTreeComparator(sortHuffmanTree1)) /* The nodes are: [0, n): the sorted leaf nodes that we start with. @@ -612,7 +610,7 @@ func BrotliBuildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint } } -func IndexOf(v []byte, v_size uint, value byte) uint { +func indexOf(v []byte, v_size uint, value byte) uint { var i uint = 0 for ; i < v_size; i++ { if v[i] == value { @@ -623,7 +621,7 @@ func IndexOf(v []byte, v_size uint, value byte) uint { return i } -func MoveToFront(v []byte, index uint) { +func moveToFront(v []byte, index uint) { var value byte = v[index] var i uint for i = index; i != 0; i-- { @@ -633,7 +631,7 @@ func MoveToFront(v []byte, index uint) { v[0] = value } -func MoveToFrontTransform(v_in []uint32, v_size uint, v_out []uint32) { +func moveToFrontTransform(v_in []uint32, v_size uint, v_out []uint32) { var i uint var mtf [256]byte var max_value uint32 @@ -655,10 +653,10 @@ func MoveToFrontTransform(v_in []uint32, v_size uint, v_out []uint32) { { var mtf_size uint = uint(max_value + 1) for i = 0; i < v_size; i++ { - var index uint = IndexOf(mtf[:], mtf_size, byte(v_in[i])) + var index uint = indexOf(mtf[:], mtf_size, byte(v_in[i])) assert(index < mtf_size) v_out[i] = uint32(index) - MoveToFront(mtf[:], index) + moveToFront(mtf[:], index) } } } @@ -669,7 +667,7 @@ func MoveToFrontTransform(v_in []uint32, v_size uint, v_out []uint32) { *max_length_prefix. Will not create prefix codes bigger than the initial value of *max_run_length_prefix. The prefix code of run length L is simply Log2Floor(L) and the number of extra bits is the same as the prefix code. */ -func RunLengthCodeZeros(in_size uint, v []uint32, out_size *uint, max_run_length_prefix *uint32) { +func runLengthCodeZeros(in_size uint, v []uint32, out_size *uint, max_run_length_prefix *uint32) { var max_reps uint32 = 0 var i uint var max_prefix uint32 @@ -724,31 +722,31 @@ func RunLengthCodeZeros(in_size uint, v []uint32, out_size *uint, max_run_length } } -const SYMBOL_BITS = 9 +const symbolBits = 9 -var EncodeContextMap_kSymbolMask uint32 = (1 << SYMBOL_BITS) - 1 +var encodeContextMap_kSymbolMask uint32 = (1 << symbolBits) - 1 -func EncodeContextMap(context_map []uint32, context_map_size uint, num_clusters uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func encodeContextMap(context_map []uint32, context_map_size uint, num_clusters uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { var i uint var rle_symbols []uint32 var max_run_length_prefix uint32 = 6 var num_rle_symbols uint = 0 - var histogram [BROTLI_MAX_CONTEXT_MAP_SYMBOLS]uint32 - var depths [BROTLI_MAX_CONTEXT_MAP_SYMBOLS]byte - var bits [BROTLI_MAX_CONTEXT_MAP_SYMBOLS]uint16 + var histogram [maxContextMapSymbols]uint32 + var depths [maxContextMapSymbols]byte + var bits [maxContextMapSymbols]uint16 - StoreVarLenUint8(num_clusters-1, storage_ix, storage) + storeVarLenUint8(num_clusters-1, storage_ix, storage) if num_clusters == 1 { return } rle_symbols = make([]uint32, context_map_size) - MoveToFrontTransform(context_map, context_map_size, rle_symbols) - RunLengthCodeZeros(context_map_size, rle_symbols, &num_rle_symbols, &max_run_length_prefix) - histogram = [BROTLI_MAX_CONTEXT_MAP_SYMBOLS]uint32{} + moveToFrontTransform(context_map, context_map_size, rle_symbols) + runLengthCodeZeros(context_map_size, rle_symbols, &num_rle_symbols, &max_run_length_prefix) + histogram = [maxContextMapSymbols]uint32{} for i = 0; i < num_rle_symbols; i++ { - histogram[rle_symbols[i]&EncodeContextMap_kSymbolMask]++ + histogram[rle_symbols[i]&encodeContextMap_kSymbolMask]++ } { var use_rle bool = (max_run_length_prefix > 0) @@ -758,10 +756,10 @@ func EncodeContextMap(context_map []uint32, context_map_size uint, num_clusters } } - BuildAndStoreHuffmanTree(histogram[:], uint(uint32(num_clusters)+max_run_length_prefix), uint(uint32(num_clusters)+max_run_length_prefix), tree, depths[:], bits[:], storage_ix, storage) + buildAndStoreHuffmanTree(histogram[:], uint(uint32(num_clusters)+max_run_length_prefix), uint(uint32(num_clusters)+max_run_length_prefix), tree, depths[:], bits[:], storage_ix, storage) for i = 0; i < num_rle_symbols; i++ { - var rle_symbol uint32 = rle_symbols[i] & EncodeContextMap_kSymbolMask - var extra_bits_val uint32 = rle_symbols[i] >> SYMBOL_BITS + var rle_symbol uint32 = rle_symbols[i] & encodeContextMap_kSymbolMask + var extra_bits_val uint32 = rle_symbols[i] >> symbolBits BrotliWriteBits(uint(depths[rle_symbol]), uint64(bits[rle_symbol]), storage_ix, storage) if rle_symbol > 0 && rle_symbol <= max_run_length_prefix { BrotliWriteBits(uint(rle_symbol), uint64(extra_bits_val), storage_ix, storage) @@ -773,8 +771,8 @@ func EncodeContextMap(context_map []uint32, context_map_size uint, num_clusters } /* Stores the block switch command with index block_ix to the bit stream. */ -func StoreBlockSwitch(code *BlockSplitCode, block_len uint32, block_type byte, is_first_block bool, storage_ix *uint, storage []byte) { - var typecode uint = NextBlockTypeCode(&code.type_code_calculator, block_type) +func storeBlockSwitch(code *blockSplitCode, block_len uint32, block_type byte, is_first_block bool, storage_ix *uint, storage []byte) { + var typecode uint = nextBlockTypeCode(&code.type_code_calculator, block_type) var lencode uint var len_nextra uint32 var len_extra uint32 @@ -782,7 +780,7 @@ func StoreBlockSwitch(code *BlockSplitCode, block_len uint32, block_type byte, i BrotliWriteBits(uint(code.type_depths[typecode]), uint64(code.type_bits[typecode]), storage_ix, storage) } - GetBlockLengthPrefixCode(block_len, &lencode, &len_nextra, &len_extra) + getBlockLengthPrefixCode(block_len, &lencode, &len_nextra, &len_extra) BrotliWriteBits(uint(code.length_depths[lencode]), uint64(code.length_bits[lencode]), storage_ix, storage) BrotliWriteBits(uint(len_nextra), uint64(len_extra), storage_ix, storage) @@ -790,42 +788,42 @@ func StoreBlockSwitch(code *BlockSplitCode, block_len uint32, block_type byte, i /* Builds a BlockSplitCode data structure from the block split given by the vector of block types and block lengths and stores it to the bit stream. */ -func BuildAndStoreBlockSplitCode(types []byte, lengths []uint32, num_blocks uint, num_types uint, tree []HuffmanTree, code *BlockSplitCode, storage_ix *uint, storage []byte) { - var type_histo [BROTLI_MAX_BLOCK_TYPE_SYMBOLS]uint32 - var length_histo [BROTLI_NUM_BLOCK_LEN_SYMBOLS]uint32 +func buildAndStoreBlockSplitCode(types []byte, lengths []uint32, num_blocks uint, num_types uint, tree []HuffmanTree, code *blockSplitCode, storage_ix *uint, storage []byte) { + var type_histo [maxBlockTypeSymbols]uint32 + var length_histo [numBlockLenSymbols]uint32 var i uint - var type_code_calculator BlockTypeCodeCalculator + var type_code_calculator blockTypeCodeCalculator for i := 0; i < int(num_types+2); i++ { type_histo[i] = 0 } - length_histo = [BROTLI_NUM_BLOCK_LEN_SYMBOLS]uint32{} - InitBlockTypeCodeCalculator(&type_code_calculator) + length_histo = [numBlockLenSymbols]uint32{} + initBlockTypeCodeCalculator(&type_code_calculator) for i = 0; i < num_blocks; i++ { - var type_code uint = NextBlockTypeCode(&type_code_calculator, types[i]) + var type_code uint = nextBlockTypeCode(&type_code_calculator, types[i]) if i != 0 { type_histo[type_code]++ } - length_histo[BlockLengthPrefixCode(lengths[i])]++ + length_histo[blockLengthPrefixCode(lengths[i])]++ } - StoreVarLenUint8(num_types-1, storage_ix, storage) + storeVarLenUint8(num_types-1, storage_ix, storage) if num_types > 1 { /* TODO: else? could StoreBlockSwitch occur? */ - BuildAndStoreHuffmanTree(type_histo[0:], num_types+2, num_types+2, tree, code.type_depths[0:], code.type_bits[0:], storage_ix, storage) - BuildAndStoreHuffmanTree(length_histo[0:], BROTLI_NUM_BLOCK_LEN_SYMBOLS, BROTLI_NUM_BLOCK_LEN_SYMBOLS, tree, code.length_depths[0:], code.length_bits[0:], storage_ix, storage) - StoreBlockSwitch(code, lengths[0], types[0], true, storage_ix, storage) + buildAndStoreHuffmanTree(type_histo[0:], num_types+2, num_types+2, tree, code.type_depths[0:], code.type_bits[0:], storage_ix, storage) + buildAndStoreHuffmanTree(length_histo[0:], numBlockLenSymbols, numBlockLenSymbols, tree, code.length_depths[0:], code.length_bits[0:], storage_ix, storage) + storeBlockSwitch(code, lengths[0], types[0], true, storage_ix, storage) } } /* Stores a context map where the histogram type is always the block type. */ -func StoreTrivialContextMap(num_types uint, context_bits uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { - StoreVarLenUint8(num_types-1, storage_ix, storage) +func storeTrivialContextMap(num_types uint, context_bits uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { + storeVarLenUint8(num_types-1, storage_ix, storage) if num_types > 1 { var repeat_code uint = context_bits - 1 var repeat_bits uint = (1 << repeat_code) - 1 var alphabet_size uint = num_types + repeat_code - var histogram [BROTLI_MAX_CONTEXT_MAP_SYMBOLS]uint32 - var depths [BROTLI_MAX_CONTEXT_MAP_SYMBOLS]byte - var bits [BROTLI_MAX_CONTEXT_MAP_SYMBOLS]uint16 + var histogram [maxContextMapSymbols]uint32 + var depths [maxContextMapSymbols]byte + var bits [maxContextMapSymbols]uint16 var i uint for i := 0; i < int(alphabet_size); i++ { histogram[i] = 0 @@ -841,7 +839,7 @@ func StoreTrivialContextMap(num_types uint, context_bits uint, tree []HuffmanTre histogram[i] = 1 } - BuildAndStoreHuffmanTree(histogram[:], alphabet_size, alphabet_size, tree, depths[:], bits[:], storage_ix, storage) + buildAndStoreHuffmanTree(histogram[:], alphabet_size, alphabet_size, tree, depths[:], bits[:], storage_ix, storage) for i = 0; i < num_types; i++ { var tmp uint if i == 0 { @@ -861,13 +859,13 @@ func StoreTrivialContextMap(num_types uint, context_bits uint, tree []HuffmanTre } /* Manages the encoding of one block category (literal, command or distance). */ -type BlockEncoder struct { +type blockEncoder struct { histogram_length_ uint num_block_types_ uint block_types_ []byte block_lengths_ []uint32 num_blocks_ uint - block_split_code_ BlockSplitCode + block_split_code_ blockSplitCode block_ix_ uint block_len_ uint entropy_ix_ uint @@ -875,13 +873,13 @@ type BlockEncoder struct { bits_ []uint16 } -func InitBlockEncoder(self *BlockEncoder, histogram_length uint, num_block_types uint, block_types []byte, block_lengths []uint32, num_blocks uint) { +func initBlockEncoder(self *blockEncoder, histogram_length uint, num_block_types uint, block_types []byte, block_lengths []uint32, num_blocks uint) { self.histogram_length_ = histogram_length self.num_block_types_ = num_block_types self.block_types_ = block_types self.block_lengths_ = block_lengths self.num_blocks_ = num_blocks - InitBlockTypeCodeCalculator(&self.block_split_code_.type_code_calculator) + initBlockTypeCodeCalculator(&self.block_split_code_.type_code_calculator) self.block_ix_ = 0 if num_blocks == 0 { self.block_len_ = 0 @@ -893,20 +891,20 @@ func InitBlockEncoder(self *BlockEncoder, histogram_length uint, num_block_types self.bits_ = nil } -func CleanupBlockEncoder(self *BlockEncoder) { +func cleanupBlockEncoder(self *blockEncoder) { self.depths_ = nil self.bits_ = nil } /* Creates entropy codes of block lengths and block types and stores them to the bit stream. */ -func BuildAndStoreBlockSwitchEntropyCodes(self *BlockEncoder, tree []HuffmanTree, storage_ix *uint, storage []byte) { - BuildAndStoreBlockSplitCode(self.block_types_, self.block_lengths_, self.num_blocks_, self.num_block_types_, tree, &self.block_split_code_, storage_ix, storage) +func buildAndStoreBlockSwitchEntropyCodes(self *blockEncoder, tree []HuffmanTree, storage_ix *uint, storage []byte) { + buildAndStoreBlockSplitCode(self.block_types_, self.block_lengths_, self.num_blocks_, self.num_block_types_, tree, &self.block_split_code_, storage_ix, storage) } /* Stores the next symbol with the entropy code of the current block type. Updates the block type and block length at block boundaries. */ -func StoreSymbol(self *BlockEncoder, symbol uint, storage_ix *uint, storage []byte) { +func storeSymbol(self *blockEncoder, symbol uint, storage_ix *uint, storage []byte) { if self.block_len_ == 0 { self.block_ix_++ var block_ix uint = self.block_ix_ @@ -914,7 +912,7 @@ func StoreSymbol(self *BlockEncoder, symbol uint, storage_ix *uint, storage []by var block_type byte = self.block_types_[block_ix] self.block_len_ = uint(block_len) self.entropy_ix_ = uint(block_type) * self.histogram_length_ - StoreBlockSwitch(&self.block_split_code_, block_len, block_type, false, storage_ix, storage) + storeBlockSwitch(&self.block_split_code_, block_len, block_type, false, storage_ix, storage) } self.block_len_-- @@ -927,7 +925,7 @@ func StoreSymbol(self *BlockEncoder, symbol uint, storage_ix *uint, storage []by /* Stores the next symbol with the entropy code of the current block type and context value. Updates the block type and block length at block boundaries. */ -func StoreSymbolWithContext(self *BlockEncoder, symbol uint, context uint, context_map []uint32, storage_ix *uint, storage []byte, context_bits uint) { +func storeSymbolWithContext(self *blockEncoder, symbol uint, context uint, context_map []uint32, storage_ix *uint, storage []byte, context_bits uint) { if self.block_len_ == 0 { self.block_ix_++ var block_ix uint = self.block_ix_ @@ -935,7 +933,7 @@ func StoreSymbolWithContext(self *BlockEncoder, symbol uint, context uint, conte var block_type byte = self.block_types_[block_ix] self.block_len_ = uint(block_len) self.entropy_ix_ = uint(block_type) << context_bits - StoreBlockSwitch(&self.block_split_code_, block_len, block_type, false, storage_ix, storage) + storeBlockSwitch(&self.block_split_code_, block_len, block_type, false, storage_ix, storage) } self.block_len_-- @@ -946,7 +944,7 @@ func StoreSymbolWithContext(self *BlockEncoder, symbol uint, context uint, conte } } -func BuildAndStoreEntropyCodesLiteral(self *BlockEncoder, histograms []HistogramLiteral, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func buildAndStoreEntropyCodesLiteral(self *blockEncoder, histograms []HistogramLiteral, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { var table_size uint = histograms_size * self.histogram_length_ self.depths_ = make([]byte, table_size) self.bits_ = make([]uint16, table_size) @@ -954,12 +952,12 @@ func BuildAndStoreEntropyCodesLiteral(self *BlockEncoder, histograms []Histogram var i uint for i = 0; i < histograms_size; i++ { var ix uint = i * self.histogram_length_ - BuildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage) + buildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage) } } } -func BuildAndStoreEntropyCodesCommand(self *BlockEncoder, histograms []HistogramCommand, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func buildAndStoreEntropyCodesCommand(self *blockEncoder, histograms []HistogramCommand, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { var table_size uint = histograms_size * self.histogram_length_ self.depths_ = make([]byte, table_size) self.bits_ = make([]uint16, table_size) @@ -967,12 +965,12 @@ func BuildAndStoreEntropyCodesCommand(self *BlockEncoder, histograms []Histogram var i uint for i = 0; i < histograms_size; i++ { var ix uint = i * self.histogram_length_ - BuildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage) + buildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage) } } } -func BuildAndStoreEntropyCodesDistance(self *BlockEncoder, histograms []HistogramDistance, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func buildAndStoreEntropyCodesDistance(self *blockEncoder, histograms []HistogramDistance, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { var table_size uint = histograms_size * self.histogram_length_ self.depths_ = make([]byte, table_size) self.bits_ = make([]uint16, table_size) @@ -980,41 +978,41 @@ func BuildAndStoreEntropyCodesDistance(self *BlockEncoder, histograms []Histogra var i uint for i = 0; i < histograms_size; i++ { var ix uint = i * self.histogram_length_ - BuildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage) + buildAndStoreHuffmanTree(histograms[i].data_[0:], self.histogram_length_, alphabet_size, tree, self.depths_[ix:], self.bits_[ix:], storage_ix, storage) } } } -func JumpToByteBoundary(storage_ix *uint, storage []byte) { +func jumpToByteBoundary(storage_ix *uint, storage []byte) { *storage_ix = (*storage_ix + 7) &^ 7 storage[*storage_ix>>3] = 0 } -func BrotliStoreMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_byte byte, prev_byte2 byte, is_last bool, params *BrotliEncoderParams, literal_context_mode int, commands []Command, n_commands uint, mb *MetaBlockSplit, storage_ix *uint, storage []byte) { +func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_byte byte, prev_byte2 byte, is_last bool, params *BrotliEncoderParams, literal_context_mode int, commands []command, n_commands uint, mb *MetaBlockSplit, storage_ix *uint, storage []byte) { var pos uint = start_pos var i uint var num_distance_symbols uint32 = params.dist.alphabet_size var num_effective_distance_symbols uint32 = num_distance_symbols var tree []HuffmanTree var literal_context_lut ContextLut = BROTLI_CONTEXT_LUT(literal_context_mode) - var literal_enc BlockEncoder - var command_enc BlockEncoder - var distance_enc BlockEncoder + var literal_enc blockEncoder + var command_enc blockEncoder + var distance_enc blockEncoder var dist *BrotliDistanceParams = ¶ms.dist if params.large_window && num_effective_distance_symbols > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS { num_effective_distance_symbols = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS } - StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage) + storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage) - tree = make([]HuffmanTree, MAX_HUFFMAN_TREE_SIZE) - InitBlockEncoder(&literal_enc, BROTLI_NUM_LITERAL_SYMBOLS, mb.literal_split.num_types, mb.literal_split.types, mb.literal_split.lengths, mb.literal_split.num_blocks) - InitBlockEncoder(&command_enc, BROTLI_NUM_COMMAND_SYMBOLS, mb.command_split.num_types, mb.command_split.types, mb.command_split.lengths, mb.command_split.num_blocks) - InitBlockEncoder(&distance_enc, uint(num_effective_distance_symbols), mb.distance_split.num_types, mb.distance_split.types, mb.distance_split.lengths, mb.distance_split.num_blocks) + tree = make([]HuffmanTree, maxHuffmanTreeSize) + initBlockEncoder(&literal_enc, numLiteralSymbols, mb.literal_split.num_types, mb.literal_split.types, mb.literal_split.lengths, mb.literal_split.num_blocks) + initBlockEncoder(&command_enc, numCommandSymbols, mb.command_split.num_types, mb.command_split.types, mb.command_split.lengths, mb.command_split.num_blocks) + initBlockEncoder(&distance_enc, uint(num_effective_distance_symbols), mb.distance_split.num_types, mb.distance_split.types, mb.distance_split.lengths, mb.distance_split.num_blocks) - BuildAndStoreBlockSwitchEntropyCodes(&literal_enc, tree, storage_ix, storage) - BuildAndStoreBlockSwitchEntropyCodes(&command_enc, tree, storage_ix, storage) - BuildAndStoreBlockSwitchEntropyCodes(&distance_enc, tree, storage_ix, storage) + buildAndStoreBlockSwitchEntropyCodes(&literal_enc, tree, storage_ix, storage) + buildAndStoreBlockSwitchEntropyCodes(&command_enc, tree, storage_ix, storage) + buildAndStoreBlockSwitchEntropyCodes(&distance_enc, tree, storage_ix, storage) BrotliWriteBits(2, uint64(dist.distance_postfix_bits), storage_ix, storage) BrotliWriteBits(4, uint64(dist.num_direct_distance_codes)>>dist.distance_postfix_bits, storage_ix, storage) @@ -1023,31 +1021,31 @@ func BrotliStoreMetaBlock(input []byte, start_pos uint, length uint, mask uint, } if mb.literal_context_map_size == 0 { - StoreTrivialContextMap(mb.literal_histograms_size, BROTLI_LITERAL_CONTEXT_BITS, tree, storage_ix, storage) + storeTrivialContextMap(mb.literal_histograms_size, BROTLI_LITERAL_CONTEXT_BITS, tree, storage_ix, storage) } else { - EncodeContextMap(mb.literal_context_map, mb.literal_context_map_size, mb.literal_histograms_size, tree, storage_ix, storage) + encodeContextMap(mb.literal_context_map, mb.literal_context_map_size, mb.literal_histograms_size, tree, storage_ix, storage) } if mb.distance_context_map_size == 0 { - StoreTrivialContextMap(mb.distance_histograms_size, BROTLI_DISTANCE_CONTEXT_BITS, tree, storage_ix, storage) + storeTrivialContextMap(mb.distance_histograms_size, BROTLI_DISTANCE_CONTEXT_BITS, tree, storage_ix, storage) } else { - EncodeContextMap(mb.distance_context_map, mb.distance_context_map_size, mb.distance_histograms_size, tree, storage_ix, storage) + encodeContextMap(mb.distance_context_map, mb.distance_context_map_size, mb.distance_histograms_size, tree, storage_ix, storage) } - BuildAndStoreEntropyCodesLiteral(&literal_enc, mb.literal_histograms, mb.literal_histograms_size, BROTLI_NUM_LITERAL_SYMBOLS, tree, storage_ix, storage) - BuildAndStoreEntropyCodesCommand(&command_enc, mb.command_histograms, mb.command_histograms_size, BROTLI_NUM_COMMAND_SYMBOLS, tree, storage_ix, storage) - BuildAndStoreEntropyCodesDistance(&distance_enc, mb.distance_histograms, mb.distance_histograms_size, uint(num_distance_symbols), tree, storage_ix, storage) + buildAndStoreEntropyCodesLiteral(&literal_enc, mb.literal_histograms, mb.literal_histograms_size, numLiteralSymbols, tree, storage_ix, storage) + buildAndStoreEntropyCodesCommand(&command_enc, mb.command_histograms, mb.command_histograms_size, numCommandSymbols, tree, storage_ix, storage) + buildAndStoreEntropyCodesDistance(&distance_enc, mb.distance_histograms, mb.distance_histograms_size, uint(num_distance_symbols), tree, storage_ix, storage) tree = nil for i = 0; i < n_commands; i++ { - var cmd Command = commands[i] + var cmd command = commands[i] var cmd_code uint = uint(cmd.cmd_prefix_) - StoreSymbol(&command_enc, cmd_code, storage_ix, storage) - StoreCommandExtra(&cmd, storage_ix, storage) + storeSymbol(&command_enc, cmd_code, storage_ix, storage) + storeCommandExtra(&cmd, storage_ix, storage) if mb.literal_context_map_size == 0 { var j uint for j = uint(cmd.insert_len_); j != 0; j-- { - StoreSymbol(&literal_enc, uint(input[pos&mask]), storage_ix, storage) + storeSymbol(&literal_enc, uint(input[pos&mask]), storage_ix, storage) pos++ } } else { @@ -1055,15 +1053,15 @@ func BrotliStoreMetaBlock(input []byte, start_pos uint, length uint, mask uint, for j = uint(cmd.insert_len_); j != 0; j-- { var context uint = uint(BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut)) var literal byte = input[pos&mask] - StoreSymbolWithContext(&literal_enc, uint(literal), context, mb.literal_context_map, storage_ix, storage, BROTLI_LITERAL_CONTEXT_BITS) + storeSymbolWithContext(&literal_enc, uint(literal), context, mb.literal_context_map, storage_ix, storage, BROTLI_LITERAL_CONTEXT_BITS) prev_byte2 = prev_byte prev_byte = literal pos++ } } - pos += uint(CommandCopyLen(&cmd)) - if CommandCopyLen(&cmd) != 0 { + pos += uint(commandCopyLen(&cmd)) + if commandCopyLen(&cmd) != 0 { prev_byte2 = input[(pos-2)&mask] prev_byte = input[(pos-1)&mask] if cmd.cmd_prefix_ >= 128 { @@ -1071,10 +1069,10 @@ func BrotliStoreMetaBlock(input []byte, start_pos uint, length uint, mask uint, var distnumextra uint32 = uint32(cmd.dist_prefix_) >> 10 var distextra uint64 = uint64(cmd.dist_extra_) if mb.distance_context_map_size == 0 { - StoreSymbol(&distance_enc, dist_code, storage_ix, storage) + storeSymbol(&distance_enc, dist_code, storage_ix, storage) } else { - var context uint = uint(CommandDistanceContext(&cmd)) - StoreSymbolWithContext(&distance_enc, dist_code, context, mb.distance_context_map, storage_ix, storage, BROTLI_DISTANCE_CONTEXT_BITS) + var context uint = uint(commandDistanceContext(&cmd)) + storeSymbolWithContext(&distance_enc, dist_code, context, mb.distance_context_map, storage_ix, storage, BROTLI_DISTANCE_CONTEXT_BITS) } BrotliWriteBits(uint(distnumextra), distextra, storage_ix, storage) @@ -1082,19 +1080,19 @@ func BrotliStoreMetaBlock(input []byte, start_pos uint, length uint, mask uint, } } - CleanupBlockEncoder(&distance_enc) - CleanupBlockEncoder(&command_enc) - CleanupBlockEncoder(&literal_enc) + cleanupBlockEncoder(&distance_enc) + cleanupBlockEncoder(&command_enc) + cleanupBlockEncoder(&literal_enc) if is_last { - JumpToByteBoundary(storage_ix, storage) + jumpToByteBoundary(storage_ix, storage) } } -func BuildHistograms(input []byte, start_pos uint, mask uint, commands []Command, n_commands uint, lit_histo *HistogramLiteral, cmd_histo *HistogramCommand, dist_histo *HistogramDistance) { +func buildHistograms(input []byte, start_pos uint, mask uint, commands []command, n_commands uint, lit_histo *HistogramLiteral, cmd_histo *HistogramCommand, dist_histo *HistogramDistance) { var pos uint = start_pos var i uint for i = 0; i < n_commands; i++ { - var cmd Command = commands[i] + var cmd command = commands[i] var j uint HistogramAddCommand(cmd_histo, uint(cmd.cmd_prefix_)) for j = uint(cmd.insert_len_); j != 0; j-- { @@ -1102,30 +1100,30 @@ func BuildHistograms(input []byte, start_pos uint, mask uint, commands []Command pos++ } - pos += uint(CommandCopyLen(&cmd)) - if CommandCopyLen(&cmd) != 0 && cmd.cmd_prefix_ >= 128 { + pos += uint(commandCopyLen(&cmd)) + if commandCopyLen(&cmd) != 0 && cmd.cmd_prefix_ >= 128 { HistogramAddDistance(dist_histo, uint(cmd.dist_prefix_)&0x3FF) } } } -func StoreDataWithHuffmanCodes(input []byte, start_pos uint, mask uint, commands []Command, n_commands uint, lit_depth []byte, lit_bits []uint16, cmd_depth []byte, cmd_bits []uint16, dist_depth []byte, dist_bits []uint16, storage_ix *uint, storage []byte) { +func storeDataWithHuffmanCodes(input []byte, start_pos uint, mask uint, commands []command, n_commands uint, lit_depth []byte, lit_bits []uint16, cmd_depth []byte, cmd_bits []uint16, dist_depth []byte, dist_bits []uint16, storage_ix *uint, storage []byte) { var pos uint = start_pos var i uint for i = 0; i < n_commands; i++ { - var cmd Command = commands[i] + var cmd command = commands[i] var cmd_code uint = uint(cmd.cmd_prefix_) var j uint BrotliWriteBits(uint(cmd_depth[cmd_code]), uint64(cmd_bits[cmd_code]), storage_ix, storage) - StoreCommandExtra(&cmd, storage_ix, storage) + storeCommandExtra(&cmd, storage_ix, storage) for j = uint(cmd.insert_len_); j != 0; j-- { var literal byte = input[pos&mask] BrotliWriteBits(uint(lit_depth[literal]), uint64(lit_bits[literal]), storage_ix, storage) pos++ } - pos += uint(CommandCopyLen(&cmd)) - if CommandCopyLen(&cmd) != 0 && cmd.cmd_prefix_ >= 128 { + pos += uint(commandCopyLen(&cmd)) + if commandCopyLen(&cmd) != 0 && cmd.cmd_prefix_ >= 128 { var dist_code uint = uint(cmd.dist_prefix_) & 0x3FF var distnumextra uint32 = uint32(cmd.dist_prefix_) >> 10 var distextra uint32 = cmd.dist_extra_ @@ -1135,57 +1133,57 @@ func StoreDataWithHuffmanCodes(input []byte, start_pos uint, mask uint, commands } } -func BrotliStoreMetaBlockTrivial(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *BrotliEncoderParams, commands []Command, n_commands uint, storage_ix *uint, storage []byte) { +func storeMetaBlockTrivial(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *BrotliEncoderParams, commands []command, n_commands uint, storage_ix *uint, storage []byte) { var lit_histo HistogramLiteral var cmd_histo HistogramCommand var dist_histo HistogramDistance - var lit_depth [BROTLI_NUM_LITERAL_SYMBOLS]byte - var lit_bits [BROTLI_NUM_LITERAL_SYMBOLS]uint16 - var cmd_depth [BROTLI_NUM_COMMAND_SYMBOLS]byte - var cmd_bits [BROTLI_NUM_COMMAND_SYMBOLS]uint16 - var dist_depth [MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]byte - var dist_bits [MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]uint16 + var lit_depth [numLiteralSymbols]byte + var lit_bits [numLiteralSymbols]uint16 + var cmd_depth [numCommandSymbols]byte + var cmd_bits [numCommandSymbols]uint16 + var dist_depth [maxSimpleDistanceAlphabetSize]byte + var dist_bits [maxSimpleDistanceAlphabetSize]uint16 var tree []HuffmanTree var num_distance_symbols uint32 = params.dist.alphabet_size - StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage) + storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage) HistogramClearLiteral(&lit_histo) HistogramClearCommand(&cmd_histo) HistogramClearDistance(&dist_histo) - BuildHistograms(input, start_pos, mask, commands, n_commands, &lit_histo, &cmd_histo, &dist_histo) + buildHistograms(input, start_pos, mask, commands, n_commands, &lit_histo, &cmd_histo, &dist_histo) BrotliWriteBits(13, 0, storage_ix, storage) - tree = make([]HuffmanTree, MAX_HUFFMAN_TREE_SIZE) - BuildAndStoreHuffmanTree(lit_histo.data_[:], BROTLI_NUM_LITERAL_SYMBOLS, BROTLI_NUM_LITERAL_SYMBOLS, tree, lit_depth[:], lit_bits[:], storage_ix, storage) - BuildAndStoreHuffmanTree(cmd_histo.data_[:], BROTLI_NUM_COMMAND_SYMBOLS, BROTLI_NUM_COMMAND_SYMBOLS, tree, cmd_depth[:], cmd_bits[:], storage_ix, storage) - BuildAndStoreHuffmanTree(dist_histo.data_[:], MAX_SIMPLE_DISTANCE_ALPHABET_SIZE, uint(num_distance_symbols), tree, dist_depth[:], dist_bits[:], storage_ix, storage) + tree = make([]HuffmanTree, maxHuffmanTreeSize) + buildAndStoreHuffmanTree(lit_histo.data_[:], numLiteralSymbols, numLiteralSymbols, tree, lit_depth[:], lit_bits[:], storage_ix, storage) + buildAndStoreHuffmanTree(cmd_histo.data_[:], numCommandSymbols, numCommandSymbols, tree, cmd_depth[:], cmd_bits[:], storage_ix, storage) + buildAndStoreHuffmanTree(dist_histo.data_[:], maxSimpleDistanceAlphabetSize, uint(num_distance_symbols), tree, dist_depth[:], dist_bits[:], storage_ix, storage) tree = nil - StoreDataWithHuffmanCodes(input, start_pos, mask, commands, n_commands, lit_depth[:], lit_bits[:], cmd_depth[:], cmd_bits[:], dist_depth[:], dist_bits[:], storage_ix, storage) + storeDataWithHuffmanCodes(input, start_pos, mask, commands, n_commands, lit_depth[:], lit_bits[:], cmd_depth[:], cmd_bits[:], dist_depth[:], dist_bits[:], storage_ix, storage) if is_last { - JumpToByteBoundary(storage_ix, storage) + jumpToByteBoundary(storage_ix, storage) } } -func BrotliStoreMetaBlockFast(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *BrotliEncoderParams, commands []Command, n_commands uint, storage_ix *uint, storage []byte) { +func storeMetaBlockFast(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *BrotliEncoderParams, commands []command, n_commands uint, storage_ix *uint, storage []byte) { var num_distance_symbols uint32 = params.dist.alphabet_size var distance_alphabet_bits uint32 = Log2FloorNonZero(uint(num_distance_symbols-1)) + 1 - StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage) + storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage) BrotliWriteBits(13, 0, storage_ix, storage) if n_commands <= 128 { - var histogram = [BROTLI_NUM_LITERAL_SYMBOLS]uint32{0} + var histogram = [numLiteralSymbols]uint32{0} var pos uint = start_pos var num_literals uint = 0 var i uint - var lit_depth [BROTLI_NUM_LITERAL_SYMBOLS]byte - var lit_bits [BROTLI_NUM_LITERAL_SYMBOLS]uint16 + var lit_depth [numLiteralSymbols]byte + var lit_bits [numLiteralSymbols]uint16 for i = 0; i < n_commands; i++ { - var cmd Command = commands[i] + var cmd command = commands[i] var j uint for j = uint(cmd.insert_len_); j != 0; j-- { histogram[input[pos&mask]]++ @@ -1193,52 +1191,52 @@ func BrotliStoreMetaBlockFast(input []byte, start_pos uint, length uint, mask ui } num_literals += uint(cmd.insert_len_) - pos += uint(CommandCopyLen(&cmd)) + pos += uint(commandCopyLen(&cmd)) } - BrotliBuildAndStoreHuffmanTreeFast(histogram[:], num_literals, /* max_bits = */ + buildAndStoreHuffmanTreeFast(histogram[:], num_literals, /* max_bits = */ 8, lit_depth[:], lit_bits[:], storage_ix, storage) StoreStaticCommandHuffmanTree(storage_ix, storage) StoreStaticDistanceHuffmanTree(storage_ix, storage) - StoreDataWithHuffmanCodes(input, start_pos, mask, commands, n_commands, lit_depth[:], lit_bits[:], kStaticCommandCodeDepth[:], kStaticCommandCodeBits[:], kStaticDistanceCodeDepth[:], kStaticDistanceCodeBits[:], storage_ix, storage) + storeDataWithHuffmanCodes(input, start_pos, mask, commands, n_commands, lit_depth[:], lit_bits[:], kStaticCommandCodeDepth[:], kStaticCommandCodeBits[:], kStaticDistanceCodeDepth[:], kStaticDistanceCodeBits[:], storage_ix, storage) } else { var lit_histo HistogramLiteral var cmd_histo HistogramCommand var dist_histo HistogramDistance - var lit_depth [BROTLI_NUM_LITERAL_SYMBOLS]byte - var lit_bits [BROTLI_NUM_LITERAL_SYMBOLS]uint16 - var cmd_depth [BROTLI_NUM_COMMAND_SYMBOLS]byte - var cmd_bits [BROTLI_NUM_COMMAND_SYMBOLS]uint16 - var dist_depth [MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]byte - var dist_bits [MAX_SIMPLE_DISTANCE_ALPHABET_SIZE]uint16 + var lit_depth [numLiteralSymbols]byte + var lit_bits [numLiteralSymbols]uint16 + var cmd_depth [numCommandSymbols]byte + var cmd_bits [numCommandSymbols]uint16 + var dist_depth [maxSimpleDistanceAlphabetSize]byte + var dist_bits [maxSimpleDistanceAlphabetSize]uint16 HistogramClearLiteral(&lit_histo) HistogramClearCommand(&cmd_histo) HistogramClearDistance(&dist_histo) - BuildHistograms(input, start_pos, mask, commands, n_commands, &lit_histo, &cmd_histo, &dist_histo) - BrotliBuildAndStoreHuffmanTreeFast(lit_histo.data_[:], lit_histo.total_count_, /* max_bits = */ + buildHistograms(input, start_pos, mask, commands, n_commands, &lit_histo, &cmd_histo, &dist_histo) + buildAndStoreHuffmanTreeFast(lit_histo.data_[:], lit_histo.total_count_, /* max_bits = */ 8, lit_depth[:], lit_bits[:], storage_ix, storage) - BrotliBuildAndStoreHuffmanTreeFast(cmd_histo.data_[:], cmd_histo.total_count_, /* max_bits = */ + buildAndStoreHuffmanTreeFast(cmd_histo.data_[:], cmd_histo.total_count_, /* max_bits = */ 10, cmd_depth[:], cmd_bits[:], storage_ix, storage) - BrotliBuildAndStoreHuffmanTreeFast(dist_histo.data_[:], dist_histo.total_count_, /* max_bits = */ + buildAndStoreHuffmanTreeFast(dist_histo.data_[:], dist_histo.total_count_, /* max_bits = */ uint(distance_alphabet_bits), dist_depth[:], dist_bits[:], storage_ix, storage) - StoreDataWithHuffmanCodes(input, start_pos, mask, commands, n_commands, lit_depth[:], lit_bits[:], cmd_depth[:], cmd_bits[:], dist_depth[:], dist_bits[:], storage_ix, storage) + storeDataWithHuffmanCodes(input, start_pos, mask, commands, n_commands, lit_depth[:], lit_bits[:], cmd_depth[:], cmd_bits[:], dist_depth[:], dist_bits[:], storage_ix, storage) } if is_last { - JumpToByteBoundary(storage_ix, storage) + jumpToByteBoundary(storage_ix, storage) } } /* This is for storing uncompressed blocks (simple raw storage of bytes-as-bytes). */ -func BrotliStoreUncompressedMetaBlock(is_final_block bool, input []byte, position uint, mask uint, len uint, storage_ix *uint, storage []byte) { +func storeUncompressedMetaBlock(is_final_block bool, input []byte, position uint, mask uint, len uint, storage_ix *uint, storage []byte) { var masked_pos uint = position & mask - BrotliStoreUncompressedMetaBlockHeader(uint(len), storage_ix, storage) - JumpToByteBoundary(storage_ix, storage) + storeUncompressedMetaBlockHeader(uint(len), storage_ix, storage) + jumpToByteBoundary(storage_ix, storage) if masked_pos+len > mask+1 { var len1 uint = mask + 1 - masked_pos @@ -1260,6 +1258,6 @@ func BrotliStoreUncompressedMetaBlock(is_final_block bool, input []byte, positio if is_final_block { BrotliWriteBits(1, 1, storage_ix, storage) /* islast */ BrotliWriteBits(1, 1, storage_ix, storage) /* isempty */ - JumpToByteBoundary(storage_ix, storage) + jumpToByteBoundary(storage_ix, storage) } } diff --git a/cluster.go b/cluster.go index 40957b6..6900853 100644 --- a/cluster.go +++ b/cluster.go @@ -14,7 +14,7 @@ package brotli */ /* Functions for clustering similar histograms together. */ -type HistogramPair struct { +type histogramPair struct { idx1 uint32 idx2 uint32 cost_combo float64 @@ -28,7 +28,7 @@ type HistogramPair struct { */ /* Functions for clustering similar histograms together. */ -func HistogramPairIsLess(p1 *HistogramPair, p2 *HistogramPair) bool { +func histogramPairIsLess(p1 *histogramPair, p2 *histogramPair) bool { if p1.cost_diff != p2.cost_diff { return p1.cost_diff > p2.cost_diff } @@ -37,7 +37,7 @@ func HistogramPairIsLess(p1 *HistogramPair, p2 *HistogramPair) bool { } /* Returns entropy reduction of the context map when we combine two clusters. */ -func ClusterCostDiff(size_a uint, size_b uint) float64 { +func clusterCostDiff(size_a uint, size_b uint) float64 { var size_c uint = size_a + size_b return float64(size_a)*FastLog2(size_a) + float64(size_b)*FastLog2(size_b) - float64(size_c)*FastLog2(size_c) } diff --git a/cluster_command.go b/cluster_command.go index e38b552..72607a7 100644 --- a/cluster_command.go +++ b/cluster_command.go @@ -9,9 +9,9 @@ package brotli /* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */ -func BrotliCompareAndPushToQueueCommand(out []HistogramCommand, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []HistogramPair, num_pairs *uint) { +func compareAndPushToQueueCommand(out []HistogramCommand, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { var is_good_pair bool = false - var p HistogramPair + var p histogramPair p.idx2 = 0 p.idx1 = p.idx2 p.cost_combo = 0 @@ -28,7 +28,7 @@ func BrotliCompareAndPushToQueueCommand(out []HistogramCommand, cluster_size []u p.idx1 = idx1 p.idx2 = idx2 - p.cost_diff = 0.5 * ClusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2])) + p.cost_diff = 0.5 * clusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2])) p.cost_diff -= out[idx1].bit_cost_ p.cost_diff -= out[idx2].bit_cost_ @@ -57,7 +57,7 @@ func BrotliCompareAndPushToQueueCommand(out []HistogramCommand, cluster_size []u if is_good_pair { p.cost_diff += p.cost_combo - if *num_pairs > 0 && HistogramPairIsLess(&pairs[0], &p) { + if *num_pairs > 0 && histogramPairIsLess(&pairs[0], &p) { /* Replace the top of the queue if needed. */ if *num_pairs < max_num_pairs { pairs[*num_pairs] = pairs[0] @@ -72,7 +72,7 @@ func BrotliCompareAndPushToQueueCommand(out []HistogramCommand, cluster_size []u } } -func BrotliHistogramCombineCommand(out []HistogramCommand, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []HistogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { +func histogramCombineCommand(out []HistogramCommand, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { var cost_diff_threshold float64 = 0.0 var min_cluster_size uint = 1 var num_pairs uint = 0 @@ -83,7 +83,7 @@ func BrotliHistogramCombineCommand(out []HistogramCommand, cluster_size []uint32 for idx1 = 0; idx1 < num_clusters; idx1++ { var idx2 uint for idx2 = idx1 + 1; idx2 < num_clusters; idx2++ { - BrotliCompareAndPushToQueueCommand(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs) + compareAndPushToQueueCommand(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs) } } } @@ -123,15 +123,15 @@ func BrotliHistogramCombineCommand(out []HistogramCommand, cluster_size []uint32 /* Remove pairs intersecting the just combined best pair. */ var copy_to_idx uint = 0 for i = 0; i < num_pairs; i++ { - var p *HistogramPair = &pairs[i] + var p *histogramPair = &pairs[i] if p.idx1 == best_idx1 || p.idx2 == best_idx1 || p.idx1 == best_idx2 || p.idx2 == best_idx2 { /* Remove invalid pair from the queue. */ continue } - if HistogramPairIsLess(&pairs[0], p) { + if histogramPairIsLess(&pairs[0], p) { /* Replace the top of the queue if needed. */ - var front HistogramPair = pairs[0] + var front histogramPair = pairs[0] pairs[0] = *p pairs[copy_to_idx] = front } else { @@ -146,7 +146,7 @@ func BrotliHistogramCombineCommand(out []HistogramCommand, cluster_size []uint32 /* Push new pairs formed with the combined histogram to the heap. */ for i = 0; i < num_clusters; i++ { - BrotliCompareAndPushToQueueCommand(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs) + compareAndPushToQueueCommand(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs) } } @@ -154,7 +154,7 @@ func BrotliHistogramCombineCommand(out []HistogramCommand, cluster_size []uint32 } /* What is the bit cost of moving histogram from cur_symbol to candidate. */ -func BrotliHistogramBitCostDistanceCommand(histogram *HistogramCommand, candidate *HistogramCommand) float64 { +func histogramBitCostDistanceCommand(histogram *HistogramCommand, candidate *HistogramCommand) float64 { if histogram.total_count_ == 0 { return 0.0 } else { @@ -168,7 +168,7 @@ func BrotliHistogramBitCostDistanceCommand(histogram *HistogramCommand, candidat When called, clusters[0..num_clusters) contains the unique values from symbols[0..in_size), but this property is not preserved in this function. Note: we assume that out[]->bit_cost_ is already up-to-date. */ -func BrotliHistogramRemapCommand(in []HistogramCommand, in_size uint, clusters []uint32, num_clusters uint, out []HistogramCommand, symbols []uint32) { +func histogramRemapCommand(in []HistogramCommand, in_size uint, clusters []uint32, num_clusters uint, out []HistogramCommand, symbols []uint32) { var i uint for i = 0; i < in_size; i++ { var best_out uint32 @@ -177,10 +177,10 @@ func BrotliHistogramRemapCommand(in []HistogramCommand, in_size uint, clusters [ } else { best_out = symbols[i-1] } - var best_bits float64 = BrotliHistogramBitCostDistanceCommand(&in[i], &out[best_out]) + var best_bits float64 = histogramBitCostDistanceCommand(&in[i], &out[best_out]) var j uint for j = 0; j < num_clusters; j++ { - var cur_bits float64 = BrotliHistogramBitCostDistanceCommand(&in[i], &out[clusters[j]]) + var cur_bits float64 = histogramBitCostDistanceCommand(&in[i], &out[clusters[j]]) if cur_bits < best_bits { best_bits = cur_bits best_out = clusters[j] @@ -211,20 +211,20 @@ func BrotliHistogramRemapCommand(in []HistogramCommand, in_size uint, clusters [ increasing order. Returns N, the number of unique values in symbols[]. */ -var BrotliHistogramReindexCommand_kInvalidIndex uint32 = BROTLI_UINT32_MAX +var histogramReindexCommand_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func BrotliHistogramReindexCommand(out []HistogramCommand, symbols []uint32, length uint) uint { +func histogramReindexCommand(out []HistogramCommand, symbols []uint32, length uint) uint { var new_index []uint32 = make([]uint32, length) var next_index uint32 var tmp []HistogramCommand var i uint for i = 0; i < length; i++ { - new_index[i] = BrotliHistogramReindexCommand_kInvalidIndex + new_index[i] = histogramReindexCommand_kInvalidIndex } next_index = 0 for i = 0; i < length; i++ { - if new_index[symbols[i]] == BrotliHistogramReindexCommand_kInvalidIndex { + if new_index[symbols[i]] == histogramReindexCommand_kInvalidIndex { new_index[symbols[i]] = next_index next_index++ } @@ -253,13 +253,13 @@ func BrotliHistogramReindexCommand(out []HistogramCommand, symbols []uint32, len return uint(next_index) } -func BrotliClusterHistogramsCommand(in []HistogramCommand, in_size uint, max_histograms uint, out []HistogramCommand, out_size *uint, histogram_symbols []uint32) { +func clusterHistogramsCommand(in []HistogramCommand, in_size uint, max_histograms uint, out []HistogramCommand, out_size *uint, histogram_symbols []uint32) { var cluster_size []uint32 = make([]uint32, in_size) var clusters []uint32 = make([]uint32, in_size) var num_clusters uint = 0 var max_input_histograms uint = 64 var pairs_capacity uint = max_input_histograms * max_input_histograms / 2 - var pairs []HistogramPair = make([]HistogramPair, (pairs_capacity + 1)) + var pairs []histogramPair = make([]histogramPair, (pairs_capacity + 1)) var i uint /* For the first pass of clustering, we allow all pairs. */ @@ -281,7 +281,7 @@ func BrotliClusterHistogramsCommand(in []HistogramCommand, in_size uint, max_his clusters[num_clusters+j] = uint32(i + j) } - num_new_clusters = BrotliHistogramCombineCommand(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity) + num_new_clusters = histogramCombineCommand(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity) num_clusters += num_new_clusters } { @@ -295,11 +295,11 @@ func BrotliClusterHistogramsCommand(in []HistogramCommand, in_size uint, max_his } else { _new_size = pairs_capacity } - var new_array []HistogramPair + var new_array []histogramPair for _new_size < (max_num_pairs + 1) { _new_size *= 2 } - new_array = make([]HistogramPair, _new_size) + new_array = make([]histogramPair, _new_size) if pairs_capacity != 0 { copy(new_array, pairs[:pairs_capacity]) } @@ -309,17 +309,17 @@ func BrotliClusterHistogramsCommand(in []HistogramCommand, in_size uint, max_his } /* Collapse similar histograms. */ - num_clusters = BrotliHistogramCombineCommand(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs) + num_clusters = histogramCombineCommand(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs) } pairs = nil cluster_size = nil /* Find the optimal map from original histograms to the final ones. */ - BrotliHistogramRemapCommand(in, in_size, clusters, num_clusters, out, histogram_symbols) + histogramRemapCommand(in, in_size, clusters, num_clusters, out, histogram_symbols) clusters = nil /* Convert the context map to a canonical form. */ - *out_size = BrotliHistogramReindexCommand(out, histogram_symbols, in_size) + *out_size = histogramReindexCommand(out, histogram_symbols, in_size) } diff --git a/cluster_distance.go b/cluster_distance.go index 7f39cdf..355e64a 100644 --- a/cluster_distance.go +++ b/cluster_distance.go @@ -9,9 +9,9 @@ package brotli /* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */ -func BrotliCompareAndPushToQueueDistance(out []HistogramDistance, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []HistogramPair, num_pairs *uint) { +func compareAndPushToQueueDistance(out []HistogramDistance, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { var is_good_pair bool = false - var p HistogramPair + var p histogramPair p.idx2 = 0 p.idx1 = p.idx2 p.cost_combo = 0 @@ -28,7 +28,7 @@ func BrotliCompareAndPushToQueueDistance(out []HistogramDistance, cluster_size [ p.idx1 = idx1 p.idx2 = idx2 - p.cost_diff = 0.5 * ClusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2])) + p.cost_diff = 0.5 * clusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2])) p.cost_diff -= out[idx1].bit_cost_ p.cost_diff -= out[idx2].bit_cost_ @@ -57,7 +57,7 @@ func BrotliCompareAndPushToQueueDistance(out []HistogramDistance, cluster_size [ if is_good_pair { p.cost_diff += p.cost_combo - if *num_pairs > 0 && HistogramPairIsLess(&pairs[0], &p) { + if *num_pairs > 0 && histogramPairIsLess(&pairs[0], &p) { /* Replace the top of the queue if needed. */ if *num_pairs < max_num_pairs { pairs[*num_pairs] = pairs[0] @@ -72,7 +72,7 @@ func BrotliCompareAndPushToQueueDistance(out []HistogramDistance, cluster_size [ } } -func BrotliHistogramCombineDistance(out []HistogramDistance, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []HistogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { +func histogramCombineDistance(out []HistogramDistance, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { var cost_diff_threshold float64 = 0.0 var min_cluster_size uint = 1 var num_pairs uint = 0 @@ -83,7 +83,7 @@ func BrotliHistogramCombineDistance(out []HistogramDistance, cluster_size []uint for idx1 = 0; idx1 < num_clusters; idx1++ { var idx2 uint for idx2 = idx1 + 1; idx2 < num_clusters; idx2++ { - BrotliCompareAndPushToQueueDistance(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs) + compareAndPushToQueueDistance(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs) } } } @@ -123,15 +123,15 @@ func BrotliHistogramCombineDistance(out []HistogramDistance, cluster_size []uint /* Remove pairs intersecting the just combined best pair. */ var copy_to_idx uint = 0 for i = 0; i < num_pairs; i++ { - var p *HistogramPair = &pairs[i] + var p *histogramPair = &pairs[i] if p.idx1 == best_idx1 || p.idx2 == best_idx1 || p.idx1 == best_idx2 || p.idx2 == best_idx2 { /* Remove invalid pair from the queue. */ continue } - if HistogramPairIsLess(&pairs[0], p) { + if histogramPairIsLess(&pairs[0], p) { /* Replace the top of the queue if needed. */ - var front HistogramPair = pairs[0] + var front histogramPair = pairs[0] pairs[0] = *p pairs[copy_to_idx] = front } else { @@ -146,7 +146,7 @@ func BrotliHistogramCombineDistance(out []HistogramDistance, cluster_size []uint /* Push new pairs formed with the combined histogram to the heap. */ for i = 0; i < num_clusters; i++ { - BrotliCompareAndPushToQueueDistance(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs) + compareAndPushToQueueDistance(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs) } } @@ -154,7 +154,7 @@ func BrotliHistogramCombineDistance(out []HistogramDistance, cluster_size []uint } /* What is the bit cost of moving histogram from cur_symbol to candidate. */ -func BrotliHistogramBitCostDistanceDistance(histogram *HistogramDistance, candidate *HistogramDistance) float64 { +func histogramBitCostDistanceDistance(histogram *HistogramDistance, candidate *HistogramDistance) float64 { if histogram.total_count_ == 0 { return 0.0 } else { @@ -168,7 +168,7 @@ func BrotliHistogramBitCostDistanceDistance(histogram *HistogramDistance, candid When called, clusters[0..num_clusters) contains the unique values from symbols[0..in_size), but this property is not preserved in this function. Note: we assume that out[]->bit_cost_ is already up-to-date. */ -func BrotliHistogramRemapDistance(in []HistogramDistance, in_size uint, clusters []uint32, num_clusters uint, out []HistogramDistance, symbols []uint32) { +func histogramRemapDistance(in []HistogramDistance, in_size uint, clusters []uint32, num_clusters uint, out []HistogramDistance, symbols []uint32) { var i uint for i = 0; i < in_size; i++ { var best_out uint32 @@ -177,10 +177,10 @@ func BrotliHistogramRemapDistance(in []HistogramDistance, in_size uint, clusters } else { best_out = symbols[i-1] } - var best_bits float64 = BrotliHistogramBitCostDistanceDistance(&in[i], &out[best_out]) + var best_bits float64 = histogramBitCostDistanceDistance(&in[i], &out[best_out]) var j uint for j = 0; j < num_clusters; j++ { - var cur_bits float64 = BrotliHistogramBitCostDistanceDistance(&in[i], &out[clusters[j]]) + var cur_bits float64 = histogramBitCostDistanceDistance(&in[i], &out[clusters[j]]) if cur_bits < best_bits { best_bits = cur_bits best_out = clusters[j] @@ -211,20 +211,20 @@ func BrotliHistogramRemapDistance(in []HistogramDistance, in_size uint, clusters increasing order. Returns N, the number of unique values in symbols[]. */ -var BrotliHistogramReindexDistance_kInvalidIndex uint32 = BROTLI_UINT32_MAX +var histogramReindexDistance_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func BrotliHistogramReindexDistance(out []HistogramDistance, symbols []uint32, length uint) uint { +func histogramReindexDistance(out []HistogramDistance, symbols []uint32, length uint) uint { var new_index []uint32 = make([]uint32, length) var next_index uint32 var tmp []HistogramDistance var i uint for i = 0; i < length; i++ { - new_index[i] = BrotliHistogramReindexDistance_kInvalidIndex + new_index[i] = histogramReindexDistance_kInvalidIndex } next_index = 0 for i = 0; i < length; i++ { - if new_index[symbols[i]] == BrotliHistogramReindexDistance_kInvalidIndex { + if new_index[symbols[i]] == histogramReindexDistance_kInvalidIndex { new_index[symbols[i]] = next_index next_index++ } @@ -253,13 +253,13 @@ func BrotliHistogramReindexDistance(out []HistogramDistance, symbols []uint32, l return uint(next_index) } -func BrotliClusterHistogramsDistance(in []HistogramDistance, in_size uint, max_histograms uint, out []HistogramDistance, out_size *uint, histogram_symbols []uint32) { +func clusterHistogramsDistance(in []HistogramDistance, in_size uint, max_histograms uint, out []HistogramDistance, out_size *uint, histogram_symbols []uint32) { var cluster_size []uint32 = make([]uint32, in_size) var clusters []uint32 = make([]uint32, in_size) var num_clusters uint = 0 var max_input_histograms uint = 64 var pairs_capacity uint = max_input_histograms * max_input_histograms / 2 - var pairs []HistogramPair = make([]HistogramPair, (pairs_capacity + 1)) + var pairs []histogramPair = make([]histogramPair, (pairs_capacity + 1)) var i uint /* For the first pass of clustering, we allow all pairs. */ @@ -281,7 +281,7 @@ func BrotliClusterHistogramsDistance(in []HistogramDistance, in_size uint, max_h clusters[num_clusters+j] = uint32(i + j) } - num_new_clusters = BrotliHistogramCombineDistance(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity) + num_new_clusters = histogramCombineDistance(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity) num_clusters += num_new_clusters } { @@ -295,11 +295,11 @@ func BrotliClusterHistogramsDistance(in []HistogramDistance, in_size uint, max_h } else { _new_size = pairs_capacity } - var new_array []HistogramPair + var new_array []histogramPair for _new_size < (max_num_pairs + 1) { _new_size *= 2 } - new_array = make([]HistogramPair, _new_size) + new_array = make([]histogramPair, _new_size) if pairs_capacity != 0 { copy(new_array, pairs[:pairs_capacity]) } @@ -309,17 +309,17 @@ func BrotliClusterHistogramsDistance(in []HistogramDistance, in_size uint, max_h } /* Collapse similar histograms. */ - num_clusters = BrotliHistogramCombineDistance(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs) + num_clusters = histogramCombineDistance(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs) } pairs = nil cluster_size = nil /* Find the optimal map from original histograms to the final ones. */ - BrotliHistogramRemapDistance(in, in_size, clusters, num_clusters, out, histogram_symbols) + histogramRemapDistance(in, in_size, clusters, num_clusters, out, histogram_symbols) clusters = nil /* Convert the context map to a canonical form. */ - *out_size = BrotliHistogramReindexDistance(out, histogram_symbols, in_size) + *out_size = histogramReindexDistance(out, histogram_symbols, in_size) } diff --git a/cluster_literal.go b/cluster_literal.go index 3cb4acf..2d4d108 100644 --- a/cluster_literal.go +++ b/cluster_literal.go @@ -9,9 +9,9 @@ package brotli /* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */ -func BrotliCompareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []HistogramPair, num_pairs *uint) { +func compareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { var is_good_pair bool = false - var p HistogramPair + var p histogramPair p.idx2 = 0 p.idx1 = p.idx2 p.cost_combo = 0 @@ -28,7 +28,7 @@ func BrotliCompareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []u p.idx1 = idx1 p.idx2 = idx2 - p.cost_diff = 0.5 * ClusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2])) + p.cost_diff = 0.5 * clusterCostDiff(uint(cluster_size[idx1]), uint(cluster_size[idx2])) p.cost_diff -= out[idx1].bit_cost_ p.cost_diff -= out[idx2].bit_cost_ @@ -57,7 +57,7 @@ func BrotliCompareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []u if is_good_pair { p.cost_diff += p.cost_combo - if *num_pairs > 0 && HistogramPairIsLess(&pairs[0], &p) { + if *num_pairs > 0 && histogramPairIsLess(&pairs[0], &p) { /* Replace the top of the queue if needed. */ if *num_pairs < max_num_pairs { pairs[*num_pairs] = pairs[0] @@ -72,7 +72,7 @@ func BrotliCompareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []u } } -func BrotliHistogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []HistogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { +func histogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { var cost_diff_threshold float64 = 0.0 var min_cluster_size uint = 1 var num_pairs uint = 0 @@ -83,7 +83,7 @@ func BrotliHistogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32 for idx1 = 0; idx1 < num_clusters; idx1++ { var idx2 uint for idx2 = idx1 + 1; idx2 < num_clusters; idx2++ { - BrotliCompareAndPushToQueueLiteral(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs) + compareAndPushToQueueLiteral(out, cluster_size, clusters[idx1], clusters[idx2], max_num_pairs, pairs[0:], &num_pairs) } } } @@ -123,15 +123,15 @@ func BrotliHistogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32 /* Remove pairs intersecting the just combined best pair. */ var copy_to_idx uint = 0 for i = 0; i < num_pairs; i++ { - var p *HistogramPair = &pairs[i] + var p *histogramPair = &pairs[i] if p.idx1 == best_idx1 || p.idx2 == best_idx1 || p.idx1 == best_idx2 || p.idx2 == best_idx2 { /* Remove invalid pair from the queue. */ continue } - if HistogramPairIsLess(&pairs[0], p) { + if histogramPairIsLess(&pairs[0], p) { /* Replace the top of the queue if needed. */ - var front HistogramPair = pairs[0] + var front histogramPair = pairs[0] pairs[0] = *p pairs[copy_to_idx] = front } else { @@ -146,7 +146,7 @@ func BrotliHistogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32 /* Push new pairs formed with the combined histogram to the heap. */ for i = 0; i < num_clusters; i++ { - BrotliCompareAndPushToQueueLiteral(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs) + compareAndPushToQueueLiteral(out, cluster_size, best_idx1, clusters[i], max_num_pairs, pairs[0:], &num_pairs) } } @@ -154,7 +154,7 @@ func BrotliHistogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32 } /* What is the bit cost of moving histogram from cur_symbol to candidate. */ -func BrotliHistogramBitCostDistanceLiteral(histogram *HistogramLiteral, candidate *HistogramLiteral) float64 { +func histogramBitCostDistanceLiteral(histogram *HistogramLiteral, candidate *HistogramLiteral) float64 { if histogram.total_count_ == 0 { return 0.0 } else { @@ -168,7 +168,7 @@ func BrotliHistogramBitCostDistanceLiteral(histogram *HistogramLiteral, candidat When called, clusters[0..num_clusters) contains the unique values from symbols[0..in_size), but this property is not preserved in this function. Note: we assume that out[]->bit_cost_ is already up-to-date. */ -func BrotliHistogramRemapLiteral(in []HistogramLiteral, in_size uint, clusters []uint32, num_clusters uint, out []HistogramLiteral, symbols []uint32) { +func histogramRemapLiteral(in []HistogramLiteral, in_size uint, clusters []uint32, num_clusters uint, out []HistogramLiteral, symbols []uint32) { var i uint for i = 0; i < in_size; i++ { var best_out uint32 @@ -177,10 +177,10 @@ func BrotliHistogramRemapLiteral(in []HistogramLiteral, in_size uint, clusters [ } else { best_out = symbols[i-1] } - var best_bits float64 = BrotliHistogramBitCostDistanceLiteral(&in[i], &out[best_out]) + var best_bits float64 = histogramBitCostDistanceLiteral(&in[i], &out[best_out]) var j uint for j = 0; j < num_clusters; j++ { - var cur_bits float64 = BrotliHistogramBitCostDistanceLiteral(&in[i], &out[clusters[j]]) + var cur_bits float64 = histogramBitCostDistanceLiteral(&in[i], &out[clusters[j]]) if cur_bits < best_bits { best_bits = cur_bits best_out = clusters[j] @@ -211,20 +211,20 @@ func BrotliHistogramRemapLiteral(in []HistogramLiteral, in_size uint, clusters [ increasing order. Returns N, the number of unique values in symbols[]. */ -var BrotliHistogramReindexLiteral_kInvalidIndex uint32 = BROTLI_UINT32_MAX +var histogramReindexLiteral_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func BrotliHistogramReindexLiteral(out []HistogramLiteral, symbols []uint32, length uint) uint { +func histogramReindexLiteral(out []HistogramLiteral, symbols []uint32, length uint) uint { var new_index []uint32 = make([]uint32, length) var next_index uint32 var tmp []HistogramLiteral var i uint for i = 0; i < length; i++ { - new_index[i] = BrotliHistogramReindexLiteral_kInvalidIndex + new_index[i] = histogramReindexLiteral_kInvalidIndex } next_index = 0 for i = 0; i < length; i++ { - if new_index[symbols[i]] == BrotliHistogramReindexLiteral_kInvalidIndex { + if new_index[symbols[i]] == histogramReindexLiteral_kInvalidIndex { new_index[symbols[i]] = next_index next_index++ } @@ -253,13 +253,13 @@ func BrotliHistogramReindexLiteral(out []HistogramLiteral, symbols []uint32, len return uint(next_index) } -func BrotliClusterHistogramsLiteral(in []HistogramLiteral, in_size uint, max_histograms uint, out []HistogramLiteral, out_size *uint, histogram_symbols []uint32) { +func clusterHistogramsLiteral(in []HistogramLiteral, in_size uint, max_histograms uint, out []HistogramLiteral, out_size *uint, histogram_symbols []uint32) { var cluster_size []uint32 = make([]uint32, in_size) var clusters []uint32 = make([]uint32, in_size) var num_clusters uint = 0 var max_input_histograms uint = 64 var pairs_capacity uint = max_input_histograms * max_input_histograms / 2 - var pairs []HistogramPair = make([]HistogramPair, (pairs_capacity + 1)) + var pairs []histogramPair = make([]histogramPair, (pairs_capacity + 1)) var i uint /* For the first pass of clustering, we allow all pairs. */ @@ -281,7 +281,7 @@ func BrotliClusterHistogramsLiteral(in []HistogramLiteral, in_size uint, max_his clusters[num_clusters+j] = uint32(i + j) } - num_new_clusters = BrotliHistogramCombineLiteral(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity) + num_new_clusters = histogramCombineLiteral(out, cluster_size, histogram_symbols[i:], clusters[num_clusters:], pairs, num_to_combine, num_to_combine, max_histograms, pairs_capacity) num_clusters += num_new_clusters } { @@ -295,11 +295,11 @@ func BrotliClusterHistogramsLiteral(in []HistogramLiteral, in_size uint, max_his } else { _new_size = pairs_capacity } - var new_array []HistogramPair + var new_array []histogramPair for _new_size < (max_num_pairs + 1) { _new_size *= 2 } - new_array = make([]HistogramPair, _new_size) + new_array = make([]histogramPair, _new_size) if pairs_capacity != 0 { copy(new_array, pairs[:pairs_capacity]) } @@ -309,17 +309,17 @@ func BrotliClusterHistogramsLiteral(in []HistogramLiteral, in_size uint, max_his } /* Collapse similar histograms. */ - num_clusters = BrotliHistogramCombineLiteral(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs) + num_clusters = histogramCombineLiteral(out, cluster_size, histogram_symbols, clusters, pairs, num_clusters, in_size, max_histograms, max_num_pairs) } pairs = nil cluster_size = nil /* Find the optimal map from original histograms to the final ones. */ - BrotliHistogramRemapLiteral(in, in_size, clusters, num_clusters, out, histogram_symbols) + histogramRemapLiteral(in, in_size, clusters, num_clusters, out, histogram_symbols) clusters = nil /* Convert the context map to a canonical form. */ - *out_size = BrotliHistogramReindexLiteral(out, histogram_symbols, in_size) + *out_size = histogramReindexLiteral(out, histogram_symbols, in_size) } diff --git a/command.go b/command.go index 6a5616c..04c8da6 100644 --- a/command.go +++ b/command.go @@ -108,7 +108,7 @@ var kCopyExtra = []uint32{ 24, } -func GetInsertLengthCode(insertlen uint) uint16 { +func getInsertLengthCode(insertlen uint) uint16 { if insertlen < 6 { return uint16(insertlen) } else if insertlen < 130 { @@ -125,7 +125,7 @@ func GetInsertLengthCode(insertlen uint) uint16 { } } -func GetCopyLengthCode(copylen uint) uint16 { +func getCopyLengthCode(copylen uint) uint16 { if copylen < 10 { return uint16(copylen - 2) } else if copylen < 134 { @@ -138,7 +138,7 @@ func GetCopyLengthCode(copylen uint) uint16 { } } -func CombineLengthCodes(inscode uint16, copycode uint16, use_last_distance bool) uint16 { +func combineLengthCodes(inscode uint16, copycode uint16, use_last_distance bool) uint16 { var bits64 uint16 = uint16(copycode&0x7 | (inscode&0x7)<<3) if use_last_distance && inscode < 8 && copycode < 16 { if copycode < 8 { @@ -163,29 +163,29 @@ func CombineLengthCodes(inscode uint16, copycode uint16, use_last_distance bool) } } -func GetLengthCode(insertlen uint, copylen uint, use_last_distance bool, code *uint16) { - var inscode uint16 = GetInsertLengthCode(insertlen) - var copycode uint16 = GetCopyLengthCode(copylen) - *code = CombineLengthCodes(inscode, copycode, use_last_distance) +func getLengthCode(insertlen uint, copylen uint, use_last_distance bool, code *uint16) { + var inscode uint16 = getInsertLengthCode(insertlen) + var copycode uint16 = getCopyLengthCode(copylen) + *code = combineLengthCodes(inscode, copycode, use_last_distance) } -func GetInsertBase(inscode uint16) uint32 { +func getInsertBase(inscode uint16) uint32 { return kInsBase[inscode] } -func GetInsertExtra(inscode uint16) uint32 { +func getInsertExtra(inscode uint16) uint32 { return kInsExtra[inscode] } -func GetCopyBase(copycode uint16) uint32 { +func getCopyBase(copycode uint16) uint32 { return kCopyBase[copycode] } -func GetCopyExtra(copycode uint16) uint32 { +func getCopyExtra(copycode uint16) uint32 { return kCopyExtra[copycode] } -type Command struct { +type command struct { insert_len_ uint32 copy_len_ uint32 dist_extra_ uint32 @@ -194,7 +194,7 @@ type Command struct { } /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */ -func InitCommand(self *Command, dist *BrotliDistanceParams, insertlen uint, copylen uint, copylen_code_delta int, distance_code uint) { +func initCommand(self *command, dist *BrotliDistanceParams, insertlen uint, copylen uint, copylen_code_delta int, distance_code uint) { /* Don't rely on signed int representation, use honest casts. */ var delta uint32 = uint32(byte(int8(copylen_code_delta))) self.insert_len_ = uint32(insertlen) @@ -205,33 +205,33 @@ func InitCommand(self *Command, dist *BrotliDistanceParams, insertlen uint, copy clustering if needed. */ PrefixEncodeCopyDistance(distance_code, uint(dist.num_direct_distance_codes), uint(dist.distance_postfix_bits), &self.dist_prefix_, &self.dist_extra_) - GetLengthCode(insertlen, uint(int(copylen)+copylen_code_delta), (self.dist_prefix_&0x3FF == 0), &self.cmd_prefix_) + getLengthCode(insertlen, uint(int(copylen)+copylen_code_delta), (self.dist_prefix_&0x3FF == 0), &self.cmd_prefix_) } -func InitInsertCommand(self *Command, insertlen uint) { +func initInsertCommand(self *command, insertlen uint) { self.insert_len_ = uint32(insertlen) self.copy_len_ = 4 << 25 self.dist_extra_ = 0 - self.dist_prefix_ = BROTLI_NUM_DISTANCE_SHORT_CODES - GetLengthCode(insertlen, 4, false, &self.cmd_prefix_) + self.dist_prefix_ = numDistanceShortCodes + getLengthCode(insertlen, 4, false, &self.cmd_prefix_) } -func CommandRestoreDistanceCode(self *Command, dist *BrotliDistanceParams) uint32 { - if uint32(self.dist_prefix_&0x3FF) < BROTLI_NUM_DISTANCE_SHORT_CODES+dist.num_direct_distance_codes { +func commandRestoreDistanceCode(self *command, dist *BrotliDistanceParams) uint32 { + if uint32(self.dist_prefix_&0x3FF) < numDistanceShortCodes+dist.num_direct_distance_codes { return uint32(self.dist_prefix_) & 0x3FF } else { var dcode uint32 = uint32(self.dist_prefix_) & 0x3FF var nbits uint32 = uint32(self.dist_prefix_) >> 10 var extra uint32 = self.dist_extra_ var postfix_mask uint32 = (1 << dist.distance_postfix_bits) - 1 - var hcode uint32 = (dcode - dist.num_direct_distance_codes - BROTLI_NUM_DISTANCE_SHORT_CODES) >> dist.distance_postfix_bits - var lcode uint32 = (dcode - dist.num_direct_distance_codes - BROTLI_NUM_DISTANCE_SHORT_CODES) & postfix_mask + var hcode uint32 = (dcode - dist.num_direct_distance_codes - numDistanceShortCodes) >> dist.distance_postfix_bits + var lcode uint32 = (dcode - dist.num_direct_distance_codes - numDistanceShortCodes) & postfix_mask var offset uint32 = ((2 + (hcode & 1)) << nbits) - 4 - return ((offset + extra) << dist.distance_postfix_bits) + lcode + dist.num_direct_distance_codes + BROTLI_NUM_DISTANCE_SHORT_CODES + return ((offset + extra) << dist.distance_postfix_bits) + lcode + dist.num_direct_distance_codes + numDistanceShortCodes } } -func CommandDistanceContext(self *Command) uint32 { +func commandDistanceContext(self *command) uint32 { var r uint32 = uint32(self.cmd_prefix_) >> 6 var c uint32 = uint32(self.cmd_prefix_) & 7 if (r == 0 || r == 2 || r == 4 || r == 7) && (c <= 2) { @@ -241,11 +241,11 @@ func CommandDistanceContext(self *Command) uint32 { return 3 } -func CommandCopyLen(self *Command) uint32 { +func commandCopyLen(self *command) uint32 { return self.copy_len_ & 0x1FFFFFF } -func CommandCopyLenCode(self *Command) uint32 { +func commandCopyLenCode(self *command) uint32 { var modifier uint32 = self.copy_len_ >> 25 var delta int32 = int32(int8(byte(modifier | (modifier&0x40)<<1))) return uint32(int32(self.copy_len_&0x1FFFFFF) + delta) diff --git a/compress_fragment.go b/compress_fragment.go index 9f389b6..02ca19a 100644 --- a/compress_fragment.go +++ b/compress_fragment.go @@ -48,12 +48,12 @@ import "encoding/binary" REQUIRES: "table_size" is an odd (9, 11, 13, 15) power of two OUTPUT: maximal copy distance <= |input_size| OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */ -func Hash5(p []byte, shift uint) uint32 { +func hash5(p []byte, shift uint) uint32 { var h uint64 = (binary.LittleEndian.Uint64(p) << 24) * uint64(kHashMul32_a) return uint32(h >> shift) } -func HashBytesAtOffset5(v uint64, offset int, shift uint) uint32 { +func hashBytesAtOffset5(v uint64, offset int, shift uint) uint32 { assert(offset >= 0) assert(offset <= 3) { @@ -62,7 +62,7 @@ func HashBytesAtOffset5(v uint64, offset int, shift uint) uint32 { } } -func IsMatch5(p1 []byte, p2 []byte) bool { +func isMatch5(p1 []byte, p2 []byte) bool { var i int for i = 0; i < 5; i++ { if p1[i] != p2[i] { @@ -81,7 +81,7 @@ func IsMatch5(p1 []byte, p2 []byte) bool { and thus have to assign a non-zero depth for each literal. Returns estimated compression ratio millibytes/char for encoding given input with generated code. */ -func BuildAndStoreLiteralPrefixCode(input []byte, input_size uint, depths []byte, bits []uint16, storage_ix *uint, storage []byte) uint { +func buildAndStoreLiteralPrefixCode(input []byte, input_size uint, depths []byte, bits []uint16, storage_ix *uint, storage []byte) uint { var histogram = [256]uint32{0} var histogram_total uint var i uint @@ -117,7 +117,7 @@ func BuildAndStoreLiteralPrefixCode(input []byte, input_size uint, depths []byte } } - BrotliBuildAndStoreHuffmanTreeFast(histogram[:], histogram_total, /* max_bits = */ + buildAndStoreHuffmanTreeFast(histogram[:], histogram_total, /* max_bits = */ 8, depths, bits, storage_ix, storage) { var literal_ratio uint = 0 @@ -134,9 +134,9 @@ func BuildAndStoreLiteralPrefixCode(input []byte, input_size uint, depths []byte /* Builds a command and distance prefix code (each 64 symbols) into "depth" and "bits" based on "histogram" and stores it into the bit stream. */ -func BuildAndStoreCommandPrefixCode1(histogram []uint32, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { +func buildAndStoreCommandPrefixCode1(histogram []uint32, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { var tree [129]HuffmanTree - var cmd_depth = [BROTLI_NUM_COMMAND_SYMBOLS]byte{0} + var cmd_depth = [numCommandSymbols]byte{0} /* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */ var cmd_bits [64]uint16 @@ -181,14 +181,14 @@ func BuildAndStoreCommandPrefixCode1(histogram []uint32, depth []byte, bits []ui cmd_depth[448+8*i] = depth[56+i] } - BrotliStoreHuffmanTree(cmd_depth[:], BROTLI_NUM_COMMAND_SYMBOLS, tree[:], storage_ix, storage) + storeHuffmanTree(cmd_depth[:], numCommandSymbols, tree[:], storage_ix, storage) } - BrotliStoreHuffmanTree(depth[64:], 64, tree[:], storage_ix, storage) + storeHuffmanTree(depth[64:], 64, tree[:], storage_ix, storage) } /* REQUIRES: insertlen < 6210 */ -func EmitInsertLen1(insertlen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { +func emitInsertLen1(insertlen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { if insertlen < 6 { var code uint = insertlen + 40 BrotliWriteBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage) @@ -215,7 +215,7 @@ func EmitInsertLen1(insertlen uint, depth []byte, bits []uint16, histo []uint32, } } -func EmitLongInsertLen(insertlen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { +func emitLongInsertLen(insertlen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { if insertlen < 22594 { BrotliWriteBits(uint(depth[62]), uint64(bits[62]), storage_ix, storage) BrotliWriteBits(14, uint64(insertlen)-6210, storage_ix, storage) @@ -227,7 +227,7 @@ func EmitLongInsertLen(insertlen uint, depth []byte, bits []uint16, histo []uint } } -func EmitCopyLen1(copylen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { +func emitCopyLen1(copylen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { if copylen < 10 { BrotliWriteBits(uint(depth[copylen+14]), uint64(bits[copylen+14]), storage_ix, storage) histo[copylen+14]++ @@ -253,7 +253,7 @@ func EmitCopyLen1(copylen uint, depth []byte, bits []uint16, histo []uint32, sto } } -func EmitCopyLenLastDistance1(copylen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { +func emitCopyLenLastDistance1(copylen uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { if copylen < 12 { BrotliWriteBits(uint(depth[copylen-4]), uint64(bits[copylen-4]), storage_ix, storage) histo[copylen-4]++ @@ -291,7 +291,7 @@ func EmitCopyLenLastDistance1(copylen uint, depth []byte, bits []uint16, histo [ } } -func EmitDistance1(distance uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { +func emitDistance1(distance uint, depth []byte, bits []uint16, histo []uint32, storage_ix *uint, storage []byte) { var d uint = distance + 3 var nbits uint32 = Log2FloorNonZero(d) - 1 var prefix uint = (d >> nbits) & 1 @@ -302,7 +302,7 @@ func EmitDistance1(distance uint, depth []byte, bits []uint16, histo []uint32, s histo[distcode]++ } -func EmitLiterals(input []byte, len uint, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { +func emitLiterals(input []byte, len uint, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { var j uint for j = 0; j < len; j++ { var lit byte = input[j] @@ -311,7 +311,7 @@ func EmitLiterals(input []byte, len uint, depth []byte, bits []uint16, storage_i } /* REQUIRES: len <= 1 << 24. */ -func BrotliStoreMetaBlockHeader1(len uint, is_uncompressed bool, storage_ix *uint, storage []byte) { +func storeMetaBlockHeader1(len uint, is_uncompressed bool, storage_ix *uint, storage []byte) { var nibbles uint = 6 /* ISLAST */ @@ -330,7 +330,7 @@ func BrotliStoreMetaBlockHeader1(len uint, is_uncompressed bool, storage_ix *uin BrotliWriteSingleBit(is_uncompressed, storage_ix, storage) } -func UpdateBits(n_bits uint, bits uint32, pos uint, array []byte) { +func updateBits(n_bits uint, bits uint32, pos uint, array []byte) { for n_bits > 0 { var byte_pos uint = pos >> 3 var n_unchanged_bits uint = pos & 7 @@ -346,23 +346,23 @@ func UpdateBits(n_bits uint, bits uint32, pos uint, array []byte) { } } -func RewindBitPosition1(new_storage_ix uint, storage_ix *uint, storage []byte) { +func rewindBitPosition1(new_storage_ix uint, storage_ix *uint, storage []byte) { var bitpos uint = new_storage_ix & 7 var mask uint = (1 << bitpos) - 1 storage[new_storage_ix>>3] &= byte(mask) *storage_ix = new_storage_ix } -var ShouldMergeBlock_kSampleRate uint = 43 +var shouldMergeBlock_kSampleRate uint = 43 -func ShouldMergeBlock(data []byte, len uint, depths []byte) bool { +func shouldMergeBlock(data []byte, len uint, depths []byte) bool { var histo = [256]uint{0} var i uint - for i = 0; i < len; i += ShouldMergeBlock_kSampleRate { + for i = 0; i < len; i += shouldMergeBlock_kSampleRate { histo[data[i]]++ } { - var total uint = (len + ShouldMergeBlock_kSampleRate - 1) / ShouldMergeBlock_kSampleRate + var total uint = (len + shouldMergeBlock_kSampleRate - 1) / shouldMergeBlock_kSampleRate var r float64 = (FastLog2(total)+0.5)*float64(total) + 200 for i = 0; i < 256; i++ { r -= float64(histo[i]) * (float64(depths[i]) + FastLog2(histo[i])) @@ -372,7 +372,7 @@ func ShouldMergeBlock(data []byte, len uint, depths []byte) bool { } } -func ShouldUseUncompressedMode(metablock_start []byte, next_emit []byte, insertlen uint, literal_ratio uint) bool { +func shouldUseUncompressedMode(metablock_start []byte, next_emit []byte, insertlen uint, literal_ratio uint) bool { var compressed uint = uint(-cap(next_emit) + cap(metablock_start)) if compressed*50 > insertlen { return false @@ -381,10 +381,10 @@ func ShouldUseUncompressedMode(metablock_start []byte, next_emit []byte, insertl } } -func EmitUncompressedMetaBlock1(begin []byte, end []byte, storage_ix_start uint, storage_ix *uint, storage []byte) { +func emitUncompressedMetaBlock1(begin []byte, end []byte, storage_ix_start uint, storage_ix *uint, storage []byte) { var len uint = uint(-cap(end) + cap(begin)) - RewindBitPosition1(storage_ix_start, storage_ix, storage) - BrotliStoreMetaBlockHeader1(uint(len), true, storage_ix, storage) + rewindBitPosition1(storage_ix_start, storage_ix, storage) + storeMetaBlockHeader1(uint(len), true, storage_ix, storage) *storage_ix = (*storage_ix + 7) &^ 7 copy(storage[*storage_ix>>3:], begin[:len]) *storage_ix += uint(len << 3) @@ -522,10 +522,10 @@ var kCmdHistoSeed = [128]uint32{ 0, } -var BrotliCompressFragmentFastImpl_kFirstBlockSize uint = 3 << 15 -var BrotliCompressFragmentFastImpl_kMergeBlockSize uint = 1 << 16 +var compressFragmentFastImpl_kFirstBlockSize uint = 3 << 15 +var compressFragmentFastImpl_kMergeBlockSize uint = 1 << 16 -func BrotliCompressFragmentFastImpl(in []byte, input_size uint, is_last bool, table []int, table_bits uint, cmd_depth []byte, cmd_bits []uint16, cmd_code_numbits *uint, cmd_code []byte, storage_ix *uint, storage []byte) { +func compressFragmentFastImpl(in []byte, input_size uint, is_last bool, table []int, table_bits uint, cmd_depth []byte, cmd_bits []uint16, cmd_code_numbits *uint, cmd_code []byte, storage_ix *uint, storage []byte) { var cmd_histo [128]uint32 var ip_end int var next_emit int = 0 @@ -534,7 +534,7 @@ func BrotliCompressFragmentFastImpl(in []byte, input_size uint, is_last bool, ta var kInputMarginBytes uint = BROTLI_WINDOW_GAP var kMinMatchLen uint = 5 var metablock_start int = input - var block_size uint = brotli_min_size_t(input_size, BrotliCompressFragmentFastImpl_kFirstBlockSize) + var block_size uint = brotli_min_size_t(input_size, compressFragmentFastImpl_kFirstBlockSize) var total_block_size uint = block_size var mlen_storage_ix uint = *storage_ix + 3 var lit_depth [256]byte @@ -553,12 +553,12 @@ func BrotliCompressFragmentFastImpl(in []byte, input_size uint, is_last bool, ta /* Save the bit position of the MLEN field of the meta-block header, so that we can update it later if we decide to extend this meta-block. */ - BrotliStoreMetaBlockHeader1(block_size, false, storage_ix, storage) + storeMetaBlockHeader1(block_size, false, storage_ix, storage) /* No block splits, no contexts. */ BrotliWriteBits(13, 0, storage_ix, storage) - literal_ratio = BuildAndStoreLiteralPrefixCode(in[input:], block_size, lit_depth[:], lit_bits[:], storage_ix, storage) + literal_ratio = buildAndStoreLiteralPrefixCode(in[input:], block_size, lit_depth[:], lit_bits[:], storage_ix, storage) { /* Store the pre-compressed command and distance prefix codes. */ var i uint @@ -592,7 +592,7 @@ emit_commands: var next_hash uint32 ip++ - for next_hash = Hash5(in[ip:], shift); ; { + for next_hash = hash5(in[ip:], shift); ; { var skip uint32 = 32 var next_ip int = ip /* Step 1: Scan forward in the input looking for a 5-byte-long match. @@ -619,16 +619,16 @@ emit_commands: var hash uint32 = next_hash var bytes_between_hash_lookups uint32 = skip >> 5 skip++ - assert(hash == Hash5(in[next_ip:], shift)) + assert(hash == hash5(in[next_ip:], shift)) ip = next_ip next_ip = int(uint32(ip) + bytes_between_hash_lookups) if next_ip > ip_limit { goto emit_remainder } - next_hash = Hash5(in[next_ip:], shift) + next_hash = hash5(in[next_ip:], shift) candidate = ip - last_distance - if IsMatch5(in[ip:], in[candidate:]) { + if isMatch5(in[ip:], in[candidate:]) { if candidate < ip { table[hash] = int(ip - base_ip) break @@ -640,14 +640,14 @@ emit_commands: assert(candidate < ip) table[hash] = int(ip - base_ip) - if !(!IsMatch5(in[ip:], in[candidate:])) { + if !(!isMatch5(in[ip:], in[candidate:])) { break } } /* Check copy distance. If candidate is not feasible, continue search. Checking is done outside of hot loop to reduce overhead. */ - if ip-candidate > MAX_DISTANCE { + if ip-candidate > maxDistance { goto trawl } @@ -666,27 +666,27 @@ emit_commands: var insert uint = uint(base - next_emit) ip += int(matched) if insert < 6210 { - EmitInsertLen1(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) - } else if ShouldUseUncompressedMode(in[metablock_start:], in[next_emit:], insert, literal_ratio) { - EmitUncompressedMetaBlock1(in[metablock_start:], in[base:], mlen_storage_ix-3, storage_ix, storage) + emitInsertLen1(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + } else if shouldUseUncompressedMode(in[metablock_start:], in[next_emit:], insert, literal_ratio) { + emitUncompressedMetaBlock1(in[metablock_start:], in[base:], mlen_storage_ix-3, storage_ix, storage) input_size -= uint(base - input) input = base next_emit = input goto next_block } else { - EmitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + emitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) } - EmitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage) + emitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage) if distance == last_distance { BrotliWriteBits(uint(cmd_depth[64]), uint64(cmd_bits[64]), storage_ix, storage) cmd_histo[64]++ } else { - EmitDistance1(uint(distance), cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + emitDistance1(uint(distance), cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) last_distance = distance } - EmitCopyLenLastDistance1(matched, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + emitCopyLenLastDistance1(matched, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) next_emit = ip if ip >= ip_limit { @@ -698,12 +698,12 @@ emit_commands: within the last copy. */ { var input_bytes uint64 = binary.LittleEndian.Uint64(in[ip-3:]) - var prev_hash uint32 = HashBytesAtOffset5(input_bytes, 0, shift) - var cur_hash uint32 = HashBytesAtOffset5(input_bytes, 3, shift) + var prev_hash uint32 = hashBytesAtOffset5(input_bytes, 0, shift) + var cur_hash uint32 = hashBytesAtOffset5(input_bytes, 3, shift) table[prev_hash] = int(ip - base_ip - 3) - prev_hash = HashBytesAtOffset5(input_bytes, 1, shift) + prev_hash = hashBytesAtOffset5(input_bytes, 1, shift) table[prev_hash] = int(ip - base_ip - 2) - prev_hash = HashBytesAtOffset5(input_bytes, 2, shift) + prev_hash = hashBytesAtOffset5(input_bytes, 2, shift) table[prev_hash] = int(ip - base_ip - 1) candidate = base_ip + table[cur_hash] @@ -711,19 +711,19 @@ emit_commands: } } - for IsMatch5(in[ip:], in[candidate:]) { + for isMatch5(in[ip:], in[candidate:]) { var base int = ip /* We have a 5-byte match at ip, and no need to emit any literal bytes prior to ip. */ var matched uint = 5 + FindMatchLengthWithLimit(in[candidate+5:], in[ip+5:], uint(ip_end-ip)-5) - if ip-candidate > MAX_DISTANCE { + if ip-candidate > maxDistance { break } ip += int(matched) last_distance = int(base - candidate) /* > 0 */ - EmitCopyLen1(matched, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) - EmitDistance1(uint(last_distance), cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + emitCopyLen1(matched, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + emitDistance1(uint(last_distance), cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) next_emit = ip if ip >= ip_limit { @@ -735,12 +735,12 @@ emit_commands: within the last copy. */ { var input_bytes uint64 = binary.LittleEndian.Uint64(in[ip-3:]) - var prev_hash uint32 = HashBytesAtOffset5(input_bytes, 0, shift) - var cur_hash uint32 = HashBytesAtOffset5(input_bytes, 3, shift) + var prev_hash uint32 = hashBytesAtOffset5(input_bytes, 0, shift) + var cur_hash uint32 = hashBytesAtOffset5(input_bytes, 3, shift) table[prev_hash] = int(ip - base_ip - 3) - prev_hash = HashBytesAtOffset5(input_bytes, 1, shift) + prev_hash = hashBytesAtOffset5(input_bytes, 1, shift) table[prev_hash] = int(ip - base_ip - 2) - prev_hash = HashBytesAtOffset5(input_bytes, 2, shift) + prev_hash = hashBytesAtOffset5(input_bytes, 2, shift) table[prev_hash] = int(ip - base_ip - 1) candidate = base_ip + table[cur_hash] @@ -749,7 +749,7 @@ emit_commands: } ip++ - next_hash = Hash5(in[ip:], shift) + next_hash = hash5(in[ip:], shift) } } @@ -757,11 +757,11 @@ emit_remainder: assert(next_emit <= ip_end) input += int(block_size) input_size -= block_size - block_size = brotli_min_size_t(input_size, BrotliCompressFragmentFastImpl_kMergeBlockSize) + block_size = brotli_min_size_t(input_size, compressFragmentFastImpl_kMergeBlockSize) /* Decide if we want to continue this meta-block instead of emitting the last insert-only command. */ - if input_size > 0 && total_block_size+block_size <= 1<<20 && ShouldMergeBlock(in[input:], block_size, lit_depth[:]) { + if input_size > 0 && total_block_size+block_size <= 1<<20 && shouldMergeBlock(in[input:], block_size, lit_depth[:]) { assert(total_block_size > 1<<16) /* Update the size of the current meta-block and continue emitting commands. @@ -769,7 +769,7 @@ emit_remainder: nibbles. */ total_block_size += block_size - UpdateBits(20, uint32(total_block_size-1), mlen_storage_ix, storage) + updateBits(20, uint32(total_block_size-1), mlen_storage_ix, storage) goto emit_commands } @@ -777,13 +777,13 @@ emit_remainder: if next_emit < ip_end { var insert uint = uint(ip_end - next_emit) if insert < 6210 { - EmitInsertLen1(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) - EmitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage) - } else if ShouldUseUncompressedMode(in[metablock_start:], in[next_emit:], insert, literal_ratio) { - EmitUncompressedMetaBlock1(in[metablock_start:], in[ip_end:], mlen_storage_ix-3, storage_ix, storage) + emitInsertLen1(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + emitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage) + } else if shouldUseUncompressedMode(in[metablock_start:], in[next_emit:], insert, literal_ratio) { + emitUncompressedMetaBlock1(in[metablock_start:], in[ip_end:], mlen_storage_ix-3, storage_ix, storage) } else { - EmitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) - EmitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage) + emitLongInsertLen(insert, cmd_depth, cmd_bits, cmd_histo[:], storage_ix, storage) + emitLiterals(in[next_emit:], insert, lit_depth[:], lit_bits[:], storage_ix, storage) } } @@ -794,20 +794,20 @@ emit_remainder: next_block: if input_size > 0 { metablock_start = input - block_size = brotli_min_size_t(input_size, BrotliCompressFragmentFastImpl_kFirstBlockSize) + block_size = brotli_min_size_t(input_size, compressFragmentFastImpl_kFirstBlockSize) total_block_size = block_size /* Save the bit position of the MLEN field of the meta-block header, so that we can update it later if we decide to extend this meta-block. */ mlen_storage_ix = *storage_ix + 3 - BrotliStoreMetaBlockHeader1(block_size, false, storage_ix, storage) + storeMetaBlockHeader1(block_size, false, storage_ix, storage) /* No block splits, no contexts. */ BrotliWriteBits(13, 0, storage_ix, storage) - literal_ratio = BuildAndStoreLiteralPrefixCode(in[input:], block_size, lit_depth[:], lit_bits[:], storage_ix, storage) - BuildAndStoreCommandPrefixCode1(cmd_histo[:], cmd_depth, cmd_bits, storage_ix, storage) + literal_ratio = buildAndStoreLiteralPrefixCode(in[input:], block_size, lit_depth[:], lit_bits[:], storage_ix, storage) + buildAndStoreCommandPrefixCode1(cmd_histo[:], cmd_depth, cmd_bits, storage_ix, storage) goto emit_commands } @@ -817,11 +817,11 @@ next_block: cmd_code[0] = 0 *cmd_code_numbits = 0 - BuildAndStoreCommandPrefixCode1(cmd_histo[:], cmd_depth, cmd_bits, cmd_code_numbits, cmd_code) + buildAndStoreCommandPrefixCode1(cmd_histo[:], cmd_depth, cmd_bits, cmd_code_numbits, cmd_code) } } -func BrotliCompressFragmentFast(input []byte, input_size uint, is_last bool, table []int, table_size uint, cmd_depth []byte, cmd_bits []uint16, cmd_code_numbits *uint, cmd_code []byte, storage_ix *uint, storage []byte) { +func compressFragmentFast(input []byte, input_size uint, is_last bool, table []int, table_size uint, cmd_depth []byte, cmd_bits []uint16, cmd_code_numbits *uint, cmd_code []byte, storage_ix *uint, storage []byte) { var initial_storage_ix uint = *storage_ix var table_bits uint = uint(Log2FloorNonZero(table_size)) @@ -833,11 +833,11 @@ func BrotliCompressFragmentFast(input []byte, input_size uint, is_last bool, tab return } - BrotliCompressFragmentFastImpl(input, input_size, is_last, table, table_bits, cmd_depth, cmd_bits, cmd_code_numbits, cmd_code, storage_ix, storage) + compressFragmentFastImpl(input, input_size, is_last, table, table_bits, cmd_depth, cmd_bits, cmd_code_numbits, cmd_code, storage_ix, storage) /* If output is larger than single uncompressed block, rewrite it. */ if *storage_ix-initial_storage_ix > 31+(input_size<<3) { - EmitUncompressedMetaBlock1(input, input[input_size:], initial_storage_ix, storage_ix, storage) + emitUncompressedMetaBlock1(input, input[input_size:], initial_storage_ix, storage_ix, storage) } if is_last { diff --git a/compress_fragment_common.go b/compress_fragment_common.go index 9df6bbd..1cdd7b0 100644 --- a/compress_fragment_common.go +++ b/compress_fragment_common.go @@ -13,7 +13,7 @@ package brotli REQUIRES: "table_size" is a power of two OUTPUT: maximal copy distance <= |input_size| OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */ -const MAX_DISTANCE = 262128 +const maxDistance = 262128 /* kHashMul32_a multiplier has these properties: * The multiplier must be odd. Otherwise we may lose the highest bit. diff --git a/compress_fragment_two_pass.go b/compress_fragment_two_pass.go index 23ad91d..dabf92a 100644 --- a/compress_fragment_two_pass.go +++ b/compress_fragment_two_pass.go @@ -26,12 +26,12 @@ import "encoding/binary" on the actual command and literal byte histograms. */ var kCompressFragmentTwoPassBlockSize uint = 1 << 17 -func Hash1(p []byte, shift uint, length uint) uint32 { +func hash1(p []byte, shift uint, length uint) uint32 { var h uint64 = (binary.LittleEndian.Uint64(p) << ((8 - length) * 8)) * uint64(kHashMul32_a) return uint32(h >> shift) } -func HashBytesAtOffset(v uint64, offset uint, shift uint, length uint) uint32 { +func hashBytesAtOffset(v uint64, offset uint, shift uint, length uint) uint32 { assert(offset <= 8-length) { var h uint64 = ((v >> (8 * offset)) << ((8 - length) * 8)) * uint64(kHashMul32_a) @@ -39,7 +39,7 @@ func HashBytesAtOffset(v uint64, offset uint, shift uint, length uint) uint32 { } } -func IsMatch1(p1 []byte, p2 []byte, length uint) bool { +func isMatch1(p1 []byte, p2 []byte, length uint) bool { var i uint for i = 0; i < length && i < 6; i++ { if p1[i] != p2[i] { @@ -52,9 +52,9 @@ func IsMatch1(p1 []byte, p2 []byte, length uint) bool { /* Builds a command and distance prefix code (each 64 symbols) into "depth" and "bits" based on "histogram" and stores it into the bit stream. */ -func BuildAndStoreCommandPrefixCode(histogram []uint32, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { +func buildAndStoreCommandPrefixCode(histogram []uint32, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { var tree [129]HuffmanTree - var cmd_depth = [BROTLI_NUM_COMMAND_SYMBOLS]byte{0} + var cmd_depth = [numCommandSymbols]byte{0} /* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */ var cmd_bits [64]uint16 @@ -98,13 +98,13 @@ func BuildAndStoreCommandPrefixCode(histogram []uint32, depth []byte, bits []uin cmd_depth[448+8*i] = depth[16+i] } - BrotliStoreHuffmanTree(cmd_depth[:], BROTLI_NUM_COMMAND_SYMBOLS, tree[:], storage_ix, storage) + storeHuffmanTree(cmd_depth[:], numCommandSymbols, tree[:], storage_ix, storage) } - BrotliStoreHuffmanTree(depth[64:], 64, tree[:], storage_ix, storage) + storeHuffmanTree(depth[64:], 64, tree[:], storage_ix, storage) } -func EmitInsertLen(insertlen uint32, commands *[]uint32) { +func emitInsertLen(insertlen uint32, commands *[]uint32) { if insertlen < 6 { (*commands)[0] = insertlen } else if insertlen < 130 { @@ -134,7 +134,7 @@ func EmitInsertLen(insertlen uint32, commands *[]uint32) { *commands = (*commands)[1:] } -func EmitCopyLen(copylen uint, commands *[]uint32) { +func emitCopyLen(copylen uint, commands *[]uint32) { if copylen < 10 { (*commands)[0] = uint32(copylen + 38) } else if copylen < 134 { @@ -158,7 +158,7 @@ func EmitCopyLen(copylen uint, commands *[]uint32) { *commands = (*commands)[1:] } -func EmitCopyLenLastDistance(copylen uint, commands *[]uint32) { +func emitCopyLenLastDistance(copylen uint, commands *[]uint32) { if copylen < 12 { (*commands)[0] = uint32(copylen + 20) *commands = (*commands)[1:] @@ -196,7 +196,7 @@ func EmitCopyLenLastDistance(copylen uint, commands *[]uint32) { } } -func EmitDistance(distance uint32, commands *[]uint32) { +func emitDistance(distance uint32, commands *[]uint32) { var d uint32 = distance + 3 var nbits uint32 = Log2FloorNonZero(uint(d)) - 1 var prefix uint32 = (d >> nbits) & 1 @@ -208,7 +208,7 @@ func EmitDistance(distance uint32, commands *[]uint32) { } /* REQUIRES: len <= 1 << 24. */ -func BrotliStoreMetaBlockHeader(len uint, is_uncompressed bool, storage_ix *uint, storage []byte) { +func storeMetaBlockHeader(len uint, is_uncompressed bool, storage_ix *uint, storage []byte) { var nibbles uint = 6 /* ISLAST */ @@ -227,7 +227,7 @@ func BrotliStoreMetaBlockHeader(len uint, is_uncompressed bool, storage_ix *uint BrotliWriteSingleBit(is_uncompressed, storage_ix, storage) } -func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr []byte, table []int, table_bits uint, min_match uint, literals *[]byte, commands *[]uint32) { +func createCommands(input []byte, block_size uint, input_size uint, base_ip_ptr []byte, table []int, table_bits uint, min_match uint, literals *[]byte, commands *[]uint32) { var ip int = 0 var shift uint = 64 - table_bits var ip_end int = int(block_size) @@ -251,7 +251,7 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var next_hash uint32 ip++ - for next_hash = Hash1(input[ip:], shift, min_match); ; { + for next_hash = hash1(input[ip:], shift, min_match); ; { var skip uint32 = 32 var next_ip int = ip /* Step 1: Scan forward in the input looking for a 6-byte-long match. @@ -280,15 +280,15 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var bytes_between_hash_lookups uint32 = skip >> 5 skip++ ip = next_ip - assert(hash == Hash1(input[ip:], shift, min_match)) + assert(hash == hash1(input[ip:], shift, min_match)) next_ip = int(uint32(ip) + bytes_between_hash_lookups) if next_ip > ip_limit { goto emit_remainder } - next_hash = Hash1(input[next_ip:], shift, min_match) + next_hash = hash1(input[next_ip:], shift, min_match) candidate = ip - last_distance - if IsMatch1(input[ip:], input[candidate:], min_match) { + if isMatch1(input[ip:], input[candidate:], min_match) { if candidate < ip { table[hash] = int(ip - base_ip) break @@ -300,14 +300,14 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr assert(candidate < ip) table[hash] = int(ip - base_ip) - if !(!IsMatch1(input[ip:], input[candidate:], min_match)) { + if !(!isMatch1(input[ip:], input[candidate:], min_match)) { break } } /* Check copy distance. If candidate is not feasible, continue search. Checking is done outside of hot loop to reduce overhead. */ - if ip-candidate > MAX_DISTANCE { + if ip-candidate > maxDistance { goto trawl } @@ -325,18 +325,18 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var insert int = int(base - next_emit) ip += int(matched) - EmitInsertLen(uint32(insert), commands) + emitInsertLen(uint32(insert), commands) copy(*literals, input[next_emit:][:uint(insert)]) *literals = (*literals)[insert:] if distance == last_distance { (*commands)[0] = 64 *commands = (*commands)[1:] } else { - EmitDistance(uint32(distance), commands) + emitDistance(uint32(distance), commands) last_distance = distance } - EmitCopyLenLastDistance(matched, commands) + emitCopyLenLastDistance(matched, commands) next_emit = ip if ip >= ip_limit { @@ -352,26 +352,26 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var prev_hash uint32 if min_match == 4 { input_bytes = binary.LittleEndian.Uint64(input[ip-3:]) - cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match) - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) + cur_hash = hashBytesAtOffset(input_bytes, 3, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 3) - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 1, shift, min_match) table[prev_hash] = int(ip - base_ip - 2) - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 1) } else { input_bytes = binary.LittleEndian.Uint64(input[ip-5:]) - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 5) - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 1, shift, min_match) table[prev_hash] = int(ip - base_ip - 4) - prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 2, shift, min_match) table[prev_hash] = int(ip - base_ip - 3) input_bytes = binary.LittleEndian.Uint64(input[ip-2:]) - cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) + cur_hash = hashBytesAtOffset(input_bytes, 2, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 2) - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 1, shift, min_match) table[prev_hash] = int(ip - base_ip - 1) } @@ -380,7 +380,7 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr } } - for ip-candidate <= MAX_DISTANCE && IsMatch1(input[ip:], input[candidate:], min_match) { + for ip-candidate <= maxDistance && isMatch1(input[ip:], input[candidate:], min_match) { var base int = ip /* We have a 6-byte match at ip, and no need to emit any literal bytes prior to ip. */ @@ -388,8 +388,8 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var matched uint = min_match + FindMatchLengthWithLimit(input[uint(candidate)+min_match:], input[uint(ip)+min_match:], uint(ip_end-ip)-min_match) ip += int(matched) last_distance = int(base - candidate) /* > 0 */ - EmitCopyLen(matched, commands) - EmitDistance(uint32(last_distance), commands) + emitCopyLen(matched, commands) + emitDistance(uint32(last_distance), commands) next_emit = ip if ip >= ip_limit { @@ -405,26 +405,26 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var prev_hash uint32 if min_match == 4 { input_bytes = binary.LittleEndian.Uint64(input[ip-3:]) - cur_hash = HashBytesAtOffset(input_bytes, 3, shift, min_match) - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) + cur_hash = hashBytesAtOffset(input_bytes, 3, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 3) - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 1, shift, min_match) table[prev_hash] = int(ip - base_ip - 2) - prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 2, shift, min_match) table[prev_hash] = int(ip - base_ip - 1) } else { input_bytes = binary.LittleEndian.Uint64(input[ip-5:]) - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 5) - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 1, shift, min_match) table[prev_hash] = int(ip - base_ip - 4) - prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 2, shift, min_match) table[prev_hash] = int(ip - base_ip - 3) input_bytes = binary.LittleEndian.Uint64(input[ip-2:]) - cur_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) - prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) + cur_hash = hashBytesAtOffset(input_bytes, 2, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 2) - prev_hash = HashBytesAtOffset(input_bytes, 1, shift, min_match) + prev_hash = hashBytesAtOffset(input_bytes, 1, shift, min_match) table[prev_hash] = int(ip - base_ip - 1) } @@ -434,7 +434,7 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr } ip++ - next_hash = Hash1(input[ip:], shift, min_match) + next_hash = hash1(input[ip:], shift, min_match) } } @@ -444,13 +444,13 @@ emit_remainder: /* Emit the remaining bytes as literals. */ if next_emit < ip_end { var insert uint32 = uint32(ip_end - next_emit) - EmitInsertLen(insert, commands) + emitInsertLen(insert, commands) copy(*literals, input[next_emit:][:insert]) *literals = (*literals)[insert:] } } -var StoreCommands_kNumExtraBits = [128]uint32{ +var storeCommands_kNumExtraBits = [128]uint32{ 0, 0, 0, @@ -580,7 +580,7 @@ var StoreCommands_kNumExtraBits = [128]uint32{ 24, 24, } -var StoreCommands_kInsertOffset = [24]uint32{ +var storeCommands_kInsertOffset = [24]uint32{ 0, 1, 2, @@ -607,7 +607,7 @@ var StoreCommands_kInsertOffset = [24]uint32{ 22594, } -func StoreCommands(literals []byte, num_literals uint, commands []uint32, num_commands uint, storage_ix *uint, storage []byte) { +func storeCommands(literals []byte, num_literals uint, commands []uint32, num_commands uint, storage_ix *uint, storage []byte) { var lit_depths [256]byte var lit_bits [256]uint16 var lit_histo = [256]uint32{0} @@ -619,7 +619,7 @@ func StoreCommands(literals []byte, num_literals uint, commands []uint32, num_co lit_histo[literals[i]]++ } - BrotliBuildAndStoreHuffmanTreeFast(lit_histo[:], num_literals, /* max_bits = */ + buildAndStoreHuffmanTreeFast(lit_histo[:], num_literals, /* max_bits = */ 8, lit_depths[:], lit_bits[:], storage_ix, storage) for i = 0; i < num_commands; i++ { @@ -632,7 +632,7 @@ func StoreCommands(literals []byte, num_literals uint, commands []uint32, num_co cmd_histo[2] += 1 cmd_histo[64] += 1 cmd_histo[84] += 1 - BuildAndStoreCommandPrefixCode(cmd_histo[:], cmd_depths[:], cmd_bits[:], storage_ix, storage) + buildAndStoreCommandPrefixCode(cmd_histo[:], cmd_depths[:], cmd_bits[:], storage_ix, storage) for i = 0; i < num_commands; i++ { var cmd uint32 = commands[i] @@ -640,9 +640,9 @@ func StoreCommands(literals []byte, num_literals uint, commands []uint32, num_co var extra uint32 = cmd >> 8 assert(code < 128) BrotliWriteBits(uint(cmd_depths[code]), uint64(cmd_bits[code]), storage_ix, storage) - BrotliWriteBits(uint(StoreCommands_kNumExtraBits[code]), uint64(extra), storage_ix, storage) + BrotliWriteBits(uint(storeCommands_kNumExtraBits[code]), uint64(extra), storage_ix, storage) if code < 24 { - var insert uint32 = StoreCommands_kInsertOffset[code] + extra + var insert uint32 = storeCommands_kInsertOffset[code] + extra var j uint32 for j = 0; j < insert; j++ { var lit byte = literals[0] @@ -654,19 +654,19 @@ func StoreCommands(literals []byte, num_literals uint, commands []uint32, num_co } /* Acceptable loss for uncompressible speedup is 2% */ -const MIN_RATIO = 0.98 +const minRatio = 0.98 -const SAMPLE_RATE = 43 +const sampleRate = 43 -func ShouldCompress(input []byte, input_size uint, num_literals uint) bool { +func shouldCompress(input []byte, input_size uint, num_literals uint) bool { var corpus_size float64 = float64(input_size) - if float64(num_literals) < MIN_RATIO*corpus_size { + if float64(num_literals) < minRatio*corpus_size { return true } else { var literal_histo = [256]uint32{0} - var max_total_bit_cost float64 = corpus_size * 8 * MIN_RATIO / SAMPLE_RATE + var max_total_bit_cost float64 = corpus_size * 8 * minRatio / sampleRate var i uint - for i = 0; i < input_size; i += SAMPLE_RATE { + for i = 0; i < input_size; i += sampleRate { literal_histo[input[i]]++ } @@ -674,22 +674,22 @@ func ShouldCompress(input []byte, input_size uint, num_literals uint) bool { } } -func RewindBitPosition(new_storage_ix uint, storage_ix *uint, storage []byte) { +func rewindBitPosition(new_storage_ix uint, storage_ix *uint, storage []byte) { var bitpos uint = new_storage_ix & 7 var mask uint = (1 << bitpos) - 1 storage[new_storage_ix>>3] &= byte(mask) *storage_ix = new_storage_ix } -func EmitUncompressedMetaBlock(input []byte, input_size uint, storage_ix *uint, storage []byte) { - BrotliStoreMetaBlockHeader(input_size, true, storage_ix, storage) +func emitUncompressedMetaBlock(input []byte, input_size uint, storage_ix *uint, storage []byte) { + storeMetaBlockHeader(input_size, true, storage_ix, storage) *storage_ix = (*storage_ix + 7) &^ 7 copy(storage[*storage_ix>>3:], input[:input_size]) *storage_ix += input_size << 3 storage[*storage_ix>>3] = 0 } -func BrotliCompressFragmentTwoPassImpl(input []byte, input_size uint, is_last bool, command_buf []uint32, literal_buf []byte, table []int, table_bits uint, min_match uint, storage_ix *uint, storage []byte) { +func compressFragmentTwoPassImpl(input []byte, input_size uint, is_last bool, command_buf []uint32, literal_buf []byte, table []int, table_bits uint, min_match uint, storage_ix *uint, storage []byte) { /* Save the start of the first block for position and distance computations. */ var base_ip []byte = input @@ -699,21 +699,21 @@ func BrotliCompressFragmentTwoPassImpl(input []byte, input_size uint, is_last bo var commands []uint32 = command_buf var literals []byte = literal_buf var num_literals uint - CreateCommands(input, block_size, input_size, base_ip, table, table_bits, min_match, &literals, &commands) + createCommands(input, block_size, input_size, base_ip, table, table_bits, min_match, &literals, &commands) num_literals = uint(-cap(literals) + cap(literal_buf)) - if ShouldCompress(input, block_size, num_literals) { + if shouldCompress(input, block_size, num_literals) { var num_commands uint = uint(-cap(commands) + cap(command_buf)) - BrotliStoreMetaBlockHeader(block_size, false, storage_ix, storage) + storeMetaBlockHeader(block_size, false, storage_ix, storage) /* No block splits, no contexts. */ BrotliWriteBits(13, 0, storage_ix, storage) - StoreCommands(literal_buf, num_literals, command_buf, num_commands, storage_ix, storage) + storeCommands(literal_buf, num_literals, command_buf, num_commands, storage_ix, storage) } else { /* Since we did not find many backward references and the entropy of the data is close to 8 bits, we can simply emit an uncompressed block. This makes compression speed of uncompressible data about 3x faster. */ - EmitUncompressedMetaBlock(input, block_size, storage_ix, storage) + emitUncompressedMetaBlock(input, block_size, storage_ix, storage) } input = input[block_size:] @@ -721,7 +721,7 @@ func BrotliCompressFragmentTwoPassImpl(input []byte, input_size uint, is_last bo } } -func BrotliCompressFragmentTwoPass(input []byte, input_size uint, is_last bool, command_buf []uint32, literal_buf []byte, table []int, table_size uint, storage_ix *uint, storage []byte) { +func compressFragmentTwoPass(input []byte, input_size uint, is_last bool, command_buf []uint32, literal_buf []byte, table []int, table_size uint, storage_ix *uint, storage []byte) { var initial_storage_ix uint = *storage_ix var table_bits uint = uint(Log2FloorNonZero(table_size)) var min_match uint @@ -730,12 +730,12 @@ func BrotliCompressFragmentTwoPass(input []byte, input_size uint, is_last bool, } else { min_match = 6 } - BrotliCompressFragmentTwoPassImpl(input, input_size, is_last, command_buf, literal_buf, table, table_bits, min_match, storage_ix, storage) + compressFragmentTwoPassImpl(input, input_size, is_last, command_buf, literal_buf, table, table_bits, min_match, storage_ix, storage) /* If output is larger than single uncompressed block, rewrite it. */ if *storage_ix-initial_storage_ix > 31+(input_size<<3) { - RewindBitPosition(initial_storage_ix, storage_ix, storage) - EmitUncompressedMetaBlock(input, input_size, storage_ix, storage) + rewindBitPosition(initial_storage_ix, storage_ix, storage) + emitUncompressedMetaBlock(input, input_size, storage_ix, storage) } if is_last { diff --git a/constants.go b/constants.go index cd284d3..876cd73 100644 --- a/constants.go +++ b/constants.go @@ -7,54 +7,54 @@ package brotli */ /* Specification: 7.3. Encoding of the context map */ -const BROTLI_CONTEXT_MAP_MAX_RLE = 16 +const contextMapMaxRle = 16 /* Specification: 2. Compressed representation overview */ -const BROTLI_MAX_NUMBER_OF_BLOCK_TYPES = 256 +const maxNumberOfBlockTypes = 256 /* Specification: 3.3. Alphabet sizes: insert-and-copy length */ -const BROTLI_NUM_LITERAL_SYMBOLS = 256 +const numLiteralSymbols = 256 -const BROTLI_NUM_COMMAND_SYMBOLS = 704 +const numCommandSymbols = 704 -const BROTLI_NUM_BLOCK_LEN_SYMBOLS = 26 +const numBlockLenSymbols = 26 -const BROTLI_MAX_CONTEXT_MAP_SYMBOLS = (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + BROTLI_CONTEXT_MAP_MAX_RLE) +const maxContextMapSymbols = (maxNumberOfBlockTypes + contextMapMaxRle) -const BROTLI_MAX_BLOCK_TYPE_SYMBOLS = (BROTLI_MAX_NUMBER_OF_BLOCK_TYPES + 2) +const maxBlockTypeSymbols = (maxNumberOfBlockTypes + 2) /* Specification: 3.5. Complex prefix codes */ -const BROTLI_REPEAT_PREVIOUS_CODE_LENGTH = 16 +const repeatPreviousCodeLength = 16 -const BROTLI_REPEAT_ZERO_CODE_LENGTH = 17 +const repeatZeroCodeLength = 17 -const BROTLI_CODE_LENGTH_CODES = (BROTLI_REPEAT_ZERO_CODE_LENGTH + 1) +const codeLengthCodes = (repeatZeroCodeLength + 1) /* "code length of 8 is repeated" */ -const BROTLI_INITIAL_REPEATED_CODE_LENGTH = 8 +const initialRepeatedCodeLength = 8 /* "Large Window Brotli" */ -const BROTLI_LARGE_MAX_DISTANCE_BITS = 62 +const largeMaxDistanceBits = 62 -const BROTLI_LARGE_MIN_WBITS = 10 +const largeMinWbits = 10 -const BROTLI_LARGE_MAX_WBITS = 30 +const largeMaxWbits = 30 /* Specification: 4. Encoding of distances */ -const BROTLI_NUM_DISTANCE_SHORT_CODES = 16 +const numDistanceShortCodes = 16 -const BROTLI_MAX_NPOSTFIX = 3 +const maxNpostfix = 3 -const BROTLI_MAX_NDIRECT = 120 +const maxNdirect = 120 -const BROTLI_MAX_DISTANCE_BITS = 24 +const maxDistanceBits = 24 -func BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX uint, NDIRECT uint, MAXNBITS uint) uint { - return BROTLI_NUM_DISTANCE_SHORT_CODES + NDIRECT + uint(MAXNBITS<<(NPOSTFIX+1)) +func distanceAlphabetSize(NPOSTFIX uint, NDIRECT uint, MAXNBITS uint) uint { + return numDistanceShortCodes + NDIRECT + uint(MAXNBITS<<(NPOSTFIX+1)) } -/* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */ -const BROTLI_NUM_DISTANCE_SYMBOLS = 1128 +/* numDistanceSymbols == 1128 */ +const numDistanceSymbols = 1128 const BROTLI_MAX_DISTANCE = 0x3FFFFFC diff --git a/decode.go b/decode.go index 084be83..ebad1ea 100644 --- a/decode.go +++ b/decode.go @@ -107,7 +107,7 @@ const HUFFMAN_TABLE_MASK = 0xFF - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ var kRingBufferWriteAheadSlack uint32 = 42 -var kCodeLengthCodeOrder = [BROTLI_CODE_LENGTH_CODES]byte{1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15} +var kCodeLengthCodeOrder = [codeLengthCodes]byte{1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15} /* Static prefix code for the complex code length code lengths. */ var kCodeLengthPrefixLength = [16]byte{2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4} @@ -583,7 +583,7 @@ func ProcessRepeatedCodeLength(code_len uint32, repeat_delta uint32, alphabet_si var old_repeat uint32 /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ var extra_bits uint32 = 3 var new_len uint32 = 0 - if code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH { + if code_len == repeatPreviousCodeLength { new_len = *prev_code_len extra_bits = 2 } @@ -658,11 +658,11 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { p = p[getBitsUnmasked(br)&uint64(bitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)):] dropBits(br, uint32(p[0].bits)) /* Use 1..5 bits. */ code_len = uint32(p[0].value) /* code_len == 0..17 */ - if code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH { + if code_len < repeatPreviousCodeLength { ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, &prev_code_len, symbol_lists, code_length_histo, next_symbol) /* code_len == 16..17, extra_bits == 2..3 */ } else { var extra_bits uint32 - if code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH { + if code_len == repeatPreviousCodeLength { extra_bits = 2 } else { extra_bits = 3 @@ -702,7 +702,7 @@ func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { } code_len = uint32(p[0].value) /* code_len == 0..17 */ - if code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH { + if code_len < repeatPreviousCodeLength { dropBits(br, uint32(p[0].bits)) ProcessSingleCodeLength(code_len, &s.symbol, &s.repeat, &s.space, &s.prev_code_len, s.symbol_lists, s.code_length_histo[:], s.next_symbol[:]) /* code_len == 16..17, extra_bits == 2..3 */ } else { @@ -728,7 +728,7 @@ func ReadCodeLengthCodeLengths(s *Reader) int { var num_codes uint32 = s.repeat var space uint32 = s.space var i uint32 = s.sub_loop_counter - for ; i < BROTLI_CODE_LENGTH_CODES; i++ { + for ; i < codeLengthCodes; i++ { var code_len_idx byte = kCodeLengthCodeOrder[i] var ix uint32 var v uint32 @@ -806,7 +806,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod s.code_length_histo[i] = 0 } - for i = 0; i < BROTLI_CODE_LENGTH_CODES; i++ { + for i = 0; i < codeLengthCodes; i++ { s.code_length_code_lengths[i] = 0 } @@ -879,7 +879,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod } s.symbol = 0 - s.prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH + s.prev_code_len = initialRepeatedCodeLength s.repeat = 0 s.repeat_code_len = 0 s.space = 32768 @@ -1651,7 +1651,7 @@ func ReadDistanceInternal(safe int, s *Reader, br *bitReader) bool { } } - s.distance_code = s.distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1 + s.distance_code = s.distance_code - numDistanceShortCodes + 1 s.block_length[2]-- return true } @@ -2098,8 +2098,8 @@ func SafeProcessCommands(s *Reader) int { /* Returns the maximum number of distance symbols which can only represent distances not exceeding BROTLI_MAX_ALLOWED_DISTANCE. */ -var BrotliMaxDistanceSymbol_bound = [BROTLI_MAX_NPOSTFIX + 1]uint32{0, 4, 12, 28} -var BrotliMaxDistanceSymbol_diff = [BROTLI_MAX_NPOSTFIX + 1]uint32{73, 126, 228, 424} +var BrotliMaxDistanceSymbol_bound = [maxNpostfix + 1]uint32{0, 4, 12, 28} +var BrotliMaxDistanceSymbol_diff = [maxNpostfix + 1]uint32{73, 126, 228, 424} func BrotliMaxDistanceSymbol(ndirect uint32, npostfix uint32) uint32 { var postfix uint32 = 1 << npostfix @@ -2263,7 +2263,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt break } - if s.window_bits < BROTLI_LARGE_MIN_WBITS || s.window_bits > BROTLI_LARGE_MAX_WBITS { + if s.window_bits < largeMinWbits || s.window_bits > largeMaxWbits { result = BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS break } @@ -2305,7 +2305,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt } if s.is_metadata != 0 || s.is_uncompressed != 0 { - if !jumpToByteBoundary(br) { + if !bitReaderJumpToByteBoundary(br) { result = BROTLI_DECODER_ERROR_FORMAT_PADDING_1 break } @@ -2393,7 +2393,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ case BROTLI_STATE_HUFFMAN_CODE_2: { - var alphabet_size uint32 = BROTLI_NUM_BLOCK_LEN_SYMBOLS + var alphabet_size uint32 = numBlockLenSymbols var tree_offset int = s.loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26 result = ReadHuffmanCode(alphabet_size, alphabet_size, s.block_len_trees[tree_offset:], nil, s) if result != BROTLI_DECODER_SUCCESS { @@ -2427,7 +2427,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt s.distance_postfix_bits = bits & bitMask(2) bits >>= 2 - s.num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES + (bits << s.distance_postfix_bits) + s.num_direct_distance_codes = numDistanceShortCodes + (bits << s.distance_postfix_bits) s.distance_postfix_mask = int(bitMask(s.distance_postfix_bits)) s.context_modes = make([]byte, uint(s.num_block_types[0])) if s.context_modes == nil { @@ -2465,14 +2465,14 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ case BROTLI_STATE_CONTEXT_MAP_2: { - var num_direct_codes uint32 = s.num_direct_distance_codes - BROTLI_NUM_DISTANCE_SHORT_CODES + var num_direct_codes uint32 = s.num_direct_distance_codes - numDistanceShortCodes var num_distance_codes uint32 var max_distance_symbol uint32 if s.large_window { - num_distance_codes = uint32(BROTLI_DISTANCE_ALPHABET_SIZE(uint(s.distance_postfix_bits), uint(num_direct_codes), BROTLI_LARGE_MAX_DISTANCE_BITS)) + num_distance_codes = uint32(distanceAlphabetSize(uint(s.distance_postfix_bits), uint(num_direct_codes), largeMaxDistanceBits)) max_distance_symbol = BrotliMaxDistanceSymbol(num_direct_codes, s.distance_postfix_bits) } else { - num_distance_codes = uint32(BROTLI_DISTANCE_ALPHABET_SIZE(uint(s.distance_postfix_bits), uint(num_direct_codes), BROTLI_MAX_DISTANCE_BITS)) + num_distance_codes = uint32(distanceAlphabetSize(uint(s.distance_postfix_bits), uint(num_direct_codes), maxDistanceBits)) max_distance_symbol = num_distance_codes } var allocation_success bool = true @@ -2481,11 +2481,11 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt break } - if !BrotliDecoderHuffmanTreeGroupInit(s, &s.literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, BROTLI_NUM_LITERAL_SYMBOLS, s.num_literal_htrees) { + if !BrotliDecoderHuffmanTreeGroupInit(s, &s.literal_hgroup, numLiteralSymbols, numLiteralSymbols, s.num_literal_htrees) { allocation_success = false } - if !BrotliDecoderHuffmanTreeGroupInit(s, &s.insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, BROTLI_NUM_COMMAND_SYMBOLS, s.num_block_types[1]) { + if !BrotliDecoderHuffmanTreeGroupInit(s, &s.insert_copy_hgroup, numCommandSymbols, numCommandSymbols, s.num_block_types[1]) { allocation_success = false } @@ -2607,7 +2607,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt break } - if !jumpToByteBoundary(br) { + if !bitReaderJumpToByteBoundary(br) { result = BROTLI_DECODER_ERROR_FORMAT_PADDING_2 break } diff --git a/encode.go b/encode.go index 6bf87e7..3d7ed27 100644 --- a/encode.go +++ b/encode.go @@ -89,13 +89,13 @@ type Writer struct { input_pos_ uint64 ringbuffer_ RingBuffer cmd_alloc_size_ uint - commands_ []Command + commands_ []command num_commands_ uint num_literals_ uint last_insert_len_ uint last_flush_pos_ uint64 last_processed_pos_ uint64 - dist_cache_ [BROTLI_NUM_DISTANCE_SHORT_CODES]int + dist_cache_ [numDistanceShortCodes]int saved_dist_cache_ [4]int last_bytes_ uint16 last_bytes_bits_ byte @@ -942,7 +942,7 @@ func ChooseContextMode(params *BrotliEncoderParams, data []byte, pos uint, mask return CONTEXT_UTF8 } -func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes uint, is_last bool, literal_context_mode int, params *BrotliEncoderParams, prev_byte byte, prev_byte2 byte, num_literals uint, num_commands uint, commands []Command, saved_dist_cache []int, dist_cache []int, storage_ix *uint, storage []byte) { +func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes uint, is_last bool, literal_context_mode int, params *BrotliEncoderParams, prev_byte byte, prev_byte2 byte, num_literals uint, num_commands uint, commands []command, saved_dist_cache []int, dist_cache []int, storage_ix *uint, storage []byte) { var wrapped_last_flush_pos uint32 = WrapPosition(last_flush_pos) var last_bytes uint16 var last_bytes_bits byte @@ -962,7 +962,7 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes CreateBackwardReferences is now unused. */ copy(dist_cache, saved_dist_cache[:4]) - BrotliStoreUncompressedMetaBlock(is_last, data, uint(wrapped_last_flush_pos), mask, bytes, storage_ix, storage) + storeUncompressedMetaBlock(is_last, data, uint(wrapped_last_flush_pos), mask, bytes, storage_ix, storage) return } @@ -970,9 +970,9 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes last_bytes = uint16(storage[1]<<8 | storage[0]) last_bytes_bits = byte(*storage_ix) if params.quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES { - BrotliStoreMetaBlockFast(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage) + storeMetaBlockFast(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage) } else if params.quality < MIN_QUALITY_FOR_BLOCK_SPLIT { - BrotliStoreMetaBlockTrivial(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage) + storeMetaBlockTrivial(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage) } else { var mb MetaBlockSplit InitMetaBlockSplit(&mb) @@ -1000,7 +1000,7 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes BrotliOptimizeHistograms(num_effective_dist_codes, &mb) } - BrotliStoreMetaBlock(data, uint(wrapped_last_flush_pos), bytes, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, num_commands, &mb, storage_ix, storage) + storeMetaBlock(data, uint(wrapped_last_flush_pos), bytes, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, num_commands, &mb, storage_ix, storage) DestroyMetaBlockSplit(&mb) } @@ -1011,7 +1011,7 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes storage[0] = byte(last_bytes) storage[1] = byte(last_bytes >> 8) *storage_ix = uint(last_bytes_bits) - BrotliStoreUncompressedMetaBlock(is_last, data, uint(wrapped_last_flush_pos), mask, bytes, storage_ix, storage) + storeUncompressedMetaBlock(is_last, data, uint(wrapped_last_flush_pos), mask, bytes, storage_ix, storage) } } @@ -1030,7 +1030,7 @@ func ChooseDistanceParams(params *BrotliEncoderParams) { } ndirect_msb = (num_direct_distance_codes >> distance_postfix_bits) & 0x0F - if distance_postfix_bits > BROTLI_MAX_NPOSTFIX || num_direct_distance_codes > BROTLI_MAX_NDIRECT || ndirect_msb< maxNpostfix || num_direct_distance_codes > maxNdirect || ndirect_msb<>25)), (last_command.dist_prefix_&0x3FF == 0), &last_command.cmd_prefix_) + getLengthCode(uint(last_command.insert_len_), uint(int(last_command.copy_len_&0x1FFFFFF)+int(last_command.copy_len_>>25)), (last_command.dist_prefix_&0x3FF == 0), &last_command.cmd_prefix_) } } @@ -1305,9 +1305,9 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu storage[1] = byte(s.last_bytes_ >> 8) table = GetHashTable(s, s.params.quality, uint(bytes), &table_size) if s.params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY { - BrotliCompressFragmentFast(data[wrapped_last_processed_pos&mask:], uint(bytes), is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage) + compressFragmentFast(data[wrapped_last_processed_pos&mask:], uint(bytes), is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage) } else { - BrotliCompressFragmentTwoPass(data[wrapped_last_processed_pos&mask:], uint(bytes), is_last, s.command_buf_, s.literal_buf_, table, table_size, &storage_ix, storage) + compressFragmentTwoPass(data[wrapped_last_processed_pos&mask:], uint(bytes), is_last, s.command_buf_, s.literal_buf_, table, table_size, &storage_ix, storage) } s.last_bytes_ = uint16(storage[storage_ix>>3]) @@ -1321,14 +1321,14 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu /* Theoretical max number of commands is 1 per 2 bytes. */ var newsize uint = uint(uint32(s.num_commands_) + bytes/2 + 1) if newsize > s.cmd_alloc_size_ { - var new_commands []Command + var new_commands []command /* Reserve a bit more memory to allow merging with a next block without reallocation: that would impact speed. */ newsize += uint((bytes / 4) + 16) s.cmd_alloc_size_ = newsize - new_commands = make([]Command, newsize) + new_commands = make([]command, newsize) if s.commands_ != nil { copy(new_commands, s.commands_[:s.num_commands_]) s.commands_ = nil @@ -1380,7 +1380,7 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu /* Create the last insert-only command. */ if s.last_insert_len_ > 0 { - InitInsertCommand(&s.commands_[s.num_commands_], s.last_insert_len_) + initInsertCommand(&s.commands_[s.num_commands_], s.last_insert_len_) s.num_commands_++ s.num_literals_ += s.last_insert_len_ s.last_insert_len_ = 0 @@ -1504,7 +1504,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by for ok && metablock_start < input_size { var metablock_end uint = brotli_min_size_t(input_size, metablock_start+max_metablock_size) var expected_num_commands uint = (metablock_end-metablock_start)/12 + 16 - var commands []Command = nil + var commands []command = nil var num_commands uint = 0 var last_insert_len uint = 0 var num_literals uint = 0 @@ -1535,7 +1535,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by new_cmd_alloc_size = brotli_max_size_t(expected_num_commands, num_commands+path_size+1) if cmd_alloc_size != new_cmd_alloc_size { - var new_commands []Command = make([]Command, new_cmd_alloc_size) + var new_commands []command = make([]command, new_cmd_alloc_size) cmd_alloc_size = new_cmd_alloc_size if commands != nil { copy(new_commands, commands[:num_commands]) @@ -1556,7 +1556,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by } if last_insert_len > 0 { - InitInsertCommand(&commands[num_commands], last_insert_len) + initInsertCommand(&commands[num_commands], last_insert_len) num_commands++ num_literals += last_insert_len } @@ -1581,7 +1581,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by storage = make([]byte, (metablock_size + 16)) storage[0] = byte(last_bytes) storage[1] = byte(last_bytes >> 8) - BrotliStoreUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage) + storeUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage) } else { var mb MetaBlockSplit var block_params BrotliEncoderParams = params @@ -1602,7 +1602,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by storage = make([]byte, (2*metablock_size + 503)) storage[0] = byte(last_bytes) storage[1] = byte(last_bytes >> 8) - BrotliStoreMetaBlock(input_buffer, metablock_start, metablock_size, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, num_commands, &mb, &storage_ix, storage) + storeMetaBlock(input_buffer, metablock_start, metablock_size, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, num_commands, &mb, &storage_ix, storage) if metablock_size+4 < storage_ix>>3 { /* Restore the distance cache and last byte. */ copy(dist_cache[:], saved_dist_cache[:4]) @@ -1610,7 +1610,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by storage[0] = byte(last_bytes) storage[1] = byte(last_bytes >> 8) storage_ix = uint(last_bytes_bits) - BrotliStoreUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage) + storeUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage) } DestroyMetaBlockSplit(&mb) @@ -1815,9 +1815,9 @@ func BrotliEncoderCompressStreamFast(s *Writer, op int, available_in *uint, next table = GetHashTable(s, s.params.quality, block_size, &table_size) if s.params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY { - BrotliCompressFragmentFast(*next_in, block_size, is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage) + compressFragmentFast(*next_in, block_size, is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage) } else { - BrotliCompressFragmentTwoPass(*next_in, block_size, is_last, command_buf, literal_buf, table, table_size, &storage_ix, storage) + compressFragmentTwoPass(*next_in, block_size, is_last, command_buf, literal_buf, table, table_size, &storage_ix, storage) } *next_in = (*next_in)[block_size:] diff --git a/entropy_encode.go b/entropy_encode.go index 6087d8a..5c98128 100644 --- a/entropy_encode.go +++ b/entropy_encode.go @@ -280,7 +280,7 @@ func BrotliWriteHuffmanTreeRepetitions(previous_value byte, value byte, repetiti var start uint = *tree_size repetitions -= 3 for { - tree[*tree_size] = BROTLI_REPEAT_PREVIOUS_CODE_LENGTH + tree[*tree_size] = repeatPreviousCodeLength extra_bits_data[*tree_size] = byte(repetitions & 0x3) (*tree_size)++ repetitions >>= 2 @@ -315,7 +315,7 @@ func BrotliWriteHuffmanTreeRepetitionsZeros(repetitions uint, tree_size *uint, t var start uint = *tree_size repetitions -= 3 for { - tree[*tree_size] = BROTLI_REPEAT_ZERO_CODE_LENGTH + tree[*tree_size] = repeatZeroCodeLength extra_bits_data[*tree_size] = byte(repetitions & 0x7) (*tree_size)++ repetitions >>= 3 @@ -509,7 +509,7 @@ func DecideOverRleUse(depth []byte, length uint, use_rle_for_non_zero *bool, use } func BrotliWriteHuffmanTree(depth []byte, length uint, tree_size *uint, tree []byte, extra_bits_data []byte) { - var previous_value byte = BROTLI_INITIAL_REPEATED_CODE_LENGTH + var previous_value byte = initialRepeatedCodeLength var i uint var use_rle_for_non_zero bool = false var use_rle_for_zero bool = false diff --git a/entropy_encode_static.go b/entropy_encode_static.go index 4436929..18ca07d 100644 --- a/entropy_encode_static.go +++ b/entropy_encode_static.go @@ -2,7 +2,7 @@ package brotli var kCodeLengthDepth = [18]byte{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 0, 4, 4} -var kStaticCommandCodeDepth = [BROTLI_NUM_COMMAND_SYMBOLS]byte{ +var kStaticCommandCodeDepth = [numCommandSymbols]byte{ 9, 9, 9, @@ -782,7 +782,7 @@ func StoreStaticCodeLengthCode(storage_ix *uint, storage []byte) { BrotliWriteBits(40, 0x0000FF55555554, storage_ix, storage) } -var kZeroRepsBits = [BROTLI_NUM_COMMAND_SYMBOLS]uint64{ +var kZeroRepsBits = [numCommandSymbols]uint64{ 0x00000000, 0x00000000, 0x00000000, @@ -1489,7 +1489,7 @@ var kZeroRepsBits = [BROTLI_NUM_COMMAND_SYMBOLS]uint64{ 0x08f9cb87, } -var kZeroRepsDepth = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{ +var kZeroRepsDepth = [numCommandSymbols]uint32{ 0, 4, 8, @@ -2196,7 +2196,7 @@ var kZeroRepsDepth = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{ 28, } -var kNonZeroRepsBits = [BROTLI_NUM_COMMAND_SYMBOLS]uint64{ +var kNonZeroRepsBits = [numCommandSymbols]uint64{ 0x0000000b, 0x0000001b, 0x0000002b, @@ -2903,7 +2903,7 @@ var kNonZeroRepsBits = [BROTLI_NUM_COMMAND_SYMBOLS]uint64{ 0x3baeb6db, } -var kNonZeroRepsDepth = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{ +var kNonZeroRepsDepth = [numCommandSymbols]uint32{ 6, 6, 6, @@ -3610,7 +3610,7 @@ var kNonZeroRepsDepth = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{ 30, } -var kStaticCommandCodeBits = [BROTLI_NUM_COMMAND_SYMBOLS]uint16{ +var kStaticCommandCodeBits = [numCommandSymbols]uint16{ 0, 256, 128, diff --git a/histogram.go b/histogram.go index 89fc8d6..f1fad8c 100644 --- a/histogram.go +++ b/histogram.go @@ -6,13 +6,13 @@ import "math" const BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS = 544 type HistogramLiteral struct { - data_ [BROTLI_NUM_LITERAL_SYMBOLS]uint32 + data_ [numLiteralSymbols]uint32 total_count_ uint bit_cost_ float64 } func HistogramClearLiteral(self *HistogramLiteral) { - self.data_ = [BROTLI_NUM_LITERAL_SYMBOLS]uint32{} + self.data_ = [numLiteralSymbols]uint32{} self.total_count_ = 0 self.bit_cost_ = math.MaxFloat64 } @@ -45,23 +45,23 @@ func HistogramAddVectorLiteral(self *HistogramLiteral, p []byte, n uint) { func HistogramAddHistogramLiteral(self *HistogramLiteral, v *HistogramLiteral) { var i uint self.total_count_ += v.total_count_ - for i = 0; i < BROTLI_NUM_LITERAL_SYMBOLS; i++ { + for i = 0; i < numLiteralSymbols; i++ { self.data_[i] += v.data_[i] } } func HistogramDataSizeLiteral() uint { - return BROTLI_NUM_LITERAL_SYMBOLS + return numLiteralSymbols } type HistogramCommand struct { - data_ [BROTLI_NUM_COMMAND_SYMBOLS]uint32 + data_ [numCommandSymbols]uint32 total_count_ uint bit_cost_ float64 } func HistogramClearCommand(self *HistogramCommand) { - self.data_ = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{} + self.data_ = [numCommandSymbols]uint32{} self.total_count_ = 0 self.bit_cost_ = math.MaxFloat64 } @@ -94,23 +94,23 @@ func HistogramAddVectorCommand(self *HistogramCommand, p []uint16, n uint) { func HistogramAddHistogramCommand(self *HistogramCommand, v *HistogramCommand) { var i uint self.total_count_ += v.total_count_ - for i = 0; i < BROTLI_NUM_COMMAND_SYMBOLS; i++ { + for i = 0; i < numCommandSymbols; i++ { self.data_[i] += v.data_[i] } } func HistogramDataSizeCommand() uint { - return BROTLI_NUM_COMMAND_SYMBOLS + return numCommandSymbols } type HistogramDistance struct { - data_ [BROTLI_NUM_DISTANCE_SYMBOLS]uint32 + data_ [numDistanceSymbols]uint32 total_count_ uint bit_cost_ float64 } func HistogramClearDistance(self *HistogramDistance) { - self.data_ = [BROTLI_NUM_DISTANCE_SYMBOLS]uint32{} + self.data_ = [numDistanceSymbols]uint32{} self.total_count_ = 0 self.bit_cost_ = math.MaxFloat64 } @@ -143,23 +143,23 @@ func HistogramAddVectorDistance(self *HistogramDistance, p []uint16, n uint) { func HistogramAddHistogramDistance(self *HistogramDistance, v *HistogramDistance) { var i uint self.total_count_ += v.total_count_ - for i = 0; i < BROTLI_NUM_DISTANCE_SYMBOLS; i++ { + for i = 0; i < numDistanceSymbols; i++ { self.data_[i] += v.data_[i] } } func HistogramDataSizeDistance() uint { - return BROTLI_NUM_DISTANCE_SYMBOLS + return numDistanceSymbols } type BlockSplitIterator struct { - split_ *BlockSplit + split_ *blockSplit idx_ uint type_ uint length_ uint } -func InitBlockSplitIterator(self *BlockSplitIterator, split *BlockSplit) { +func InitBlockSplitIterator(self *BlockSplitIterator, split *blockSplit) { self.split_ = split self.idx_ = 0 self.type_ = 0 @@ -180,7 +180,7 @@ func BlockSplitIteratorNext(self *BlockSplitIterator) { self.length_-- } -func BrotliBuildHistogramsWithContext(cmds []Command, num_commands uint, 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) { +func BrotliBuildHistogramsWithContext(cmds []command, num_commands uint, 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) { var pos uint = start_pos var literal_it BlockSplitIterator var insert_and_copy_it BlockSplitIterator @@ -191,7 +191,7 @@ func BrotliBuildHistogramsWithContext(cmds []Command, num_commands uint, literal InitBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split) InitBlockSplitIterator(&dist_it, dist_split) for i = 0; i < num_commands; i++ { - var cmd *Command = &cmds[i] + var cmd *command = &cmds[i] var j uint BlockSplitIteratorNext(&insert_and_copy_it) HistogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_], uint(cmd.cmd_prefix_)) @@ -212,14 +212,14 @@ func BrotliBuildHistogramsWithContext(cmds []Command, num_commands uint, literal pos++ } - pos += uint(CommandCopyLen(cmd)) - if CommandCopyLen(cmd) != 0 { + pos += uint(commandCopyLen(cmd)) + if commandCopyLen(cmd) != 0 { prev_byte2 = ringbuffer[(pos-2)&mask] prev_byte = ringbuffer[(pos-1)&mask] if cmd.cmd_prefix_ >= 128 { var context uint BlockSplitIteratorNext(&dist_it) - context = uint(uint32(dist_it.type_<= 128 { - PrefixEncodeCopyDistance(uint(CommandRestoreDistanceCode(cmd, orig_params)), uint(new_params.num_direct_distance_codes), uint(new_params.distance_postfix_bits), &cmd.dist_prefix_, &cmd.dist_extra_) + var cmd *command = &cmds[i] + if commandCopyLen(cmd) != 0 && cmd.cmd_prefix_ >= 128 { + PrefixEncodeCopyDistance(uint(commandRestoreDistanceCode(cmd, orig_params)), uint(new_params.num_direct_distance_codes), uint(new_params.distance_postfix_bits), &cmd.dist_prefix_, &cmd.dist_extra_) } } } -func ComputeDistanceCost(cmds []Command, num_commands uint, orig_params *BrotliDistanceParams, new_params *BrotliDistanceParams, cost *float64) bool { +func ComputeDistanceCost(cmds []command, num_commands uint, orig_params *BrotliDistanceParams, new_params *BrotliDistanceParams, cost *float64) bool { var i uint var equal_params bool = false var dist_prefix uint16 @@ -143,12 +143,12 @@ func ComputeDistanceCost(cmds []Command, num_commands uint, orig_params *BrotliD } for i = 0; i < num_commands; i++ { - var cmd *Command = &cmds[i] - if CommandCopyLen(cmd) != 0 && cmd.cmd_prefix_ >= 128 { + var cmd *command = &cmds[i] + if commandCopyLen(cmd) != 0 && cmd.cmd_prefix_ >= 128 { if equal_params { dist_prefix = cmd.dist_prefix_ } else { - var distance uint32 = CommandRestoreDistanceCode(cmd, orig_params) + var distance uint32 = commandRestoreDistanceCode(cmd, orig_params) if distance > uint32(new_params.max_distance) { return false } @@ -167,7 +167,7 @@ func ComputeDistanceCost(cmds []Command, num_commands uint, orig_params *BrotliD var BrotliBuildMetaBlock_kMaxNumberOfHistograms uint = 256 -func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *BrotliEncoderParams, prev_byte byte, prev_byte2 byte, cmds []Command, num_commands uint, literal_context_mode int, mb *MetaBlockSplit) { +func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *BrotliEncoderParams, prev_byte byte, prev_byte2 byte, cmds []command, num_commands uint, literal_context_mode int, mb *MetaBlockSplit) { var distance_histograms []HistogramDistance var literal_histograms []HistogramLiteral var literal_context_modes []int = nil @@ -184,7 +184,7 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli var new_params BrotliEncoderParams = *params - for npostfix = 0; npostfix <= BROTLI_MAX_NPOSTFIX; npostfix++ { + for npostfix = 0; npostfix <= maxNpostfix; npostfix++ { for ; ndirect_msb < 16; ndirect_msb++ { var ndirect uint32 = ndirect_msb << npostfix var skip bool @@ -221,7 +221,7 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli RecomputeDistancePrefixes(cmds, num_commands, &orig_params.dist, ¶ms.dist) - BrotliSplitBlock(cmds, num_commands, ringbuffer, pos, mask, params, &mb.literal_split, &mb.command_split, &mb.distance_split) + splitBlock(cmds, num_commands, ringbuffer, pos, mask, params, &mb.literal_split, &mb.command_split, &mb.distance_split) if !params.disable_literal_context_modeling { literal_context_multiplier = 1 << BROTLI_LITERAL_CONTEXT_BITS @@ -255,7 +255,7 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli mb.literal_histograms_size = mb.literal_context_map_size mb.literal_histograms = make([]HistogramLiteral, (mb.literal_histograms_size)) - BrotliClusterHistogramsLiteral(literal_histograms, literal_histograms_size, BrotliBuildMetaBlock_kMaxNumberOfHistograms, mb.literal_histograms, &mb.literal_histograms_size, mb.literal_context_map) + clusterHistogramsLiteral(literal_histograms, literal_histograms_size, BrotliBuildMetaBlock_kMaxNumberOfHistograms, mb.literal_histograms, &mb.literal_histograms_size, mb.literal_context_map) literal_histograms = nil if params.disable_literal_context_modeling { @@ -277,7 +277,7 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli mb.distance_histograms_size = mb.distance_context_map_size mb.distance_histograms = make([]HistogramDistance, (mb.distance_histograms_size)) - BrotliClusterHistogramsDistance(distance_histograms, mb.distance_context_map_size, BrotliBuildMetaBlock_kMaxNumberOfHistograms, mb.distance_histograms, &mb.distance_histograms_size, mb.distance_context_map) + clusterHistogramsDistance(distance_histograms, mb.distance_context_map_size, BrotliBuildMetaBlock_kMaxNumberOfHistograms, mb.distance_histograms, &mb.distance_histograms_size, mb.distance_context_map) distance_histograms = nil } @@ -292,7 +292,7 @@ type ContextBlockSplitter struct { min_block_size_ uint split_threshold_ float64 num_blocks_ uint - split_ *BlockSplit + split_ *blockSplit histograms_ []HistogramLiteral histograms_size_ *uint target_block_size_ uint @@ -303,14 +303,14 @@ type ContextBlockSplitter struct { merge_last_count_ uint } -func InitContextBlockSplitter(self *ContextBlockSplitter, alphabet_size uint, num_contexts uint, min_block_size uint, split_threshold float64, num_symbols uint, split *BlockSplit, histograms *[]HistogramLiteral, histograms_size *uint) { +func InitContextBlockSplitter(self *ContextBlockSplitter, alphabet_size uint, num_contexts uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]HistogramLiteral, histograms_size *uint) { var max_num_blocks uint = num_symbols/min_block_size + 1 var max_num_types uint assert(num_contexts <= BROTLI_MAX_STATIC_CONTEXTS) self.alphabet_size_ = alphabet_size self.num_contexts_ = num_contexts - self.max_block_types_ = BROTLI_MAX_NUMBER_OF_BLOCK_TYPES / num_contexts + self.max_block_types_ = maxNumberOfBlockTypes / num_contexts self.min_block_size_ = min_block_size self.split_threshold_ = split_threshold self.num_blocks_ = 0 @@ -345,7 +345,7 @@ func InitContextBlockSplitter(self *ContextBlockSplitter, alphabet_size uint, nu (2) emits the current block with the type of the second last block; (3) merges the current block with the last block. */ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool) { - var split *BlockSplit = self.split_ + var split *blockSplit = self.split_ var num_contexts uint = self.num_contexts_ var last_entropy []float64 = self.last_entropy_[:] var histograms []HistogramLiteral = self.histograms_ @@ -496,7 +496,7 @@ func MapStaticContexts(num_contexts uint, static_context_map []uint32, mb *MetaB } } -func BrotliBuildMetaBlockGreedyInternal(ringbuffer []byte, pos uint, mask uint, prev_byte byte, prev_byte2 byte, literal_context_lut ContextLut, num_contexts uint, static_context_map []uint32, commands []Command, n_commands uint, mb *MetaBlockSplit) { +func BrotliBuildMetaBlockGreedyInternal(ringbuffer []byte, pos uint, mask uint, prev_byte byte, prev_byte2 byte, literal_context_lut ContextLut, num_contexts uint, static_context_map []uint32, commands []command, n_commands uint, mb *MetaBlockSplit) { var lit_blocks struct { plain BlockSplitterLiteral ctx ContextBlockSplitter @@ -515,11 +515,11 @@ func BrotliBuildMetaBlockGreedyInternal(ringbuffer []byte, pos uint, mask uint, InitContextBlockSplitter(&lit_blocks.ctx, 256, num_contexts, 512, 400.0, num_literals, &mb.literal_split, &mb.literal_histograms, &mb.literal_histograms_size) } - InitBlockSplitterCommand(&cmd_blocks, BROTLI_NUM_COMMAND_SYMBOLS, 1024, 500.0, n_commands, &mb.command_split, &mb.command_histograms, &mb.command_histograms_size) + InitBlockSplitterCommand(&cmd_blocks, numCommandSymbols, 1024, 500.0, n_commands, &mb.command_split, &mb.command_histograms, &mb.command_histograms_size) InitBlockSplitterDistance(&dist_blocks, 64, 512, 100.0, n_commands, &mb.distance_split, &mb.distance_histograms, &mb.distance_histograms_size) for i = 0; i < n_commands; i++ { - var cmd Command = commands[i] + var cmd command = commands[i] var j uint BlockSplitterAddSymbolCommand(&cmd_blocks, uint(cmd.cmd_prefix_)) for j = uint(cmd.insert_len_); j != 0; j-- { @@ -536,8 +536,8 @@ func BrotliBuildMetaBlockGreedyInternal(ringbuffer []byte, pos uint, mask uint, pos++ } - pos += uint(CommandCopyLen(&cmd)) - if CommandCopyLen(&cmd) != 0 { + pos += uint(commandCopyLen(&cmd)) + if commandCopyLen(&cmd) != 0 { prev_byte2 = ringbuffer[(pos-2)&mask] prev_byte = ringbuffer[(pos-1)&mask] if cmd.cmd_prefix_ >= 128 { @@ -560,7 +560,7 @@ func BrotliBuildMetaBlockGreedyInternal(ringbuffer []byte, pos uint, mask uint, } } -func BrotliBuildMetaBlockGreedy(ringbuffer []byte, pos uint, mask uint, prev_byte byte, prev_byte2 byte, literal_context_lut ContextLut, num_contexts uint, static_context_map []uint32, commands []Command, n_commands uint, mb *MetaBlockSplit) { +func BrotliBuildMetaBlockGreedy(ringbuffer []byte, pos uint, mask uint, prev_byte byte, prev_byte2 byte, literal_context_lut ContextLut, num_contexts uint, static_context_map []uint32, commands []command, n_commands uint, mb *MetaBlockSplit) { if num_contexts == 1 { BrotliBuildMetaBlockGreedyInternal(ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_lut, 1, nil, commands, n_commands, mb) } else { @@ -569,14 +569,14 @@ func BrotliBuildMetaBlockGreedy(ringbuffer []byte, pos uint, mask uint, prev_byt } func BrotliOptimizeHistograms(num_distance_codes uint32, mb *MetaBlockSplit) { - var good_for_rle [BROTLI_NUM_COMMAND_SYMBOLS]byte + var good_for_rle [numCommandSymbols]byte var i uint for i = 0; i < mb.literal_histograms_size; i++ { BrotliOptimizeHuffmanCountsForRle(256, mb.literal_histograms[i].data_[:], good_for_rle[:]) } for i = 0; i < mb.command_histograms_size; i++ { - BrotliOptimizeHuffmanCountsForRle(BROTLI_NUM_COMMAND_SYMBOLS, mb.command_histograms[i].data_[:], good_for_rle[:]) + BrotliOptimizeHuffmanCountsForRle(numCommandSymbols, mb.command_histograms[i].data_[:], good_for_rle[:]) } for i = 0; i < mb.distance_histograms_size; i++ { diff --git a/metablock_command.go b/metablock_command.go index 4b4edc1..4cef356 100644 --- a/metablock_command.go +++ b/metablock_command.go @@ -14,7 +14,7 @@ type BlockSplitterCommand struct { min_block_size_ uint split_threshold_ float64 num_blocks_ uint - split_ *BlockSplit + split_ *blockSplit histograms_ []HistogramCommand histograms_size_ *uint target_block_size_ uint @@ -25,9 +25,9 @@ type BlockSplitterCommand struct { merge_last_count_ uint } -func InitBlockSplitterCommand(self *BlockSplitterCommand, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *BlockSplit, histograms *[]HistogramCommand, histograms_size *uint) { +func InitBlockSplitterCommand(self *BlockSplitterCommand, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]HistogramCommand, histograms_size *uint) { var max_num_blocks uint = num_symbols/min_block_size + 1 - var max_num_types uint = brotli_min_size_t(max_num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES+1) + var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1) /* We have to allocate one more histogram than the maximum number of block types for the current histogram when the meta-block is too big. */ self.alphabet_size_ = alphabet_size @@ -61,7 +61,7 @@ func InitBlockSplitterCommand(self *BlockSplitterCommand, alphabet_size uint, mi (2) emits the current block with the type of the second last block; (3) merges the current block with the last block. */ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) { - var split *BlockSplit = self.split_ + var split *blockSplit = self.split_ var last_entropy []float64 = self.last_entropy_[:] var histograms []HistogramCommand = self.histograms_ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_) @@ -93,7 +93,7 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) diff[j] = combined_entropy[j] - entropy - last_entropy[j] } - if split.num_types < BROTLI_MAX_NUMBER_OF_BLOCK_TYPES && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ { + if split.num_types < maxNumberOfBlockTypes && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ { /* Create new block. */ split.lengths[self.num_blocks_] = uint32(self.block_size_) diff --git a/metablock_distance.go b/metablock_distance.go index 24849a5..574d7a3 100644 --- a/metablock_distance.go +++ b/metablock_distance.go @@ -14,7 +14,7 @@ type BlockSplitterDistance struct { min_block_size_ uint split_threshold_ float64 num_blocks_ uint - split_ *BlockSplit + split_ *blockSplit histograms_ []HistogramDistance histograms_size_ *uint target_block_size_ uint @@ -25,9 +25,9 @@ type BlockSplitterDistance struct { merge_last_count_ uint } -func InitBlockSplitterDistance(self *BlockSplitterDistance, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *BlockSplit, histograms *[]HistogramDistance, histograms_size *uint) { +func InitBlockSplitterDistance(self *BlockSplitterDistance, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]HistogramDistance, histograms_size *uint) { var max_num_blocks uint = num_symbols/min_block_size + 1 - var max_num_types uint = brotli_min_size_t(max_num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES+1) + var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1) /* We have to allocate one more histogram than the maximum number of block types for the current histogram when the meta-block is too big. */ self.alphabet_size_ = alphabet_size @@ -61,7 +61,7 @@ func InitBlockSplitterDistance(self *BlockSplitterDistance, alphabet_size uint, (2) emits the current block with the type of the second last block; (3) merges the current block with the last block. */ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool) { - var split *BlockSplit = self.split_ + var split *blockSplit = self.split_ var last_entropy []float64 = self.last_entropy_[:] var histograms []HistogramDistance = self.histograms_ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_) @@ -93,7 +93,7 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool diff[j] = combined_entropy[j] - entropy - last_entropy[j] } - if split.num_types < BROTLI_MAX_NUMBER_OF_BLOCK_TYPES && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ { + if split.num_types < maxNumberOfBlockTypes && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ { /* Create new block. */ split.lengths[self.num_blocks_] = uint32(self.block_size_) diff --git a/metablock_literal.go b/metablock_literal.go index b37ccd4..2552334 100644 --- a/metablock_literal.go +++ b/metablock_literal.go @@ -14,7 +14,7 @@ type BlockSplitterLiteral struct { min_block_size_ uint split_threshold_ float64 num_blocks_ uint - split_ *BlockSplit + split_ *blockSplit histograms_ []HistogramLiteral histograms_size_ *uint target_block_size_ uint @@ -25,9 +25,9 @@ type BlockSplitterLiteral struct { merge_last_count_ uint } -func InitBlockSplitterLiteral(self *BlockSplitterLiteral, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *BlockSplit, histograms *[]HistogramLiteral, histograms_size *uint) { +func InitBlockSplitterLiteral(self *BlockSplitterLiteral, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]HistogramLiteral, histograms_size *uint) { var max_num_blocks uint = num_symbols/min_block_size + 1 - var max_num_types uint = brotli_min_size_t(max_num_blocks, BROTLI_MAX_NUMBER_OF_BLOCK_TYPES+1) + var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1) /* We have to allocate one more histogram than the maximum number of block types for the current histogram when the meta-block is too big. */ self.alphabet_size_ = alphabet_size @@ -61,7 +61,7 @@ func InitBlockSplitterLiteral(self *BlockSplitterLiteral, alphabet_size uint, mi (2) emits the current block with the type of the second last block; (3) merges the current block with the last block. */ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) { - var split *BlockSplit = self.split_ + var split *blockSplit = self.split_ var last_entropy []float64 = self.last_entropy_[:] var histograms []HistogramLiteral = self.histograms_ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_) @@ -93,7 +93,7 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) diff[j] = combined_entropy[j] - entropy - last_entropy[j] } - if split.num_types < BROTLI_MAX_NUMBER_OF_BLOCK_TYPES && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ { + if split.num_types < maxNumberOfBlockTypes && diff[0] > self.split_threshold_ && diff[1] > self.split_threshold_ { /* Create new block. */ split.lengths[self.num_blocks_] = uint32(self.block_size_) diff --git a/prefix.go b/prefix.go index 2e3648d..83a3cf0 100644 --- a/prefix.go +++ b/prefix.go @@ -33,19 +33,19 @@ package brotli /* Here distance_code is an intermediate code, i.e. one of the special codes or the actual distance increased by BROTLI_NUM_DISTANCE_SHORT_CODES - 1. */ func PrefixEncodeCopyDistance(distance_code uint, num_direct_codes uint, postfix_bits uint, code *uint16, extra_bits *uint32) { - if distance_code < BROTLI_NUM_DISTANCE_SHORT_CODES+num_direct_codes { + if distance_code < numDistanceShortCodes+num_direct_codes { *code = uint16(distance_code) *extra_bits = 0 return } else { - var dist uint = (uint(1) << (postfix_bits + 2)) + (distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES - num_direct_codes) + var dist uint = (uint(1) << (postfix_bits + 2)) + (distance_code - numDistanceShortCodes - num_direct_codes) var bucket uint = uint(Log2FloorNonZero(dist) - 1) var postfix_mask uint = (1 << postfix_bits) - 1 var postfix uint = dist & postfix_mask var prefix uint = (dist >> bucket) & 1 var offset uint = (2 + prefix) << bucket var nbits uint = bucket - postfix_bits - *code = uint16(nbits<<10 | (BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_codes + ((2*(nbits-1) + prefix) << postfix_bits) + postfix)) + *code = uint16(nbits<<10 | (numDistanceShortCodes + num_direct_codes + ((2*(nbits-1) + prefix) << postfix_bits) + postfix)) *extra_bits = uint32((dist - offset) >> postfix_bits) } } diff --git a/prefix_dec.go b/prefix_dec.go index 01ccd02..84958b5 100644 --- a/prefix_dec.go +++ b/prefix_dec.go @@ -16,7 +16,7 @@ type PrefixCodeRange1 struct { nbits byte } -var kBlockLengthPrefixCode1 = [BROTLI_NUM_BLOCK_LEN_SYMBOLS]struct { +var kBlockLengthPrefixCode1 = [numBlockLenSymbols]struct { offset uint16 nbits byte }{ @@ -135,7 +135,7 @@ type CmdLutElement struct { copy_len_offset uint16 } -var kCmdLut = [BROTLI_NUM_COMMAND_SYMBOLS]CmdLutElement{ +var kCmdLut = [numCommandSymbols]CmdLutElement{ CmdLutElement{0x00, 0x00, 0, 0x00, 0x0000, 0x0002}, CmdLutElement{0x00, 0x00, 0, 0x01, 0x0000, 0x0003}, CmdLutElement{0x00, 0x00, 0, 0x02, 0x0000, 0x0004}, diff --git a/state.go b/state.go index 65aeebd..7a686b6 100644 --- a/state.go +++ b/state.go @@ -149,9 +149,9 @@ type Reader struct { space uint32 table [32]HuffmanCode symbol_lists SymbolList - symbols_lists_array [BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + BROTLI_NUM_COMMAND_SYMBOLS]uint16 + symbols_lists_array [BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + numCommandSymbols]uint16 next_symbol [32]int - code_length_code_lengths [BROTLI_CODE_LENGTH_CODES]byte + code_length_code_lengths [codeLengthCodes]byte code_length_histo [16]uint16 htree_index int next []HuffmanCode