runtime: use internal/byteorder

To simplify the code.

Change-Id: Ib1af5009cc25bb29fd26fdb7b29ff4579f0150aa
GitHub-Last-Rev: f698a8a771ac8c6ecb745ea4c27a7c677c1789d1
GitHub-Pull-Request: golang/go#73255
Reviewed-on: https://go-review.googlesource.com/c/go/+/663735
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
apocelipes 2025-04-08 10:00:13 +00:00 committed by Michael Pratt
parent 14b15a2bea
commit 38a2a3c7ce
6 changed files with 20 additions and 40 deletions

View File

@ -6,6 +6,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/byteorder"
"internal/cpu" "internal/cpu"
"internal/goarch" "internal/goarch"
"internal/runtime/sys" "internal/runtime/sys"
@ -474,16 +475,15 @@ func initAlgAES() {
func readUnaligned32(p unsafe.Pointer) uint32 { func readUnaligned32(p unsafe.Pointer) uint32 {
q := (*[4]byte)(p) q := (*[4]byte)(p)
if goarch.BigEndian { if goarch.BigEndian {
return uint32(q[3]) | uint32(q[2])<<8 | uint32(q[1])<<16 | uint32(q[0])<<24 return byteorder.BEUint32(q[:])
} }
return uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24 return byteorder.LEUint32(q[:])
} }
func readUnaligned64(p unsafe.Pointer) uint64 { func readUnaligned64(p unsafe.Pointer) uint64 {
q := (*[8]byte)(p) q := (*[8]byte)(p)
if goarch.BigEndian { if goarch.BigEndian {
return uint64(q[7]) | uint64(q[6])<<8 | uint64(q[5])<<16 | uint64(q[4])<<24 | return byteorder.BEUint64(q[:])
uint64(q[3])<<32 | uint64(q[2])<<40 | uint64(q[1])<<48 | uint64(q[0])<<56
} }
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 return byteorder.LEUint64(q[:])
} }

View File

@ -27,6 +27,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/byteorder"
"internal/runtime/atomic" "internal/runtime/atomic"
"internal/runtime/sys" "internal/runtime/sys"
"unsafe" "unsafe"
@ -477,14 +478,7 @@ func (l *debugLogWriter) writeSync(tick, nano uint64) {
//go:nosplit //go:nosplit
func (l *debugLogWriter) writeUint64LE(x uint64) { func (l *debugLogWriter) writeUint64LE(x uint64) {
var b [8]byte var b [8]byte
b[0] = byte(x) byteorder.LEPutUint64(b[:], x)
b[1] = byte(x >> 8)
b[2] = byte(x >> 16)
b[3] = byte(x >> 24)
b[4] = byte(x >> 32)
b[5] = byte(x >> 40)
b[6] = byte(x >> 48)
b[7] = byte(x >> 56)
l.bytes(b[:]) l.bytes(b[:])
} }
@ -576,10 +570,7 @@ func (r *debugLogReader) readUint64LEAt(pos uint64) uint64 {
b[i] = r.data.b[pos%uint64(len(r.data.b))] b[i] = r.data.b[pos%uint64(len(r.data.b))]
pos++ pos++
} }
return uint64(b[0]) | uint64(b[1])<<8 | return byteorder.LEUint64(b[:])
uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 |
uint64(b[6])<<48 | uint64(b[7])<<56
} }
func (r *debugLogReader) peek() (tick uint64) { func (r *debugLogReader) peek() (tick uint64) {

View File

@ -7,6 +7,7 @@ package runtime_test
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"internal/byteorder"
"internal/race" "internal/race"
"internal/testenv" "internal/testenv"
"math" "math"
@ -326,10 +327,7 @@ func genPerm(h *HashSet, b []byte, s []uint32, n int) {
return return
} }
for _, v := range s { for _, v := range s {
b[n] = byte(v) byteorder.LEPutUint32(b[n:], v)
b[n+1] = byte(v >> 8)
b[n+2] = byte(v >> 16)
b[n+3] = byte(v >> 24)
genPerm(h, b, s, n+4) genPerm(h, b, s, n+4)
} }
} }

View File

@ -6,6 +6,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/byteorder"
"internal/runtime/atomic" "internal/runtime/atomic"
"internal/stringslite" "internal/stringslite"
"unsafe" "unsafe"
@ -574,8 +575,7 @@ func timesplit(u uint64) (sec int64, nsec int32)
func frombe(u uint64) uint64 { func frombe(u uint64) uint64 {
b := (*[8]byte)(unsafe.Pointer(&u)) b := (*[8]byte)(unsafe.Pointer(&u))
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | return byteorder.BEUint64(b[:])
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
} }
//go:nosplit //go:nosplit

View File

@ -5,6 +5,7 @@
package pprof package pprof
import ( import (
"internal/byteorder"
"os" "os"
"unsafe" "unsafe"
) )
@ -39,7 +40,7 @@ func machVMInfo(addMapping func(lo, hi, offset uint64, file, buildID string)) bo
// offset is usually 0. // offset is usually 0.
addMapping(addr, addMapping(addr,
addr+memRegionSize, addr+memRegionSize,
read64(&info.Offset), byteorder.LEUint64(info.Offset[:]),
regionFilename(addr), regionFilename(addr),
"") "")
added = true added = true
@ -48,11 +49,6 @@ func machVMInfo(addMapping func(lo, hi, offset uint64, file, buildID string)) bo
} }
} }
func read64(p *[8]byte) uint64 {
// all supported darwin platforms are little endian
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
}
func regionFilename(address uint64) string { func regionFilename(address uint64) string {
buf := make([]byte, _MAXPATHLEN) buf := make([]byte, _MAXPATHLEN)
r := proc_regionfilename( r := proc_regionfilename(

View File

@ -4,7 +4,10 @@
package runtime package runtime
import "unsafe" import (
"internal/byteorder"
"unsafe"
)
var ( var (
writeHeader = []byte{6 /* ANDROID_LOG_ERROR */, 'G', 'o', 0} writeHeader = []byte{6 /* ANDROID_LOG_ERROR */, 'G', 'o', 0}
@ -148,18 +151,10 @@ func writeLogdHeader() int {
// hdr[7:11] nsec unsigned uint32, little endian. // hdr[7:11] nsec unsigned uint32, little endian.
hdr[0] = 0 // LOG_ID_MAIN hdr[0] = 0 // LOG_ID_MAIN
sec, nsec, _ := time_now() sec, nsec, _ := time_now()
packUint32(hdr[3:7], uint32(sec)) byteorder.LEPutUint32(hdr[3:7], uint32(sec))
packUint32(hdr[7:11], uint32(nsec)) byteorder.LEPutUint32(hdr[7:11], uint32(nsec))
// TODO(hakim): hdr[1:2] = gettid? // TODO(hakim): hdr[1:2] = gettid?
return 11 + len(writeHeader) return 11 + len(writeHeader)
} }
func packUint32(b []byte, v uint32) {
// little-endian.
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
b[3] = byte(v >> 24)
}