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