github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/binary/be-in-be-cpu.go (about) 1 // For license and copyright information please see the LEGAL file in the code repository 2 3 //go:build armbe || arm64be || mips || mips64 || mips64p32 || ppc || ppc64 || s390 || s390x || sparc || sparc64 4 5 package binary 6 7 import ( 8 "unsafe" 9 ) 10 11 // BigEndian is the big-endian implementation to get|set from be binary to be cpu 12 var BigEndian bigEndian 13 14 type bigEndian struct{} 15 16 func (bigEndian) Uint16(b []byte) uint16 { 17 _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 18 return uint16(b[0]) | uint16(b[1])<<8 19 } 20 func (bigEndian) Uint32(b []byte) uint32 { 21 _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 22 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 23 } 24 func (bigEndian) Uint64(b []byte) uint64 { 25 _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 26 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | 27 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 28 } 29 30 func (bigEndian) PutUint16(b []byte, v uint16) { 31 _ = b[1] // early bounds check to guarantee safety of writes below 32 b[0] = byte(v) 33 b[1] = byte(v >> 8) 34 } 35 func (bigEndian) PutUint32(b []byte, v uint32) { 36 _ = b[3] // early bounds check to guarantee safety of writes below 37 b[0] = byte(v) 38 b[1] = byte(v >> 8) 39 b[2] = byte(v >> 16) 40 b[3] = byte(v >> 24) 41 } 42 func (bigEndian) PutUint64(b []byte, v uint64) { 43 _ = b[7] // early bounds check to guarantee safety of writes below 44 b[0] = byte(v) 45 b[1] = byte(v >> 8) 46 b[2] = byte(v >> 16) 47 b[3] = byte(v >> 24) 48 b[4] = byte(v >> 32) 49 b[5] = byte(v >> 40) 50 b[6] = byte(v >> 48) 51 b[7] = byte(v >> 56) 52 } 53 54 func init() { 55 i := uint32(1) 56 b := (*[4]byte)(unsafe.Pointer(&i)) 57 if b[0] == 1 { 58 panic("Expect BigEndian CPU but have LittleEndian CPU that cause many problem in other packages") 59 } 60 }