github.com/moontrade/nogc@v0.1.7/pointer.go (about)

     1  package nogc
     2  
     3  import (
     4  	"github.com/moontrade/nogc/hash"
     5  	"unsafe"
     6  )
     7  
     8  // PtrSize is the size of a pointer in bytes - unsafe.Sizeof(uintptr(0)) but as an ideal constant.
     9  // It is also the size of the machine's native word size (that is, 4 on 32-bit systems, 8 on 64-bit).
    10  const PtrSize = 4 << (^uintptr(0) >> 63)
    11  
    12  // Pointer is a wrapper around a raw pointer that is not unsafe.Pointer
    13  // so Go won't confuse it for a potential GC managed pointer.
    14  type Pointer uintptr
    15  
    16  func PointerOfString(s string) Pointer {
    17  	h := *(*_string)(unsafe.Pointer(&s))
    18  	return Pointer(h.ptr)
    19  }
    20  
    21  func (p Pointer) ToFat(length int) FatPointer {
    22  	return FatPointer{Pointer: p, len: uintptr(length)}
    23  }
    24  
    25  //goland:noinspection GoVetUnsafePointer
    26  func (p Pointer) Unsafe() unsafe.Pointer {
    27  	return unsafe.Pointer(p)
    28  }
    29  
    30  // Add is Pointer arithmetic.
    31  func (p Pointer) Add(offset int) Pointer {
    32  	return Pointer(uintptr(int(p) + offset))
    33  }
    34  
    35  // Free deallocates memory pointed by Pointer
    36  func (p *Pointer) Free() {
    37  	if p == nil || *p == 0 {
    38  		return
    39  	}
    40  	Free(*p)
    41  	*p = 0
    42  }
    43  
    44  // Sizeof returns the size of the allocation provided by the platform allocator.
    45  func (p Pointer) SizeOf() uintptr {
    46  	return Sizeof(p)
    47  }
    48  
    49  // Clone the memory starting at offset for size number of bytes and return the new Pointer.
    50  func (p Pointer) Clone(offset, size int) Pointer {
    51  	clone := Alloc(uintptr(size))
    52  	p.Copy(offset, size, clone)
    53  	return clone
    54  }
    55  
    56  // Zero zeroes out the entire allocation.
    57  func (p Pointer) Zero(size uintptr) {
    58  	Zero(p.Unsafe(), size)
    59  }
    60  
    61  // Move does a memmove
    62  //goland:noinspection GoVetUnsafePointer
    63  func (p Pointer) Move(offset, size int, to Pointer) {
    64  	Move(unsafe.Pointer(to), unsafe.Pointer(uintptr(int(p)+offset)), uintptr(size))
    65  }
    66  
    67  // Copy does a memcpy
    68  //goland:noinspection GoVetUnsafePointer
    69  func (p Pointer) Copy(offset, size int, to Pointer) {
    70  	Copy(unsafe.Pointer(to), unsafe.Pointer(uintptr(int(p)+offset)), uintptr(size))
    71  }
    72  
    73  // Equals does a memequal
    74  //goland:noinspection GoVetUnsafePointer
    75  func (p Pointer) Equals(offset, size int, to Pointer) bool {
    76  	return Equals(unsafe.Pointer(to), unsafe.Pointer(uintptr(int(p)+offset)), uintptr(size))
    77  }
    78  
    79  // Compare does a memcmp
    80  //goland:noinspection GoVetUnsafePointer
    81  func (p Pointer) Compare(offset, size int, to Pointer) int {
    82  	return Compare(unsafe.Pointer(to), unsafe.Pointer(uintptr(int(p)+offset)), uintptr(size))
    83  }
    84  
    85  ///////////////////////////////////////////////////////////////////////////////////////////////
    86  // Byte
    87  ///////////////////////////////////////////////////////////////////////////////////////////////
    88  
    89  //goland:noinspection GoVetUnsafePointer
    90  func (p Pointer) Int8(offset int) int8 {
    91  	return *(*int8)(unsafe.Pointer(uintptr(int(p) + offset)))
    92  }
    93  
    94  //goland:noinspection GoVetUnsafePointer
    95  func (p Pointer) Uint8(offset int) uint8 {
    96  	return *(*uint8)(unsafe.Pointer(uintptr(int(p) + offset)))
    97  }
    98  
    99  //goland:noinspection GoVetUnsafePointer
   100  func (p Pointer) Byte(offset int) byte {
   101  	return *(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))
   102  }
   103  
   104  ///////////////////////////////////////////////////////////////////////////////////////////////
   105  // Put Byte
   106  ///////////////////////////////////////////////////////////////////////////////////////////////
   107  
   108  //goland:noinspection GoVetUnsafePointer
   109  func (p Pointer) SetInt8(offset int, v int8) {
   110  	*(*int8)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   111  }
   112  
   113  //goland:noinspection GoVetUnsafePointer
   114  func (p Pointer) SetUint8(offset int, v uint8) {
   115  	*(*uint8)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   116  }
   117  
   118  //goland:noinspection GoVetUnsafePointer
   119  func (p Pointer) SetByte(offset int, v byte) {
   120  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   121  }
   122  
   123  ///////////////////////////////////////////////////////////////////////////////////////////////
   124  // Int16 Native Endian
   125  ///////////////////////////////////////////////////////////////////////////////////////////////
   126  
   127  //goland:noinspection GoVetUnsafePointer
   128  func (p Pointer) Int16(offset int) int16 {
   129  	return *(*int16)(unsafe.Pointer(uintptr(int(p) + offset)))
   130  }
   131  
   132  //goland:noinspection GoVetUnsafePointer
   133  func (p Pointer) SetInt16(offset int, v int16) {
   134  	*(*int16)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   135  }
   136  
   137  ///////////////////////////////////////////////////////////////////////////////////////////////
   138  // Uint16 Native Endian
   139  ///////////////////////////////////////////////////////////////////////////////////////////////
   140  
   141  //goland:noinspection GoVetUnsafePointer
   142  func (p Pointer) Uint16(offset int) uint16 {
   143  	return *(*uint16)(unsafe.Pointer(uintptr(int(p) + offset)))
   144  }
   145  
   146  //goland:noinspection GoVetUnsafePointer
   147  func (p Pointer) SetUint16(offset int, v uint16) {
   148  	*(*uint16)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   149  }
   150  
   151  ///////////////////////////////////////////////////////////////////////////////////////////////
   152  // Int32 Native Endian
   153  ///////////////////////////////////////////////////////////////////////////////////////////////
   154  
   155  //goland:noinspection GoVetUnsafePointer
   156  func (p Pointer) Int32(offset int) int32 {
   157  	return *(*int32)(unsafe.Pointer(uintptr(int(p) + offset)))
   158  }
   159  
   160  //goland:noinspection GoVetUnsafePointer
   161  func (p Pointer) SetInt32(offset int, v int32) {
   162  	*(*int32)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   163  }
   164  
   165  ///////////////////////////////////////////////////////////////////////////////////////////////
   166  // Uint32 Native Endian
   167  ///////////////////////////////////////////////////////////////////////////////////////////////
   168  
   169  //goland:noinspection GoVetUnsafePointer
   170  func (p Pointer) Uint32(offset int) uint32 {
   171  	return *(*uint32)(unsafe.Pointer(uintptr(int(p) + offset)))
   172  }
   173  
   174  //goland:noinspection GoVetUnsafePointer
   175  func (p Pointer) SetUint32(offset int, v uint32) {
   176  	*(*uint32)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   177  }
   178  
   179  ///////////////////////////////////////////////////////////////////////////////////////////////
   180  // Int64 Native Endian
   181  ///////////////////////////////////////////////////////////////////////////////////////////////
   182  
   183  //goland:noinspection GoVetUnsafePointer
   184  func (p Pointer) Int64(offset int) int64 {
   185  	return *(*int64)(unsafe.Pointer(uintptr(int(p) + offset)))
   186  }
   187  
   188  //goland:noinspection GoVetUnsafePointer
   189  func (p Pointer) SetInt64(offset int, v int64) {
   190  	*(*int64)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   191  }
   192  
   193  ///////////////////////////////////////////////////////////////////////////////////////////////
   194  // Uint64 Native Endian
   195  ///////////////////////////////////////////////////////////////////////////////////////////////
   196  
   197  //goland:noinspection GoVetUnsafePointer
   198  func (p Pointer) Uint64(offset int) uint64 {
   199  	return *(*uint64)(unsafe.Pointer(uintptr(int(p) + offset)))
   200  }
   201  
   202  //goland:noinspection GoVetUnsafePointer
   203  func (p Pointer) SetUint64(offset int, v uint64) {
   204  	*(*uint64)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   205  }
   206  
   207  ///////////////////////////////////////////////////////////////////////////////////////////////
   208  // Float32 Native Endian
   209  ///////////////////////////////////////////////////////////////////////////////////////////////
   210  
   211  //goland:noinspection GoVetUnsafePointer
   212  func (p Pointer) Float32(offset int) float32 {
   213  	return *(*float32)(unsafe.Pointer(uintptr(int(p) + offset)))
   214  }
   215  
   216  //goland:noinspection GoVetUnsafePointer
   217  func (p Pointer) SetFloat32(offset int, v float32) {
   218  	*(*float32)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   219  }
   220  
   221  ///////////////////////////////////////////////////////////////////////////////////////////////
   222  // Float64 Native Endian
   223  ///////////////////////////////////////////////////////////////////////////////////////////////
   224  
   225  //goland:noinspection GoVetUnsafePointer
   226  func (p Pointer) Float64(offset int) float64 {
   227  	return *(*float64)(unsafe.Pointer(uintptr(int(p) + offset)))
   228  }
   229  
   230  //goland:noinspection GoVetUnsafePointer
   231  func (p Pointer) SetFloat64(offset int, v float64) {
   232  	*(*float64)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   233  }
   234  
   235  ///////////////////////////////////////////////////////////////////////////////////////////////
   236  // int
   237  ///////////////////////////////////////////////////////////////////////////////////////////////
   238  
   239  //goland:noinspection GoVetUnsafePointer
   240  func (p Pointer) Int(offset int) int {
   241  	return *(*int)(unsafe.Pointer(uintptr(int(p) + offset)))
   242  }
   243  
   244  //goland:noinspection GoVetUnsafePointer
   245  func (p Pointer) SetInt(offset int, v int) {
   246  	*(*int)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   247  }
   248  
   249  ///////////////////////////////////////////////////////////////////////////////////////////////
   250  // uint
   251  ///////////////////////////////////////////////////////////////////////////////////////////////
   252  
   253  //goland:noinspection GoVetUnsafePointer
   254  func (p Pointer) Uint(offset int) uint {
   255  	return *(*uint)(unsafe.Pointer(uintptr(int(p) + offset)))
   256  }
   257  
   258  //goland:noinspection GoVetUnsafePointer
   259  func (p Pointer) SetUint(offset int, v uint) {
   260  	*(*uint)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   261  }
   262  
   263  ///////////////////////////////////////////////////////////////////////////////////////////////
   264  // uintptr
   265  ///////////////////////////////////////////////////////////////////////////////////////////////
   266  
   267  //goland:noinspection GoVetUnsafePointer
   268  func (p Pointer) Uintptr(offset int) uintptr {
   269  	return *(*uintptr)(unsafe.Pointer(uintptr(int(p) + offset)))
   270  }
   271  
   272  //goland:noinspection GoVetUnsafePointer
   273  func (p Pointer) SetUintptr(offset int, v uintptr) {
   274  	*(*uintptr)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   275  }
   276  
   277  ///////////////////////////////////////////////////////////////////////////////////////////////
   278  // Pointer
   279  ///////////////////////////////////////////////////////////////////////////////////////////////
   280  
   281  //goland:noinspection GoVetUnsafePointer
   282  func (p Pointer) Pointer(offset int) Pointer {
   283  	return *(*Pointer)(unsafe.Pointer(uintptr(int(p) + offset)))
   284  }
   285  
   286  //goland:noinspection GoVetUnsafePointer
   287  func (p Pointer) SetPointer(offset int, v Pointer) {
   288  	*(*Pointer)(unsafe.Pointer(uintptr(int(p) + offset))) = v
   289  }
   290  
   291  ///////////////////////////////////////////////////////////////////////////////////////////////
   292  // String
   293  ///////////////////////////////////////////////////////////////////////////////////////////////
   294  
   295  type _string struct {
   296  	ptr uintptr
   297  	len int
   298  }
   299  
   300  //goland:noinspection GoVetUnsafePointer
   301  func (p Pointer) String(offset, size int) string {
   302  	return *(*string)(unsafe.Pointer(&_string{
   303  		ptr: uintptr(int(p) + offset),
   304  		len: size,
   305  	}))
   306  }
   307  
   308  func (p Pointer) SetString(offset int, value string) {
   309  	dst := *(*[]byte)(unsafe.Pointer(&_bytes{
   310  		Data: uintptr(int(p) + offset),
   311  		Len:  len(value),
   312  		Cap:  len(value),
   313  	}))
   314  	copy(dst, value)
   315  }
   316  
   317  func (p Pointer) SetBytes(offset int, value []byte) {
   318  	dst := *(*[]byte)(unsafe.Pointer(&_bytes{
   319  		Data: uintptr(int(p) + offset),
   320  		Len:  len(value),
   321  		Cap:  len(value),
   322  	}))
   323  	copy(dst, value)
   324  }
   325  
   326  ///////////////////////////////////////////////////////////////////////////////////////////////
   327  // Byte Slice
   328  ///////////////////////////////////////////////////////////////////////////////////////////////
   329  
   330  type _bytes struct {
   331  	Data uintptr
   332  	Len  int
   333  	Cap  int
   334  }
   335  
   336  //goland:noinspection GoVetUnsafePointer
   337  func (p Pointer) Bytes(offset, length, capacity int) []byte {
   338  	return *(*[]byte)(unsafe.Pointer(&_bytes{
   339  		Data: uintptr(int(p) + offset),
   340  		Len:  length,
   341  		Cap:  capacity,
   342  	}))
   343  }
   344  
   345  ///////////////////////////////////////////////////////////////////////////////////////////////
   346  // Int24 Little Endian
   347  ///////////////////////////////////////////////////////////////////////////////////////////////
   348  
   349  //goland:noinspection GoVetUnsafePointer
   350  func (p Pointer) Int24LE(offset int) int32 {
   351  	return int32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   352  		int32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   353  		int32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16
   354  }
   355  
   356  //goland:noinspection GoVetUnsafePointer
   357  func (p Pointer) SetInt24LE(offset int, v int32) {
   358  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   359  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   360  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   361  }
   362  
   363  ///////////////////////////////////////////////////////////////////////////////////////////////
   364  // Int24 Big Endian
   365  ///////////////////////////////////////////////////////////////////////////////////////////////
   366  
   367  //goland:noinspection GoVetUnsafePointer
   368  func (p Pointer) Int24BE(offset int) int32 {
   369  	return int32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2)))) |
   370  		int32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   371  		int32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<16
   372  }
   373  
   374  //goland:noinspection GoVetUnsafePointer
   375  func (p Pointer) SetInt24BE(offset int, v int32) {
   376  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 16)
   377  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   378  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v)
   379  }
   380  
   381  ///////////////////////////////////////////////////////////////////////////////////////////////
   382  // Uint24 Little Endian
   383  ///////////////////////////////////////////////////////////////////////////////////////////////
   384  
   385  //goland:noinspection GoVetUnsafePointer
   386  func (p Pointer) Uint24LE(offset int) uint32 {
   387  	return uint32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   388  		uint32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   389  		uint32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16
   390  }
   391  
   392  //goland:noinspection GoVetUnsafePointer
   393  func (p Pointer) SetUint24LE(offset int, v uint32) {
   394  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   395  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   396  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   397  }
   398  
   399  ///////////////////////////////////////////////////////////////////////////////////////////////
   400  // Uint24 Big Endian
   401  ///////////////////////////////////////////////////////////////////////////////////////////////
   402  
   403  //goland:noinspection GoVetUnsafePointer
   404  func (p Pointer) Uint24BE(offset int) uint32 {
   405  	return uint32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2)))) |
   406  		uint32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   407  		uint32(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<16
   408  }
   409  
   410  //goland:noinspection GoVetUnsafePointer
   411  func (p Pointer) SetUint24BE(offset int, v uint32) {
   412  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 16)
   413  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   414  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v)
   415  }
   416  
   417  ///////////////////////////////////////////////////////////////////////////////////////////////
   418  // Int40 Little Endian
   419  ///////////////////////////////////////////////////////////////////////////////////////////////
   420  
   421  //goland:noinspection GoVetUnsafePointer
   422  func (p Pointer) Int40LE(offset int) int64 {
   423  	return int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   424  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   425  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   426  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   427  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<32
   428  }
   429  
   430  //goland:noinspection GoVetUnsafePointer
   431  func (p Pointer) SetInt40LE(offset int, v int64) {
   432  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   433  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   434  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   435  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   436  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 32)
   437  }
   438  
   439  ///////////////////////////////////////////////////////////////////////////////////////////////
   440  // Int40 Big Endian
   441  ///////////////////////////////////////////////////////////////////////////////////////////////
   442  
   443  //goland:noinspection GoVetUnsafePointer
   444  func (p Pointer) Int40BE(offset int) int64 {
   445  	return int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4)))) |
   446  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<8 |
   447  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   448  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<24 |
   449  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<32
   450  }
   451  
   452  //goland:noinspection GoVetUnsafePointer
   453  func (p Pointer) SetInt40BE(offset int, v int64) {
   454  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 32)
   455  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 24)
   456  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   457  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 8)
   458  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v)
   459  }
   460  
   461  ///////////////////////////////////////////////////////////////////////////////////////////////
   462  // Uint40 Little Endian
   463  ///////////////////////////////////////////////////////////////////////////////////////////////
   464  
   465  //goland:noinspection GoVetUnsafePointer
   466  func (p Pointer) Uint40LE(offset int) uint64 {
   467  	return uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   468  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   469  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   470  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   471  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<32
   472  }
   473  
   474  //goland:noinspection GoVetUnsafePointer
   475  func (p Pointer) SetUint40LE(offset int, v uint64) {
   476  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   477  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   478  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   479  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   480  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 32)
   481  }
   482  
   483  ///////////////////////////////////////////////////////////////////////////////////////////////
   484  // Uint40 Big Endian
   485  ///////////////////////////////////////////////////////////////////////////////////////////////
   486  
   487  //goland:noinspection GoVetUnsafePointer
   488  func (p Pointer) Uint40BE(offset int) uint64 {
   489  	return uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4)))) |
   490  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<8 |
   491  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   492  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<24 |
   493  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<32
   494  }
   495  
   496  //goland:noinspection GoVetUnsafePointer
   497  func (p Pointer) SetUint40BE(offset int, v uint64) {
   498  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 32)
   499  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 24)
   500  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   501  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 8)
   502  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v)
   503  }
   504  
   505  ///////////////////////////////////////////////////////////////////////////////////////////////
   506  // Int48 Little Endian
   507  ///////////////////////////////////////////////////////////////////////////////////////////////
   508  
   509  //goland:noinspection GoVetUnsafePointer
   510  func (p Pointer) Int48LE(offset int) int64 {
   511  	return int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   512  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   513  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   514  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   515  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<32 |
   516  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))))<<40
   517  }
   518  
   519  //goland:noinspection GoVetUnsafePointer
   520  func (p Pointer) SetInt48LE(offset int, v int64) {
   521  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   522  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   523  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   524  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   525  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 32)
   526  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v >> 40)
   527  }
   528  
   529  ///////////////////////////////////////////////////////////////////////////////////////////////
   530  // Int48 Big Endian
   531  ///////////////////////////////////////////////////////////////////////////////////////////////
   532  
   533  //goland:noinspection GoVetUnsafePointer
   534  func (p Pointer) Int48BE(offset int) int64 {
   535  	return int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5)))) |
   536  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<8 |
   537  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<16 |
   538  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<24 |
   539  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<32 |
   540  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<40
   541  }
   542  
   543  //goland:noinspection GoVetUnsafePointer
   544  func (p Pointer) SetInt48BE(offset int, v int64) {
   545  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 40)
   546  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 32)
   547  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 24)
   548  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 16)
   549  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 8)
   550  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v)
   551  }
   552  
   553  ///////////////////////////////////////////////////////////////////////////////////////////////
   554  // Uint48 Little Endian
   555  ///////////////////////////////////////////////////////////////////////////////////////////////
   556  
   557  //goland:noinspection GoVetUnsafePointer
   558  func (p Pointer) Uint48LE(offset int) uint64 {
   559  	return uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   560  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   561  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   562  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   563  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<32 |
   564  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))))<<40
   565  }
   566  
   567  //goland:noinspection GoVetUnsafePointer
   568  func (p Pointer) SetUint48LE(offset int, v uint64) {
   569  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   570  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   571  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   572  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   573  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 32)
   574  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v >> 40)
   575  }
   576  
   577  ///////////////////////////////////////////////////////////////////////////////////////////////
   578  // Uint48 Big Endian
   579  ///////////////////////////////////////////////////////////////////////////////////////////////
   580  
   581  //goland:noinspection GoVetUnsafePointer
   582  func (p Pointer) Uint48BE(offset int) uint64 {
   583  	return uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5)))) |
   584  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<8 |
   585  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<16 |
   586  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<24 |
   587  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<32 |
   588  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<40
   589  }
   590  
   591  //goland:noinspection GoVetUnsafePointer
   592  func (p Pointer) SetUint48BE(offset int, v uint64) {
   593  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 40)
   594  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 32)
   595  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 24)
   596  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 16)
   597  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 8)
   598  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v)
   599  }
   600  
   601  ///////////////////////////////////////////////////////////////////////////////////////////////
   602  // Int56 Little Endian
   603  ///////////////////////////////////////////////////////////////////////////////////////////////
   604  
   605  //goland:noinspection GoVetUnsafePointer
   606  func (p Pointer) Int56LE(offset int) int64 {
   607  	return int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   608  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   609  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   610  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   611  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<32 |
   612  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))))<<40 |
   613  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6))))<<48
   614  }
   615  
   616  //goland:noinspection GoVetUnsafePointer
   617  func (p Pointer) SetInt56LE(offset int, v int64) {
   618  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   619  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   620  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   621  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   622  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 32)
   623  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v >> 40)
   624  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6))) = byte(v >> 48)
   625  }
   626  
   627  ///////////////////////////////////////////////////////////////////////////////////////////////
   628  // Int56 Big Endian
   629  ///////////////////////////////////////////////////////////////////////////////////////////////
   630  
   631  //goland:noinspection GoVetUnsafePointer
   632  func (p Pointer) Int56BE(offset int) int64 {
   633  	return int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6)))) |
   634  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))))<<8 |
   635  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<16 |
   636  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   637  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<32 |
   638  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<40 |
   639  		int64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<48
   640  }
   641  
   642  //goland:noinspection GoVetUnsafePointer
   643  func (p Pointer) SetInt56BE(offset int, v int64) {
   644  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 48)
   645  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 40)
   646  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 32)
   647  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   648  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 16)
   649  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v >> 8)
   650  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6))) = byte(v)
   651  }
   652  
   653  ///////////////////////////////////////////////////////////////////////////////////////////////
   654  // Uint56 Little Endian
   655  ///////////////////////////////////////////////////////////////////////////////////////////////
   656  
   657  //goland:noinspection GoVetUnsafePointer
   658  func (p Pointer) Uint56LE(offset int) uint64 {
   659  	return uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset)))) |
   660  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<8 |
   661  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<16 |
   662  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   663  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<32 |
   664  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))))<<40 |
   665  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6))))<<48
   666  }
   667  
   668  //goland:noinspection GoVetUnsafePointer
   669  func (p Pointer) SetUint56LE(offset int, v uint64) {
   670  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v)
   671  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 8)
   672  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 16)
   673  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   674  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 32)
   675  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v >> 40)
   676  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6))) = byte(v >> 48)
   677  }
   678  
   679  ///////////////////////////////////////////////////////////////////////////////////////////////
   680  // Uint56 Big Endian
   681  ///////////////////////////////////////////////////////////////////////////////////////////////
   682  
   683  //goland:noinspection GoVetUnsafePointer
   684  func (p Pointer) Uint56BE(offset int) uint64 {
   685  	return uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6)))) |
   686  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))))<<8 |
   687  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))))<<16 |
   688  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))))<<24 |
   689  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))))<<32 |
   690  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))))<<40 |
   691  		uint64(*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))))<<48
   692  }
   693  
   694  //goland:noinspection GoVetUnsafePointer
   695  func (p Pointer) SetUint56BE(offset int, v uint64) {
   696  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset))) = byte(v >> 48)
   697  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 1))) = byte(v >> 40)
   698  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 2))) = byte(v >> 32)
   699  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 3))) = byte(v >> 24)
   700  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 4))) = byte(v >> 16)
   701  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 5))) = byte(v >> 8)
   702  	*(*byte)(unsafe.Pointer(uintptr(int(p) + offset + 6))) = byte(v)
   703  }
   704  
   705  func (p Pointer) Hash32(length int) uint32 {
   706  	return p.Hash32At(0, length)
   707  }
   708  
   709  func (p Pointer) Hash32At(offset, length int) uint32 {
   710  	const (
   711  		offset32 = uint32(2166136261)
   712  		prime32  = uint32(16777619)
   713  	)
   714  	hash := offset32
   715  
   716  	start := uintptr(int(p) + offset)
   717  	end := start + uintptr(length)
   718  	for ; start < end; start++ {
   719  		hash ^= uint32(*(*byte)(unsafe.Pointer(start)))
   720  		hash *= prime32
   721  	}
   722  	return hash
   723  }
   724  
   725  func (p Pointer) Hash64(length int) uint64 {
   726  	return p.Hash64At(0, length)
   727  }
   728  
   729  func (p Pointer) Hash64At(offset, length int) uint64 {
   730  	const (
   731  		offset64 = uint64(14695981039346656037)
   732  		prime64  = uint64(1099511628211)
   733  	)
   734  	hash := offset64
   735  	start := uintptr(int(p) + offset)
   736  	end := start + uintptr(length)
   737  	for ; start < end; start++ {
   738  		hash ^= uint64(*(*byte)(unsafe.Pointer(start)))
   739  		hash *= prime64
   740  	}
   741  	return hash
   742  }
   743  
   744  func (p Pointer) WyHash64(seed uint64, offset, length int) uint64 {
   745  	return hash.WyHash(p.Bytes(offset, length, length), seed)
   746  }
   747  
   748  func (p Pointer) Metro64(seed uint64, offset, length int) uint64 {
   749  	return hash.Metro(p.Bytes(offset, length, length), seed)
   750  }