-
Notifications
You must be signed in to change notification settings - Fork 32
/
PackedArraySIMD.c
1499 lines (1224 loc) · 44.6 KB
/
PackedArraySIMD.c
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
// see README.md for usage instructions.
// (‑●‑●)> released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
#ifndef PACKEDARRAY_SELF
#define PACKEDARRAY_SELF "PackedArraySIMD.c"
#endif
#ifdef PACKEDARRAY_IMPL
#ifndef PACKEDARRAY_JOIN
#define PACKEDARRAY_JOIN(lhs, rhs) PACKEDARRAY_JOIN_(lhs, rhs)
#define PACKEDARRAY_JOIN_(lhs, rhs) PACKEDARRAY_JOIN__(lhs, rhs)
#define PACKEDARRAY_JOIN__(lhs, rhs) lhs##rhs
#endif // #ifndef PACKEDARRAY_JOIN
#ifndef PACKEDARRAY_IMPL_BITS_PER_ITEM
#error PACKEDARRAY_IMPL_BITS_PER_ITEM undefined
#endif // #ifndef PACKEDARRAY_IMPL_BITS_PER_ITEM
#if defined(PACKEDARRAY_IMPL_PACK_CASES) || defined(PACKEDARRAY_IMPL_UNPACK_CASES)
#ifndef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 0
#elif PACKEDARRAY_IMPL_CASE_I == 0
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 1
#elif PACKEDARRAY_IMPL_CASE_I == 1
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 2
#elif PACKEDARRAY_IMPL_CASE_I == 2
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 3
#elif PACKEDARRAY_IMPL_CASE_I == 3
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 4
#elif PACKEDARRAY_IMPL_CASE_I == 4
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 5
#elif PACKEDARRAY_IMPL_CASE_I == 5
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 6
#elif PACKEDARRAY_IMPL_CASE_I == 6
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 7
#elif PACKEDARRAY_IMPL_CASE_I == 7
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 8
#elif PACKEDARRAY_IMPL_CASE_I == 8
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 9
#elif PACKEDARRAY_IMPL_CASE_I == 9
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 10
#elif PACKEDARRAY_IMPL_CASE_I == 10
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 11
#elif PACKEDARRAY_IMPL_CASE_I == 11
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 12
#elif PACKEDARRAY_IMPL_CASE_I == 12
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 13
#elif PACKEDARRAY_IMPL_CASE_I == 13
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 14
#elif PACKEDARRAY_IMPL_CASE_I == 14
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 15
#elif PACKEDARRAY_IMPL_CASE_I == 15
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 16
#elif PACKEDARRAY_IMPL_CASE_I == 16
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 17
#elif PACKEDARRAY_IMPL_CASE_I == 17
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 18
#elif PACKEDARRAY_IMPL_CASE_I == 18
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 19
#elif PACKEDARRAY_IMPL_CASE_I == 19
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 20
#elif PACKEDARRAY_IMPL_CASE_I == 20
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 21
#elif PACKEDARRAY_IMPL_CASE_I == 21
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 22
#elif PACKEDARRAY_IMPL_CASE_I == 22
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 23
#elif PACKEDARRAY_IMPL_CASE_I == 23
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 24
#elif PACKEDARRAY_IMPL_CASE_I == 24
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 25
#elif PACKEDARRAY_IMPL_CASE_I == 25
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 26
#elif PACKEDARRAY_IMPL_CASE_I == 26
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 27
#elif PACKEDARRAY_IMPL_CASE_I == 27
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 28
#elif PACKEDARRAY_IMPL_CASE_I == 28
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 29
#elif PACKEDARRAY_IMPL_CASE_I == 29
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 30
#elif PACKEDARRAY_IMPL_CASE_I == 30
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 31
#elif PACKEDARRAY_IMPL_CASE_I == 31
#undef PACKEDARRAY_IMPL_CASE_I
#define PACKEDARRAY_IMPL_CASE_I 32
#endif // #ifndef PACKEDARRAY_IMPL_CASE_I
#ifndef PACKEDARRAY_IMPL_BITS_AVAILABLE
#define PACKEDARRAY_IMPL_BITS_AVAILABLE (32 - ((PACKEDARRAY_IMPL_CASE_I * PACKEDARRAY_IMPL_BITS_PER_ITEM) % 32))
#endif
#ifndef PACKEDARRAY_IMPL_START_BIT
#define PACKEDARRAY_IMPL_START_BIT ((PACKEDARRAY_IMPL_CASE_I * PACKEDARRAY_IMPL_BITS_PER_ITEM) % 32)
#endif
#if defined(PACKEDARRAY_IMPL_PACK_CASES)
#ifndef PACKEDARRAY_IMPL_PACK_CASE_BREAK
#define PACKEDARRAY_IMPL_PACK_CASE_BREAK
#endif
case PACKEDARRAY_IMPL_CASE_I:
#if (PACKEDARRAY_IMPL_BITS_PER_ITEM <= PACKEDARRAY_IMPL_BITS_AVAILABLE)
in_4 = PackedArray_loadu_uint32x4(in);
packed = PackedArray_vsli0_uint32x4(packed, in_4, PACKEDARRAY_IMPL_START_BIT);
in += 4;
#if (PACKEDARRAY_IMPL_BITS_PER_ITEM == PACKEDARRAY_IMPL_BITS_AVAILABLE)
PackedArray_store_uint32x4(out, packed);
out += 4;
packed = PackedArray_uint32x4_zero;
#endif
#else
in_4 = PackedArray_loadu_uint32x4(in);
packed = PackedArray_vsli0_uint32x4(packed, in_4, PACKEDARRAY_IMPL_START_BIT);
PackedArray_store_uint32x4(out, packed);
out += 4;
packed = PackedArray_shr_uint32x4(in_4, PACKEDARRAY_IMPL_BITS_AVAILABLE);
in += 4;
#endif
PACKEDARRAY_IMPL_PACK_CASE_BREAK
#if PACKEDARRAY_IMPL_CASE_I < 31
#include PACKEDARRAY_SELF
#else
#undef PACKEDARRAY_IMPL_CASE_I
#undef PACKEDARRAY_IMPL_PACK_CASE_BREAK
#undef PACKEDARRAY_IMPL_PACK_CASES
#endif
#elif defined(PACKEDARRAY_IMPL_UNPACK_CASES) // #if defined(PACKEDARRAY_IMPL_PACK_CASES)
#ifndef PACKEDARRAY_IMPL_UNPACK_CASE_BREAK
#define PACKEDARRAY_IMPL_UNPACK_CASE_BREAK
#endif
case PACKEDARRAY_IMPL_CASE_I:
#if (PACKEDARRAY_IMPL_BITS_PER_ITEM <= PACKEDARRAY_IMPL_BITS_AVAILABLE)
out_4 = PackedArray_and_uint32x4(PackedArray_shr_uint32x4(packed, PACKEDARRAY_IMPL_START_BIT), PackedArray_set_uint32x4(PACKEDARRAY_IMPL_MASK));
PackedArray_storeu_uint32x4(out, out_4);
out += 4;
PACKEDARRAY_IMPL_UNPACK_CASE_BREAK
#if (PACKEDARRAY_IMPL_CASE_I < 31) && (PACKEDARRAY_IMPL_BITS_PER_ITEM == PACKEDARRAY_IMPL_BITS_AVAILABLE)
in += 4;
packed = PackedArray_load_uint32x4(in);
#endif
#else
out_4 = PackedArray_shr_uint32x4(packed, PACKEDARRAY_IMPL_START_BIT);
in += 4;
packed = PackedArray_load_uint32x4(in);
out_4 = PackedArray_vsli0_uint32x4(out_4, packed, PACKEDARRAY_IMPL_BITS_AVAILABLE);
out_4 = PackedArray_and_uint32x4(out_4, PackedArray_set_uint32x4(PACKEDARRAY_IMPL_MASK));
PackedArray_storeu_uint32x4(out, out_4);
out += 4;
PACKEDARRAY_IMPL_UNPACK_CASE_BREAK
#endif
#if PACKEDARRAY_IMPL_CASE_I < 31
#include PACKEDARRAY_SELF
#else
#undef PACKEDARRAY_IMPL_CASE_I
#undef PACKEDARRAY_IMPL_UNPACK_CASE_BREAK
#undef PACKEDARRAY_IMPL_UNPACK_CASES
#endif
#endif // #elif defined(PACKEDARRAY_IMPL_UNPACK_CASES)
#else // #if defined(PACKEDARRAY_IMPL_PACK_CASES) || defined(PACKEDARRAY_IMPL_UNPACK_CASES)
#ifndef PACKEDARRAY_IMPL_MASK
#define PACKEDARRAY_IMPL_MASK (uint32_t)((1ULL << PACKEDARRAY_IMPL_BITS_PER_ITEM) - 1)
#endif
void PACKEDARRAY_JOIN(__PackedArray_pack_, PACKEDARRAY_IMPL_BITS_PER_ITEM)(uint32_t* __restrict buffer, uint32_t offset, const uint32_t* __restrict in, uint32_t count)
{
uint32_t pre, post;
uint32_t* __restrict out;
const uint32_t* __restrict end;
uint32_t startBit;
PackedArray_uint32x4_t packed, in_4, mask;
uint32_t offset_4;
pre = (offset + 3) / 4 * 4 - offset;
pre = pre > count ? count : pre;
if (pre > 0)
{
__PackedArray_pack_scalar(buffer, PACKEDARRAY_IMPL_BITS_PER_ITEM, PACKEDARRAY_IMPL_MASK, offset, in, pre);
offset += pre;
in += pre;
count -= pre;
}
post = count % 4;
count -= post;
if (count > 0)
{
out = &buffer[(offset / 4 * PACKEDARRAY_IMPL_BITS_PER_ITEM) / 32 * 4];
startBit = (offset / 4 * PACKEDARRAY_IMPL_BITS_PER_ITEM) % 32;
packed = PackedArray_load_uint32x4(out);
mask = PackedArray_sub_uint32x4(PackedArray_shl_uint32x4(PackedArray_set_uint32x4(1), startBit), PackedArray_set_uint32x4(1));
packed = PackedArray_and_uint32x4(packed, mask);
offset_4 = offset % 128;
offset += count;
if (count >= 128 - offset_4)
{
int32_t n;
n = (count + offset_4) / 128;
count -= 128 * n - offset_4;
switch (offset_4 / 4)
{
do
{
#define PACKEDARRAY_IMPL_PACK_CASES
#include PACKEDARRAY_SELF
} while (--n > 0);
}
if (count == 0)
goto PACKEDARRAY_JOIN(PACKEDARRAY_JOIN(__PackedArray_pack_, PACKEDARRAY_IMPL_BITS_PER_ITEM), _post);
offset_4 = 0;
startBit = 0;
}
end = in + count;
switch (offset_4 / 4)
{
#define PACKEDARRAY_IMPL_PACK_CASES
#define PACKEDARRAY_IMPL_PACK_CASE_BREAK \
if (in == end)\
break;
#include PACKEDARRAY_SELF
}
PACKEDARRAY_ASSERT(in == end);
if ((count / 4 * PACKEDARRAY_IMPL_BITS_PER_ITEM + startBit) % 32)
{
in_4 = PackedArray_loadu_uint32x4(out);
mask = PackedArray_sub_uint32x4(PackedArray_shl_uint32x4(PackedArray_set_uint32x4(1), ((count / 4 * PACKEDARRAY_IMPL_BITS_PER_ITEM + startBit - 1) % 32) + 1), PackedArray_set_uint32x4(1));
in_4 = PackedArray_andnot_uint32x4(in_4, mask);
packed = PackedArray_or_uint32x4(packed, in_4);
PackedArray_store_uint32x4(out, packed);
}
}
PACKEDARRAY_JOIN(PACKEDARRAY_JOIN(__PackedArray_pack_, PACKEDARRAY_IMPL_BITS_PER_ITEM), _post):
if (post > 0)
__PackedArray_pack_scalar(buffer, PACKEDARRAY_IMPL_BITS_PER_ITEM, PACKEDARRAY_IMPL_MASK, offset, in, post);
}
void PACKEDARRAY_JOIN(__PackedArray_unpack_, PACKEDARRAY_IMPL_BITS_PER_ITEM)(const uint32_t* __restrict buffer, uint32_t offset, uint32_t* __restrict out, uint32_t count)
{
uint32_t pre, post;
const uint32_t* __restrict in;
const uint32_t* __restrict end;
PackedArray_uint32x4_t packed, out_4;
uint32_t offset_4;
pre = (offset + 3) / 4 * 4 - offset;
pre = pre > count ? count : pre;
if (pre > 0)
{
__PackedArray_unpack_scalar(buffer, PACKEDARRAY_IMPL_BITS_PER_ITEM, PACKEDARRAY_IMPL_MASK, offset, out, pre);
offset += pre;
out += pre;
count -= pre;
}
post = count % 4;
count -= post;
if (count > 0)
{
in = &buffer[(offset / 4 * PACKEDARRAY_IMPL_BITS_PER_ITEM) / 32 * 4];
packed = PackedArray_load_uint32x4(in);
offset_4 = offset % 128;
offset += count;
if (count >= 128 - offset_4)
{
int32_t n;
n = (count + offset_4) / 128;
count -= 128 * n - offset_4;
switch (offset_4 / 4)
{
do
{
in += 4;
packed = PackedArray_load_uint32x4(in);
#define PACKEDARRAY_IMPL_UNPACK_CASES
#include PACKEDARRAY_SELF
} while (--n > 0);
}
if (count == 0)
goto PACKEDARRAY_JOIN(PACKEDARRAY_JOIN(__PackedArray_unpack_, PACKEDARRAY_IMPL_BITS_PER_ITEM), _post);
in += 4;
packed = PackedArray_loadu_uint32x4(in);
offset_4 = 0;
}
end = out + count;
switch (offset_4 / 4)
{
#define PACKEDARRAY_IMPL_UNPACK_CASES
#define PACKEDARRAY_IMPL_UNPACK_CASE_BREAK \
if (out == end)\
break;
#include PACKEDARRAY_SELF
}
PACKEDARRAY_ASSERT(out == end);
}
PACKEDARRAY_JOIN(PACKEDARRAY_JOIN(__PackedArray_unpack_, PACKEDARRAY_IMPL_BITS_PER_ITEM), _post):
if (post > 0)
__PackedArray_unpack_scalar(buffer, PACKEDARRAY_IMPL_BITS_PER_ITEM, PACKEDARRAY_IMPL_MASK, offset, out, post);
}
#undef PACKEDARRAY_IMPL_BITS_PER_ITEM
#undef PACKEDARRAY_IMPL_BITS_AVAILABLE
#undef PACKEDARRAY_IMPL_START_BIT
#undef PACKEDARRAY_IMPL_START_MASK
#endif // #if defined(PACKEDARRAY_IMPL_PACK_CASES) || defined(PACKEDARRAY_IMPL_UNPACK_CASES)
#else
#include "PackedArray.h"
#if !defined(PACKEDARRAY_ASSERT)
#include <assert.h>
#define PACKEDARRAY_ASSERT(expression) assert(expression)
#endif
#include <stddef.h>
static void __PackedArray_pack_scalar(uint32_t* buffer, const uint32_t bitsPerItem, const uint32_t mask, uint32_t offset, const uint32_t* in, uint32_t count)
{
uint32_t* __restrict out;
uint32_t startBit;
uint32_t bitsAvailable;
PACKEDARRAY_ASSERT(buffer != NULL);
PACKEDARRAY_ASSERT(in != NULL);
PACKEDARRAY_ASSERT(count != 0);
while (count--)
{
uint32_t value = *in++;
PACKEDARRAY_ASSERT(0 == (~mask & value));
out = &buffer[((uint64_t)offset / 4 * (uint64_t)bitsPerItem) / 32 * 4 + offset % 4];
startBit = ((uint64_t)offset / 4 * (uint64_t)bitsPerItem) % 32;
++offset;
bitsAvailable = 32 - startBit;
if (bitsPerItem <= bitsAvailable)
{
out[0] = (out[0] & ~(mask << startBit)) | (value << startBit);
}
else
{
// value spans 2 buffer cells
uint32_t low, high;
low = value << startBit;
high = value >> bitsAvailable;
out[0] = (out[0] & ~(mask << startBit)) | low;
out[4] = (out[4] & ~(mask >> bitsAvailable)) | high;
}
}
}
static void __PackedArray_unpack_scalar(const uint32_t* buffer, const uint32_t bitsPerItem, const uint32_t mask, uint32_t offset, uint32_t* out, uint32_t count)
{
const uint32_t* __restrict in;
uint32_t startBit;
uint32_t bitsAvailable;
PACKEDARRAY_ASSERT(buffer != NULL);
PACKEDARRAY_ASSERT(out != NULL);
PACKEDARRAY_ASSERT(count != 0);
while (count--)
{
uint32_t value;
in = &buffer[((uint64_t)offset / 4 * (uint64_t)bitsPerItem) / 32 * 4 + offset % 4];
startBit = ((uint64_t)offset / 4 * (uint64_t)bitsPerItem) % 32;
++offset;
bitsAvailable = 32 - startBit;
if (bitsPerItem <= bitsAvailable)
{
value = (in[0] >> startBit) & mask;
}
else
{
// value spans 2 buffer cells
uint32_t low, high;
low = in[0] >> startBit;
high = in[4] << bitsAvailable;
value = (low | high) & mask;
}
*out++ = value;
}
}
#if defined(__SSE2__) || defined(_M_IX86) || defined(_M_X64)
#include <emmintrin.h>
#define PackedArray_uint32x4_t __m128i
#define PackedArray_uint32x4_zero _mm_setzero_si128()
#define PackedArray_set_uint32x4(i) _mm_set1_epi32(i)
#define PackedArray_sub_uint32x4(lhs, rhs) _mm_sub_epi32(lhs, rhs)
#define PackedArray_loadu_uint32x4(ptr) _mm_loadu_si128((const __m128i*)ptr)
#define PackedArray_storeu_uint32x4(ptr, v) _mm_storeu_si128((__m128i*)ptr, v)
#define PackedArray_load_uint32x4(ptr) _mm_load_si128((const __m128i*)ptr)
#define PackedArray_store_uint32x4(ptr, v) _mm_store_si128((__m128i*)ptr, v)
#define PackedArray_shl_uint32x4(v, shift) _mm_slli_epi32(v, shift)
#define PackedArray_shr_uint32x4(v, shift) _mm_srli_epi32(v, shift)
#define PackedArray_or_uint32x4(lhs, rhs) _mm_or_si128(lhs, rhs)
#define PackedArray_and_uint32x4(lhs, rhs) _mm_and_si128(lhs, rhs)
#define PackedArray_andnot_uint32x4(lhs, rhs) _mm_andnot_si128(rhs, lhs)
// assumes dst bits are cleared at inserted bit positions
#define PackedArray_vsli0_uint32x4(dst, src, shift) PackedArray_or_uint32x4(dst, PackedArray_shl_uint32x4(src, shift))
#elif defined(__ARM_NEON__) || defined(_M_ARM)
#include <arm_neon.h>
#define PackedArray_uint32x4_t uint32x4_t
#define PackedArray_uint32x4_zero vdupq_n_u32(0)
#define PackedArray_set_uint32x4(i) vdupq_n_u32(i)
#define PackedArray_sub_uint32x4(lhs, rhs) vsubq_u32(lhs, rhs)
#define PackedArray_loadu_uint32x4(ptr) vld1q_u32((const uint32_t*)ptr)
#define PackedArray_storeu_uint32x4(ptr, v) vst1q_u32(ptr, v)
#if defined(__GNUC__)
// because __builtin_assume_aligned isn't always available...
typedef uint32_t __attribute__((aligned(16))) PackedArray_aligned_uint32_t;
#define PackedArray_load_uint32x4(ptr) vld1q_u32((const PackedArray_aligned_uint32_t*)ptr)
#define PackedArray_store_uint32x4(ptr, v) vst1q_u32((PackedArray_aligned_uint32_t*)ptr, v)
#else
#define PackedArray_load_uint32x4(ptr) vld1q_u32((const uint32_t*)ptr)
#define PackedArray_store_uint32x4(ptr, v) vst1q_u32(ptr, v)
#endif
#define PackedArray_shl_uint32x4(v, shift) vshlq_u32(v, vdupq_n_s32(shift))
#define PackedArray_shr_uint32x4(v, shift) vshlq_u32(v, vdupq_n_s32(-shift))
#define PackedArray_or_uint32x4(lhs, rhs) vorrq_u32(lhs, rhs)
#define PackedArray_and_uint32x4(lhs, rhs) vandq_u32(lhs, rhs)
#define PackedArray_andnot_uint32x4(lhs, rhs) vbicq_u32(lhs, rhs)
// assumes dst bits are cleared at inserted bit positions
#define PackedArray_vsli0_uint32x4(dst, src, shift) vsliq_n_u32(dst, src, shift)
#else
#error unsupported SIMD platform
#endif
#define PACKEDARRAY_IMPL
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 1
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 2
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 3
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 4
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 5
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 6
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 7
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 8
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 9
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 10
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 11
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 12
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 13
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 14
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 15
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 16
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 17
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 18
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 19
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 20
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 21
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 22
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 23
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 24
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 25
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 26
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 27
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 28
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 29
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 30
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 31
#include PACKEDARRAY_SELF
#define PACKEDARRAY_IMPL_BITS_PER_ITEM 32
#include PACKEDARRAY_SELF
#undef PACKEDARRAY_IMPL
#if !defined(PACKEDARRAY_ALIGNED_MALLOC) || !defined(PACKEDARRAY_FREE)
#include <stdlib.h>
#endif
#if !defined(PACKEDARRAY_ALIGNED_MALLOC)
#if defined (_MSC_VER)
#define PACKEDARRAY_ALIGNED_MALLOC(alignment, size) _aligned_malloc(size, alignment)
#elif defined (ANDROID) || defined (__ANDROID__)
#define PACKEDARRAY_ALIGNED_MALLOC(alignment, size) memalign(alignment, size)
#else
static void* __PackedArray_aligned_malloc(size_t alignment, size_t size)
{
void* p = NULL;
posix_memalign(&p, alignment, size);
return p;
}
#define PACKEDARRAY_ALIGNED_MALLOC(alignment, size) __PackedArray_aligned_malloc(alignment, size)
#endif
#endif
#if !defined(PACKEDARRAY_FREE)
#if defined (_MSC_VER)
#define PACKEDARRAY_FREE(p) _aligned_free(p)
#else
#define PACKEDARRAY_FREE(p) free(p)
#endif
#endif
PackedArray* PackedArray_create(uint32_t bitsPerItem, uint32_t count)
{
PackedArray* a;
size_t bufferSize;
PACKEDARRAY_ASSERT(bitsPerItem > 0);
PACKEDARRAY_ASSERT(bitsPerItem <= 32);
bufferSize = sizeof(uint32_t) * (((uint64_t)count / 4 * (uint64_t)bitsPerItem + 31) / 32 * 4);
bufferSize += count < 4 ? sizeof(uint32_t) * count : sizeof(uint32_t) * 4;
a = (PackedArray*)PACKEDARRAY_ALIGNED_MALLOC(16, sizeof(PackedArray) + bufferSize);
PACKEDARRAY_ASSERT((uint64_t)a->buffer % 16 == 0);
if (a != NULL)
{
a->buffer[bufferSize / sizeof(uint32_t) - 1] = 0;
a->bitsPerItem = bitsPerItem;
a->count = count;
}
return a;
}
void PackedArray_destroy(PackedArray* a)
{
PACKEDARRAY_ASSERT(a);
PACKEDARRAY_FREE(a);
}
void PackedArray_pack(PackedArray* a, const uint32_t offset, const uint32_t* in, uint32_t count)
{
PACKEDARRAY_ASSERT(a != NULL);
PACKEDARRAY_ASSERT(in != NULL);
switch (a->bitsPerItem)
{
case 1: __PackedArray_pack_1(a->buffer, offset, in, count); break;
case 2: __PackedArray_pack_2(a->buffer, offset, in, count); break;
case 3: __PackedArray_pack_3(a->buffer, offset, in, count); break;
case 4: __PackedArray_pack_4(a->buffer, offset, in, count); break;
case 5: __PackedArray_pack_5(a->buffer, offset, in, count); break;
case 6: __PackedArray_pack_6(a->buffer, offset, in, count); break;
case 7: __PackedArray_pack_7(a->buffer, offset, in, count); break;
case 8: __PackedArray_pack_8(a->buffer, offset, in, count); break;
case 9: __PackedArray_pack_9(a->buffer, offset, in, count); break;
case 10: __PackedArray_pack_10(a->buffer, offset, in, count); break;
case 11: __PackedArray_pack_11(a->buffer, offset, in, count); break;
case 12: __PackedArray_pack_12(a->buffer, offset, in, count); break;
case 13: __PackedArray_pack_13(a->buffer, offset, in, count); break;
case 14: __PackedArray_pack_14(a->buffer, offset, in, count); break;
case 15: __PackedArray_pack_15(a->buffer, offset, in, count); break;
case 16: __PackedArray_pack_16(a->buffer, offset, in, count); break;
case 17: __PackedArray_pack_17(a->buffer, offset, in, count); break;
case 18: __PackedArray_pack_18(a->buffer, offset, in, count); break;
case 19: __PackedArray_pack_19(a->buffer, offset, in, count); break;
case 20: __PackedArray_pack_20(a->buffer, offset, in, count); break;
case 21: __PackedArray_pack_21(a->buffer, offset, in, count); break;
case 22: __PackedArray_pack_22(a->buffer, offset, in, count); break;
case 23: __PackedArray_pack_23(a->buffer, offset, in, count); break;
case 24: __PackedArray_pack_24(a->buffer, offset, in, count); break;
case 25: __PackedArray_pack_25(a->buffer, offset, in, count); break;
case 26: __PackedArray_pack_26(a->buffer, offset, in, count); break;
case 27: __PackedArray_pack_27(a->buffer, offset, in, count); break;
case 28: __PackedArray_pack_28(a->buffer, offset, in, count); break;
case 29: __PackedArray_pack_29(a->buffer, offset, in, count); break;
case 30: __PackedArray_pack_30(a->buffer, offset, in, count); break;
case 31: __PackedArray_pack_31(a->buffer, offset, in, count); break;
case 32: __PackedArray_pack_32(a->buffer, offset, in, count); break;
}
}
void PackedArray_unpack(const PackedArray* a, const uint32_t offset, uint32_t* out, uint32_t count)
{
PACKEDARRAY_ASSERT(a != NULL);
PACKEDARRAY_ASSERT(out != NULL);
switch (a->bitsPerItem)
{
case 1: __PackedArray_unpack_1(a->buffer, offset, out, count); break;
case 2: __PackedArray_unpack_2(a->buffer, offset, out, count); break;
case 3: __PackedArray_unpack_3(a->buffer, offset, out, count); break;
case 4: __PackedArray_unpack_4(a->buffer, offset, out, count); break;
case 5: __PackedArray_unpack_5(a->buffer, offset, out, count); break;
case 6: __PackedArray_unpack_6(a->buffer, offset, out, count); break;
case 7: __PackedArray_unpack_7(a->buffer, offset, out, count); break;
case 8: __PackedArray_unpack_8(a->buffer, offset, out, count); break;
case 9: __PackedArray_unpack_9(a->buffer, offset, out, count); break;
case 10: __PackedArray_unpack_10(a->buffer, offset, out, count); break;
case 11: __PackedArray_unpack_11(a->buffer, offset, out, count); break;
case 12: __PackedArray_unpack_12(a->buffer, offset, out, count); break;
case 13: __PackedArray_unpack_13(a->buffer, offset, out, count); break;
case 14: __PackedArray_unpack_14(a->buffer, offset, out, count); break;
case 15: __PackedArray_unpack_15(a->buffer, offset, out, count); break;
case 16: __PackedArray_unpack_16(a->buffer, offset, out, count); break;
case 17: __PackedArray_unpack_17(a->buffer, offset, out, count); break;
case 18: __PackedArray_unpack_18(a->buffer, offset, out, count); break;
case 19: __PackedArray_unpack_19(a->buffer, offset, out, count); break;
case 20: __PackedArray_unpack_20(a->buffer, offset, out, count); break;
case 21: __PackedArray_unpack_21(a->buffer, offset, out, count); break;
case 22: __PackedArray_unpack_22(a->buffer, offset, out, count); break;
case 23: __PackedArray_unpack_23(a->buffer, offset, out, count); break;
case 24: __PackedArray_unpack_24(a->buffer, offset, out, count); break;
case 25: __PackedArray_unpack_25(a->buffer, offset, out, count); break;
case 26: __PackedArray_unpack_26(a->buffer, offset, out, count); break;
case 27: __PackedArray_unpack_27(a->buffer, offset, out, count); break;
case 28: __PackedArray_unpack_28(a->buffer, offset, out, count); break;
case 29: __PackedArray_unpack_29(a->buffer, offset, out, count); break;
case 30: __PackedArray_unpack_30(a->buffer, offset, out, count); break;
case 31: __PackedArray_unpack_31(a->buffer, offset, out, count); break;
case 32: __PackedArray_unpack_32(a->buffer, offset, out, count); break;
}
}
void PackedArray_set(PackedArray* a, const uint32_t offset, const uint32_t in)
{
uint32_t* __restrict out;
uint32_t bitsPerItem;
uint32_t startBit;
uint32_t bitsAvailable;
uint32_t mask;
PACKEDARRAY_ASSERT(a != NULL);
bitsPerItem = a->bitsPerItem;
out = &a->buffer[4 * (((uint64_t)offset / 4 * (uint64_t)bitsPerItem) / 32) + (offset % 4)];
startBit = ((uint64_t)offset / 4 * (uint64_t)bitsPerItem) % 32;
bitsAvailable = 32 - startBit;
mask = (uint32_t)(1ULL << bitsPerItem) - 1;
PACKEDARRAY_ASSERT(0 == (~mask & in));
if (bitsPerItem <= bitsAvailable)
{
out[0] = (out[0] & ~(mask << startBit)) | (in << startBit);
}
else
{
// value spans 2 buffer cells
uint32_t low, high;
low = in << startBit;
high = in >> bitsAvailable;
out[0] = (out[0] & ~(mask << startBit)) | low;
out[4] = (out[4] & ~(mask >> (32 - startBit))) | high;
}
}
uint32_t PackedArray_get(const PackedArray* a, const uint32_t offset)
{
const uint32_t* __restrict in;
uint32_t bitsPerItem;
uint32_t startBit;
uint32_t bitsAvailable;
uint32_t mask;
uint32_t out;
PACKEDARRAY_ASSERT(a != NULL);
bitsPerItem = a->bitsPerItem;
in = &a->buffer[4 * (((uint64_t)offset / 4 * (uint64_t)bitsPerItem) / 32) + (offset % 4)];
startBit = ((uint64_t)offset / 4 * (uint64_t)bitsPerItem) % 32;
bitsAvailable = 32 - startBit;
mask = (uint32_t)(1ULL << bitsPerItem) - 1;
if (bitsPerItem <= bitsAvailable)
{
out = (in[0] >> startBit) & mask;
}
else
{
// value spans 2 buffer cells
uint32_t low, high;
low = in[0] >> startBit;
high = in[4] << (32 - startBit);
out = low ^ ((low ^ high) & (mask >> bitsAvailable << bitsAvailable));
}
return out;
}
uint32_t PackedArray_bufferSize(const PackedArray* a)
{
size_t bufferSize;
uint32_t bitsPerItem, count;
PACKEDARRAY_ASSERT(a != NULL);
bitsPerItem = a->bitsPerItem;
count = a->count;
bufferSize = ((uint64_t)count / 4 * (uint64_t)bitsPerItem + 31) / 32 * 4;
bufferSize += count < 4 ? count : 4;
return (uint32_t)bufferSize;
}
#if !(defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(__GNUC__)
// log base 2 of an integer, aka the position of the highest bit set
static uint32_t __PackedArray_log2(uint32_t v)
{
// references
// http://aggregate.org/MAGIC
// http://graphics.stanford.edu/~seander/bithacks.html
static const uint32_t multiplyDeBruijnBitPosition[32] =
{
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return multiplyDeBruijnBitPosition[(uint32_t)(v * 0x7C4ACDDU) >> 27];
}
#endif
// position of the highest bit set
static int __PackedArray_highestBitSet(uint32_t v)
{
#if defined(_MSC_VER) && _MSC_VER >= 1400
unsigned long index;
return _BitScanReverse(&index, v) ? index : -1;
#elif defined(__GNUC__)
return v == 0 ? -1 : 31 - __builtin_clz(v);
#else
return v != 0 ? __PackedArray_log2(v) : -1;
#endif
}
uint32_t PackedArray_computeBitsPerItem(const uint32_t* in, uint32_t count)
{
uint32_t i, in_max, bitsPerItem;
in_max = 0;
for (i = 0; i < count; ++i)
in_max = in[i] > in_max ? in[i] : in_max;
bitsPerItem = __PackedArray_highestBitSet(in_max) + 1;
return bitsPerItem == 0 ? 1 : bitsPerItem;
}
// - 8< ------------------------------------------------------------------------
#if defined(PACKEDARRAY_SELF_TEST) && defined(PACKEDARRAY_SELF_BENCH)
#error choose either PACKEDARRAY_SELF_TEST or PACKEDARRAY_SELF_BENCH
#endif
#if defined(PACKEDARRAY_SELF_TEST)
#undef NDEBUG // we want asserts
#include <assert.h>
#include <stdio.h>
#include <string.h> // memcmp
static void PackedArray_pack_reference(PackedArray* a, uint32_t offset, const uint32_t* in, uint32_t count)
{
uint32_t* __restrict out;
uint32_t bitsPerItem;
uint32_t startBit;
uint32_t bitsAvailable;
uint32_t mask;
assert(a != NULL);
assert(in != NULL);
bitsPerItem = a->bitsPerItem;
mask = (uint32_t)((1ULL << bitsPerItem) - 1);
while (count--)
{
uint32_t value = *in++;
assert(0 == (~mask & value));
out = &a->buffer[((uint64_t)offset / 4 * (uint64_t)bitsPerItem) / 32 * 4 + offset % 4];
startBit = ((uint64_t)offset / 4 * (uint64_t)bitsPerItem) % 32;
++offset;
bitsAvailable = 32 - startBit;
if (bitsPerItem <= bitsAvailable)
{
out[0] = (out[0] & ~(mask << startBit)) | (value << startBit);
}
else
{
// value spans 2 buffer cells
uint32_t low, high;
low = value << startBit;
high = value >> bitsAvailable;
out[0] = (out[0] & ~(mask << startBit)) | low;
out[4] = (out[4] & ~(mask >> bitsAvailable)) | high;
}
}
}
static void PackedArray_unpack_reference(const PackedArray* a, uint32_t offset, uint32_t* out, uint32_t count)
{
const uint32_t* __restrict in;
uint32_t bitsPerItem;
uint32_t startBit;
uint32_t bitsAvailable;
uint32_t mask;
assert(a != NULL);
assert(out != NULL);
bitsPerItem = a->bitsPerItem;
mask = (uint32_t)((1ULL << bitsPerItem) - 1);
while (count--)
{
uint32_t value;
in = &a->buffer[((uint64_t)offset / 4 * (uint64_t)bitsPerItem) / 32 * 4 + offset % 4];
startBit = ((uint64_t)offset / 4 * (uint64_t)bitsPerItem) % 32;
++offset;
bitsAvailable = 32 - startBit;
if (bitsPerItem <= bitsAvailable)
{
value = (in[0] >> startBit) & mask;
}
else
{
// value spans 2 buffer cells
uint32_t low, high;
low = in[0] >> startBit;
high = in[4] << bitsAvailable;
value = low ^ ((low ^ high) & (mask >> bitsAvailable << bitsAvailable));
}
*out++ = value;
}
}
int main(void)
{
uint32_t bitsPerItem;
printf("-- PackedArray self test -------------------------------------------------------\n");
printf("\n");
printf("sizeof(PackedArray) = %d\n", (int)sizeof(PackedArray));
printf("\n");
printf("1 by 1 packing / unpacking:\n");
for (bitsPerItem = 1; bitsPerItem <= 32; ++bitsPerItem)
{
uint32_t mask = (uint32_t)(1ULL << bitsPerItem) - 1;
int count;
for (count = 1; count <= 1024; ++count)
{
PackedArray* a1 = PackedArray_create(bitsPerItem, count);
PackedArray* a2 = PackedArray_create(bitsPerItem, count);
int i;
assert(a1->count == (uint32_t)count);
assert(a2->count == (uint32_t)count);
assert(a1->bitsPerItem == bitsPerItem);
assert(a2->bitsPerItem == bitsPerItem);
assert(PackedArray_bufferSize(a1) == PackedArray_bufferSize(a2));
for (i = 0; i < (int)PackedArray_bufferSize(a1); ++i)
a1->buffer[i] = a2->buffer[i] = rand();
for (i = 0; i < count; ++i)
{
uint32_t v1, v2;
v1 = rand() & mask;
v2 = v1 + 1;