Skip to content

Commit

Permalink
Use in more places.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Jan 14, 2025
1 parent 75ce7ef commit 07a5dff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
19 changes: 10 additions & 9 deletions flate/huffman_bit_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package flate

import (
"encoding/binary"
"fmt"
"io"
"math"

"github.com/klauspost/compress/internal/le"
)

const (
Expand Down Expand Up @@ -438,7 +439,7 @@ func (w *huffmanBitWriter) writeOutBits() {
n := w.nbytes

// We over-write, but faster...
binary.LittleEndian.PutUint64(w.bytes[n:], bits)
le.Store64(w.bytes[n:], bits)

Check failure on line 442 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 442 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 442 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 442 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 442 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 442 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 442 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
n += 6

if n >= bufferFlushSize {
Expand Down Expand Up @@ -854,7 +855,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
bits |= c.code64() << (nbits & 63)
nbits += c.len()
if nbits >= 48 {
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
le.Store64(w.bytes[nbytes:], bits)

Check failure on line 858 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 858 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 858 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 858 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 858 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 858 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 858 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
bits >>= 48
nbits -= 48
Expand Down Expand Up @@ -882,7 +883,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
bits |= c.code64() << (nbits & 63)
nbits += c.len()
if nbits >= 48 {
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
le.Store64(w.bytes[nbytes:], bits)

Check failure on line 886 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 886 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 886 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 886 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 886 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 886 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 886 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
bits >>= 48
nbits -= 48
Expand All @@ -905,7 +906,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
bits |= uint64(extraLength) << (nbits & 63)
nbits += extraLengthBits
if nbits >= 48 {
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
le.Store64(w.bytes[nbytes:], bits)

Check failure on line 909 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 909 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 909 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 909 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 909 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 909 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 909 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
bits >>= 48
nbits -= 48
Expand All @@ -931,7 +932,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
bits |= c.code64() << (nbits & 63)
nbits += c.len()
if nbits >= 48 {
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
le.Store64(w.bytes[nbytes:], bits)

Check failure on line 935 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 935 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 935 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 935 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 935 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 935 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 935 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
bits >>= 48
nbits -= 48
Expand All @@ -953,7 +954,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
bits |= uint64((offset-(offsetComb>>8))&matchOffsetOnlyMask) << (nbits & 63)
nbits += uint8(offsetComb)
if nbits >= 48 {
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
le.Store64(w.bytes[nbytes:], bits)

Check failure on line 957 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 957 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 957 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 957 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 957 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 957 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 957 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
bits >>= 48
nbits -= 48
Expand Down Expand Up @@ -1107,7 +1108,7 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
// We must have at least 48 bits free.
if nbits >= 8 {
n := nbits >> 3
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
le.Store64(w.bytes[nbytes:], bits)

Check failure on line 1111 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 1111 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 1111 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 1111 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 1111 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 1111 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 1111 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
bits >>= (n * 8) & 63
nbits -= n * 8
nbytes += n
Expand Down Expand Up @@ -1136,7 +1137,7 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
// Remaining...
for _, t := range input {
if nbits >= 48 {
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
le.Store64(w.bytes[nbytes:], bits)

Check failure on line 1140 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other (nounsafe)

undefined: le.Store64

Check failure on line 1140 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-other ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 1140 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd (nounsafe)

undefined: le.Store64

Check failure on line 1140 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-zstd ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 1140 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 (nounsafe)

undefined: le.Store64

Check failure on line 1140 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / fuzz-s2 ("noasm,nounsafe")

undefined: le.Store64

Check failure on line 1140 in flate/huffman_bit_writer.go

View workflow job for this annotation

GitHub Actions / build-special

undefined: le.Store64
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
bits >>= 48
nbits -= 48
Expand Down
31 changes: 16 additions & 15 deletions flate/level1.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package flate

import (
"encoding/binary"
"fmt"
"math/bits"

"github.com/klauspost/compress/internal/le"
)

// fastGen maintains the table for matches,
Expand Down Expand Up @@ -126,26 +127,26 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
l = e.matchlenLong(s+4, t+4, src) + 4
} else {
// inlined:
a := src[s+4:]
b := src[t+4:]
for len(a) >= 8 {
if diff := binary.LittleEndian.Uint64(a) ^ binary.LittleEndian.Uint64(b); diff != 0 {
a := src[s:]
b := src[t:]
left := len(a)
for left >= 8 {
if diff := le.Load64(a, l) ^ le.Load64(b, l); diff != 0 {
l += int32(bits.TrailingZeros64(diff) >> 3)
break
goto endMatch
}
l += 8
a = a[8:]
b = b[8:]
left -= 8
}
if len(a) < 8 {
b = b[:len(a)]
for i := range a {
if a[i] != b[i] {
break
}
l++
a = a[l:]
b = b[l:]
for i := range a {
if a[i] != b[i] {
break
}
l++
}
endMatch:
}

// Extend backwards
Expand Down
11 changes: 7 additions & 4 deletions internal/le/unsafe_enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,40 @@ import (
)

// Load16 will load from b at index i.
// If the compiler can prove that b is at least 1 byte this will be without bounds check.
func Load16[I Indexer](b []byte, i I) uint16 {
//return binary.LittleEndian.Uint16(b[i:])
//return *(*uint16)(unsafe.Pointer(&b[i]))
return *(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(b))) + uintptr(i)*unsafe.Sizeof(b[0])))
}

// Load32 will load from b at index i.
// If the compiler can prove that b is at least 1 byte this will be without bounds check.
func Load32[I Indexer](b []byte, i I) uint32 {
//return binary.LittleEndian.Uint32(b[i:])
//return *(*uint32)(unsafe.Pointer(&b[i]))
return *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(b))) + uintptr(i)*unsafe.Sizeof(b[0])))
}

// Load64 will load from b at index i.
// If the compiler can prove that b is at least 1 byte this will be without bounds check.
func Load64[I Indexer](b []byte, i I) uint64 {
//return binary.LittleEndian.Uint64(b[i:])
//return *(*uint64)(unsafe.Pointer(&b[i]))
return *(*uint64)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(b))) + uintptr(i)*unsafe.Sizeof(b[0])))
}

// Store16 will store v at b.
// If the compiler can prove
func Store16(b []byte, v uint16) {
//binary.LittleEndian.PutUint16(b, v)
*(*uint16)(unsafe.Pointer(unsafe.SliceData(b))) = v
}

// Store32 will store v at b.
func Store32(b []byte, v uint32) {
//binary.LittleEndian.PutUint32(b, v)
*(*uint32)(unsafe.Pointer(unsafe.SliceData(b))) = v
}

// Store64 will store v at b.
func Store64(b []byte, v uint64) {
//binary.LittleEndian.PutUint64(b, v)
*(*uint64)(unsafe.Pointer(unsafe.SliceData(b))) = v
}

0 comments on commit 07a5dff

Please sign in to comment.