mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-02 07:07:33 -04:00
cpu: remove encoding/binary dependency
It's unnecessarily heavy, bringing in reflect, etc. I'd like for this package to become a leaf package with no dependencies so we can use it from the Go runtime directly and remove internal/cpu. Change-Id: I1794854add9ad87cc13b685bae4a039e1449c22d Reviewed-on: https://go-review.googlesource.com/c/sys/+/194646 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Martin Möhrmann <moehrmann@google.com>
This commit is contained in:
+34
-4
@@ -5,26 +5,56 @@
|
|||||||
package cpu
|
package cpu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// byteOrder is a subset of encoding/binary.ByteOrder.
|
||||||
|
type byteOrder interface {
|
||||||
|
Uint32([]byte) uint32
|
||||||
|
Uint64([]byte) uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type littleEndian struct{}
|
||||||
|
type bigEndian struct{}
|
||||||
|
|
||||||
|
func (littleEndian) Uint32(b []byte) uint32 {
|
||||||
|
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
|
||||||
|
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
|
||||||
|
}
|
||||||
|
|
||||||
|
func (littleEndian) Uint64(b []byte) uint64 {
|
||||||
|
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
|
||||||
|
return uint64(b[0]) | uint64(b[1])<<8 | 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 (bigEndian) Uint32(b []byte) uint32 {
|
||||||
|
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
|
||||||
|
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bigEndian) Uint64(b []byte) uint64 {
|
||||||
|
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
|
||||||
|
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
|
||||||
|
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
|
||||||
|
}
|
||||||
|
|
||||||
// hostByteOrder returns binary.LittleEndian on little-endian machines and
|
// hostByteOrder returns binary.LittleEndian on little-endian machines and
|
||||||
// binary.BigEndian on big-endian machines.
|
// binary.BigEndian on big-endian machines.
|
||||||
func hostByteOrder() binary.ByteOrder {
|
func hostByteOrder() byteOrder {
|
||||||
switch runtime.GOARCH {
|
switch runtime.GOARCH {
|
||||||
case "386", "amd64", "amd64p32",
|
case "386", "amd64", "amd64p32",
|
||||||
"arm", "arm64",
|
"arm", "arm64",
|
||||||
"mipsle", "mips64le", "mips64p32le",
|
"mipsle", "mips64le", "mips64p32le",
|
||||||
"ppc64le",
|
"ppc64le",
|
||||||
"riscv", "riscv64":
|
"riscv", "riscv64":
|
||||||
return binary.LittleEndian
|
return littleEndian{}
|
||||||
case "armbe", "arm64be",
|
case "armbe", "arm64be",
|
||||||
"mips", "mips64", "mips64p32",
|
"mips", "mips64", "mips64p32",
|
||||||
"ppc", "ppc64",
|
"ppc", "ppc64",
|
||||||
"s390", "s390x",
|
"s390", "s390x",
|
||||||
"sparc", "sparc64":
|
"sparc", "sparc64":
|
||||||
return binary.BigEndian
|
return bigEndian{}
|
||||||
}
|
}
|
||||||
panic("unknown architecture")
|
panic("unknown architecture")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user