unsafe_enabled.go 1.6 KB

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