github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/internal/runtimex/readunaligned_bigendian.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 ppc64 || s390x || mips || mips64
    16  // +build ppc64 s390x mips mips64
    17  
    18  //
    19  // from golang-go/src/os/endian_little.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[7]) | uint64(q[6])<<8 | uint64(q[5])<<16 | uint64(q[4])<<24 |
    32  		uint64(q[3])<<32 | uint64(q[2])<<40 | uint64(q[1])<<48 | uint64(q[0])<<56
    33  }
    34  
    35  func ReadUnaligned32(p unsafe.Pointer) uint64 {
    36  	q := (*[4]byte)(p)
    37  	return uint64(uint32(q[3]) | uint32(q[2])<<8 | uint32(q[1])<<16 | uint32(q[0])<<24)
    38  }
    39  
    40  func ReadUnaligned16(p unsafe.Pointer) uint64 {
    41  	q := (*[2]byte)(p)
    42  	return uint64(uint32(q[1]) | uint32(q[0])<<8)
    43  }