]> Cypherpunks repositories - gostls13.git/commitdiff
hash: use more internal/byteorder functions to simplify the code
authorapocelipes <seve3r@outlook.com>
Fri, 17 May 2024 21:40:55 +0000 (21:40 +0000)
committerGopher Robot <gobot@golang.org>
Sat, 18 May 2024 22:36:41 +0000 (22:36 +0000)
A follow-up for the CL 585015.

Change-Id: I412f33f1d75abe1446cb3fd742d44d3cb4350380
GitHub-Last-Rev: 554ace757cc75389e7a3c441d3a60cfa85c815a5
GitHub-Pull-Request: golang/go#67476
Reviewed-on: https://go-review.googlesource.com/c/go/+/586240
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Commit-Queue: Ian Lance Taylor <iant@google.com>

src/hash/crc32/crc32_generic.go
src/hash/crc64/crc64.go
src/hash/fnv/fnv.go

index abacbb663d45380e8aff449c2022f75d3499d9cb..d581710bc82ee6513ff635e78adb752031aed83d 100644 (file)
@@ -12,6 +12,8 @@
 
 package crc32
 
+import "internal/byteorder"
+
 // simpleMakeTable allocates and constructs a Table for the specified
 // polynomial. The table is suitable for use with the simple algorithm
 // (simpleUpdate).
@@ -74,7 +76,7 @@ func slicingUpdate(crc uint32, tab *slicing8Table, p []byte) uint32 {
        if len(p) >= slicing8Cutoff {
                crc = ^crc
                for len(p) > 8 {
-                       crc ^= uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
+                       crc ^= byteorder.LeUint32(p)
                        crc = tab[0][p[7]] ^ tab[1][p[6]] ^ tab[2][p[5]] ^ tab[3][p[4]] ^
                                tab[4][crc>>24] ^ tab[5][(crc>>16)&0xFF] ^
                                tab[6][(crc>>8)&0xFF] ^ tab[7][crc&0xFF]
index e3f1d1627f414655ff672f8939fe3edbaa7295f4..4cdb4c7e77f777c96adf1fcf47b0d38a2e7c507b 100644 (file)
@@ -153,8 +153,7 @@ func update(crc uint64, tab *Table, p []byte) uint64 {
                }
                // Update using slicing-by-8
                for len(p) > 8 {
-                       crc ^= uint64(p[0]) | uint64(p[1])<<8 | uint64(p[2])<<16 | uint64(p[3])<<24 |
-                               uint64(p[4])<<32 | uint64(p[5])<<40 | uint64(p[6])<<48 | uint64(p[7])<<56
+                       crc ^= byteorder.LeUint64(p)
                        crc = helperTable[7][crc&0xff] ^
                                helperTable[6][(crc>>8)&0xff] ^
                                helperTable[5][(crc>>16)&0xff] ^
index dc77b30788c1c5153df581a4b961f22c00b6ae1f..bf95bb32a3c6e040fcdf9bc051fefd6a59539715 100644 (file)
@@ -179,36 +179,32 @@ func (s *sum128a) BlockSize() int { return 1 }
 
 func (s *sum32) Sum(in []byte) []byte {
        v := uint32(*s)
-       return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
+       return byteorder.BeAppendUint32(in, v)
 }
 
 func (s *sum32a) Sum(in []byte) []byte {
        v := uint32(*s)
-       return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
+       return byteorder.BeAppendUint32(in, v)
 }
 
 func (s *sum64) Sum(in []byte) []byte {
        v := uint64(*s)
-       return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
+       return byteorder.BeAppendUint64(in, v)
 }
 
 func (s *sum64a) Sum(in []byte) []byte {
        v := uint64(*s)
-       return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
+       return byteorder.BeAppendUint64(in, v)
 }
 
 func (s *sum128) Sum(in []byte) []byte {
-       return append(in,
-               byte(s[0]>>56), byte(s[0]>>48), byte(s[0]>>40), byte(s[0]>>32), byte(s[0]>>24), byte(s[0]>>16), byte(s[0]>>8), byte(s[0]),
-               byte(s[1]>>56), byte(s[1]>>48), byte(s[1]>>40), byte(s[1]>>32), byte(s[1]>>24), byte(s[1]>>16), byte(s[1]>>8), byte(s[1]),
-       )
+       ret := byteorder.BeAppendUint64(in, s[0])
+       return byteorder.BeAppendUint64(ret, s[1])
 }
 
 func (s *sum128a) Sum(in []byte) []byte {
-       return append(in,
-               byte(s[0]>>56), byte(s[0]>>48), byte(s[0]>>40), byte(s[0]>>32), byte(s[0]>>24), byte(s[0]>>16), byte(s[0]>>8), byte(s[0]),
-               byte(s[1]>>56), byte(s[1]>>48), byte(s[1]>>40), byte(s[1]>>32), byte(s[1]>>24), byte(s[1]>>16), byte(s[1]>>8), byte(s[1]),
-       )
+       ret := byteorder.BeAppendUint64(in, s[0])
+       return byteorder.BeAppendUint64(ret, s[1])
 }
 
 const (