Use some Go library functions.

This commit is contained in:
Andy Balholm 2019-03-08 19:45:16 -08:00
parent 7ed41e6356
commit 74ae18c776
19 changed files with 61 additions and 75 deletions

View File

@ -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
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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))
}

4
h10.go
View File

@ -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. */

4
h2.go
View File

@ -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. */

4
h3.go
View File

@ -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. */

4
h4.go
View File

@ -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. */

4
h40.go
View File

@ -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. */

4
h41.go
View File

@ -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. */

4
h42.go
View File

@ -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. */

4
h5.go
View File

@ -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. */

4
h54.go
View File

@ -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. */

4
h6.go
View File

@ -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. */

View File

@ -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. */

View File

@ -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) {

View File

@ -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

View File

@ -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. */

View File

@ -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)
}