github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/internal/runtimex/readunaligned.go (about)

     1  //go:build 386 || amd64 || arm || arm64 || ppc64le || mips64le || mipsle || riscv64 || wasm
     2  // +build 386 amd64 arm arm64 ppc64le mips64le mipsle riscv64 wasm
     3  
     4  //
     5  // from golang-go/src/os/endian_big.go
     6  
     7  package runtimex
     8  
     9  import (
    10  	"unsafe"
    11  )
    12  
    13  func ReadUnaligned64(p unsafe.Pointer) uint64 {
    14  	// Equal to runtime.readUnaligned64, but this function can be inlined
    15  	// compared to  use runtime.readUnaligned64 via go:linkname.
    16  	q := (*[8]byte)(p)
    17  	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
    18  }
    19  
    20  func ReadUnaligned32(p unsafe.Pointer) uint64 {
    21  	q := (*[4]byte)(p)
    22  	return uint64(uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24)
    23  }
    24  
    25  func ReadUnaligned16(p unsafe.Pointer) uint64 {
    26  	q := (*[2]byte)(p)
    27  	return uint64(uint32(q[0]) | uint32(q[1])<<8)
    28  }