Skip to content

Commit

Permalink
Use some Go library functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalholm committed Mar 9, 2019
1 parent 7ed41e6 commit 74ae18c
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 75 deletions.
4 changes: 3 additions & 1 deletion bit_reader.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* Copyright 2013 Google Inc. All Rights Reserved.
Distributed under MIT license.
Expand Down Expand Up @@ -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
}
}
Expand Down
8 changes: 5 additions & 3 deletions compress_fragment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* Copyright 2015 Google Inc. All Rights Reserved.
Distributed under MIT license.
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions compress_fragment_two_pass.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* Copyright 2015 Google Inc. All Rights Reserved.
Distributed under MIT license.
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion fast_log.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "math"

/* Copyright 2013 Google Inc. All Rights Reserved.
Distributed under MIT license.
Expand Down Expand Up @@ -311,5 +313,5 @@ func FastLog2(v uint) float64 {
return float64(kLog2Table[v])
}

return log2(float64(v))
return math.Log2(float64(v))
}
4 changes: 3 additions & 1 deletion h10.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2016 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h2.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2010 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h3.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2010 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h4.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2010 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h40.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2016 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h41.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2016 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h42.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2016 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h5.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2010 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h54.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2010 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion h6.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package brotli

import "encoding/binary"

/* NOLINT(build/header_guard) */
/* Copyright 2010 Google Inc. All Rights Reserved.
Expand All @@ -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. */
Expand Down
4 changes: 3 additions & 1 deletion hash.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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. */
Expand Down
8 changes: 5 additions & 3 deletions histogram.go
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 74ae18c

Please sign in to comment.