Skip to content

Commit 07fc591

Browse files
mateusz834gopherbot
authored andcommitted
hash: use internal/byteorder
Change-Id: I58c24a58a7b32d3f8d544509db04baac1ea1b56e GitHub-Last-Rev: 7a648fd GitHub-Pull-Request: #67318 Reviewed-on: https://go-review.googlesource.com/c/go/+/585015 Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]>
1 parent 9623a35 commit 07fc591

File tree

5 files changed

+36
-142
lines changed

5 files changed

+36
-142
lines changed

src/hash/adler32/adler32.go

+3-20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package adler32
1616
import (
1717
"errors"
1818
"hash"
19+
"internal/byteorder"
1920
)
2021

2122
const (
@@ -59,7 +60,7 @@ const (
5960
func (d *digest) MarshalBinary() ([]byte, error) {
6061
b := make([]byte, 0, marshaledSize)
6162
b = append(b, magic...)
62-
b = appendUint32(b, uint32(*d))
63+
b = byteorder.BeAppendUint32(b, uint32(*d))
6364
return b, nil
6465
}
6566

@@ -70,28 +71,10 @@ func (d *digest) UnmarshalBinary(b []byte) error {
7071
if len(b) != marshaledSize {
7172
return errors.New("hash/adler32: invalid hash state size")
7273
}
73-
*d = digest(readUint32(b[len(magic):]))
74+
*d = digest(byteorder.BeUint32(b[len(magic):]))
7475
return nil
7576
}
7677

77-
// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
78-
// We copied this function because we can not import "encoding/binary" here.
79-
func appendUint32(b []byte, x uint32) []byte {
80-
return append(b,
81-
byte(x>>24),
82-
byte(x>>16),
83-
byte(x>>8),
84-
byte(x),
85-
)
86-
}
87-
88-
// readUint32 is semantically the same as [binary.BigEndian.Uint32]
89-
// We copied this function because we can not import "encoding/binary" here.
90-
func readUint32(b []byte) uint32 {
91-
_ = b[3]
92-
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
93-
}
94-
9578
// Add p to the running checksum d.
9679
func update(d digest, p []byte) digest {
9780
s1, s2 := uint32(d&0xffff), uint32(d>>16)

src/hash/crc32/crc32.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package crc32
1515
import (
1616
"errors"
1717
"hash"
18+
"internal/byteorder"
1819
"sync"
1920
"sync/atomic"
2021
)
@@ -172,8 +173,8 @@ const (
172173
func (d *digest) MarshalBinary() ([]byte, error) {
173174
b := make([]byte, 0, marshaledSize)
174175
b = append(b, magic...)
175-
b = appendUint32(b, tableSum(d.tab))
176-
b = appendUint32(b, d.crc)
176+
b = byteorder.BeAppendUint32(b, tableSum(d.tab))
177+
b = byteorder.BeAppendUint32(b, d.crc)
177178
return b, nil
178179
}
179180

@@ -184,31 +185,13 @@ func (d *digest) UnmarshalBinary(b []byte) error {
184185
if len(b) != marshaledSize {
185186
return errors.New("hash/crc32: invalid hash state size")
186187
}
187-
if tableSum(d.tab) != readUint32(b[4:]) {
188+
if tableSum(d.tab) != byteorder.BeUint32(b[4:]) {
188189
return errors.New("hash/crc32: tables do not match")
189190
}
190-
d.crc = readUint32(b[8:])
191+
d.crc = byteorder.BeUint32(b[8:])
191192
return nil
192193
}
193194

194-
// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
195-
// We copied this function because we can not import "encoding/binary" here.
196-
func appendUint32(b []byte, x uint32) []byte {
197-
return append(b,
198-
byte(x>>24),
199-
byte(x>>16),
200-
byte(x>>8),
201-
byte(x),
202-
)
203-
}
204-
205-
// readUint32 is semantically the same as [binary.BigEndian.Uint32]
206-
// We copied this function because we can not import "encoding/binary" here.
207-
func readUint32(b []byte) uint32 {
208-
_ = b[3]
209-
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
210-
}
211-
212195
func update(crc uint32, tab *Table, p []byte, checkInitIEEE bool) uint32 {
213196
switch {
214197
case haveCastagnoli.Load() && tab == castagnoliTable:
@@ -261,7 +244,7 @@ func tableSum(t *Table) uint32 {
261244
b := a[:0]
262245
if t != nil {
263246
for _, x := range t {
264-
b = appendUint32(b, x)
247+
b = byteorder.BeAppendUint32(b, x)
265248
}
266249
}
267250
return ChecksumIEEE(b)

src/hash/crc64/crc64.go

+6-28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package crc64
1010
import (
1111
"errors"
1212
"hash"
13+
"internal/byteorder"
1314
"sync"
1415
)
1516

@@ -113,8 +114,8 @@ const (
113114
func (d *digest) MarshalBinary() ([]byte, error) {
114115
b := make([]byte, 0, marshaledSize)
115116
b = append(b, magic...)
116-
b = appendUint64(b, tableSum(d.tab))
117-
b = appendUint64(b, d.crc)
117+
b = byteorder.BeAppendUint64(b, tableSum(d.tab))
118+
b = byteorder.BeAppendUint64(b, d.crc)
118119
return b, nil
119120
}
120121

@@ -125,36 +126,13 @@ func (d *digest) UnmarshalBinary(b []byte) error {
125126
if len(b) != marshaledSize {
126127
return errors.New("hash/crc64: invalid hash state size")
127128
}
128-
if tableSum(d.tab) != readUint64(b[4:]) {
129+
if tableSum(d.tab) != byteorder.BeUint64(b[4:]) {
129130
return errors.New("hash/crc64: tables do not match")
130131
}
131-
d.crc = readUint64(b[12:])
132+
d.crc = byteorder.BeUint64(b[12:])
132133
return nil
133134
}
134135

135-
// appendUint64 is semantically the same as [binary.BigEndian.AppendUint64]
136-
// We copied this function because we can not import "encoding/binary" here.
137-
func appendUint64(b []byte, x uint64) []byte {
138-
return append(b,
139-
byte(x>>56),
140-
byte(x>>48),
141-
byte(x>>40),
142-
byte(x>>32),
143-
byte(x>>24),
144-
byte(x>>16),
145-
byte(x>>8),
146-
byte(x),
147-
)
148-
}
149-
150-
// readUint64 is semantically the same as [binary.BigEndian.Uint64]
151-
// We copied this function because we can not import "encoding/binary" here.
152-
func readUint64(b []byte) uint64 {
153-
_ = b[7]
154-
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
155-
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
156-
}
157-
158136
func update(crc uint64, tab *Table, p []byte) uint64 {
159137
buildSlicing8TablesOnce()
160138
crc = ^crc
@@ -222,7 +200,7 @@ func tableSum(t *Table) uint64 {
222200
b := a[:0]
223201
if t != nil {
224202
for _, x := range t {
225-
b = appendUint64(b, x)
203+
b = byteorder.BeAppendUint64(b, x)
226204
}
227205
}
228206
return Checksum(b, MakeTable(ISO))

src/hash/fnv/fnv.go

+17-57
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package fnv
1515
import (
1616
"errors"
1717
"hash"
18+
"internal/byteorder"
1819
"math/bits"
1920
)
2021

@@ -225,44 +226,44 @@ const (
225226
func (s *sum32) MarshalBinary() ([]byte, error) {
226227
b := make([]byte, 0, marshaledSize32)
227228
b = append(b, magic32...)
228-
b = appendUint32(b, uint32(*s))
229+
b = byteorder.BeAppendUint32(b, uint32(*s))
229230
return b, nil
230231
}
231232

232233
func (s *sum32a) MarshalBinary() ([]byte, error) {
233234
b := make([]byte, 0, marshaledSize32)
234235
b = append(b, magic32a...)
235-
b = appendUint32(b, uint32(*s))
236+
b = byteorder.BeAppendUint32(b, uint32(*s))
236237
return b, nil
237238
}
238239

239240
func (s *sum64) MarshalBinary() ([]byte, error) {
240241
b := make([]byte, 0, marshaledSize64)
241242
b = append(b, magic64...)
242-
b = appendUint64(b, uint64(*s))
243+
b = byteorder.BeAppendUint64(b, uint64(*s))
243244
return b, nil
244245
}
245246

246247
func (s *sum64a) MarshalBinary() ([]byte, error) {
247248
b := make([]byte, 0, marshaledSize64)
248249
b = append(b, magic64a...)
249-
b = appendUint64(b, uint64(*s))
250+
b = byteorder.BeAppendUint64(b, uint64(*s))
250251
return b, nil
251252
}
252253

253254
func (s *sum128) MarshalBinary() ([]byte, error) {
254255
b := make([]byte, 0, marshaledSize128)
255256
b = append(b, magic128...)
256-
b = appendUint64(b, s[0])
257-
b = appendUint64(b, s[1])
257+
b = byteorder.BeAppendUint64(b, s[0])
258+
b = byteorder.BeAppendUint64(b, s[1])
258259
return b, nil
259260
}
260261

261262
func (s *sum128a) MarshalBinary() ([]byte, error) {
262263
b := make([]byte, 0, marshaledSize128)
263264
b = append(b, magic128a...)
264-
b = appendUint64(b, s[0])
265-
b = appendUint64(b, s[1])
265+
b = byteorder.BeAppendUint64(b, s[0])
266+
b = byteorder.BeAppendUint64(b, s[1])
266267
return b, nil
267268
}
268269

@@ -273,7 +274,7 @@ func (s *sum32) UnmarshalBinary(b []byte) error {
273274
if len(b) != marshaledSize32 {
274275
return errors.New("hash/fnv: invalid hash state size")
275276
}
276-
*s = sum32(readUint32(b[4:]))
277+
*s = sum32(byteorder.BeUint32(b[4:]))
277278
return nil
278279
}
279280

@@ -284,7 +285,7 @@ func (s *sum32a) UnmarshalBinary(b []byte) error {
284285
if len(b) != marshaledSize32 {
285286
return errors.New("hash/fnv: invalid hash state size")
286287
}
287-
*s = sum32a(readUint32(b[4:]))
288+
*s = sum32a(byteorder.BeUint32(b[4:]))
288289
return nil
289290
}
290291

@@ -295,7 +296,7 @@ func (s *sum64) UnmarshalBinary(b []byte) error {
295296
if len(b) != marshaledSize64 {
296297
return errors.New("hash/fnv: invalid hash state size")
297298
}
298-
*s = sum64(readUint64(b[4:]))
299+
*s = sum64(byteorder.BeUint64(b[4:]))
299300
return nil
300301
}
301302

@@ -306,7 +307,7 @@ func (s *sum64a) UnmarshalBinary(b []byte) error {
306307
if len(b) != marshaledSize64 {
307308
return errors.New("hash/fnv: invalid hash state size")
308309
}
309-
*s = sum64a(readUint64(b[4:]))
310+
*s = sum64a(byteorder.BeUint64(b[4:]))
310311
return nil
311312
}
312313

@@ -317,8 +318,8 @@ func (s *sum128) UnmarshalBinary(b []byte) error {
317318
if len(b) != marshaledSize128 {
318319
return errors.New("hash/fnv: invalid hash state size")
319320
}
320-
s[0] = readUint64(b[4:])
321-
s[1] = readUint64(b[12:])
321+
s[0] = byteorder.BeUint64(b[4:])
322+
s[1] = byteorder.BeUint64(b[12:])
322323
return nil
323324
}
324325

@@ -329,48 +330,7 @@ func (s *sum128a) UnmarshalBinary(b []byte) error {
329330
if len(b) != marshaledSize128 {
330331
return errors.New("hash/fnv: invalid hash state size")
331332
}
332-
s[0] = readUint64(b[4:])
333-
s[1] = readUint64(b[12:])
333+
s[0] = byteorder.BeUint64(b[4:])
334+
s[1] = byteorder.BeUint64(b[12:])
334335
return nil
335336
}
336-
337-
// readUint32 is semantically the same as [binary.BigEndian.Uint32]
338-
// We copied this function because we can not import "encoding/binary" here.
339-
func readUint32(b []byte) uint32 {
340-
_ = b[3]
341-
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
342-
}
343-
344-
// appendUint32 is semantically the same as [binary.BigEndian.AppendUint32]
345-
// We copied this function because we can not import "encoding/binary" here.
346-
func appendUint32(b []byte, x uint32) []byte {
347-
return append(b,
348-
byte(x>>24),
349-
byte(x>>16),
350-
byte(x>>8),
351-
byte(x),
352-
)
353-
}
354-
355-
// appendUint64 is semantically the same as [binary.BigEndian.AppendUint64]
356-
// We copied this function because we can not import "encoding/binary" here.
357-
func appendUint64(b []byte, x uint64) []byte {
358-
return append(b,
359-
byte(x>>56),
360-
byte(x>>48),
361-
byte(x>>40),
362-
byte(x>>32),
363-
byte(x>>24),
364-
byte(x>>16),
365-
byte(x>>8),
366-
byte(x),
367-
)
368-
}
369-
370-
// readUint64 is semantically the same as [binary.BigEndian.Uint64]
371-
// We copied this function because we can not import "encoding/binary" here.
372-
func readUint64(b []byte) uint64 {
373-
_ = b[7]
374-
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
375-
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
376-
}

src/hash/maphash/maphash_purego.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package maphash
88

99
import (
1010
"crypto/rand"
11+
"internal/byteorder"
1112
"math/bits"
1213
)
1314

@@ -25,7 +26,7 @@ func rthashString(s string, state uint64) uint64 {
2526
func randUint64() uint64 {
2627
buf := make([]byte, 8)
2728
_, _ = rand.Read(buf)
28-
return leUint64(buf)
29+
return byteorder.LeUint64(buf)
2930
}
3031

3132
// This is a port of wyhash implementation in runtime/hash64.go,
@@ -80,25 +81,14 @@ func r3(p []byte, k uint64) uint64 {
8081
}
8182

8283
func r4(p []byte) uint64 {
83-
return uint64(leUint32(p))
84+
return uint64(byteorder.LeUint32(p))
8485
}
8586

8687
func r8(p []byte) uint64 {
87-
return leUint64(p)
88+
return byteorder.LeUint64(p)
8889
}
8990

9091
func mix(a, b uint64) uint64 {
9192
hi, lo := bits.Mul64(a, b)
9293
return hi ^ lo
9394
}
94-
95-
func leUint32(b []byte) uint32 {
96-
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
97-
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
98-
}
99-
100-
func leUint64(b []byte) uint64 {
101-
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
102-
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
103-
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
104-
}

0 commit comments

Comments
 (0)