Skip to content

Commit f698a8a

Browse files
committed
runtime: use internal/byteorder
To simplify the code.
1 parent af278bf commit f698a8a

File tree

6 files changed

+20
-40
lines changed

6 files changed

+20
-40
lines changed

src/runtime/alg.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime
66

77
import (
88
"internal/abi"
9+
"internal/byteorder"
910
"internal/cpu"
1011
"internal/goarch"
1112
"internal/runtime/sys"
@@ -474,16 +475,15 @@ func initAlgAES() {
474475
func readUnaligned32(p unsafe.Pointer) uint32 {
475476
q := (*[4]byte)(p)
476477
if goarch.BigEndian {
477-
return uint32(q[3]) | uint32(q[2])<<8 | uint32(q[1])<<16 | uint32(q[0])<<24
478+
return byteorder.BEUint32(q[:])
478479
}
479-
return uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24
480+
return byteorder.LEUint32(q[:])
480481
}
481482

482483
func readUnaligned64(p unsafe.Pointer) uint64 {
483484
q := (*[8]byte)(p)
484485
if goarch.BigEndian {
485-
return uint64(q[7]) | uint64(q[6])<<8 | uint64(q[5])<<16 | uint64(q[4])<<24 |
486-
uint64(q[3])<<32 | uint64(q[2])<<40 | uint64(q[1])<<48 | uint64(q[0])<<56
486+
return byteorder.BEUint64(q[:])
487487
}
488-
return uint64(q[0]) | uint64(q[1])<<8 | uint64(q[2])<<16 | uint64(q[3])<<24 | uint64(q[4])<<32 | uint64(q[5])<<40 | uint64(q[6])<<48 | uint64(q[7])<<56
488+
return byteorder.LEUint64(q[:])
489489
}

src/runtime/debuglog.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package runtime
2727

2828
import (
2929
"internal/abi"
30+
"internal/byteorder"
3031
"internal/runtime/atomic"
3132
"internal/runtime/sys"
3233
"unsafe"
@@ -477,14 +478,7 @@ func (l *debugLogWriter) writeSync(tick, nano uint64) {
477478
//go:nosplit
478479
func (l *debugLogWriter) writeUint64LE(x uint64) {
479480
var b [8]byte
480-
b[0] = byte(x)
481-
b[1] = byte(x >> 8)
482-
b[2] = byte(x >> 16)
483-
b[3] = byte(x >> 24)
484-
b[4] = byte(x >> 32)
485-
b[5] = byte(x >> 40)
486-
b[6] = byte(x >> 48)
487-
b[7] = byte(x >> 56)
481+
byteorder.LEPutUint64(b[:], x)
488482
l.bytes(b[:])
489483
}
490484

@@ -576,10 +570,7 @@ func (r *debugLogReader) readUint64LEAt(pos uint64) uint64 {
576570
b[i] = r.data.b[pos%uint64(len(r.data.b))]
577571
pos++
578572
}
579-
return uint64(b[0]) | uint64(b[1])<<8 |
580-
uint64(b[2])<<16 | uint64(b[3])<<24 |
581-
uint64(b[4])<<32 | uint64(b[5])<<40 |
582-
uint64(b[6])<<48 | uint64(b[7])<<56
573+
return byteorder.LEUint64(b[:])
583574
}
584575

585576
func (r *debugLogReader) peek() (tick uint64) {

src/runtime/hash_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package runtime_test
77
import (
88
"encoding/binary"
99
"fmt"
10+
"internal/byteorder"
1011
"internal/race"
1112
"internal/testenv"
1213
"math"
@@ -326,10 +327,7 @@ func genPerm(h *HashSet, b []byte, s []uint32, n int) {
326327
return
327328
}
328329
for _, v := range s {
329-
b[n] = byte(v)
330-
b[n+1] = byte(v >> 8)
331-
b[n+2] = byte(v >> 16)
332-
b[n+3] = byte(v >> 24)
330+
byteorder.LEPutUint32(b[n:], v)
333331
genPerm(h, b, s, n+4)
334332
}
335333
}

src/runtime/os_plan9.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime
66

77
import (
88
"internal/abi"
9+
"internal/byteorder"
910
"internal/runtime/atomic"
1011
"internal/stringslite"
1112
"unsafe"
@@ -574,8 +575,7 @@ func timesplit(u uint64) (sec int64, nsec int32)
574575

575576
func frombe(u uint64) uint64 {
576577
b := (*[8]byte)(unsafe.Pointer(&u))
577-
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
578-
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
578+
return byteorder.BEUint64(b[:])
579579
}
580580

581581
//go:nosplit

src/runtime/pprof/vminfo_darwin.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package pprof
66

77
import (
8+
"internal/byteorder"
89
"os"
910
"unsafe"
1011
)
@@ -39,7 +40,7 @@ func machVMInfo(addMapping func(lo, hi, offset uint64, file, buildID string)) bo
3940
// offset is usually 0.
4041
addMapping(addr,
4142
addr+memRegionSize,
42-
read64(&info.Offset),
43+
byteorder.LEUint64(info.Offset[:]),
4344
regionFilename(addr),
4445
"")
4546
added = true
@@ -48,11 +49,6 @@ func machVMInfo(addMapping func(lo, hi, offset uint64, file, buildID string)) bo
4849
}
4950
}
5051

51-
func read64(p *[8]byte) uint64 {
52-
// all supported darwin platforms are little endian
53-
return 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
54-
}
55-
5652
func regionFilename(address uint64) string {
5753
buf := make([]byte, _MAXPATHLEN)
5854
r := proc_regionfilename(

src/runtime/write_err_android.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package runtime
66

7-
import "unsafe"
7+
import (
8+
"internal/byteorder"
9+
"unsafe"
10+
)
811

912
var (
1013
writeHeader = []byte{6 /* ANDROID_LOG_ERROR */, 'G', 'o', 0}
@@ -148,18 +151,10 @@ func writeLogdHeader() int {
148151
// hdr[7:11] nsec unsigned uint32, little endian.
149152
hdr[0] = 0 // LOG_ID_MAIN
150153
sec, nsec, _ := time_now()
151-
packUint32(hdr[3:7], uint32(sec))
152-
packUint32(hdr[7:11], uint32(nsec))
154+
byteorder.LEPutUint32(hdr[3:7], uint32(sec))
155+
byteorder.LEPutUint32(hdr[7:11], uint32(nsec))
153156

154157
// TODO(hakim): hdr[1:2] = gettid?
155158

156159
return 11 + len(writeHeader)
157160
}
158-
159-
func packUint32(b []byte, v uint32) {
160-
// little-endian.
161-
b[0] = byte(v)
162-
b[1] = byte(v >> 8)
163-
b[2] = byte(v >> 16)
164-
b[3] = byte(v >> 24)
165-
}

0 commit comments

Comments
 (0)