Skip to content

Commit f3a7439

Browse files
mateusz834gopherbot
authored andcommitted
internal/byteorder: new package
Currently in a lot of packages we define functions for appending/decoding mostly BigEndian data (see internal/chacha8rand, net/netip, internal/boring/sha, hash/crc64, and probably more), because we don't want to depend on encoding/binary, because of #54097. This change introduces a new package internal/byteorder, that will allow us to remove all of the functions and replace them with internal/byteorder. Updates #54097 Change-Id: I03e5ea1eb721dd98bdabdb25786f889cc5de54c5 GitHub-Last-Rev: 3f07d3d GitHub-Pull-Request: #67183 Reviewed-on: https://go-review.googlesource.com/c/go/+/583298 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]>
1 parent f6c3a3e commit f3a7439

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

src/cmd/compile/internal/inline/inl.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,28 @@ opSwitch:
545545
}
546546
}
547547
}
548+
549+
if n.Fun.Op() == ir.ONAME {
550+
name := n.Fun.(*ir.Name)
551+
if name.Class == ir.PFUNC {
552+
// Special case: on architectures that can do unaligned loads,
553+
// explicitly mark internal/byteorder methods as cheap,
554+
// because in practice they are, even though our inlining
555+
// budgeting system does not see that. See issue 42958.
556+
if base.Ctxt.Arch.CanMergeLoads && name.Sym().Pkg.Path == "internal/byteorder" {
557+
switch name.Sym().Name {
558+
case "LeUint64", "LeUint32", "LeUint16",
559+
"BeUint64", "BeUint32", "BeUint16",
560+
"LePutUint64", "LePutUint32", "LePutUint16",
561+
"BePutUint64", "BePutUint32", "BePutUint16",
562+
"LeAppendUint64", "LeAppendUint32", "LeAppendUint16",
563+
"BeAppendUint64", "BeAppendUint32", "BeAppendUint16":
564+
cheap = true
565+
}
566+
}
567+
}
568+
}
569+
548570
if cheap {
549571
break // treat like any other node, that is, cost of 1
550572
}

src/go/build/deps_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var depsRules = `
6767
internal/goos
6868
< internal/bytealg
6969
< internal/stringslite
70+
< internal/byteorder
7071
< internal/itoa
7172
< internal/unsafeheader
7273
< runtime/internal/sys

src/internal/byteorder/byteorder.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package byteorder provides functions for decoding and encoding
6+
// little and big endian integer types from/to byte slices.
7+
package byteorder
8+
9+
func LeUint16(b []byte) uint16 {
10+
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
11+
return uint16(b[0]) | uint16(b[1])<<8
12+
}
13+
14+
func LePutUint16(b []byte, v uint16) {
15+
_ = b[1] // early bounds check to guarantee safety of writes below
16+
b[0] = byte(v)
17+
b[1] = byte(v >> 8)
18+
}
19+
20+
func LeAppendUint16(b []byte, v uint16) []byte {
21+
return append(b,
22+
byte(v),
23+
byte(v>>8),
24+
)
25+
}
26+
27+
func LeUint32(b []byte) uint32 {
28+
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
29+
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
30+
}
31+
32+
func LePutUint32(b []byte, v uint32) {
33+
_ = b[3] // early bounds check to guarantee safety of writes below
34+
b[0] = byte(v)
35+
b[1] = byte(v >> 8)
36+
b[2] = byte(v >> 16)
37+
b[3] = byte(v >> 24)
38+
}
39+
40+
func LeAppendUint32(b []byte, v uint32) []byte {
41+
return append(b,
42+
byte(v),
43+
byte(v>>8),
44+
byte(v>>16),
45+
byte(v>>24),
46+
)
47+
}
48+
49+
func LeUint64(b []byte) uint64 {
50+
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
51+
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
52+
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
53+
}
54+
55+
func LePutUint64(b []byte, v uint64) {
56+
_ = b[7] // early bounds check to guarantee safety of writes below
57+
b[0] = byte(v)
58+
b[1] = byte(v >> 8)
59+
b[2] = byte(v >> 16)
60+
b[3] = byte(v >> 24)
61+
b[4] = byte(v >> 32)
62+
b[5] = byte(v >> 40)
63+
b[6] = byte(v >> 48)
64+
b[7] = byte(v >> 56)
65+
}
66+
67+
func LeAppendUint64(b []byte, v uint64) []byte {
68+
return append(b,
69+
byte(v),
70+
byte(v>>8),
71+
byte(v>>16),
72+
byte(v>>24),
73+
byte(v>>32),
74+
byte(v>>40),
75+
byte(v>>48),
76+
byte(v>>56),
77+
)
78+
}
79+
80+
func BeUint16(b []byte) uint16 {
81+
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
82+
return uint16(b[1]) | uint16(b[0])<<8
83+
}
84+
85+
func BePutUint16(b []byte, v uint16) {
86+
_ = b[1] // early bounds check to guarantee safety of writes below
87+
b[0] = byte(v >> 8)
88+
b[1] = byte(v)
89+
}
90+
91+
func BeAppendUint16(b []byte, v uint16) []byte {
92+
return append(b,
93+
byte(v>>8),
94+
byte(v),
95+
)
96+
}
97+
98+
func BeUint32(b []byte) uint32 {
99+
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
100+
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
101+
}
102+
103+
func BePutUint32(b []byte, v uint32) {
104+
_ = b[3] // early bounds check to guarantee safety of writes below
105+
b[0] = byte(v >> 24)
106+
b[1] = byte(v >> 16)
107+
b[2] = byte(v >> 8)
108+
b[3] = byte(v)
109+
}
110+
111+
func BeAppendUint32(b []byte, v uint32) []byte {
112+
return append(b,
113+
byte(v>>24),
114+
byte(v>>16),
115+
byte(v>>8),
116+
byte(v),
117+
)
118+
}
119+
120+
func BeUint64(b []byte) uint64 {
121+
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
122+
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
123+
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
124+
}
125+
126+
func BePutUint64(b []byte, v uint64) {
127+
_ = b[7] // early bounds check to guarantee safety of writes below
128+
b[0] = byte(v >> 56)
129+
b[1] = byte(v >> 48)
130+
b[2] = byte(v >> 40)
131+
b[3] = byte(v >> 32)
132+
b[4] = byte(v >> 24)
133+
b[5] = byte(v >> 16)
134+
b[6] = byte(v >> 8)
135+
b[7] = byte(v)
136+
}
137+
138+
func BeAppendUint64(b []byte, v uint64) []byte {
139+
return append(b,
140+
byte(v>>56),
141+
byte(v>>48),
142+
byte(v>>40),
143+
byte(v>>32),
144+
byte(v>>24),
145+
byte(v>>16),
146+
byte(v>>8),
147+
byte(v),
148+
)
149+
}

0 commit comments

Comments
 (0)