Start converting hasher functions to methods.

This commit is contained in:
Andy Balholm 2019-03-08 15:10:41 -08:00
parent 6a14da654a
commit bbbdedf380
17 changed files with 212 additions and 332 deletions

View File

@ -1598,7 +1598,7 @@ func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []by
var path_size uint var path_size uint
var new_cmd_alloc_size uint var new_cmd_alloc_size uint
BrotliInitZopfliNodes(nodes, block_size+1) BrotliInitZopfliNodes(nodes, block_size+1)
StitchToPreviousBlockH10(hasher, block_size, block_start, input_buffer, mask) hasher.StitchToPreviousBlock(block_size, block_start, input_buffer, mask)
path_size = BrotliZopfliComputeShortestPath(block_size, block_start, input_buffer, mask, &params, dist_cache[:], hasher, nodes) path_size = BrotliZopfliComputeShortestPath(block_size, block_start, input_buffer, mask, &params, dist_cache[:], hasher, nodes)
/* We allocate a command buffer in the first iteration of this loop that /* We allocate a command buffer in the first iteration of this loop that

23
h10.go
View File

@ -45,20 +45,18 @@ func ForestH10(self *H10) []uint32 {
return []uint32(self.forest) return []uint32(self.forest)
} }
func InitializeH10(handle HasherHandle, params *BrotliEncoderParams) { func (h *H10) Initialize(params *BrotliEncoderParams) {
var self *H10 = SelfH10(handle) h.window_mask_ = (1 << params.lgwin) - 1
self.window_mask_ = (1 << params.lgwin) - 1 h.invalid_pos_ = uint32(0 - h.window_mask_)
self.invalid_pos_ = uint32(0 - self.window_mask_)
var num_nodes uint = uint(1) << params.lgwin var num_nodes uint = uint(1) << params.lgwin
self.forest = make([]uint32, 2*num_nodes) h.forest = make([]uint32, 2*num_nodes)
} }
func PrepareH10(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H10) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H10 = SelfH10(handle) var invalid_pos uint32 = h.invalid_pos_
var invalid_pos uint32 = self.invalid_pos_
var i uint32 var i uint32
for i = 0; i < 1<<17; i++ { for i = 0; i < 1<<17; i++ {
self.buckets_[i] = invalid_pos h.buckets_[i] = invalid_pos
} }
} }
@ -261,8 +259,7 @@ func StoreRangeH10(handle HasherHandle, data []byte, mask uint, ix_start uint, i
} }
} }
func StitchToPreviousBlockH10(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { func (h *H10) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
var self *H10 = SelfH10(handle)
if num_bytes >= HashTypeLengthH10()-1 && position >= 128 { if num_bytes >= HashTypeLengthH10()-1 && position >= 128 {
var i_start uint = position - 128 + 1 var i_start uint = position - 128 + 1
var i_end uint = brotli_min_size_t(position, i_start+num_bytes) var i_end uint = brotli_min_size_t(position, i_start+num_bytes)
@ -276,12 +273,12 @@ func StitchToPreviousBlockH10(handle HasherHandle, num_bytes uint, position uint
Furthermore, we have to make sure that we don't look further back Furthermore, we have to make sure that we don't look further back
from the start of the next block than the window size, otherwise we from the start of the next block than the window size, otherwise we
could access already overwritten areas of the ring-buffer. */ could access already overwritten areas of the ring-buffer. */
var max_backward uint = self.window_mask_ - brotli_max_size_t(BROTLI_WINDOW_GAP-1, position-i) var max_backward uint = h.window_mask_ - brotli_max_size_t(BROTLI_WINDOW_GAP-1, position-i)
/* We know that i + 128 <= position + num_bytes, i.e. the /* We know that i + 128 <= position + num_bytes, i.e. the
end of the current block and that we have at least end of the current block and that we have at least
128 tail in the ring-buffer. */ 128 tail in the ring-buffer. */
StoreAndFindMatchesH10(self, ringbuffer, i, ringbuffer_mask, 128, max_backward, nil, nil) StoreAndFindMatchesH10(h, ringbuffer, i, ringbuffer_mask, 128, max_backward, nil, nil)
} }
} }
} }

19
h2.go
View File

@ -43,18 +43,17 @@ func SelfH2(handle HasherHandle) *H2 {
return handle.(*H2) return handle.(*H2)
} }
func InitializeH2(handle HasherHandle, params *BrotliEncoderParams) { func (*H2) Initialize(params *BrotliEncoderParams) {
} }
func PrepareH2(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H2) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H2 = SelfH2(handle)
var partial_prepare_threshold uint = (4 << 16) >> 7 var partial_prepare_threshold uint = (4 << 16) >> 7
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
var i uint var i uint
for i = 0; i < input_size; i++ { for i = 0; i < input_size; i++ {
var key uint32 = HashBytesH2(data[i:]) var key uint32 = HashBytesH2(data[i:])
self.buckets_[key] = 0 h.buckets_[key] = 0
} }
} else { } else {
/* It is not strictly necessary to fill this buffer here, but /* It is not strictly necessary to fill this buffer here, but
@ -62,8 +61,8 @@ func PrepareH2(handle HasherHandle, one_shot bool, input_size uint, data []byte)
(but correct). This is because random data would cause the (but correct). This is because random data would cause the
system to find accidentally good backward references here and there. */ system to find accidentally good backward references here and there. */
var i int var i int
for i = 0; i < len(self.buckets_); i++ { for i = 0; i < len(h.buckets_); i++ {
self.buckets_[i] = 0 h.buckets_[i] = 0
} }
} }
} }
@ -85,15 +84,15 @@ func StoreRangeH2(handle HasherHandle, data []byte, mask uint, ix_start uint, ix
} }
} }
func StitchToPreviousBlockH2(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { func (h *H2) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
if num_bytes >= HashTypeLengthH2()-1 && position >= 3 { if num_bytes >= HashTypeLengthH2()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH2(handle, ringbuffer, ringbuffer_mask, position-3) StoreH2(h, ringbuffer, ringbuffer_mask, position-3)
StoreH2(handle, ringbuffer, ringbuffer_mask, position-2) StoreH2(h, ringbuffer, ringbuffer_mask, position-2)
StoreH2(handle, ringbuffer, ringbuffer_mask, position-1) StoreH2(h, ringbuffer, ringbuffer_mask, position-1)
} }
} }

19
h3.go
View File

@ -39,11 +39,10 @@ func SelfH3(handle HasherHandle) *H3 {
return handle.(*H3) return handle.(*H3)
} }
func InitializeH3(handle HasherHandle, params *BrotliEncoderParams) { func (*H3) Initialize(params *BrotliEncoderParams) {
} }
func PrepareH3(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H3) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H3 = SelfH3(handle)
var partial_prepare_threshold uint = (4 << 16) >> 7 var partial_prepare_threshold uint = (4 << 16) >> 7
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
@ -51,7 +50,7 @@ func PrepareH3(handle HasherHandle, one_shot bool, input_size uint, data []byte)
for i = 0; i < input_size; i++ { for i = 0; i < input_size; i++ {
var key uint32 = HashBytesH3(data[i:]) var key uint32 = HashBytesH3(data[i:])
for i := 0; i < int(2); i++ { for i := 0; i < int(2); i++ {
self.buckets_[key:][i] = 0 h.buckets_[key:][i] = 0
} }
} }
} else { } else {
@ -60,8 +59,8 @@ func PrepareH3(handle HasherHandle, one_shot bool, input_size uint, data []byte)
(but correct). This is because random data would cause the (but correct). This is because random data would cause the
system to find accidentally good backward references here and there. */ system to find accidentally good backward references here and there. */
var i int var i int
for i = 0; i < len(self.buckets_); i++ { for i = 0; i < len(h.buckets_); i++ {
self.buckets_[i] = 0 h.buckets_[i] = 0
} }
} }
} }
@ -83,15 +82,15 @@ func StoreRangeH3(handle HasherHandle, data []byte, mask uint, ix_start uint, ix
} }
} }
func StitchToPreviousBlockH3(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { func (h *H3) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
if num_bytes >= HashTypeLengthH3()-1 && position >= 3 { if num_bytes >= HashTypeLengthH3()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH3(handle, ringbuffer, ringbuffer_mask, position-3) StoreH3(h, ringbuffer, ringbuffer_mask, position-3)
StoreH3(handle, ringbuffer, ringbuffer_mask, position-2) StoreH3(h, ringbuffer, ringbuffer_mask, position-2)
StoreH3(handle, ringbuffer, ringbuffer_mask, position-1) StoreH3(h, ringbuffer, ringbuffer_mask, position-1)
} }
} }

41
h35.go
View File

@ -42,42 +42,40 @@ func SelfH35(handle HasherHandle) *H35 {
return handle.(*H35) return handle.(*H35)
} }
func InitializeH35(handle HasherHandle, params *BrotliEncoderParams) { func (h *H35) Initialize(params *BrotliEncoderParams) {
var self *H35 = SelfH35(handle) h.ha = nil
self.ha = nil h.hb = nil
self.hb = nil h.params = params
self.params = params
} }
/* TODO: Initialize of the hashers is defered to Prepare (and params /* TODO: Initialize of the hashers is defered to Prepare (and params
remembered here) because we don't get the one_shot and input_size params remembered here) because we don't get the one_shot and input_size params
here that are needed to know the memory size of them. Instead provide here that are needed to know the memory size of them. Instead provide
those params to all hashers InitializeH35 */ those params to all hashers InitializeH35 */
func PrepareH35(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H35) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H35 = SelfH35(handle) if h.ha == nil {
if self.ha == nil {
var common_a *HasherCommon var common_a *HasherCommon
var common_b *HasherCommon var common_b *HasherCommon
self.ha = new(H3) h.ha = new(H3)
common_a = self.ha.Common() common_a = h.ha.Common()
common_a.params = self.params.hasher common_a.params = h.params.hasher
common_a.is_prepared_ = false common_a.is_prepared_ = false
common_a.dict_num_lookups = 0 common_a.dict_num_lookups = 0
common_a.dict_num_matches = 0 common_a.dict_num_matches = 0
InitializeH3(self.ha, self.params) h.ha.Initialize(h.params)
self.hb = new(HROLLING_FAST) h.hb = new(HROLLING_FAST)
common_b = self.hb.Common() common_b = h.hb.Common()
common_b.params = self.params.hasher common_b.params = h.params.hasher
common_b.is_prepared_ = false common_b.is_prepared_ = false
common_b.dict_num_lookups = 0 common_b.dict_num_lookups = 0
common_b.dict_num_matches = 0 common_b.dict_num_matches = 0
InitializeHROLLING_FAST(self.hb, self.params) h.hb.Initialize(h.params)
} }
PrepareH3(self.ha, one_shot, input_size, data) h.ha.Prepare(one_shot, input_size, data)
PrepareHROLLING_FAST(self.hb, one_shot, input_size, data) h.hb.Prepare(one_shot, input_size, data)
} }
func StoreH35(handle HasherHandle, data []byte, mask uint, ix uint) { func StoreH35(handle HasherHandle, data []byte, mask uint, ix uint) {
@ -92,10 +90,9 @@ func StoreRangeH35(handle HasherHandle, data []byte, mask uint, ix_start uint, i
StoreRangeHROLLING_FAST(self.hb, data, mask, ix_start, ix_end) StoreRangeHROLLING_FAST(self.hb, data, mask, ix_start, ix_end)
} }
func StitchToPreviousBlockH35(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *H35) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
var self *H35 = SelfH35(handle) h.ha.StitchToPreviousBlock(num_bytes, position, ringbuffer, ring_buffer_mask)
StitchToPreviousBlockH3(self.ha, num_bytes, position, ringbuffer, ring_buffer_mask) h.hb.StitchToPreviousBlock(num_bytes, position, ringbuffer, ring_buffer_mask)
StitchToPreviousBlockHROLLING_FAST(self.hb, num_bytes, position, ringbuffer, ring_buffer_mask)
} }
func PrepareDistanceCacheH35(handle HasherHandle, distance_cache []int) { func PrepareDistanceCacheH35(handle HasherHandle, distance_cache []int) {

19
h4.go
View File

@ -39,11 +39,10 @@ func SelfH4(handle HasherHandle) *H4 {
return handle.(*H4) return handle.(*H4)
} }
func InitializeH4(handle HasherHandle, params *BrotliEncoderParams) { func (*H4) Initialize(params *BrotliEncoderParams) {
} }
func PrepareH4(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H4) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H4 = SelfH4(handle)
var partial_prepare_threshold uint = (4 << 17) >> 7 var partial_prepare_threshold uint = (4 << 17) >> 7
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
@ -51,7 +50,7 @@ func PrepareH4(handle HasherHandle, one_shot bool, input_size uint, data []byte)
for i = 0; i < input_size; i++ { for i = 0; i < input_size; i++ {
var key uint32 = HashBytesH4(data[i:]) var key uint32 = HashBytesH4(data[i:])
for i := 0; i < int(4); i++ { for i := 0; i < int(4); i++ {
self.buckets_[key:][i] = 0 h.buckets_[key:][i] = 0
} }
} }
} else { } else {
@ -60,8 +59,8 @@ func PrepareH4(handle HasherHandle, one_shot bool, input_size uint, data []byte)
(but correct). This is because random data would cause the (but correct). This is because random data would cause the
system to find accidentally good backward references here and there. */ system to find accidentally good backward references here and there. */
var i int var i int
for i = 0; i < len(self.buckets_); i++ { for i = 0; i < len(h.buckets_); i++ {
self.buckets_[i] = 0 h.buckets_[i] = 0
} }
} }
} }
@ -83,15 +82,15 @@ func StoreRangeH4(handle HasherHandle, data []byte, mask uint, ix_start uint, ix
} }
} }
func StitchToPreviousBlockH4(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { func (h *H4) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
if num_bytes >= HashTypeLengthH4()-1 && position >= 3 { if num_bytes >= HashTypeLengthH4()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH4(handle, ringbuffer, ringbuffer_mask, position-3) StoreH4(h, ringbuffer, ringbuffer_mask, position-3)
StoreH4(handle, ringbuffer, ringbuffer_mask, position-2) StoreH4(h, ringbuffer, ringbuffer_mask, position-2)
StoreH4(handle, ringbuffer, ringbuffer_mask, position-1) StoreH4(h, ringbuffer, ringbuffer_mask, position-1)
} }
} }

29
h40.go
View File

@ -53,18 +53,17 @@ func SelfH40(handle HasherHandle) *H40 {
return handle.(*H40) return handle.(*H40)
} }
func InitializeH40(handle HasherHandle, params *BrotliEncoderParams) { func (h *H40) Initialize(params *BrotliEncoderParams) {
var q uint var q uint
if params.quality > 6 { if params.quality > 6 {
q = 7 q = 7
} else { } else {
q = 8 q = 8
} }
SelfH40(handle).max_hops = q << uint(params.quality-4) h.max_hops = q << uint(params.quality-4)
} }
func PrepareH40(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H40) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H40 = SelfH40(handle)
var partial_prepare_threshold uint = (1 << 15) >> 6 var partial_prepare_threshold uint = (1 << 15) >> 6
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
@ -73,24 +72,24 @@ func PrepareH40(handle HasherHandle, one_shot bool, input_size uint, data []byte
var bucket uint = HashBytesH40(data[i:]) var bucket uint = HashBytesH40(data[i:])
/* See InitEmpty comment. */ /* See InitEmpty comment. */
self.addr[bucket] = 0xCCCCCCCC h.addr[bucket] = 0xCCCCCCCC
self.head[bucket] = 0xCCCC h.head[bucket] = 0xCCCC
} }
} else { } else {
/* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
processed by hasher never reaches 3GB + 64M; this makes all new chains processed by hasher never reaches 3GB + 64M; this makes all new chains
to be terminated after the first node. */ to be terminated after the first node. */
var i int var i int
for i = 0; i < len(self.addr); i++ { for i = 0; i < len(h.addr); i++ {
self.addr[i] = 0xCCCCCCCC h.addr[i] = 0xCCCCCCCC
} }
self.head = [1 << 15]uint16{} h.head = [1 << 15]uint16{}
} }
self.tiny_hash = [65536]byte{} h.tiny_hash = [65536]byte{}
self.free_slot_idx = [1]uint16{} h.free_slot_idx = [1]uint16{}
} }
/* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
@ -120,15 +119,15 @@ func StoreRangeH40(handle HasherHandle, data []byte, mask uint, ix_start uint, i
} }
} }
func StitchToPreviousBlockH40(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *H40) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
if num_bytes >= HashTypeLengthH40()-1 && position >= 3 { if num_bytes >= HashTypeLengthH40()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH40(handle, ringbuffer, ring_buffer_mask, position-3) StoreH40(h, ringbuffer, ring_buffer_mask, position-3)
StoreH40(handle, ringbuffer, ring_buffer_mask, position-2) StoreH40(h, ringbuffer, ring_buffer_mask, position-2)
StoreH40(handle, ringbuffer, ring_buffer_mask, position-1) StoreH40(h, ringbuffer, ring_buffer_mask, position-1)
} }
} }

29
h41.go
View File

@ -53,18 +53,17 @@ func SelfH41(handle HasherHandle) *H41 {
return handle.(*H41) return handle.(*H41)
} }
func InitializeH41(handle HasherHandle, params *BrotliEncoderParams) { func (h *H41) Initialize(params *BrotliEncoderParams) {
var tmp uint var tmp uint
if params.quality > 6 { if params.quality > 6 {
tmp = 7 tmp = 7
} else { } else {
tmp = 8 tmp = 8
} }
SelfH41(handle).max_hops = tmp << uint(params.quality-4) h.max_hops = tmp << uint(params.quality-4)
} }
func PrepareH41(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H41) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H41 = SelfH41(handle)
var partial_prepare_threshold uint = (1 << 15) >> 6 var partial_prepare_threshold uint = (1 << 15) >> 6
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
@ -73,24 +72,24 @@ func PrepareH41(handle HasherHandle, one_shot bool, input_size uint, data []byte
var bucket uint = HashBytesH41(data[i:]) var bucket uint = HashBytesH41(data[i:])
/* See InitEmpty comment. */ /* See InitEmpty comment. */
self.addr[bucket] = 0xCCCCCCCC h.addr[bucket] = 0xCCCCCCCC
self.head[bucket] = 0xCCCC h.head[bucket] = 0xCCCC
} }
} else { } else {
/* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
processed by hasher never reaches 3GB + 64M; this makes all new chains processed by hasher never reaches 3GB + 64M; this makes all new chains
to be terminated after the first node. */ to be terminated after the first node. */
var i int var i int
for i = 0; i < len(self.addr); i++ { for i = 0; i < len(h.addr); i++ {
self.addr[i] = 0xCCCCCCCC h.addr[i] = 0xCCCCCCCC
} }
self.head = [1 << 15]uint16{} h.head = [1 << 15]uint16{}
} }
self.tiny_hash = [65536]byte{} h.tiny_hash = [65536]byte{}
self.free_slot_idx = [1]uint16{} h.free_slot_idx = [1]uint16{}
} }
/* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
@ -120,15 +119,15 @@ func StoreRangeH41(handle HasherHandle, data []byte, mask uint, ix_start uint, i
} }
} }
func StitchToPreviousBlockH41(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *H41) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
if num_bytes >= HashTypeLengthH41()-1 && position >= 3 { if num_bytes >= HashTypeLengthH41()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH41(handle, ringbuffer, ring_buffer_mask, position-3) StoreH41(h, ringbuffer, ring_buffer_mask, position-3)
StoreH41(handle, ringbuffer, ring_buffer_mask, position-2) StoreH41(h, ringbuffer, ring_buffer_mask, position-2)
StoreH41(handle, ringbuffer, ring_buffer_mask, position-1) StoreH41(h, ringbuffer, ring_buffer_mask, position-1)
} }
} }

29
h42.go
View File

@ -53,18 +53,17 @@ func SelfH42(handle HasherHandle) *H42 {
return handle.(*H42) return handle.(*H42)
} }
func InitializeH42(handle HasherHandle, params *BrotliEncoderParams) { func (h *H42) Initialize(params *BrotliEncoderParams) {
var tmp uint var tmp uint
if params.quality > 6 { if params.quality > 6 {
tmp = 7 tmp = 7
} else { } else {
tmp = 8 tmp = 8
} }
SelfH42(handle).max_hops = tmp << uint(params.quality-4) h.max_hops = tmp << uint(params.quality-4)
} }
func PrepareH42(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H42) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H42 = SelfH42(handle)
var partial_prepare_threshold uint = (1 << 15) >> 6 var partial_prepare_threshold uint = (1 << 15) >> 6
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
@ -73,24 +72,24 @@ func PrepareH42(handle HasherHandle, one_shot bool, input_size uint, data []byte
var bucket uint = HashBytesH42(data[i:]) var bucket uint = HashBytesH42(data[i:])
/* See InitEmpty comment. */ /* See InitEmpty comment. */
self.addr[bucket] = 0xCCCCCCCC h.addr[bucket] = 0xCCCCCCCC
self.head[bucket] = 0xCCCC h.head[bucket] = 0xCCCC
} }
} else { } else {
/* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position /* Fill |addr| array with 0xCCCCCCCC value. Because of wrapping, position
processed by hasher never reaches 3GB + 64M; this makes all new chains processed by hasher never reaches 3GB + 64M; this makes all new chains
to be terminated after the first node. */ to be terminated after the first node. */
var i int var i int
for i = 0; i < len(self.addr); i++ { for i = 0; i < len(h.addr); i++ {
self.addr[i] = 0xCCCCCCCC h.addr[i] = 0xCCCCCCCC
} }
self.head = [1 << 15]uint16{} h.head = [1 << 15]uint16{}
} }
self.tiny_hash = [65536]byte{} h.tiny_hash = [65536]byte{}
self.free_slot_idx = [512]uint16{} h.free_slot_idx = [512]uint16{}
} }
/* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and prepend
@ -120,15 +119,15 @@ func StoreRangeH42(handle HasherHandle, data []byte, mask uint, ix_start uint, i
} }
} }
func StitchToPreviousBlockH42(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *H42) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
if num_bytes >= HashTypeLengthH42()-1 && position >= 3 { if num_bytes >= HashTypeLengthH42()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH42(handle, ringbuffer, ring_buffer_mask, position-3) StoreH42(h, ringbuffer, ring_buffer_mask, position-3)
StoreH42(handle, ringbuffer, ring_buffer_mask, position-2) StoreH42(h, ringbuffer, ring_buffer_mask, position-2)
StoreH42(handle, ringbuffer, ring_buffer_mask, position-1) StoreH42(h, ringbuffer, ring_buffer_mask, position-1)
} }
} }

35
h5.go
View File

@ -52,30 +52,27 @@ func BucketsH5(self *H5) []uint32 {
return []uint32(self.buckets) return []uint32(self.buckets)
} }
func InitializeH5(handle HasherHandle, params *BrotliEncoderParams) { func (h *H5) Initialize(params *BrotliEncoderParams) {
var common *HasherCommon = handle.Common() h.hash_shift_ = 32 - h.params.bucket_bits
var self *H5 = SelfH5(handle) h.bucket_size_ = uint(1) << uint(h.params.bucket_bits)
self.hash_shift_ = 32 - common.params.bucket_bits h.block_size_ = uint(1) << uint(h.params.block_bits)
self.bucket_size_ = uint(1) << uint(common.params.bucket_bits) h.block_mask_ = uint32(h.block_size_ - 1)
self.block_size_ = uint(1) << uint(common.params.block_bits) h.num = make([]uint16, h.bucket_size_)
self.block_mask_ = uint32(self.block_size_ - 1) h.buckets = make([]uint32, h.block_size_*h.bucket_size_)
self.num = make([]uint16, self.bucket_size_)
self.buckets = make([]uint32, self.block_size_*self.bucket_size_)
} }
func PrepareH5(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H5) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H5 = SelfH5(handle) var num []uint16 = h.num
var num []uint16 = NumH5(self) var partial_prepare_threshold uint = h.bucket_size_ >> 6
var partial_prepare_threshold uint = self.bucket_size_ >> 6
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
var i uint var i uint
for i = 0; i < input_size; i++ { for i = 0; i < input_size; i++ {
var key uint32 = HashBytesH5(data[i:], self.hash_shift_) var key uint32 = HashBytesH5(data[i:], h.hash_shift_)
num[key] = 0 num[key] = 0
} }
} else { } else {
for i := 0; i < int(self.bucket_size_); i++ { for i := 0; i < int(h.bucket_size_); i++ {
num[i] = 0 num[i] = 0
} }
} }
@ -100,15 +97,15 @@ func StoreRangeH5(handle HasherHandle, data []byte, mask uint, ix_start uint, ix
} }
} }
func StitchToPreviousBlockH5(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { func (h *H5) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
if num_bytes >= HashTypeLengthH5()-1 && position >= 3 { if num_bytes >= HashTypeLengthH5()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH5(handle, ringbuffer, ringbuffer_mask, position-3) StoreH5(h, ringbuffer, ringbuffer_mask, position-3)
StoreH5(handle, ringbuffer, ringbuffer_mask, position-2) StoreH5(h, ringbuffer, ringbuffer_mask, position-2)
StoreH5(handle, ringbuffer, ringbuffer_mask, position-1) StoreH5(h, ringbuffer, ringbuffer_mask, position-1)
} }
} }

17
h54.go
View File

@ -39,11 +39,10 @@ func SelfH54(handle HasherHandle) *H54 {
return handle.(*H54) return handle.(*H54)
} }
func InitializeH54(handle HasherHandle, params *BrotliEncoderParams) { func (*H54) Initialize(params *BrotliEncoderParams) {
} }
func PrepareH54(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H54) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H54 = SelfH54(handle)
var partial_prepare_threshold uint = (4 << 20) >> 7 var partial_prepare_threshold uint = (4 << 20) >> 7
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
@ -51,7 +50,7 @@ func PrepareH54(handle HasherHandle, one_shot bool, input_size uint, data []byte
for i = 0; i < input_size; i++ { for i = 0; i < input_size; i++ {
var key uint32 = HashBytesH54(data[i:]) var key uint32 = HashBytesH54(data[i:])
for i := 0; i < int(4); i++ { for i := 0; i < int(4); i++ {
self.buckets_[key:][i] = 0 h.buckets_[key:][i] = 0
} }
} }
} else { } else {
@ -59,7 +58,7 @@ func PrepareH54(handle HasherHandle, one_shot bool, input_size uint, data []byte
not filling will make the results of the compression stochastic not filling will make the results of the compression stochastic
(but correct). This is because random data would cause the (but correct). This is because random data would cause the
system to find accidentally good backward references here and there. */ system to find accidentally good backward references here and there. */
self.buckets_ = [(1 << 20) + 4]uint32{} h.buckets_ = [(1 << 20) + 4]uint32{}
} }
} }
@ -80,15 +79,15 @@ func StoreRangeH54(handle HasherHandle, data []byte, mask uint, ix_start uint, i
} }
} }
func StitchToPreviousBlockH54(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { func (h *H54) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
if num_bytes >= HashTypeLengthH54()-1 && position >= 3 { if num_bytes >= HashTypeLengthH54()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH54(handle, ringbuffer, ringbuffer_mask, position-3) StoreH54(h, ringbuffer, ringbuffer_mask, position-3)
StoreH54(handle, ringbuffer, ringbuffer_mask, position-2) StoreH54(h, ringbuffer, ringbuffer_mask, position-2)
StoreH54(handle, ringbuffer, ringbuffer_mask, position-1) StoreH54(h, ringbuffer, ringbuffer_mask, position-1)
} }
} }

41
h55.go
View File

@ -40,42 +40,40 @@ func SelfH55(handle HasherHandle) *H55 {
return handle.(*H55) return handle.(*H55)
} }
func InitializeH55(handle HasherHandle, params *BrotliEncoderParams) { func (h *H55) Initialize(params *BrotliEncoderParams) {
var self *H55 = SelfH55(handle) h.ha = nil
self.ha = nil h.hb = nil
self.hb = nil h.params = params
self.params = params
} }
/* TODO: Initialize of the hashers is defered to Prepare (and params /* TODO: Initialize of the hashers is defered to Prepare (and params
remembered here) because we don't get the one_shot and input_size params remembered here) because we don't get the one_shot and input_size params
here that are needed to know the memory size of them. Instead provide here that are needed to know the memory size of them. Instead provide
those params to all hashers InitializeH55 */ those params to all hashers InitializeH55 */
func PrepareH55(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H55) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H55 = SelfH55(handle) if h.ha == nil {
if self.ha == nil {
var common_a *HasherCommon var common_a *HasherCommon
var common_b *HasherCommon var common_b *HasherCommon
self.ha = new(H54) h.ha = new(H54)
common_a = self.ha.Common() common_a = h.ha.Common()
common_a.params = self.params.hasher common_a.params = h.params.hasher
common_a.is_prepared_ = false common_a.is_prepared_ = false
common_a.dict_num_lookups = 0 common_a.dict_num_lookups = 0
common_a.dict_num_matches = 0 common_a.dict_num_matches = 0
InitializeH54(self.ha, self.params) h.ha.Initialize(h.params)
self.hb = new(HROLLING_FAST) h.hb = new(HROLLING_FAST)
common_b = self.hb.Common() common_b = h.hb.Common()
common_b.params = self.params.hasher common_b.params = h.params.hasher
common_b.is_prepared_ = false common_b.is_prepared_ = false
common_b.dict_num_lookups = 0 common_b.dict_num_lookups = 0
common_b.dict_num_matches = 0 common_b.dict_num_matches = 0
InitializeHROLLING_FAST(self.hb, self.params) h.hb.Initialize(h.params)
} }
PrepareH54(self.ha, one_shot, input_size, data) h.ha.Prepare(one_shot, input_size, data)
PrepareHROLLING_FAST(self.hb, one_shot, input_size, data) h.hb.Prepare(one_shot, input_size, data)
} }
func StoreH55(handle HasherHandle, data []byte, mask uint, ix uint) { func StoreH55(handle HasherHandle, data []byte, mask uint, ix uint) {
@ -90,10 +88,9 @@ func StoreRangeH55(handle HasherHandle, data []byte, mask uint, ix_start uint, i
StoreRangeHROLLING_FAST(self.hb, data, mask, ix_start, ix_end) StoreRangeHROLLING_FAST(self.hb, data, mask, ix_start, ix_end)
} }
func StitchToPreviousBlockH55(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *H55) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
var self *H55 = SelfH55(handle) h.ha.StitchToPreviousBlock(num_bytes, position, ringbuffer, ring_buffer_mask)
StitchToPreviousBlockH54(self.ha, num_bytes, position, ringbuffer, ring_buffer_mask) h.hb.StitchToPreviousBlock(num_bytes, position, ringbuffer, ring_buffer_mask)
StitchToPreviousBlockHROLLING_FAST(self.hb, num_bytes, position, ringbuffer, ring_buffer_mask)
} }
func PrepareDistanceCacheH55(handle HasherHandle, distance_cache []int) { func PrepareDistanceCacheH55(handle HasherHandle, distance_cache []int) {

37
h6.go
View File

@ -53,31 +53,28 @@ func BucketsH6(self *H6) []uint32 {
return []uint32(self.buckets) return []uint32(self.buckets)
} }
func InitializeH6(handle HasherHandle, params *BrotliEncoderParams) { func (h *H6) Initialize(params *BrotliEncoderParams) {
var common *HasherCommon = handle.Common() h.hash_shift_ = 64 - h.params.bucket_bits
var self *H6 = SelfH6(handle) h.hash_mask_ = (^(uint64(0))) >> uint(64-8*h.params.hash_len)
self.hash_shift_ = 64 - common.params.bucket_bits h.bucket_size_ = uint(1) << uint(h.params.bucket_bits)
self.hash_mask_ = (^(uint64(0))) >> uint(64-8*common.params.hash_len) h.block_size_ = uint(1) << uint(h.params.block_bits)
self.bucket_size_ = uint(1) << uint(common.params.bucket_bits) h.block_mask_ = uint32(h.block_size_ - 1)
self.block_size_ = uint(1) << uint(common.params.block_bits) h.num = make([]uint16, h.bucket_size_)
self.block_mask_ = uint32(self.block_size_ - 1) h.buckets = make([]uint32, h.block_size_*h.bucket_size_)
self.num = make([]uint16, self.bucket_size_)
self.buckets = make([]uint32, self.block_size_*self.bucket_size_)
} }
func PrepareH6(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H6) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H6 = SelfH6(handle) var num []uint16 = h.num
var num []uint16 = NumH6(self) var partial_prepare_threshold uint = h.bucket_size_ >> 6
var partial_prepare_threshold uint = self.bucket_size_ >> 6
/* Partial preparation is 100 times slower (per socket). */ /* Partial preparation is 100 times slower (per socket). */
if one_shot && input_size <= partial_prepare_threshold { if one_shot && input_size <= partial_prepare_threshold {
var i uint var i uint
for i = 0; i < input_size; i++ { for i = 0; i < input_size; i++ {
var key uint32 = HashBytesH6(data[i:], self.hash_mask_, self.hash_shift_) var key uint32 = HashBytesH6(data[i:], h.hash_mask_, h.hash_shift_)
num[key] = 0 num[key] = 0
} }
} else { } else {
for i := 0; i < int(self.bucket_size_); i++ { for i := 0; i < int(h.bucket_size_); i++ {
num[i] = 0 num[i] = 0
} }
} }
@ -102,15 +99,15 @@ func StoreRangeH6(handle HasherHandle, data []byte, mask uint, ix_start uint, ix
} }
} }
func StitchToPreviousBlockH6(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { func (h *H6) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
if num_bytes >= HashTypeLengthH6()-1 && position >= 3 { if num_bytes >= HashTypeLengthH6()-1 && position >= 3 {
/* Prepare the hashes for three last bytes of the last write. /* Prepare the hashes for three last bytes of the last write.
These could not be calculated before, since they require knowledge These could not be calculated before, since they require knowledge
of both the previous and the current block. */ of both the previous and the current block. */
StoreH6(handle, ringbuffer, ringbuffer_mask, position-3) StoreH6(h, ringbuffer, ringbuffer_mask, position-3)
StoreH6(handle, ringbuffer, ringbuffer_mask, position-2) StoreH6(h, ringbuffer, ringbuffer_mask, position-2)
StoreH6(handle, ringbuffer, ringbuffer_mask, position-1) StoreH6(h, ringbuffer, ringbuffer_mask, position-1)
} }
} }

41
h65.go
View File

@ -40,42 +40,40 @@ func SelfH65(handle HasherHandle) *H65 {
return handle.(*H65) return handle.(*H65)
} }
func InitializeH65(handle HasherHandle, params *BrotliEncoderParams) { func (h *H65) Initialize(params *BrotliEncoderParams) {
var self *H65 = SelfH65(handle) h.ha = nil
self.ha = nil h.hb = nil
self.hb = nil h.params = params
self.params = params
} }
/* TODO: Initialize of the hashers is defered to Prepare (and params /* TODO: Initialize of the hashers is defered to Prepare (and params
remembered here) because we don't get the one_shot and input_size params remembered here) because we don't get the one_shot and input_size params
here that are needed to know the memory size of them. Instead provide here that are needed to know the memory size of them. Instead provide
those params to all hashers InitializeH65 */ those params to all hashers InitializeH65 */
func PrepareH65(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *H65) Prepare(one_shot bool, input_size uint, data []byte) {
var self *H65 = SelfH65(handle) if h.ha == nil {
if self.ha == nil {
var common_a *HasherCommon var common_a *HasherCommon
var common_b *HasherCommon var common_b *HasherCommon
self.ha = new(H6) h.ha = new(H6)
common_a = self.ha.Common() common_a = h.ha.Common()
common_a.params = self.params.hasher common_a.params = h.params.hasher
common_a.is_prepared_ = false common_a.is_prepared_ = false
common_a.dict_num_lookups = 0 common_a.dict_num_lookups = 0
common_a.dict_num_matches = 0 common_a.dict_num_matches = 0
InitializeH6(self.ha, self.params) h.ha.Initialize(h.params)
self.hb = new(HROLLING) h.hb = new(HROLLING)
common_b = self.hb.Common() common_b = h.hb.Common()
common_b.params = self.params.hasher common_b.params = h.params.hasher
common_b.is_prepared_ = false common_b.is_prepared_ = false
common_b.dict_num_lookups = 0 common_b.dict_num_lookups = 0
common_b.dict_num_matches = 0 common_b.dict_num_matches = 0
InitializeHROLLING(self.hb, self.params) h.hb.Initialize(h.params)
} }
PrepareH6(self.ha, one_shot, input_size, data) h.ha.Prepare(one_shot, input_size, data)
PrepareHROLLING(self.hb, one_shot, input_size, data) h.hb.Prepare(one_shot, input_size, data)
} }
func StoreH65(handle HasherHandle, data []byte, mask uint, ix uint) { func StoreH65(handle HasherHandle, data []byte, mask uint, ix uint) {
@ -90,10 +88,9 @@ func StoreRangeH65(handle HasherHandle, data []byte, mask uint, ix_start uint, i
StoreRangeHROLLING(self.hb, data, mask, ix_start, ix_end) StoreRangeHROLLING(self.hb, data, mask, ix_start, ix_end)
} }
func StitchToPreviousBlockH65(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *H65) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
var self *H65 = SelfH65(handle) h.ha.StitchToPreviousBlock(num_bytes, position, ringbuffer, ring_buffer_mask)
StitchToPreviousBlockH6(self.ha, num_bytes, position, ringbuffer, ring_buffer_mask) h.hb.StitchToPreviousBlock(num_bytes, position, ringbuffer, ring_buffer_mask)
StitchToPreviousBlockHROLLING(self.hb, num_bytes, position, ringbuffer, ring_buffer_mask)
} }
func PrepareDistanceCacheH65(handle HasherHandle, distance_cache []int) { func PrepareDistanceCacheH65(handle HasherHandle, distance_cache []int) {

101
hash.go
View File

@ -31,6 +31,9 @@ func (h *HasherCommon) Common() *HasherCommon {
type HasherHandle interface { type HasherHandle interface {
Common() *HasherCommon Common() *HasherCommon
Initialize(params *BrotliEncoderParams)
Prepare(one_shot bool, input_size uint, data []byte)
StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint)
} }
type score_t uint type score_t uint
@ -280,75 +283,13 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte,
*handle = self *handle = self
common = self.Common() common = self.Common()
common.params = params.hasher common.params = params.hasher
switch common.params.type_ { self.Initialize(params)
case 2:
InitializeH2(*handle, params)
case 3:
InitializeH3(*handle, params)
case 4:
InitializeH4(*handle, params)
case 5:
InitializeH5(*handle, params)
case 6:
InitializeH6(*handle, params)
case 40:
InitializeH40(*handle, params)
case 41:
InitializeH41(*handle, params)
case 42:
InitializeH42(*handle, params)
case 54:
InitializeH54(*handle, params)
case 35:
InitializeH35(*handle, params)
case 55:
InitializeH55(*handle, params)
case 65:
InitializeH65(*handle, params)
case 10:
InitializeH10(*handle, params)
default:
break
}
HasherReset(*handle)
} }
self = *handle self = *handle
common = self.Common() common = self.Common()
if !common.is_prepared_ { if !common.is_prepared_ {
switch common.params.type_ { self.Prepare(one_shot, input_size, data)
case 2:
PrepareH2(self, one_shot, input_size, data)
case 3:
PrepareH3(self, one_shot, input_size, data)
case 4:
PrepareH4(self, one_shot, input_size, data)
case 5:
PrepareH5(self, one_shot, input_size, data)
case 6:
PrepareH6(self, one_shot, input_size, data)
case 40:
PrepareH40(self, one_shot, input_size, data)
case 41:
PrepareH41(self, one_shot, input_size, data)
case 42:
PrepareH42(self, one_shot, input_size, data)
case 54:
PrepareH54(self, one_shot, input_size, data)
case 35:
PrepareH35(self, one_shot, input_size, data)
case 55:
PrepareH55(self, one_shot, input_size, data)
case 65:
PrepareH65(self, one_shot, input_size, data)
case 10:
PrepareH10(self, one_shot, input_size, data)
default:
break
}
if position == 0 { if position == 0 {
common.dict_num_lookups = 0 common.dict_num_lookups = 0
@ -363,35 +304,5 @@ func InitOrStitchToPreviousBlock(handle *HasherHandle, data []byte, mask uint, p
var self HasherHandle var self HasherHandle
HasherSetup(handle, params, data, position, input_size, is_last) HasherSetup(handle, params, data, position, input_size, is_last)
self = *handle self = *handle
switch self.Common().params.type_ { self.StitchToPreviousBlock(input_size, position, data, mask)
case 2:
StitchToPreviousBlockH2(self, input_size, position, data, mask)
case 3:
StitchToPreviousBlockH3(self, input_size, position, data, mask)
case 4:
StitchToPreviousBlockH4(self, input_size, position, data, mask)
case 5:
StitchToPreviousBlockH5(self, input_size, position, data, mask)
case 6:
StitchToPreviousBlockH6(self, input_size, position, data, mask)
case 40:
StitchToPreviousBlockH40(self, input_size, position, data, mask)
case 41:
StitchToPreviousBlockH41(self, input_size, position, data, mask)
case 42:
StitchToPreviousBlockH42(self, input_size, position, data, mask)
case 54:
StitchToPreviousBlockH54(self, input_size, position, data, mask)
case 35:
StitchToPreviousBlockH35(self, input_size, position, data, mask)
case 55:
StitchToPreviousBlockH55(self, input_size, position, data, mask)
case 65:
StitchToPreviousBlockH65(self, input_size, position, data, mask)
case 10:
StitchToPreviousBlockH10(self, input_size, position, data, mask)
default:
break
}
} }

View File

@ -54,39 +54,37 @@ func SelfHROLLING(handle HasherHandle) *HROLLING {
return handle.(*HROLLING) return handle.(*HROLLING)
} }
func InitializeHROLLING(handle HasherHandle, params *BrotliEncoderParams) { func (h *HROLLING) Initialize(params *BrotliEncoderParams) {
var self *HROLLING = SelfHROLLING(handle)
var i uint var i uint
self.state = 0 h.state = 0
self.next_ix = 0 h.next_ix = 0
self.factor = kRollingHashMul32HROLLING h.factor = kRollingHashMul32HROLLING
/* Compute the factor of the oldest byte to remove: factor**steps modulo /* Compute the factor of the oldest byte to remove: factor**steps modulo
0xffffffff (the multiplications rely on 32-bit overflow) */ 0xffffffff (the multiplications rely on 32-bit overflow) */
self.factor_remove = 1 h.factor_remove = 1
for i = 0; i < 32; i += 1 { for i = 0; i < 32; i += 1 {
self.factor_remove *= self.factor h.factor_remove *= h.factor
} }
self.table = make([]uint32, 16777216) h.table = make([]uint32, 16777216)
for i = 0; i < 16777216; i++ { for i = 0; i < 16777216; i++ {
self.table[i] = kInvalidPosHROLLING h.table[i] = kInvalidPosHROLLING
} }
} }
func PrepareHROLLING(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *HROLLING) Prepare(one_shot bool, input_size uint, data []byte) {
var self *HROLLING = SelfHROLLING(handle)
var i uint var i uint
/* Too small size, cannot use this hasher. */ /* Too small size, cannot use this hasher. */
if input_size < 32 { if input_size < 32 {
return return
} }
self.state = 0 h.state = 0
for i = 0; i < 32; i += 1 { for i = 0; i < 32; i += 1 {
self.state = HashRollingFunctionInitialHROLLING(self.state, data[i], self.factor) h.state = HashRollingFunctionInitialHROLLING(h.state, data[i], h.factor)
} }
} }
@ -96,8 +94,7 @@ func StoreHROLLING(handle HasherHandle, data []byte, mask uint, ix uint) {
func StoreRangeHROLLING(handle HasherHandle, data []byte, mask uint, ix_start uint, ix_end uint) { func StoreRangeHROLLING(handle HasherHandle, data []byte, mask uint, ix_start uint, ix_end uint) {
} }
func StitchToPreviousBlockHROLLING(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *HROLLING) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
var self *HROLLING = SelfHROLLING(handle)
var position_masked uint var position_masked uint
/* In this case we must re-initialize the hasher from scratch from the /* In this case we must re-initialize the hasher from scratch from the
current position. */ current position. */
@ -120,8 +117,8 @@ func StitchToPreviousBlockHROLLING(handle HasherHandle, num_bytes uint, position
available = ring_buffer_mask - position_masked available = ring_buffer_mask - position_masked
} }
PrepareHROLLING(handle, false, available, ringbuffer[position&ring_buffer_mask:]) h.Prepare(false, available, ringbuffer[position&ring_buffer_mask:])
self.next_ix = position h.next_ix = position
} }
func PrepareDistanceCacheHROLLING(handle HasherHandle, distance_cache *int) { func PrepareDistanceCacheHROLLING(handle HasherHandle, distance_cache *int) {

View File

@ -52,39 +52,37 @@ func SelfHROLLING_FAST(handle HasherHandle) *HROLLING_FAST {
return handle.(*HROLLING_FAST) return handle.(*HROLLING_FAST)
} }
func InitializeHROLLING_FAST(handle HasherHandle, params *BrotliEncoderParams) { func (h *HROLLING_FAST) Initialize(params *BrotliEncoderParams) {
var self *HROLLING_FAST = SelfHROLLING_FAST(handle)
var i uint var i uint
self.state = 0 h.state = 0
self.next_ix = 0 h.next_ix = 0
self.factor = kRollingHashMul32HROLLING_FAST h.factor = kRollingHashMul32HROLLING_FAST
/* Compute the factor of the oldest byte to remove: factor**steps modulo /* Compute the factor of the oldest byte to remove: factor**steps modulo
0xffffffff (the multiplications rely on 32-bit overflow) */ 0xffffffff (the multiplications rely on 32-bit overflow) */
self.factor_remove = 1 h.factor_remove = 1
for i = 0; i < 32; i += 4 { for i = 0; i < 32; i += 4 {
self.factor_remove *= self.factor h.factor_remove *= h.factor
} }
self.table = make([]uint32, 16777216) h.table = make([]uint32, 16777216)
for i = 0; i < 16777216; i++ { for i = 0; i < 16777216; i++ {
self.table[i] = kInvalidPosHROLLING_FAST h.table[i] = kInvalidPosHROLLING_FAST
} }
} }
func PrepareHROLLING_FAST(handle HasherHandle, one_shot bool, input_size uint, data []byte) { func (h *HROLLING_FAST) Prepare(one_shot bool, input_size uint, data []byte) {
var self *HROLLING_FAST = SelfHROLLING_FAST(handle)
var i uint var i uint
/* Too small size, cannot use this hasher. */ /* Too small size, cannot use this hasher. */
if input_size < 32 { if input_size < 32 {
return return
} }
self.state = 0 h.state = 0
for i = 0; i < 32; i += 4 { for i = 0; i < 32; i += 4 {
self.state = HashRollingFunctionInitialHROLLING_FAST(self.state, data[i], self.factor) h.state = HashRollingFunctionInitialHROLLING_FAST(h.state, data[i], h.factor)
} }
} }
@ -94,8 +92,7 @@ func StoreHROLLING_FAST(handle HasherHandle, data []byte, mask uint, ix uint) {
func StoreRangeHROLLING_FAST(handle HasherHandle, data []byte, mask uint, ix_start uint, ix_end uint) { func StoreRangeHROLLING_FAST(handle HasherHandle, data []byte, mask uint, ix_start uint, ix_end uint) {
} }
func StitchToPreviousBlockHROLLING_FAST(handle HasherHandle, num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) { func (h *HROLLING_FAST) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ring_buffer_mask uint) {
var self *HROLLING_FAST = SelfHROLLING_FAST(handle)
var position_masked uint var position_masked uint
/* In this case we must re-initialize the hasher from scratch from the /* In this case we must re-initialize the hasher from scratch from the
current position. */ current position. */
@ -118,8 +115,8 @@ func StitchToPreviousBlockHROLLING_FAST(handle HasherHandle, num_bytes uint, pos
available = ring_buffer_mask - position_masked available = ring_buffer_mask - position_masked
} }
PrepareHROLLING_FAST(handle, false, available, ringbuffer[position&ring_buffer_mask:]) h.Prepare(false, available, ringbuffer[position&ring_buffer_mask:])
self.next_ix = position h.next_ix = position
} }
func PrepareDistanceCacheHROLLING_FAST(handle HasherHandle, distance_cache *int) { func PrepareDistanceCacheHROLLING_FAST(handle HasherHandle, distance_cache *int) {