github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/binary/le-in-le-cpu.go (about) 1 // For license and copyright information please see the LEGAL file in the code repository 2 3 //go:build 386 || amd64 || amd64p32 || arm || arm64 || loong64 || mips64le || mips64p32le || mipsle || ppc64le || riscv || riscv64 || wasm 4 5 package binary 6 7 import ( 8 "unsafe" 9 ) 10 11 // LittleEndian is the little-endian implementation to get|set from le binary to le cpu 12 var LittleEndian littleEndian 13 14 type littleEndian struct{} 15 16 func (littleEndian) 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 (littleEndian) 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 (littleEndian) 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 (littleEndian) 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 (littleEndian) 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 (littleEndian) 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 // *data = math.Float32frombits(order.Uint32(bs)) 55 // *data = math.Float64frombits(order.Uint64(bs)) 56 57 func init() { 58 i := uint32(1) 59 b := (*[4]byte)(unsafe.Pointer(&i)) 60 if b[0] != 1 { 61 panic("Expect LittleEndian CPU but have BigEndian CPU that cause many problem in other packages") 62 } 63 }