diff --git a/bit_reader.go b/bit_reader.go index b5a208a..3842d9f 100644 --- a/bit_reader.go +++ b/bit_reader.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* Copyright 2013 Google Inc. All Rights Reserved. Distributed under MIT license. @@ -112,7 +114,7 @@ func BrotliFillBitWindow(br *BrotliBitReader, n_bits uint32) { if br.bit_pos_ >= 32 { br.val_ >>= 32 br.bit_pos_ ^= 32 /* here same as -= 32 because of the if condition */ - br.val_ |= (uint64(BROTLI_UNALIGNED_LOAD32LE(br.input[br.byte_pos:]))) << 32 + br.val_ |= (uint64(binary.LittleEndian.Uint32(br.input[br.byte_pos:]))) << 32 br.byte_pos += 4 } } diff --git a/compress_fragment.go b/compress_fragment.go index 6569dff..9f389b6 100644 --- a/compress_fragment.go +++ b/compress_fragment.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* Copyright 2015 Google Inc. All Rights Reserved. Distributed under MIT license. @@ -47,7 +49,7 @@ package brotli OUTPUT: maximal copy distance <= |input_size| OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */ func Hash5(p []byte, shift uint) uint32 { - var h uint64 = (BROTLI_UNALIGNED_LOAD64LE(p) << 24) * uint64(kHashMul32_a) + var h uint64 = (binary.LittleEndian.Uint64(p) << 24) * uint64(kHashMul32_a) return uint32(h >> shift) } @@ -695,7 +697,7 @@ emit_commands: compression we first update "table" with the hashes of some positions within the last copy. */ { - var input_bytes uint64 = BROTLI_UNALIGNED_LOAD64LE(in[ip-3:]) + 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) table[prev_hash] = int(ip - base_ip - 3) @@ -732,7 +734,7 @@ emit_commands: compression we first update "table" with the hashes of some positions within the last copy. */ { - var input_bytes uint64 = BROTLI_UNALIGNED_LOAD64LE(in[ip-3:]) + 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) table[prev_hash] = int(ip - base_ip - 3) diff --git a/compress_fragment_two_pass.go b/compress_fragment_two_pass.go index 87d4095..d621a27 100644 --- a/compress_fragment_two_pass.go +++ b/compress_fragment_two_pass.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* Copyright 2015 Google Inc. All Rights Reserved. Distributed under MIT license. @@ -25,7 +27,7 @@ package brotli var kCompressFragmentTwoPassBlockSize uint = 1 << 17 func Hash1(p []byte, shift uint, length uint) uint32 { - var h uint64 = (BROTLI_UNALIGNED_LOAD64LE(p) << ((8 - length) * 8)) * uint64(kHashMul32_a) + var h uint64 = (binary.LittleEndian.Uint64(p) << ((8 - length) * 8)) * uint64(kHashMul32_a) return uint32(h >> shift) } @@ -349,7 +351,7 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var prev_hash uint32 if min_match == 4 { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(input[ip-3:]) + 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) table[prev_hash] = int(ip - base_ip - 3) @@ -358,14 +360,14 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr prev_hash = HashBytesAtOffset(input_bytes, 0, shift, min_match) table[prev_hash] = int(ip - base_ip - 1) } else { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(input[ip-5:]) + input_bytes = binary.LittleEndian.Uint64(input[ip-5:]) 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) table[prev_hash] = int(ip - base_ip - 4) prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) table[prev_hash] = int(ip - base_ip - 3) - input_bytes = BROTLI_UNALIGNED_LOAD64LE(input[ip-2:]) + 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) table[prev_hash] = int(ip - base_ip - 2) @@ -402,7 +404,7 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var prev_hash uint32 if min_match == 4 { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(input[ip-3:]) + 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) table[prev_hash] = int(ip - base_ip - 3) @@ -411,14 +413,14 @@ func CreateCommands(input []byte, block_size uint, input_size uint, base_ip_ptr prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) table[prev_hash] = int(ip - base_ip - 1) } else { - input_bytes = BROTLI_UNALIGNED_LOAD64LE(input[ip-5:]) + input_bytes = binary.LittleEndian.Uint64(input[ip-5:]) 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) table[prev_hash] = int(ip - base_ip - 4) prev_hash = HashBytesAtOffset(input_bytes, 2, shift, min_match) table[prev_hash] = int(ip - base_ip - 3) - input_bytes = BROTLI_UNALIGNED_LOAD64LE(input[ip-2:]) + 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) table[prev_hash] = int(ip - base_ip - 2) diff --git a/fast_log.go b/fast_log.go index dacd2a4..219bffe 100644 --- a/fast_log.go +++ b/fast_log.go @@ -1,5 +1,7 @@ package brotli +import "math" + /* Copyright 2013 Google Inc. All Rights Reserved. Distributed under MIT license. @@ -311,5 +313,5 @@ func FastLog2(v uint) float64 { return float64(kLog2Table[v]) } - return log2(float64(v)) + return math.Log2(float64(v)) } diff --git a/h10.go b/h10.go index 5db09c1..41844f2 100644 --- a/h10.go +++ b/h10.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2016 Google Inc. All Rights Reserved. @@ -22,7 +24,7 @@ func (*H10) StoreLookahead() uint { } func HashBytesH10(data []byte) uint32 { - var h uint32 = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32 + var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h2.go b/h2.go index 463ba97..b0f529b 100644 --- a/h2.go +++ b/h2.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2010 Google Inc. All Rights Reserved. @@ -22,7 +24,7 @@ func (*H2) StoreLookahead() uint { the address in. The HashLongestMatch and H2 classes have separate, different implementations of hashing. */ func HashBytesH2(data []byte) uint32 { - var h uint64 = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8*5)) * kHashMul64) + var h uint64 = ((binary.LittleEndian.Uint64(data) << (64 - 8*5)) * kHashMul64) /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h3.go b/h3.go index 632f75e..5bb0dfa 100644 --- a/h3.go +++ b/h3.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2010 Google Inc. All Rights Reserved. @@ -18,7 +20,7 @@ func (*H3) StoreLookahead() uint { the address in. The HashLongestMatch and H3 classes have separate, different implementations of hashing. */ func HashBytesH3(data []byte) uint32 { - var h uint64 = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8*5)) * kHashMul64) + var h uint64 = ((binary.LittleEndian.Uint64(data) << (64 - 8*5)) * kHashMul64) /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h4.go b/h4.go index f96618b..5169a42 100644 --- a/h4.go +++ b/h4.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2010 Google Inc. All Rights Reserved. @@ -18,7 +20,7 @@ func (*H4) StoreLookahead() uint { the address in. The HashLongestMatch and H4 classes have separate, different implementations of hashing. */ func HashBytesH4(data []byte) uint32 { - var h uint64 = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8*5)) * kHashMul64) + var h uint64 = ((binary.LittleEndian.Uint64(data) << (64 - 8*5)) * kHashMul64) /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h40.go b/h40.go index 11241b9..7e787ae 100644 --- a/h40.go +++ b/h40.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2016 Google Inc. All Rights Reserved. @@ -23,7 +25,7 @@ func (*H40) StoreLookahead() uint { /* HashBytes is the function that chooses the bucket to place the address in.*/ func HashBytesH40(data []byte) uint { - var h uint32 = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32 + var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h41.go b/h41.go index 266f0f6..8ae1ae5 100644 --- a/h41.go +++ b/h41.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2016 Google Inc. All Rights Reserved. @@ -23,7 +25,7 @@ func (*H41) StoreLookahead() uint { /* HashBytes is the function that chooses the bucket to place the address in.*/ func HashBytesH41(data []byte) uint { - var h uint32 = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32 + var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h42.go b/h42.go index dc7b611..ad63a5c 100644 --- a/h42.go +++ b/h42.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2016 Google Inc. All Rights Reserved. @@ -23,7 +25,7 @@ func (*H42) StoreLookahead() uint { /* HashBytes is the function that chooses the bucket to place the address in.*/ func HashBytesH42(data []byte) uint { - var h uint32 = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32 + var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h5.go b/h5.go index fb01800..162acfc 100644 --- a/h5.go +++ b/h5.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2010 Google Inc. All Rights Reserved. @@ -23,7 +25,7 @@ func (*H5) StoreLookahead() uint { /* HashBytes is the function that chooses the bucket to place the address in. */ func HashBytesH5(data []byte, shift int) uint32 { - var h uint32 = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32 + var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h54.go b/h54.go index 8d3e24c..54154e4 100644 --- a/h54.go +++ b/h54.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2010 Google Inc. All Rights Reserved. @@ -18,7 +20,7 @@ func (*H54) StoreLookahead() uint { the address in. The HashLongestMatch and H54 classes have separate, different implementations of hashing. */ func HashBytesH54(data []byte) uint32 { - var h uint64 = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8*7)) * kHashMul64) + var h uint64 = ((binary.LittleEndian.Uint64(data) << (64 - 8*7)) * kHashMul64) /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/h6.go b/h6.go index f844484..c7361c5 100644 --- a/h6.go +++ b/h6.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* NOLINT(build/header_guard) */ /* Copyright 2010 Google Inc. All Rights Reserved. @@ -23,7 +25,7 @@ func (*H6) StoreLookahead() uint { /* HashBytes is the function that chooses the bucket to place the address in. */ func HashBytesH6(data []byte, mask uint64, shift int) uint32 { - var h uint64 = (BROTLI_UNALIGNED_LOAD64LE(data) & mask) * kHashMul64Long + var h uint64 = (binary.LittleEndian.Uint64(data) & mask) * kHashMul64Long /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/hash.go b/hash.go index 5202bec..f81d98c 100644 --- a/hash.go +++ b/hash.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* Matches data against static dictionary words, and for each length l, for which a match is found, updates matches[l] to be the minimum possible (distance << 5) + len_code. @@ -70,7 +72,7 @@ var kHashMul64 uint64 = 0x1E35A7BD1E35A7BD var kHashMul64Long uint64 = 0x1FE35A7BD3579BD3 func Hash14(data []byte) uint32 { - var h uint32 = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32 + var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/histogram.go b/histogram.go index 14d61fe..89fc8d6 100644 --- a/histogram.go +++ b/histogram.go @@ -1,5 +1,7 @@ package brotli +import "math" + /* The distance symbols effectively used by "Large Window Brotli" (32-bit). */ const BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS = 544 @@ -12,7 +14,7 @@ type HistogramLiteral struct { func HistogramClearLiteral(self *HistogramLiteral) { self.data_ = [BROTLI_NUM_LITERAL_SYMBOLS]uint32{} self.total_count_ = 0 - self.bit_cost_ = HUGE_VAL + self.bit_cost_ = math.MaxFloat64 } func ClearHistogramsLiteral(array []HistogramLiteral, length uint) { @@ -61,7 +63,7 @@ type HistogramCommand struct { func HistogramClearCommand(self *HistogramCommand) { self.data_ = [BROTLI_NUM_COMMAND_SYMBOLS]uint32{} self.total_count_ = 0 - self.bit_cost_ = HUGE_VAL + self.bit_cost_ = math.MaxFloat64 } func ClearHistogramsCommand(array []HistogramCommand, length uint) { @@ -110,7 +112,7 @@ type HistogramDistance struct { func HistogramClearDistance(self *HistogramDistance) { self.data_ = [BROTLI_NUM_DISTANCE_SYMBOLS]uint32{} self.total_count_ = 0 - self.bit_cost_ = HUGE_VAL + self.bit_cost_ = math.MaxFloat64 } func ClearHistogramsDistance(array []HistogramDistance, length uint) { diff --git a/platform.go b/platform.go index 25e2e6c..237947b 100644 --- a/platform.go +++ b/platform.go @@ -30,46 +30,6 @@ package brotli */ type brotli_reg_t uint64 -/* Read / store values byte-wise; hopefully compiler will understand. */ -func BROTLI_UNALIGNED_LOAD16LE(p []byte) uint16 { - var in []byte = []byte(p) - return uint16(in[0] | in[1]<<8) -} - -func BROTLI_UNALIGNED_LOAD32LE(p []byte) uint32 { - var in []byte = []byte(p) - var value uint32 = uint32(in[0]) - value |= uint32(in[1]) << 8 - value |= uint32(in[2]) << 16 - value |= uint32(in[3]) << 24 - return value -} - -func BROTLI_UNALIGNED_LOAD64LE(p []byte) uint64 { - var in []byte = []byte(p) - var value uint64 = uint64(in[0]) - value |= uint64(in[1]) << 8 - value |= uint64(in[2]) << 16 - value |= uint64(in[3]) << 24 - value |= uint64(in[4]) << 32 - value |= uint64(in[5]) << 40 - value |= uint64(in[6]) << 48 - value |= uint64(in[7]) << 56 - return value -} - -func BROTLI_UNALIGNED_STORE64LE(p []byte, v uint64) { - var out []byte = []byte(p) - out[0] = byte(v) - out[1] = byte(v >> 8) - out[2] = byte(v >> 16) - out[3] = byte(v >> 24) - out[4] = byte(v >> 32) - out[5] = byte(v >> 40) - out[6] = byte(v >> 48) - out[7] = byte(v >> 56) -} - func brotli_min_double(a float64, b float64) float64 { if a < b { return a diff --git a/static_dict.go b/static_dict.go index 57a93bf..80e6363 100644 --- a/static_dict.go +++ b/static_dict.go @@ -1,5 +1,7 @@ package brotli +import "encoding/binary" + /* Copyright 2013 Google Inc. All Rights Reserved. Distributed under MIT license. @@ -17,7 +19,7 @@ var kInvalidMatch uint32 = 0xFFFFFFF See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ func Hash(data []byte) uint32 { - var h uint32 = BROTLI_UNALIGNED_LOAD32LE(data) * kDictHashMul32 + var h uint32 = binary.LittleEndian.Uint32(data) * kDictHashMul32 /* The higher bits contain more mixture from the multiplication, so we take our results from there. */ diff --git a/util.go b/util.go index e89b1c9..a84553a 100644 --- a/util.go +++ b/util.go @@ -1,15 +1,7 @@ package brotli -import "math" - -const HUGE_VAL = math.MaxFloat64 - func assert(cond bool) { if !cond { panic("assertion failure") } } - -func log2(n float64) float64 { - return math.Log2(n) -}