github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/binary/le-in-le-cpu.go (about) 1 // For license and copyright information please see LEGAL file in 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 { return uint16(b[0]) | uint16(b[1])<<8 } 17 func (littleEndian) Uint32(b []byte) uint32 { 18 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 19 } 20 func (littleEndian) Uint64(b []byte) uint64 { 21 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | 22 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 23 } 24 25 func (littleEndian) PutUint16(b []byte, v uint16) { 26 b[0] = byte(v) 27 b[1] = byte(v >> 8) 28 } 29 func (littleEndian) PutUint32(b []byte, v uint32) { 30 b[0] = byte(v) 31 b[1] = byte(v >> 8) 32 b[2] = byte(v >> 16) 33 b[3] = byte(v >> 24) 34 } 35 func (littleEndian) PutUint64(b []byte, v uint64) { 36 b[0] = byte(v) 37 b[1] = byte(v >> 8) 38 b[2] = byte(v >> 16) 39 b[3] = byte(v >> 24) 40 b[4] = byte(v >> 32) 41 b[5] = byte(v >> 40) 42 b[6] = byte(v >> 48) 43 b[7] = byte(v >> 56) 44 } 45 46 // *data = math.Float32frombits(order.Uint32(bs)) 47 // *data = math.Float64frombits(order.Uint64(bs)) 48 49 func init() { 50 i := uint32(1) 51 b := (*[4]byte)(unsafe.Pointer(&i)) 52 if b[0] != 1 { 53 panic("Expect LittleEndian CPU but have BigEndian CPU that cause many problem in other packages") 54 } 55 }