unsafe_enabled.go raw

   1  // We enable 64 bit LE platforms:
   2  
   3  //go:build (amd64 || arm64 || ppc64le || riscv64) && !nounsafe && !purego && !appengine
   4  
   5  package le
   6  
   7  import (
   8  	"unsafe"
   9  )
  10  
  11  // Load8 will load from b at index i.
  12  func Load8[I Indexer](b []byte, i I) byte {
  13  	//return binary.LittleEndian.Uint16(b[i:])
  14  	//return *(*uint16)(unsafe.Pointer(&b[i]))
  15  	return *(*byte)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
  16  }
  17  
  18  // Load16 will load from b at index i.
  19  func Load16[I Indexer](b []byte, i I) uint16 {
  20  	//return binary.LittleEndian.Uint16(b[i:])
  21  	//return *(*uint16)(unsafe.Pointer(&b[i]))
  22  	return *(*uint16)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
  23  }
  24  
  25  // Load32 will load from b at index i.
  26  func Load32[I Indexer](b []byte, i I) uint32 {
  27  	//return binary.LittleEndian.Uint32(b[i:])
  28  	//return *(*uint32)(unsafe.Pointer(&b[i]))
  29  	return *(*uint32)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
  30  }
  31  
  32  // Load64 will load from b at index i.
  33  func Load64[I Indexer](b []byte, i I) uint64 {
  34  	//return binary.LittleEndian.Uint64(b[i:])
  35  	//return *(*uint64)(unsafe.Pointer(&b[i]))
  36  	return *(*uint64)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i))
  37  }
  38  
  39  // Store16 will store v at b.
  40  func Store16(b []byte, v uint16) {
  41  	*(*uint16)(unsafe.Pointer(unsafe.SliceData(b))) = v
  42  }
  43  
  44  // Store32 will store v at b.
  45  func Store32(b []byte, v uint32) {
  46  	*(*uint32)(unsafe.Pointer(unsafe.SliceData(b))) = v
  47  }
  48  
  49  // Store64 will store v at b[i:].
  50  func Store64[I Indexer](b []byte, i I, v uint64) {
  51  	*(*uint64)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(b)), i)) = v
  52  }
  53