-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbit_utilities.cc
1327 lines (1091 loc) · 30 KB
/
bit_utilities.cc
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
// bit_utilities.cc-- bit-related utility functions.
#include <cstdint>
#include <algorithm> // (for std::min)
#include "bit_utilities.h"
#define u8 std::uint8_t
#define u64 std::uint64_t
#define least_significant(type,numBits) ((((type)1)<<(numBits))-1)
//----------
//
// lookup table(s)
//
//----------
// popCount8 maps a byte to the number of 1s in that byte
static const u8 popCount8[256] =
{
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
};
//----------
//
// bitwise_is_all_zeros, bitwise_is_all_ones --
// Determine if a bit array is full of zeros (or ones).
//
//----------
//
// Arguments:
// const void* bits: Bit array to read.
// u64 numBits: The length of the bit array, counted in *bits*.
// .. See note (1) below.
//
// Returns:
// True if every bit in the array is zero (or, for bitwise_all_ones, one);
// false otherwise.
//
//----------
//
// Notes:
// (1) The number of bytes in the bit arrays is ceil(numBits/8). When numBits
// is not a multiple of 8, the remaining bits are read from the least
// significant bits of the final byte.
//
//----------
bool bitwise_is_all_zeros
(const void* bits,
const u64 numBits)
{
u64* scan = (u64*) bits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
{ if (*(scan++) != 0) return false; }
if (n == 0) return true;
u8* scanb = (u8*) scan;
for ( ; n>=8 ; n-=8)
{ if (*(scanb++) != 0) return false; }
if (n == 0) return true;
u8 mask = least_significant(u8,n);
return (((*scanb) & mask) == 0);
}
bool bitwise_is_all_ones
(const void* bits,
const u64 numBits)
{
u64* scan = (u64*) bits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
{ if (*(scan++) != ((u64) -1)) return false; }
if (n == 0) return true;
u8* scanb = (u8*) scan;
for ( ; n>=8 ; n-=8)
{ if (*(scanb++) != ((u8) -1)) return false; }
if (n == 0) return true;
u8 mask = least_significant(u8,n);
return (*scanb == mask);
}
//----------
//
// bitwise_copy--
// Copy one bit array to another.
//
//----------
//
// Arguments:
// const void* bits: Bit array to read.
// void* dstBits: Bit array to write.
// u64 numBits: The length of the bit arrays, counted in *bits*.
// .. See note (1) below.
//
// Returns:
// (nothing)
//
//----------
//
// Notes:
// (1) The number of bytes in the bit arrays is ceil(numBits/8). When numBits
// is not a multiple of 8, the remaining bits are read from the least
// significant bits of the final byte.
// (2) We process the bytes in 64-bit chunks until we get to the final chunk.
// The final chunk is processed byte-by-byte, so that we do not access
// any bytes beyond the bit arrays.
//
//----------
void bitwise_copy
(const void* bits,
void* dstBits,
const u64 numBits)
{
u64* scan = (u64*) bits;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = *(scan++);
if (n == 0) return;
u8* scanb = (u8*) scan;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = *(scanb++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (*scanb) & mask; // leftover bits intentionally set to zero
}
//----------
//
// bitwise_and, bitwise_mask, bitwise_or, bitwise_or_not, bitwise_xor,
// and bitwise_xnor--
// Create the bitwise AND, ANDNOT, OR, XOR, or XNOR of two bit arrays.
//
//----------
//
// Arguments:
// const void* bits1, bits2: Bit arrays to read.
// void* dstBits: Bit array to fill.
// u64 numBits: The length of the bit arrays, counted in *bits*.
// .. See note (1) below.
//
// Returns:
// (nothing)
//
//----------
//
// Notes:
// (1) The number of bytes in the bit arrays is ceil(numBits/8). When numBits
// is not a multiple of 8, the remaining bits are read from the least
// significant bits of the final byte.
// (2) We process the bytes in 64-bit chunks until we get to the final chunk.
// The final chunk is processed byte-by-byte, so that we do not access
// any bytes beyond the bit arrays.
// (3) Equivalences to set operations:
// function logic operation set operation
// ------------ --------------- --------------------
// bitwise_and a AND b a intersect b
// bitwise_mask a AND (NOT b) a\b (set difference)
// bitwise_or a OR b a union b
// bitwise_or_not a OR (NOT b) a union complement(b)
// bitwise_xor a XOR b a\b union b\a
// bitwise_xnor NOT (a XOR b) a == b
//
//----------
void bitwise_and
(const void* bits1,
const void* bits2,
void* dstBits,
const u64 numBits)
{
u64* scan1 = (u64*) bits1;
u64* scan2 = (u64*) bits2;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = *(scan1++) & *(scan2++);
if (n == 0) return;
u8* scan1b = (u8*) scan1;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = *(scan1b++) & *(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (*scan1b & *scan2b) & mask; // leftover bits intentionally set to zero
}
void bitwise_and
(void* dstBits,
const void* bits2,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64* scan2 = (u64*) bits2;
u64 n;
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst &= *(scan2++);
if (n == 0) return;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8,dstb++)
*dstb &= *(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ((*dstb & *scan2b) & mask) | (*dstb & ~mask);
}
void bitwise_mask
(const void* bits1,
const void* bits2,
void* dstBits,
const u64 numBits)
{
u64* scan1 = (u64*) bits1;
u64* scan2 = (u64*) bits2;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = *(scan1++) & ~*(scan2++);
if (n == 0) return;
u8* scan1b = (u8*) scan1;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = *(scan1b++) & ~*(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (*scan1b & ~*scan2b) & mask; // leftover bits intentionally set to zero
}
void bitwise_mask
(void* dstBits,
const void* bits2,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64* scan2 = (u64*) bits2;
u64 n;
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst &= ~*(scan2++);
if (n == 0) return;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8,dstb++)
*dstb &= ~*(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ((*dstb & ~*scan2b) & mask) | (*dstb & ~mask);
}
void bitwise_or
(const void* bits1,
const void* bits2,
void* dstBits,
const u64 numBits)
{
u64* scan1 = (u64*) bits1;
u64* scan2 = (u64*) bits2;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = *(scan1++) | *(scan2++);
if (n == 0) return;
u8* scan1b = (u8*) scan1;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = *(scan1b++) | *(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (*scan1b | *scan2b) & mask; // leftover bits intentionally set to zero
}
void bitwise_or
(void* dstBits,
const void* bits2,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64* scan2 = (u64*) bits2;
u64 n;
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst |= *(scan2++);
if (n == 0) return;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8,dstb++)
*dstb |= *(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ((*dstb | *scan2b) & mask) | (*dstb & ~mask);
}
void bitwise_or_not
(const void* bits1,
const void* bits2,
void* dstBits,
const u64 numBits)
{
u64* scan1 = (u64*) bits1;
u64* scan2 = (u64*) bits2;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = *(scan1++) | ~*(scan2++);
if (n == 0) return;
u8* scan1b = (u8*) scan1;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = *(scan1b++) | ~*(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (*scan1b | ~*scan2b) & mask; // leftover bits intentionally set to zero
}
void bitwise_or_not
(void* dstBits,
const void* bits2,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64* scan2 = (u64*) bits2;
u64 n;
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst |= ~*(scan2++);
if (n == 0) return;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8,dstb++)
*dstb |= ~*(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ((*dstb | ~*scan2b) & mask) | (*dstb & ~mask);
}
void bitwise_xor
(const void* bits1,
const void* bits2,
void* dstBits,
const u64 numBits)
{
u64* scan1 = (u64*) bits1;
u64* scan2 = (u64*) bits2;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = *(scan1++) ^ *(scan2++);
if (n == 0) return;
u8* scan1b = (u8*) scan1;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = *(scan1b++) ^ *(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (*scan1b ^ *scan2b) & mask; // leftover bits intentionally set to zero
}
void bitwise_xor
(void* dstBits,
const void* bits2,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64* scan2 = (u64*) bits2;
u64 n;
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst ^= *(scan2++);
if (n == 0) return;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8,dstb++)
*dstb ^= *(scan2b++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ((*dstb ^ *scan2b) & mask) | (*dstb & ~mask);
}
void bitwise_xnor
(const void* bits1,
const void* bits2,
void* dstBits,
const u64 numBits)
{
u64* scan1 = (u64*) bits1;
u64* scan2 = (u64*) bits2;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = ~(*(scan1++) ^ *(scan2++));
if (n == 0) return;
u8* scan1b = (u8*) scan1;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = ~(*(scan1b++) ^ *(scan2b++));
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ~((*scan1b ^ *scan2b)) & mask; // leftover bits intentionally set to zero
}
void bitwise_xnor
(void* dstBits,
const void* bits2,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64* scan2 = (u64*) bits2;
u64 n;
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst = ~(*dst ^ *(scan2++));
if (n == 0) return;
u8* scan2b = (u8*) scan2;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8,dstb++)
*dstb = ~(*dstb ^ *(scan2b++));
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (~((*dstb ^ *scan2b)) & mask) | (*dstb & ~mask);
}
//----------
//
// bitwise_complement--
// Create the bitwise complement of a bit array.
//
//----------
//
// Arguments:
// const void* bits: Bit array to read.
// void* dstBits: Bit array to fill.
// u64 numBits: The length of the bit arrays, counted in *bits*.
// .. See note (1) below.
//
// Returns:
// (nothing)
//
//----------
//
// Notes:
// (1) The number of bytes in the bit arrays is ceil(numBits/8). When numBits
// is not a multiple of 8, the remaining bits are read from the least
// significant bits of the final byte.
// (2) We process the bytes in 64-bit chunks until we get to the final chunk.
// The final chunk is processed byte-by-byte, so that we do not access
// any bytes beyond the bit arrays.
//
//----------
void bitwise_complement
(const void* bits,
void* dstBits,
const u64 numBits)
{
u64* scan = (u64*) bits;
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64)
*(dst++) = ~*(scan++);
if (n == 0) return;
u8* scanb = (u8*) scan;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8)
*(dstb++) = ~*(scanb++);
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ~((*scanb) & mask); // leftover bits intentionally set to zero
}
void bitwise_complement
(void* dstBits,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64 n;
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst = ~*dst;
if (n == 0) return;
u8* dstb = (u8*) dst;
for ( ; n>=8 ; n-=8,dstb++)
*dstb = ~*dstb;
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = ~((*dstb) & mask) | (*dstb & ~mask);
}
//----------
//
// bitwise_fill--
// Fill a bit array with 1s or 0s.
//
//----------
//
// Arguments:
// void* dstBits: Bit array to fill.
// int bitVal: Value to fill the bit array with. Only the least
// .. significant bit is used (the other bits are
// .. ignored).
// u64 numBits: The length of the bit arrays, counted in *bits*.
// .. See note (1) below.
//
// Returns:
// (nothing)
//
//----------
//
// Notes:
// (1) The number of bytes in the bit arrays is ceil(numBits/8). When numBits
// is not a multiple of 8, the remaining bits are in the least significant
// bits of the final byte.
// (2) We process the bytes in 64-bit chunks until we get to the final chunk.
// The final chunk is processed byte-by-byte, so that we do not access
// any bytes beyond the bit arrays.
//
//----------
void bitwise_fill
(void* dstBits,
const int bitVal,
const u64 numBits)
{
u64* dst = (u64*) dstBits;
u64 n;
u64 chunk64 = 0;
if ((bitVal & 1) == 1)
chunk64 = (u64) (-1);
for (n=numBits ; n>=64 ; n-=64,dst++)
*dst = chunk64;
if (n == 0) return;
u8* dstb = (u8*) dst;
u8 chunk8 = (u8) chunk64;
for ( ; n>=8 ; n-=8,dstb++)
*dstb = chunk8;
if (n == 0) return;
u8 mask = least_significant(u8,n);
*dstb = (chunk8 & mask) | (*dstb & ~mask);
}
//----------
//
// bitwise_squeeze--
// Make a copy of a bit array, only copying specified bit positions and
// packing the result, so that unspecified bit positions are squeezed out.
//
// Example:
// bits: 11000110001110001101000111011100111010010010001010
// specBits: 01110011011100100110111111000100110010001001100100
// copied bits: -100--10-011--0--10-000111---1--11--1---0--00--0--
// result: 1001001101000011111110000
//
//----------
//
// Arguments:
// const void* bits: Bit array to read.
// const void* specBits: Bit array indicating the bit positions to copy.
// .. 1s indicate positions to be copied.
// .. 0s indicate positions to be ignored.
// u64 numBits: The length of the input bit arrays (bits and
// .. specBits), counted in *bits*. See note (1) below.
// void* dstBits: Bit array to fill. Note that this must be as long
// .. as bits and specBits.
// u64 numDstBits: The length of the output bit array (dstBits),
// .. counted in *bits*. By default we assume this is
// .. the same as the input bit arrays.
//
// Returns:
// The number of bits copied to dstBits; note that the rest of dstBits has
// been cleared. It is up to the caller to modify whatever data object
// contains dstBits so that it knows the new length (if that's important).
// Note that the return value is never greater than numDstBits.
//
//----------
//
// Notes:
// (1) The number of bytes in the bit arrays is ceil(numBits/8) or
// ceil(numDstBits/8). When numBits is not a multiple of 8, the remaining
// bits are read from the least significant bits of the final byte.
// (2) We process the bytes in 64-bit chunks until we get to the final chunk.
// The final chunk is processed byte-by-byte, so that we do not access
// any bytes beyond the bit arrays.
//
//----------
std::uint64_t bitwise_squeeze
(const void* bits,
const void* specBits,
const u64 numBits,
void* dstBits,
const u64 _numDstBits)
{
u64* src = (u64*) bits;
u64* scan = (u64*) specBits;
u64* dst = (u64*) dstBits;
u64 numDstBits;
u64 n;
u8* scanb, *srcb, *chunkb, *dstb;
u64 bitsWritten = 0; // placate compiler, re goto hopping over true initialization
if (_numDstBits == ((u64)-1)) numDstBits = numBits;
else numDstBits = _numDstBits;
// copy from full 64-bit chunks
u64 dstChunk = 0;
u64 bitsInChunk = 0;
u64 bitsInDst = 0;
for (n=numBits ; n>=64 ; n-=64)
{
u64 specChunk = *(scan++);
u64 srcChunk = *(src++);
for (u8 b=0 ; b<64 ; b++)
{
if ((specChunk&1) == 1)
{
dstChunk |= (srcChunk&1) << bitsInChunk;
if (++bitsInChunk == 64)
{
if (bitsInDst+64 > numDstBits) goto overrun;
*(dst++) = dstChunk;
dstChunk = 0;
bitsInChunk = 0;
bitsInDst += 64;
}
}
specChunk >>= 1;
srcChunk >>= 1;
}
}
// copy from full bytes, if any remain
scanb = (u8*) scan;
srcb = (u8*) src;
if (n > 0)
{
for ( ; n>=8 ; n-=8)
{
u8 specByte = *(scanb++);
u8 srcByte = *(srcb++);
for (u8 b=0 ; b<8 ; b++)
{
if ((specByte&1) == 1)
{
dstChunk |= ((u64) (srcByte&1)) << bitsInChunk;
if (++bitsInChunk == 64)
{
if (bitsInDst+64 > numDstBits) goto overrun;
*(dst++) = dstChunk;
dstChunk = 0;
bitsInChunk = 0;
bitsInDst += 64;
}
}
specByte >>= 1;
srcByte >>= 1;
}
}
}
// copy from partial byte, if one remains
if (n > 0)
{
u8 specByte = *scanb;
u8 srcByte = *srcb;
for (u8 b=0 ; b<n ; b++)
{
if ((specByte&1) == 1)
{
dstChunk |= ((u64) (srcByte&1)) << bitsInChunk;
if (++bitsInChunk == 64)
{
if (bitsInDst+64 > numDstBits) goto overrun;
*(dst++) = dstChunk;
dstChunk = 0;
bitsInChunk = 0;
bitsInDst += 64;
}
}
specByte >>= 1;
srcByte >>= 1;
}
}
// write partial chunk, if we have one; note that if we're writing into a
// final partial chunk of the destination vector, we also clear the rest
// of that partial chunk and return
if (bitsInDst + bitsInChunk > numDstBits) goto overrun;
bitsWritten = bitsInDst;
if (bitsInChunk > 0)
{
if (numDstBits-bitsInDst >= 64)
{
*(dst++) = dstChunk;
bitsInDst += bitsInChunk;
bitsWritten += 64;
}
else
{
u64 bytesLeft = (numDstBits+7-bitsInDst) / 8;
chunkb = (u8*) &dstChunk;
dstb = (u8*) dst;
for (n=bitsInChunk ; n>=8 ; n-=8)
{ *(dstb++) = *(chunkb++); bytesLeft--; }
if (n > 0)
{ *(dstb++) = *(chunkb++); bytesLeft--; }
while (bytesLeft > 0) // erase remaining bytes in this partial chunk
{ *(dstb++) = 0; bytesLeft--; }
bitsInDst += bitsInChunk;
return bitsInDst;
}
}
// erase full 64-bit chunks, if any remain
for ( ; bitsWritten+64<numDstBits ; bitsWritten+=64)
{ *(dst++) = 0; bitsWritten += 64; }
// erase bytes, if any remain
dstb = (u8*) dst;
for ( ; bitsWritten<numDstBits ; bitsWritten+=8)
{ *(dstb++) = 0; bitsWritten += 8; }
return bitsInDst;
// we have a full or partial 64-bit chunk but we don't have enough bits
// left in the destination vector; write some of the chunk, byte-by-byte
overrun:
if (bitsInDst == numDstBits)
return bitsInDst;
u64 bitsToWrite = numDstBits - bitsInDst; // guaranteed to be less than 64
if (bitsInChunk > bitsToWrite) bitsInChunk = bitsToWrite;
dstChunk &= (((u64)1) << bitsToWrite) - 1; // erase excess bits from ms end of chunk
chunkb = (u8*) &dstChunk;
dstb = (u8*) dst;
for ( ; bitsToWrite>=8 ; bitsToWrite-=8)
*(dstb++) = *(chunkb++);
if (bitsToWrite > 0)
*(dstb++) = *(chunkb++);
return bitsInDst + bitsInChunk;
}
//----------
//
// bitwise_unsqueeze--
// Make a copy of a bit array, only copying *into* specified bit positions and
// expanding the result, so that unspecified bit positions are filled with
// zeros.
//
// This is a partial inverse of bitwise_squeeze, but the bits lost by that
// operation are zero-filled here. If instead one-fill is desired, the caller
// can OR the result with the complement of specBits.
//
// Example:
// bits: 1001001101000011111110000
// specBits: 01110011011100100110111111000100110010001001100100
// copied bits: -100--10-011--0--10-000111---1--11--1---0--00--0--
// result: 01000010001100000100000111000100110010000000000000
//
//----------
//
// Arguments:
// const void* bits: Bit array to read.
// u64 numBits: The length of the input bit array (bits), counted
// .. in *bits*. See note (1) below.
// const void* specBits: Bit array indicating the bit positions to copy to.
// .. 1s indicate positions to be copied to.
// .. 0s indicate positions to be zero-filled.
// u64 numSpecBits:The length of the specBits array, counted in
// .. *bits*. See note (1) below.
// void* dstBits: Bit array to fill. Note that this must be as long
// .. as bits and specBits.
// u64 numDstBits: The length of the output bit array (dstBits),
// .. counted in *bits*. By default we assume this is
// .. the same as the specBits array.
//
// Returns:
// The number of bits copied to dstBits; note that the rest of dstBits has
// been cleared. It is up to the caller to modify whatever data object
// contains dstBits so that it knows the new length (if that's important).
// Note that the return value is never greater than numDstBits.
//
//----------
//
// Notes:
// (1) The number of bytes in the bit arrays is ceil(numBits/8) or
// ceil(numDstBits/8). When numBits is not a multiple of 8, the remaining
// bits are read from the least significant bits of the final byte.
// (2) We process the bytes in 64-bit chunks until we get to the final chunk.
// The final chunk is processed byte-by-byte, so that we do not access
// any bytes beyond the bit arrays.
//
//----------
std::uint64_t bitwise_unsqueeze
(const void* bits,
const u64 numBits,
const void* specBits,
const u64 numSpecBits,
void* dstBits,
const u64 _numDstBits)
{
u64* src = (u64*) bits;
u64* scan = (u64*) specBits;
u64* dst = (u64*) dstBits;
u64 numDstBits;
u64 n;
u8* scanb, *chunkb, *dstb;
u64 bitsWritten = 0; // placate compiler, re goto hopping over true initialization
if (_numDstBits == ((u64)-1)) numDstBits = numSpecBits;
else numDstBits = _numDstBits;
// copy from full 64-bit chunks
u64 srcChunk = 0;
u64 bitsInSrcChunk = 0; // bits remaining in src chunk
u64 bitsInSrc = numSpecBits; // bits remaining in entire src
u8* srcb = nullptr;
u64 dstChunk = 0;
u64 bitsInDstChunk = 0;
u64 bitsInDst = 0;
for (n=numSpecBits ; n>=64 ; n-=64)
{
u64 specChunk = *(scan++);
for (u8 b=0 ; b<64 ; b++)
{
if ((specChunk&1) == 0)
{
// zero fill one bit in dst
; // nothing to do
}
else // if ((specChunk&1) == 1)
{
// copy one bit from src to dst
if (bitsInSrc == 0)
goto underrun;
if (bitsInSrcChunk == 0)
{
if (bitsInSrc >= 64)
{
srcChunk = *(src++);
bitsInSrcChunk = 64;
}
else
{
// (we might not need all the bits in this byte)
if (srcb == nullptr) srcb = (u8*) src;
srcChunk = *(srcb++);
bitsInSrcChunk = 8;
}
}
dstChunk |= (srcChunk&1) << bitsInDstChunk;
srcChunk >>= 1;