github.com/aaabigfish/gopkg@v1.1.0/internal/runtimex/readunaligned.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build 386 || amd64 || arm || arm64 || ppc64le || mips64le || mipsle || riscv64 || wasm
    16  // +build 386 amd64 arm arm64 ppc64le mips64le mipsle riscv64 wasm
    17  
    18  //
    19  // from golang-go/src/os/endian_big.go
    20  
    21  package runtimex
    22  
    23  import (
    24  	"unsafe"
    25  )
    26  
    27  func ReadUnaligned64(p unsafe.Pointer) uint64 {
    28  	// Equal to runtime.readUnaligned64, but this function can be inlined
    29  	// compared to  use runtime.readUnaligned64 via go:linkname.
    30  	q := (*[8]byte)(p)
    31  	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
    32  }
    33  
    34  func ReadUnaligned32(p unsafe.Pointer) uint64 {
    35  	q := (*[4]byte)(p)
    36  	return uint64(uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24)
    37  }
    38  
    39  func ReadUnaligned16(p unsafe.Pointer) uint64 {
    40  	q := (*[2]byte)(p)
    41  	return uint64(uint32(q[0]) | uint32(q[1])<<8)
    42  }