From e3d87d19320001e6081449550292d76ef660ab03 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Fri, 17 May 2024 21:40:55 +0000 Subject: [PATCH] hash: use more internal/byteorder functions to simplify the code 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 Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Auto-Submit: Ian Lance Taylor Auto-Submit: Dmitri Shuralyov Commit-Queue: Ian Lance Taylor --- src/hash/crc32/crc32_generic.go | 4 +++- src/hash/crc64/crc64.go | 3 +-- src/hash/fnv/fnv.go | 20 ++++++++------------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/hash/crc32/crc32_generic.go b/src/hash/crc32/crc32_generic.go index abacbb663d..d581710bc8 100644 --- a/src/hash/crc32/crc32_generic.go +++ b/src/hash/crc32/crc32_generic.go @@ -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] diff --git a/src/hash/crc64/crc64.go b/src/hash/crc64/crc64.go index e3f1d1627f..4cdb4c7e77 100644 --- a/src/hash/crc64/crc64.go +++ b/src/hash/crc64/crc64.go @@ -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] ^ diff --git a/src/hash/fnv/fnv.go b/src/hash/fnv/fnv.go index dc77b30788..bf95bb32a3 100644 --- a/src/hash/fnv/fnv.go +++ b/src/hash/fnv/fnv.go @@ -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 ( -- 2.48.1