-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoding.go
1894 lines (1735 loc) · 46.4 KB
/
encoding.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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package simple8b implements the 64bit integer encoding algoritm as published
// by Ann and Moffat in "Index compression using 64-bit words", Softw. Pract. Exper. 2010; 40:131–147
//
// It is capable of encoding multiple integers with values betweeen 0 and to 1^60 -1, in a single word.
//
// Imported from github.com/jwilder/encoding
package s8b
// Simple8b is 64bit word-sized encoder that packs multiple integers into a single word using
// a 4 bit selector values and up to 60 bits for the remaining values. Integers are encoded using
// the following table:
//
// ┌──────────────┬─────────────────────────────────────────────────────────────┐
// │ Selector │ 0 1 2 3 4 5 6 7 8 9 0 11 12 13 14 15│
// ├──────────────┼─────────────────────────────────────────────────────────────┤
// │ Bits │ 0 0 1 2 3 4 5 6 7 8 10 12 15 20 30 60│
// ├──────────────┼─────────────────────────────────────────────────────────────┤
// │ N │ 240 120 60 30 20 15 12 10 8 7 6 5 4 3 2 1│
// ├──────────────┼─────────────────────────────────────────────────────────────┤
// │ Wasted Bits│ 60 60 0 0 0 0 12 0 4 4 0 0 0 0 0 0│
// └──────────────┴─────────────────────────────────────────────────────────────┘
//
// For example, when the number of values can be encoded using 4 bits, selected 5 is encoded in the
// 4 most significant bits followed by 15 values encoded used 4 bits each in the remaing 60 bits.
import (
"encoding/binary"
"errors"
"fmt"
"unsafe"
)
const MaxValue = (1 << 60) - 1
// Encoder converts a stream of unsigned 64bit integers to a compressed byte slice.
type Encoder struct {
// most recently written integers that have not been flushed
buf []uint64
// index in buf of the head of the buf
h int
// index in buf of the tail of the buf
t int
// index into bytes of written bytes
bp int
// current bytes written and flushed
bytes []byte
b []byte
}
// NewEncoder returns an Encoder able to convert uint64s to compressed byte slices
func NewEncoder() *Encoder {
return &Encoder{
buf: make([]uint64, 240),
b: make([]byte, 8),
bytes: make([]byte, 128),
}
}
func (e *Encoder) SetValues(v []uint64) {
e.buf = v
e.t = len(v)
e.h = 0
e.bytes = e.bytes[:0]
}
func (e *Encoder) Reset() {
e.t = 0
e.h = 0
e.bp = 0
e.buf = e.buf[:240]
e.b = e.b[:8]
e.bytes = e.bytes[:128]
}
func (e *Encoder) Write(v uint64) error {
if e.t >= len(e.buf) {
if err := e.flush(); err != nil {
return err
}
}
// The buf is full but there is space at the front, just shift
// the values down for now. TODO: use ring buffer
if e.t >= len(e.buf) {
copy(e.buf, e.buf[e.h:])
e.t -= e.h
e.h = 0
}
e.buf[e.t] = v
e.t += 1
return nil
}
func (e *Encoder) flush() error {
if e.t == 0 {
return nil
}
// encode as many values into one as we can
encoded, n, err := Encode(e.buf[e.h:e.t])
if err != nil {
return err
}
binary.BigEndian.PutUint64(e.b, encoded)
if e.bp+8 > len(e.bytes) {
e.bytes = append(e.bytes, e.b...)
e.bp = len(e.bytes)
} else {
copy(e.bytes[e.bp:e.bp+8], e.b)
e.bp += 8
}
// Move the head forward since we encoded those values
e.h += n
// If we encoded them all, reset the head/tail pointers to the beginning
if e.h == e.t {
e.h = 0
e.t = 0
}
return nil
}
func (e *Encoder) Bytes() ([]byte, error) {
for e.t > 0 {
if err := e.flush(); err != nil {
return nil, err
}
}
return e.bytes[:e.bp], nil
}
// Decoder converts a compressed byte slice to a stream of unsigned 64bit integers.
type Decoder struct {
bytes []byte
buf [240]uint64
i int
n int
}
// NewDecoder returns a Decoder from a byte slice
func NewDecoder(b []byte) *Decoder {
return &Decoder{
bytes: b,
}
}
// Next returns true if there are remaining values to be read. Successive
// calls to Next advance the current element pointer.
func (d *Decoder) Next() bool {
d.i += 1
if d.i >= d.n {
d.read()
}
return len(d.bytes) >= 8 || (d.i >= 0 && d.i < d.n)
}
func (d *Decoder) SetBytes(b []byte) {
d.bytes = b
d.i = 0
d.n = 0
}
// Read returns the current value. Successive calls to Read return the same
// value.
func (d *Decoder) Read() uint64 {
v := d.buf[d.i]
return v
}
func (d *Decoder) read() {
if len(d.bytes) < 8 {
return
}
v := binary.BigEndian.Uint64(d.bytes[:8])
d.bytes = d.bytes[8:]
d.n, _ = Decode(&d.buf, v)
d.i = 0
}
type packing struct {
n, bit int
unpack func(uint64, unsafe.Pointer)
pack func([]uint64) uint64
}
var selector [16]packing = [16]packing{
{240, 0, unpack240, pack240},
{120, 0, unpack120, pack120},
{60, 1, unpack60, pack60},
{30, 2, unpack30, pack30},
{20, 3, unpack20, pack20},
{15, 4, unpack15, pack15},
{12, 5, unpack12, pack12},
{10, 6, unpack10, pack10},
{8, 7, unpack8, pack8},
{7, 8, unpack7, pack7},
{6, 10, unpack6, pack6},
{5, 12, unpack5, pack5},
{4, 15, unpack4, pack4},
{3, 20, unpack3, pack3},
{2, 30, unpack2, pack2},
{1, 60, unpack1, pack1},
}
type packing32 struct {
n, bit int
unpack func(uint64, unsafe.Pointer)
pack func([]uint64) uint64
}
var selector32 [16]packing32 = [16]packing32{
{240, 0, unpack32bit240, pack240},
{120, 0, unpack32bit120, pack120},
{60, 1, unpack32bit60, pack60},
{30, 2, unpack32bit30, pack30},
{20, 3, unpack32bit20, pack20},
{15, 4, unpack32bit15, pack15},
{12, 5, unpack32bit12, pack12},
{10, 6, unpack32bit10, pack10},
{8, 7, unpack32bit8, pack8},
{7, 8, unpack32bit7, pack7},
{6, 10, unpack32bit6, pack6},
{5, 12, unpack32bit5, pack5},
{4, 15, unpack32bit4, pack4},
{3, 20, unpack32bit3, pack3},
{2, 30, unpack32bit2, pack2},
{1, 60, unpack32bit1, pack1},
}
type packing16 struct {
n, bit int
unpack func(uint64, unsafe.Pointer)
pack func([]uint64) uint64
}
var selector16 [16]packing16 = [16]packing16{
{240, 0, unpack16bit240, pack240},
{120, 0, unpack16bit120, pack120},
{60, 1, unpack16bit60, pack60},
{30, 2, unpack16bit30, pack30},
{20, 3, unpack16bit20, pack20},
{15, 4, unpack16bit15, pack15},
{12, 5, unpack16bit12, pack12},
{10, 6, unpack16bit10, pack10},
{8, 7, unpack16bit8, pack8},
{7, 8, unpack16bit7, pack7},
{6, 10, unpack16bit6, pack6},
{5, 12, unpack16bit5, pack5},
{4, 15, unpack16bit4, pack4},
{3, 20, unpack16bit3, pack3},
{2, 30, unpack16bit2, pack2},
{1, 60, unpack16bit1, pack1},
}
type packing8 struct {
n, bit int
unpack func(uint64, unsafe.Pointer)
pack func([]uint64) uint64
}
var selector8 [16]packing8 = [16]packing8{
{240, 0, unpack8bit240, pack240},
{120, 0, unpack8bit120, pack120},
{60, 1, unpack8bit60, pack60},
{30, 2, unpack8bit30, pack30},
{20, 3, unpack8bit20, pack20},
{15, 4, unpack8bit15, pack15},
{12, 5, unpack8bit12, pack12},
{10, 6, unpack8bit10, pack10},
{8, 7, unpack8bit8, pack8},
{7, 8, unpack8bit7, pack7},
{6, 10, unpack8bit6, pack6},
{5, 12, unpack8bit5, pack5},
{4, 15, unpack8bit4, pack4},
{3, 20, unpack8bit3, pack3},
{2, 30, unpack8bit2, pack2},
{1, 60, unpack8bit1, pack1},
}
// Count returns the number of integers encoded in the byte slice
func CountValues(b []byte) (int, error) {
return countValues(b)
}
// Count returns the number of integers encoded in the byte slice
func CountValuesBigEndian(b []byte) (int, error) {
return countValuesBigEndian(b)
}
// Count returns the number of integers encoded within an uint64
func Count(v uint64) (int, error) {
sel := v >> 60
if sel >= 16 {
return 0, fmt.Errorf("invalid selector value: %v", sel)
}
return selector[sel].n, nil
}
func ForEach(b []byte, fn func(v uint64) bool) error {
for len(b) >= 8 {
v := binary.BigEndian.Uint64(b[:8])
b = b[8:]
sel := v >> 60
if sel >= 16 {
return fmt.Errorf("invalid selector value: %v", sel)
}
n := selector[sel].n
bits := uint(selector[sel].bit)
mask := uint64(^(int64(^0) << bits))
for i := 0; i < n; i++ {
val := v & mask
if !fn(val) {
return nil
}
v = v >> bits
}
}
return nil
}
func CountBytesBetween(b []byte, min, max uint64) (int, error) {
var count int
for len(b) >= 8 {
v := binary.BigEndian.Uint64(b[:8])
b = b[8:]
sel := v >> 60
if sel >= 16 {
return 0, fmt.Errorf("invalid selector value: %v", sel)
}
// If the max value that could be encoded by the uint64 is less than the min
// skip the whole thing.
maxValue := uint64((1 << uint64(selector[sel].bit)) - 1)
if maxValue < min {
continue
}
mask := uint64(^(int64(^0) << uint(selector[sel].bit)))
for i := 0; i < selector[sel].n; i++ {
val := v & mask
if val >= min && val < max {
count++
} else if val > max {
break
}
v = v >> uint(selector[sel].bit)
}
}
if len(b) > 0 {
return 0, fmt.Errorf("invalid slice len remaining: %v", len(b))
}
return count, nil
}
// Encode packs as many values into a single uint64. It returns the packed
// uint64, how many values from src were packed, or an error if the values exceed
// the maximum value range.
func Encode(src []uint64) (value uint64, n int, err error) {
if canPack(src, 240, 0) {
return uint64(0), 240, nil
} else if canPack(src, 120, 0) {
return 1 << 60, 120, nil
} else if canPack(src, 60, 1) {
return pack60(src[:60]), 60, nil
} else if canPack(src, 30, 2) {
return pack30(src[:30]), 30, nil
} else if canPack(src, 20, 3) {
return pack20(src[:20]), 20, nil
} else if canPack(src, 15, 4) {
return pack15(src[:15]), 15, nil
} else if canPack(src, 12, 5) {
return pack12(src[:12]), 12, nil
} else if canPack(src, 10, 6) {
return pack10(src[:10]), 10, nil
} else if canPack(src, 8, 7) {
return pack8(src[:8]), 8, nil
} else if canPack(src, 7, 8) {
return pack7(src[:7]), 7, nil
} else if canPack(src, 6, 10) {
return pack6(src[:6]), 6, nil
} else if canPack(src, 5, 12) {
return pack5(src[:5]), 5, nil
} else if canPack(src, 4, 15) {
return pack4(src[:4]), 4, nil
} else if canPack(src, 3, 20) {
return pack3(src[:3]), 3, nil
} else if canPack(src, 2, 30) {
return pack2(src[:2]), 2, nil
} else if canPack(src, 1, 60) {
return pack1(src[:1]), 1, nil
} else {
if len(src) > 0 {
return 0, 0, fmt.Errorf("value out of bounds: %v", src)
}
return 0, 0, nil
}
}
const (
S8B_BIT_SIZE = 60
)
var (
numBits = [...][2]byte{
// { number of values, max bits per value }
{60, 1},
{30, 2},
{20, 3},
{15, 4},
{12, 5},
{10, 6},
{8, 7},
{7, 8},
{6, 10},
{5, 12},
{4, 15},
{3, 20},
{2, 30},
{1, 60},
}
ErrValueOutOfBounds = errors.New("value out of bounds")
)
// Encode returns a packed slice of the values from src. If a value is over
// 1 << 60, an error is returned. The input src is modified to avoid extra
// allocations. If you need to re-use, use a copy.
func EncodeAll(src []uint64) ([]uint64, error) {
i := 0
// Re-use the input slice and write encoded values back in place
dst := src
j := 0
NEXTVALUE:
for i < len(src) {
remaining := src[i:]
// try to pack run of 240 or 120 1s
if len(remaining) >= 120 {
// Invariant: len(a) is fixed to 120 or 240 values
var a []uint64
if len(remaining) >= 240 {
a = remaining[:240]
} else {
a = remaining[:120]
}
// search for the longest sequence of 1s in a
// Postcondition: k equals the index of the last 1 or -1
k := 0
for k = range a {
if a[k] != 1 {
k--
break
}
}
v := uint64(0)
switch {
case k == 239:
// 240 1s
i += 240
case k >= 119:
// at least 120 1s
v = 1 << 60
i += 120
default:
goto CODES
}
dst[j] = v
j++
continue
}
CODES:
for code := range numBits {
intN := int(numBits[code][0])
bitN := numBits[code][1]
if intN > len(remaining) {
continue
}
maxVal := uint64(1 << (bitN & 0x3f))
val := uint64(code+2) << S8B_BIT_SIZE
for k, inV := range remaining {
if k < intN {
if inV >= maxVal {
continue CODES
}
val |= inV << ((byte(k) * bitN) & 0x3f)
} else {
break
}
}
dst[j] = val
j += 1
i += intN
continue NEXTVALUE
}
return nil, ErrValueOutOfBounds
}
return dst[:j], nil
}
func Decode(dst *[240]uint64, v uint64) (n int, err error) {
sel := v >> 60
if sel >= 16 {
return 0, fmt.Errorf("invalid selector value: %b", sel)
}
selector[sel].unpack(v, (unsafe.Pointer)(dst))
return selector[sel].n, nil
}
// Decode writes the uncompressed values from src to dst. It returns the number
// of values written or an error.
// nocheckptr while the underlying struct layout doesn't change
//
//go:nocheckptr
func DecodeAll(dst, src []uint64) (value int, err error) {
j := 0
for _, v := range src {
sel := (v >> 60) & 0xf
selector[sel].unpack(v, unsafe.Pointer(&dst[j]))
j += selector[sel].n
}
return j, nil
}
// Decode writes the uncompressed values from src to dst. It returns the number
// of values written or an error.
func DecodeAllUint64(dst []uint64, src []byte) (value int, err error) {
return decodeAllUint64(dst, src)
}
// Decode writes the uncompressed values from src to dst. It returns the number
// of values written or an error.
func DecodeAllUint32(dst []uint32, src []byte) (value int, err error) {
return decodeAllUint32(dst, src)
}
// Decode writes the uncompressed values from src to dst. It returns the number
// of values written or an error.
func DecodeAllUint16(dst []uint16, src []byte) (value int, err error) {
return decodeAllUint16(dst, src)
}
// Decode writes the uncompressed values from src to dst. It returns the number
// of values written or an error.
func DecodeAllUint8(dst []uint8, src []byte) (value int, err error) {
return decodeAllUint8(dst, src)
}
// DecodeBytesBigEndian writes the compressed, big-endian values from src to dst. It returns the number
// of values written or an error.
func DecodeBytesBigEndian(dst []uint64, src []byte) (value int, err error) {
return decodeBytesBigEndian(dst, src)
}
// canPack returs true if n elements from in can be stored using bits per element
func canPack(src []uint64, n, bits int) bool {
if len(src) < n {
return false
}
// Selector 0,1 are special and use 0 bits to encode runs of 1's
if bits == 0 {
for _, v := range src {
if v != 1 {
return false
}
}
return true
}
max := uint64((1 << uint64(bits)) - 1)
for _, s := range src[:n] {
if s > max {
return false
}
}
return true
}
// pack240 packs 240 ones from in using 1 bit each
func pack240(src []uint64) uint64 {
return 0
}
// pack120 packs 120 ones from in using 1 bit each
func pack120(src []uint64) uint64 {
return 0
}
// pack60 packs 60 values from in using 1 bit each
func pack60(src []uint64) uint64 {
_ = src[59] // eliminate multiple bounds checks
return 2<<60 |
src[0] |
src[1]<<1 |
src[2]<<2 |
src[3]<<3 |
src[4]<<4 |
src[5]<<5 |
src[6]<<6 |
src[7]<<7 |
src[8]<<8 |
src[9]<<9 |
src[10]<<10 |
src[11]<<11 |
src[12]<<12 |
src[13]<<13 |
src[14]<<14 |
src[15]<<15 |
src[16]<<16 |
src[17]<<17 |
src[18]<<18 |
src[19]<<19 |
src[20]<<20 |
src[21]<<21 |
src[22]<<22 |
src[23]<<23 |
src[24]<<24 |
src[25]<<25 |
src[26]<<26 |
src[27]<<27 |
src[28]<<28 |
src[29]<<29 |
src[30]<<30 |
src[31]<<31 |
src[32]<<32 |
src[33]<<33 |
src[34]<<34 |
src[35]<<35 |
src[36]<<36 |
src[37]<<37 |
src[38]<<38 |
src[39]<<39 |
src[40]<<40 |
src[41]<<41 |
src[42]<<42 |
src[43]<<43 |
src[44]<<44 |
src[45]<<45 |
src[46]<<46 |
src[47]<<47 |
src[48]<<48 |
src[49]<<49 |
src[50]<<50 |
src[51]<<51 |
src[52]<<52 |
src[53]<<53 |
src[54]<<54 |
src[55]<<55 |
src[56]<<56 |
src[57]<<57 |
src[58]<<58 |
src[59]<<59
}
// pack30 packs 30 values from in using 2 bits each
func pack30(src []uint64) uint64 {
_ = src[29] // eliminate multiple bounds checks
return 3<<60 |
src[0] |
src[1]<<2 |
src[2]<<4 |
src[3]<<6 |
src[4]<<8 |
src[5]<<10 |
src[6]<<12 |
src[7]<<14 |
src[8]<<16 |
src[9]<<18 |
src[10]<<20 |
src[11]<<22 |
src[12]<<24 |
src[13]<<26 |
src[14]<<28 |
src[15]<<30 |
src[16]<<32 |
src[17]<<34 |
src[18]<<36 |
src[19]<<38 |
src[20]<<40 |
src[21]<<42 |
src[22]<<44 |
src[23]<<46 |
src[24]<<48 |
src[25]<<50 |
src[26]<<52 |
src[27]<<54 |
src[28]<<56 |
src[29]<<58
}
// pack20 packs 20 values from in using 3 bits each
func pack20(src []uint64) uint64 {
_ = src[19] // eliminate multiple bounds checks
return 4<<60 |
src[0] |
src[1]<<3 |
src[2]<<6 |
src[3]<<9 |
src[4]<<12 |
src[5]<<15 |
src[6]<<18 |
src[7]<<21 |
src[8]<<24 |
src[9]<<27 |
src[10]<<30 |
src[11]<<33 |
src[12]<<36 |
src[13]<<39 |
src[14]<<42 |
src[15]<<45 |
src[16]<<48 |
src[17]<<51 |
src[18]<<54 |
src[19]<<57
}
// pack15 packs 15 values from in using 3 bits each
func pack15(src []uint64) uint64 {
_ = src[14] // eliminate multiple bounds checks
return 5<<60 |
src[0] |
src[1]<<4 |
src[2]<<8 |
src[3]<<12 |
src[4]<<16 |
src[5]<<20 |
src[6]<<24 |
src[7]<<28 |
src[8]<<32 |
src[9]<<36 |
src[10]<<40 |
src[11]<<44 |
src[12]<<48 |
src[13]<<52 |
src[14]<<56
}
// pack12 packs 12 values from in using 5 bits each
func pack12(src []uint64) uint64 {
_ = src[11] // eliminate multiple bounds checks
return 6<<60 |
src[0] |
src[1]<<5 |
src[2]<<10 |
src[3]<<15 |
src[4]<<20 |
src[5]<<25 |
src[6]<<30 |
src[7]<<35 |
src[8]<<40 |
src[9]<<45 |
src[10]<<50 |
src[11]<<55
}
// pack10 packs 10 values from in using 6 bits each
func pack10(src []uint64) uint64 {
_ = src[9] // eliminate multiple bounds checks
return 7<<60 |
src[0] |
src[1]<<6 |
src[2]<<12 |
src[3]<<18 |
src[4]<<24 |
src[5]<<30 |
src[6]<<36 |
src[7]<<42 |
src[8]<<48 |
src[9]<<54
}
// pack8 packs 8 values from in using 7 bits each
func pack8(src []uint64) uint64 {
_ = src[7] // eliminate multiple bounds checks
return 8<<60 |
src[0] |
src[1]<<7 |
src[2]<<14 |
src[3]<<21 |
src[4]<<28 |
src[5]<<35 |
src[6]<<42 |
src[7]<<49
}
// pack7 packs 7 values from in using 8 bits each
func pack7(src []uint64) uint64 {
_ = src[6] // eliminate multiple bounds checks
return 9<<60 |
src[0] |
src[1]<<8 |
src[2]<<16 |
src[3]<<24 |
src[4]<<32 |
src[5]<<40 |
src[6]<<48
}
// pack6 packs 6 values from in using 10 bits each
func pack6(src []uint64) uint64 {
_ = src[5] // eliminate multiple bounds checks
return 10<<60 |
src[0] |
src[1]<<10 |
src[2]<<20 |
src[3]<<30 |
src[4]<<40 |
src[5]<<50
}
// pack5 packs 5 values from in using 12 bits each
func pack5(src []uint64) uint64 {
_ = src[4] // eliminate multiple bounds checks
return 11<<60 |
src[0] |
src[1]<<12 |
src[2]<<24 |
src[3]<<36 |
src[4]<<48
}
// pack4 packs 4 values from in using 15 bits each
func pack4(src []uint64) uint64 {
_ = src[3] // eliminate multiple bounds checks
return 12<<60 |
src[0] |
src[1]<<15 |
src[2]<<30 |
src[3]<<45
}
// pack3 packs 3 values from in using 20 bits each
func pack3(src []uint64) uint64 {
_ = src[2] // eliminate multiple bounds checks
return 13<<60 |
src[0] |
src[1]<<20 |
src[2]<<40
}
// pack2 packs 2 values from in using 30 bits each
func pack2(src []uint64) uint64 {
_ = src[1] // eliminate multiple bounds checks
return 14<<60 |
src[0] |
src[1]<<30
}
// pack1 packs 1 values from in using 60 bits each
func pack1(src []uint64) uint64 {
return 15<<60 |
src[0]
}
func unpack240(v uint64, p unsafe.Pointer) {
dst := (*[240]uint64)(p)
for i := range dst {
dst[i] = 1
}
}
func unpack120(v uint64, p unsafe.Pointer) {
dst := (*[120]uint64)(p)
for i := range dst {
dst[i] = 1
}
}
func unpack60(v uint64, p unsafe.Pointer) {
dst := (*[60]uint64)(p)
dst[0] = v & 1
dst[1] = (v >> 1) & 1
dst[2] = (v >> 2) & 1
dst[3] = (v >> 3) & 1
dst[4] = (v >> 4) & 1
dst[5] = (v >> 5) & 1
dst[6] = (v >> 6) & 1
dst[7] = (v >> 7) & 1
dst[8] = (v >> 8) & 1
dst[9] = (v >> 9) & 1
dst[10] = (v >> 10) & 1
dst[11] = (v >> 11) & 1
dst[12] = (v >> 12) & 1
dst[13] = (v >> 13) & 1
dst[14] = (v >> 14) & 1
dst[15] = (v >> 15) & 1
dst[16] = (v >> 16) & 1
dst[17] = (v >> 17) & 1
dst[18] = (v >> 18) & 1
dst[19] = (v >> 19) & 1
dst[20] = (v >> 20) & 1
dst[21] = (v >> 21) & 1
dst[22] = (v >> 22) & 1
dst[23] = (v >> 23) & 1
dst[24] = (v >> 24) & 1
dst[25] = (v >> 25) & 1
dst[26] = (v >> 26) & 1
dst[27] = (v >> 27) & 1
dst[28] = (v >> 28) & 1
dst[29] = (v >> 29) & 1
dst[30] = (v >> 30) & 1
dst[31] = (v >> 31) & 1
dst[32] = (v >> 32) & 1
dst[33] = (v >> 33) & 1
dst[34] = (v >> 34) & 1
dst[35] = (v >> 35) & 1
dst[36] = (v >> 36) & 1
dst[37] = (v >> 37) & 1
dst[38] = (v >> 38) & 1
dst[39] = (v >> 39) & 1
dst[40] = (v >> 40) & 1
dst[41] = (v >> 41) & 1
dst[42] = (v >> 42) & 1
dst[43] = (v >> 43) & 1
dst[44] = (v >> 44) & 1
dst[45] = (v >> 45) & 1
dst[46] = (v >> 46) & 1
dst[47] = (v >> 47) & 1
dst[48] = (v >> 48) & 1
dst[49] = (v >> 49) & 1
dst[50] = (v >> 50) & 1
dst[51] = (v >> 51) & 1
dst[52] = (v >> 52) & 1
dst[53] = (v >> 53) & 1
dst[54] = (v >> 54) & 1
dst[55] = (v >> 55) & 1
dst[56] = (v >> 56) & 1
dst[57] = (v >> 57) & 1
dst[58] = (v >> 58) & 1
dst[59] = (v >> 59) & 1
}
func unpack30(v uint64, p unsafe.Pointer) {
dst := (*[30]uint64)(p)
dst[0] = v & 3
dst[1] = (v >> 2) & 3
dst[2] = (v >> 4) & 3
dst[3] = (v >> 6) & 3
dst[4] = (v >> 8) & 3
dst[5] = (v >> 10) & 3
dst[6] = (v >> 12) & 3
dst[7] = (v >> 14) & 3
dst[8] = (v >> 16) & 3
dst[9] = (v >> 18) & 3
dst[10] = (v >> 20) & 3
dst[11] = (v >> 22) & 3
dst[12] = (v >> 24) & 3
dst[13] = (v >> 26) & 3
dst[14] = (v >> 28) & 3
dst[15] = (v >> 30) & 3
dst[16] = (v >> 32) & 3
dst[17] = (v >> 34) & 3
dst[18] = (v >> 36) & 3
dst[19] = (v >> 38) & 3
dst[20] = (v >> 40) & 3
dst[21] = (v >> 42) & 3
dst[22] = (v >> 44) & 3
dst[23] = (v >> 46) & 3
dst[24] = (v >> 48) & 3
dst[25] = (v >> 50) & 3
dst[26] = (v >> 52) & 3
dst[27] = (v >> 54) & 3
dst[28] = (v >> 56) & 3
dst[29] = (v >> 58) & 3
}
func unpack20(v uint64, p unsafe.Pointer) {
dst := (*[20]uint64)(p)
dst[0] = v & 7
dst[1] = (v >> 3) & 7
dst[2] = (v >> 6) & 7