]> Cypherpunks repositories - gostls13.git/commitdiff
hash: simplify binary operations
authorapocelipes <seve3r@outlook.com>
Thu, 26 Oct 2023 02:28:28 +0000 (02:28 +0000)
committerKeith Randall <khr@golang.org>
Sun, 19 Nov 2023 15:24:07 +0000 (15:24 +0000)
We can not use encoding/binary in hash packages because of import cycles.

So just keep the appendUint and readUint helper functions same as that
in the encoding/binary standard package.

There is no notable performance impacts.

Updates #63719

Change-Id: If47a7faaf9d422d772f32bbe1fa2f2c8a16485f4
GitHub-Last-Rev: f334fee408eff6869a7cc5f306df525d4d55d2cf
GitHub-Pull-Request: golang/go#63746
Reviewed-on: https://go-review.googlesource.com/c/go/+/537796
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
src/hash/adler32/adler32.go
src/hash/crc32/crc32.go
src/hash/crc64/crc64.go
src/hash/fnv/fnv.go

index 415af81dff1411548a6584c13c03585029bf96db..07695e947a165765f7e3fe5adc4a54860bad36d8 100644 (file)
@@ -74,16 +74,19 @@ func (d *digest) UnmarshalBinary(b []byte) error {
        return nil
 }
 
+// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
+// We copied this function because we can not import "encoding/binary" here.
 func appendUint32(b []byte, x uint32) []byte {
-       a := [4]byte{
-               byte(x >> 24),
-               byte(x >> 16),
-               byte(x >> 8),
+       return append(b,
+               byte(x>>24),
+               byte(x>>16),
+               byte(x>>8),
                byte(x),
-       }
-       return append(b, a[:]...)
+       )
 }
 
+// readUint32 is semantically the same as [binary.BigEndian.Uint32]
+// We copied this function because we can not import "encoding/binary" here.
 func readUint32(b []byte) uint32 {
        _ = b[3]
        return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
index 6d2421afabab653fd43334286be12420a025267a..170f05cf8a172e25aa95142063f0aa975a5590f0 100644 (file)
@@ -191,16 +191,19 @@ func (d *digest) UnmarshalBinary(b []byte) error {
        return nil
 }
 
+// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
+// We copied this function because we can not import "encoding/binary" here.
 func appendUint32(b []byte, x uint32) []byte {
-       a := [4]byte{
-               byte(x >> 24),
-               byte(x >> 16),
-               byte(x >> 8),
+       return append(b,
+               byte(x>>24),
+               byte(x>>16),
+               byte(x>>8),
                byte(x),
-       }
-       return append(b, a[:]...)
+       )
 }
 
+// readUint32 is semantically the same as [binary.BigEndian.Uint32]
+// We copied this function because we can not import "encoding/binary" here.
 func readUint32(b []byte) uint32 {
        _ = b[3]
        return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
index 1915ac61237ee3660910bd0fff23a035f50ae83b..17ee8eb04e2b91b19eb27856a6731694f7790858 100644 (file)
@@ -132,20 +132,23 @@ func (d *digest) UnmarshalBinary(b []byte) error {
        return nil
 }
 
+// appendUint64 is semantically the same as [binary.BigEndian.AppendUint64]
+// We copied this function because we can not import "encoding/binary" here.
 func appendUint64(b []byte, x uint64) []byte {
-       a := [8]byte{
-               byte(x >> 56),
-               byte(x >> 48),
-               byte(x >> 40),
-               byte(x >> 32),
-               byte(x >> 24),
-               byte(x >> 16),
-               byte(x >> 8),
+       return append(b,
+               byte(x>>56),
+               byte(x>>48),
+               byte(x>>40),
+               byte(x>>32),
+               byte(x>>24),
+               byte(x>>16),
+               byte(x>>8),
                byte(x),
-       }
-       return append(b, a[:]...)
+       )
 }
 
+// readUint64 is semantically the same as [binary.BigEndian.Uint64]
+// We copied this function because we can not import "encoding/binary" here.
 func readUint64(b []byte) uint64 {
        _ = b[7]
        return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
index e770f871d962cff01337c49f94f2cc436d6ba78c..a3a944a05eb1d98d04cb67c2786cf3e7f11c88f7 100644 (file)
@@ -335,35 +335,41 @@ func (s *sum128a) UnmarshalBinary(b []byte) error {
        return nil
 }
 
+// readUint32 is semantically the same as [binary.BigEndian.Uint32]
+// We copied this function because we can not import "encoding/binary" here.
 func readUint32(b []byte) uint32 {
        _ = b[3]
        return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
 }
 
+// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
+// We copied this function because we can not import "encoding/binary" here.
 func appendUint32(b []byte, x uint32) []byte {
-       a := [4]byte{
-               byte(x >> 24),
-               byte(x >> 16),
-               byte(x >> 8),
+       return append(b,
+               byte(x>>24),
+               byte(x>>16),
+               byte(x>>8),
                byte(x),
-       }
-       return append(b, a[:]...)
+       )
 }
 
+// appendUint64 is semantically the same as [binary.BigEndian.AppendUint64]
+// We copied this function because we can not import "encoding/binary" here.
 func appendUint64(b []byte, x uint64) []byte {
-       a := [8]byte{
-               byte(x >> 56),
-               byte(x >> 48),
-               byte(x >> 40),
-               byte(x >> 32),
-               byte(x >> 24),
-               byte(x >> 16),
-               byte(x >> 8),
+       return append(b,
+               byte(x>>56),
+               byte(x>>48),
+               byte(x>>40),
+               byte(x>>32),
+               byte(x>>24),
+               byte(x>>16),
+               byte(x>>8),
                byte(x),
-       }
-       return append(b, a[:]...)
+       )
 }
 
+// readUint64 is semantically the same as [binary.BigEndian.Uint64]
+// We copied this function because we can not import "encoding/binary" here.
 func readUint64(b []byte) uint64 {
        _ = b[7]
        return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |