-
Notifications
You must be signed in to change notification settings - Fork 44
/
bit_array.c
3181 lines (2620 loc) · 81.8 KB
/
bit_array.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
/*
bit_array.c
project: bit array C library
url: https://github.com/noporpoise/BitArray/
maintainer: Isaac Turner <[email protected]>
license: Public Domain, no warranty
date: Aug 2014
*/
// 64 bit words
// Array length can be zero
// Unused top bits must be zero
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <limits.h> // ULONG_MAX
#include <errno.h>
#include <signal.h> // needed for abort()
#include <string.h> // memset()
#include <assert.h>
#include <time.h> // needed for seeding rand()
#include <unistd.h> // need for getpid() for seeding rand number
#include <ctype.h> // need for tolower()
#include <errno.h> // perror()
#include <sys/time.h> // for seeding random
// Windows includes
#if defined(_WIN32)
#include <intrin.h>
#endif
#include "bit_array.h"
#include "bit_macros.h"
//
// Tables of constants
//
// byte reverse look up table
static const word_t reverse_table[256] =
{
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF,
};
// Morton table for interleaving bytes
static const word_t morton_table0[256] =
{
0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015,
0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055,
0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115,
0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155,
0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415,
0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455,
0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515,
0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555,
0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015,
0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055,
0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115,
0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155,
0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415,
0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455,
0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515,
0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555,
0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015,
0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055,
0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115,
0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155,
0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415,
0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455,
0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515,
0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555,
0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015,
0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055,
0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115,
0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155,
0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415,
0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455,
0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515,
0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555,
};
// Morton table for interleaving bytes, shifted left 1 bit
static const word_t morton_table1[256] =
{
0x0000, 0x0002, 0x0008, 0x000A, 0x0020, 0x0022, 0x0028, 0x002A,
0x0080, 0x0082, 0x0088, 0x008A, 0x00A0, 0x00A2, 0x00A8, 0x00AA,
0x0200, 0x0202, 0x0208, 0x020A, 0x0220, 0x0222, 0x0228, 0x022A,
0x0280, 0x0282, 0x0288, 0x028A, 0x02A0, 0x02A2, 0x02A8, 0x02AA,
0x0800, 0x0802, 0x0808, 0x080A, 0x0820, 0x0822, 0x0828, 0x082A,
0x0880, 0x0882, 0x0888, 0x088A, 0x08A0, 0x08A2, 0x08A8, 0x08AA,
0x0A00, 0x0A02, 0x0A08, 0x0A0A, 0x0A20, 0x0A22, 0x0A28, 0x0A2A,
0x0A80, 0x0A82, 0x0A88, 0x0A8A, 0x0AA0, 0x0AA2, 0x0AA8, 0x0AAA,
0x2000, 0x2002, 0x2008, 0x200A, 0x2020, 0x2022, 0x2028, 0x202A,
0x2080, 0x2082, 0x2088, 0x208A, 0x20A0, 0x20A2, 0x20A8, 0x20AA,
0x2200, 0x2202, 0x2208, 0x220A, 0x2220, 0x2222, 0x2228, 0x222A,
0x2280, 0x2282, 0x2288, 0x228A, 0x22A0, 0x22A2, 0x22A8, 0x22AA,
0x2800, 0x2802, 0x2808, 0x280A, 0x2820, 0x2822, 0x2828, 0x282A,
0x2880, 0x2882, 0x2888, 0x288A, 0x28A0, 0x28A2, 0x28A8, 0x28AA,
0x2A00, 0x2A02, 0x2A08, 0x2A0A, 0x2A20, 0x2A22, 0x2A28, 0x2A2A,
0x2A80, 0x2A82, 0x2A88, 0x2A8A, 0x2AA0, 0x2AA2, 0x2AA8, 0x2AAA,
0x8000, 0x8002, 0x8008, 0x800A, 0x8020, 0x8022, 0x8028, 0x802A,
0x8080, 0x8082, 0x8088, 0x808A, 0x80A0, 0x80A2, 0x80A8, 0x80AA,
0x8200, 0x8202, 0x8208, 0x820A, 0x8220, 0x8222, 0x8228, 0x822A,
0x8280, 0x8282, 0x8288, 0x828A, 0x82A0, 0x82A2, 0x82A8, 0x82AA,
0x8800, 0x8802, 0x8808, 0x880A, 0x8820, 0x8822, 0x8828, 0x882A,
0x8880, 0x8882, 0x8888, 0x888A, 0x88A0, 0x88A2, 0x88A8, 0x88AA,
0x8A00, 0x8A02, 0x8A08, 0x8A0A, 0x8A20, 0x8A22, 0x8A28, 0x8A2A,
0x8A80, 0x8A82, 0x8A88, 0x8A8A, 0x8AA0, 0x8AA2, 0x8AA8, 0x8AAA,
0xA000, 0xA002, 0xA008, 0xA00A, 0xA020, 0xA022, 0xA028, 0xA02A,
0xA080, 0xA082, 0xA088, 0xA08A, 0xA0A0, 0xA0A2, 0xA0A8, 0xA0AA,
0xA200, 0xA202, 0xA208, 0xA20A, 0xA220, 0xA222, 0xA228, 0xA22A,
0xA280, 0xA282, 0xA288, 0xA28A, 0xA2A0, 0xA2A2, 0xA2A8, 0xA2AA,
0xA800, 0xA802, 0xA808, 0xA80A, 0xA820, 0xA822, 0xA828, 0xA82A,
0xA880, 0xA882, 0xA888, 0xA88A, 0xA8A0, 0xA8A2, 0xA8A8, 0xA8AA,
0xAA00, 0xAA02, 0xAA08, 0xAA0A, 0xAA20, 0xAA22, 0xAA28, 0xAA2A,
0xAA80, 0xAA82, 0xAA88, 0xAA8A, 0xAAA0, 0xAAA2, 0xAAA8, 0xAAAA,
};
//
// Macros
//
// WORD_SIZE is the number of bits per word
// sizeof gives size in bytes (8 bits per byte)
#define WORD_SIZE 64
// #define WORD_SIZE (sizeof(word_t) * 8)
// POPCOUNT is number of bits set
#if defined(_WIN32)
// See http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
static word_t __inline windows_popcount(word_t w)
{
w = w - ((w >> 1) & (word_t)~(word_t)0/3);
w = (w & (word_t)~(word_t)0/15*3) + ((w >> 2) & (word_t)~(word_t)0/15*3);
w = (w + (w >> 4)) & (word_t)~(word_t)0/255*15;
c = (word_t)(w * ((word_t)~(word_t)0/255)) >> (sizeof(word_t) - 1) * 8;
}
static word_t __inline windows_parity(word_t w)
{
w ^= w >> 1;
w ^= w >> 2;
w = (w & 0x1111111111111111UL) * 0x1111111111111111UL;
return (w >> 60) & 1;
}
#define POPCOUNT(x) windows_popcountl(x)
#define PARITY(x) windows_parity(x)
#else
#define POPCOUNT(x) (unsigned)__builtin_popcountll(x)
#define PARITY(x) (unsigned)__builtin_parityll(x)
#endif
#define MIN(a, b) (((a) <= (b)) ? (a) : (b))
#define MAX(a, b) (((a) >= (b)) ? (a) : (b))
// Make this a power of two
#define INIT_CAPACITY_WORDS 2
// word of all 1s
#define WORD_MAX (~(word_t)0)
#define SET_REGION(arr,start,len) _set_region((arr),(start),(len),FILL_REGION)
#define CLEAR_REGION(arr,start,len) _set_region((arr),(start),(len),ZERO_REGION)
#define TOGGLE_REGION(arr,start,len) _set_region((arr),(start),(len),SWAP_REGION)
// Have we initialised with srand() ?
static char rand_initiated = 0;
static void _seed_rand()
{
if(!rand_initiated)
{
// Initialise random number generator
struct timeval time;
gettimeofday(&time, NULL);
srand((((time.tv_sec ^ getpid()) * 1000001) + time.tv_usec));
rand_initiated = 1;
}
}
//
// Common internal functions
//
#define bits_in_top_word(nbits) ((nbits) ? bitset64_idx((nbits) - 1) + 1 : 0)
// Mostly used for debugging
static inline void _print_word(word_t word, FILE* out)
{
word_offset_t i;
for(i = 0; i < WORD_SIZE; i++)
{
fprintf(out, "%c", ((word >> i) & (word_t)0x1) == 0 ? '0' : '1');
}
}
// prints right to left
static inline char* _word_to_str(word_t word, char str[WORD_SIZE+1])
__attribute__((unused));
static inline char* _word_to_str(word_t word, char str[WORD_SIZE+1])
{
word_offset_t i;
for(i = 0; i < WORD_SIZE; i++)
{
str[WORD_SIZE-i-1] = ((word >> i) & (word_t)0x1) == 0 ? '0' : '1';
}
str[WORD_SIZE] = '\0';
return str;
}
// Used in debugging
#ifdef DEBUG
#define DEBUG_PRINT(msg,...) printf("[%s:%i] "msg, __FILE__, __LINE__, ##__VA_ARGS__);
#define DEBUG_VALIDATE(a) validate_bitarr((a), __FILE__, __LINE__)
#else
#define DEBUG_PRINT(msg,...)
#define DEBUG_VALIDATE(a)
#endif
void validate_bitarr(BIT_ARRAY *arr, const char *file, int lineno)
{
// Check top word is masked
word_addr_t tw = arr->num_of_words == 0 ? 0 : arr->num_of_words - 1;
bit_index_t top_bits = bits_in_top_word(arr->num_of_bits);
int err = 0;
if(arr->words[tw] > bitmask64(top_bits))
{
_print_word(arr->words[tw], stderr);
fprintf(stderr, "\n[%s:%i] Expected %i bits in top word[%i]\n",
file, lineno, (int)top_bits, (int)tw);
err = 1;
}
// Check num of words is correct
word_addr_t num_words = roundup_bits2words64(arr->num_of_bits);
if(num_words != arr->num_of_words)
{
fprintf(stderr, "\n[%s:%i] num of words wrong "
"[bits: %i, word: %i, actual words: %i]\n", file, lineno,
(int)arr->num_of_bits, (int)num_words, (int)arr->num_of_words);
err = 1;
}
if(err) abort();
}
// Reverse a word
static inline word_t _reverse_word(word_t word)
{
word_t reverse = (reverse_table[(word) & 0xff] << 56) |
(reverse_table[(word >> 8) & 0xff] << 48) |
(reverse_table[(word >> 16) & 0xff] << 40) |
(reverse_table[(word >> 24) & 0xff] << 32) |
(reverse_table[(word >> 32) & 0xff] << 24) |
(reverse_table[(word >> 40) & 0xff] << 16) |
(reverse_table[(word >> 48) & 0xff] << 8) |
(reverse_table[(word >> 56) & 0xff]);
return reverse;
}
static inline void _mask_top_word(BIT_ARRAY* bitarr)
{
// Mask top word
word_addr_t num_of_words = MAX(1, bitarr->num_of_words);
word_offset_t bits_active = bits_in_top_word(bitarr->num_of_bits);
bitarr->words[num_of_words-1] &= bitmask64(bits_active);
}
//
// Get and set words (internal use only -- no bounds checking)
//
static inline word_t _get_word(const BIT_ARRAY* bitarr, bit_index_t start)
{
word_addr_t word_index = bitset64_wrd(start);
word_offset_t word_offset = bitset64_idx(start);
word_t result = bitarr->words[word_index] >> word_offset;
word_offset_t bits_taken = WORD_SIZE - word_offset;
// word_offset is now the number of bits we need from the next word
// Check the next word has at least some bits
if(word_offset > 0 && start + bits_taken < bitarr->num_of_bits)
{
result |= bitarr->words[word_index+1] << (WORD_SIZE - word_offset);
}
return result;
}
// Set 64 bits from a particular start position
// Doesn't extend bit array
static inline void _set_word(BIT_ARRAY* bitarr, bit_index_t start, word_t word)
{
word_addr_t word_index = bitset64_wrd(start);
word_offset_t word_offset = bitset64_idx(start);
if(word_offset == 0)
{
bitarr->words[word_index] = word;
}
else
{
bitarr->words[word_index]
= (word << word_offset) |
(bitarr->words[word_index] & bitmask64(word_offset));
if(word_index+1 < bitarr->num_of_words)
{
bitarr->words[word_index+1]
= (word >> (WORD_SIZE - word_offset)) |
(bitarr->words[word_index+1] & (WORD_MAX << word_offset));
}
}
// Mask top word
_mask_top_word(bitarr);
DEBUG_VALIDATE(bitarr);
}
static inline void _set_byte(BIT_ARRAY *bitarr, bit_index_t start, uint8_t byte)
{
word_t w = _get_word(bitarr, start);
_set_word(bitarr, start, (w & ~(word_t)0xff) | byte);
}
// 4 bits
static inline void _set_nibble(BIT_ARRAY *bitarr, bit_index_t start,
uint8_t nibble)
{
word_t w = _get_word(bitarr, start);
_set_word(bitarr, start, (w & ~(word_t)0xf) | nibble);
}
// Wrap around
static inline word_t _get_word_cyclic(const BIT_ARRAY* bitarr, bit_index_t start)
{
word_t word = _get_word(bitarr, start);
bit_index_t bits_taken = bitarr->num_of_bits - start;
if(bits_taken < WORD_SIZE)
{
word |= (bitarr->words[0] << bits_taken);
if(bitarr->num_of_bits < (bit_index_t)WORD_SIZE)
{
// Mask word to prevent repetition of the same bits
word = word & bitmask64(bitarr->num_of_bits);
}
}
return word;
}
// Wrap around
static inline void _set_word_cyclic(BIT_ARRAY* bitarr,
bit_index_t start, word_t word)
{
_set_word(bitarr, start, word);
bit_index_t bits_set = bitarr->num_of_bits - start;
if(bits_set < WORD_SIZE && start > 0)
{
word >>= bits_set;
// Prevent overwriting the bits we've just set
// by setting 'start' as the upper bound for the number of bits to write
word_offset_t bits_remaining = MIN(WORD_SIZE - bits_set, start);
word_t mask = bitmask64(bits_remaining);
bitarr->words[0] = bitmask_merge(word, bitarr->words[0], mask);
}
}
//
// Fill a region (internal use only)
//
// FillAction is fill with 0 or 1 or toggle
typedef enum {ZERO_REGION, FILL_REGION, SWAP_REGION} FillAction;
static inline void _set_region(BIT_ARRAY* bitarr, bit_index_t start,
bit_index_t length, FillAction action)
{
if(length == 0) return;
word_addr_t first_word = bitset64_wrd(start);
word_addr_t last_word = bitset64_wrd(start+length-1);
word_offset_t foffset = bitset64_idx(start);
word_offset_t loffset = bitset64_idx(start+length-1);
if(first_word == last_word)
{
word_t mask = bitmask64(length) << foffset;
switch(action)
{
case ZERO_REGION: bitarr->words[first_word] &= ~mask; break;
case FILL_REGION: bitarr->words[first_word] |= mask; break;
case SWAP_REGION: bitarr->words[first_word] ^= mask; break;
}
}
else
{
// Set first word
switch(action)
{
case ZERO_REGION: bitarr->words[first_word] &= bitmask64(foffset); break;
case FILL_REGION: bitarr->words[first_word] |= ~bitmask64(foffset); break;
case SWAP_REGION: bitarr->words[first_word] ^= ~bitmask64(foffset); break;
}
word_addr_t i;
// Set whole words
switch(action)
{
case ZERO_REGION:
for(i = first_word + 1; i < last_word; i++)
bitarr->words[i] = (word_t)0;
break;
case FILL_REGION:
for(i = first_word + 1; i < last_word; i++)
bitarr->words[i] = WORD_MAX;
break;
case SWAP_REGION:
for(i = first_word + 1; i < last_word; i++)
bitarr->words[i] ^= WORD_MAX;
break;
}
// Set last word
switch(action)
{
case ZERO_REGION: bitarr->words[last_word] &= ~bitmask64(loffset+1); break;
case FILL_REGION: bitarr->words[last_word] |= bitmask64(loffset+1); break;
case SWAP_REGION: bitarr->words[last_word] ^= bitmask64(loffset+1); break;
}
}
}
//
// Constructor
//
// If cannot allocate memory, set errno to ENOMEM, return NULL
BIT_ARRAY* bit_array_alloc(BIT_ARRAY* bitarr, bit_index_t nbits)
{
bitarr->num_of_bits = nbits;
bitarr->num_of_words = roundup_bits2words64(nbits);
bitarr->capacity_in_words = MAX(8, roundup2pow(bitarr->num_of_words));
bitarr->words = (word_t*)calloc(bitarr->capacity_in_words, sizeof(word_t));
if(bitarr->words == NULL) {
errno = ENOMEM;
return NULL;
}
return bitarr;
}
void bit_array_dealloc(BIT_ARRAY* bitarr)
{
free(bitarr->words);
memset(bitarr, 0, sizeof(BIT_ARRAY));
}
// If cannot allocate memory, set errno to ENOMEM, return NULL
BIT_ARRAY* bit_array_create(bit_index_t nbits)
{
BIT_ARRAY* bitarr = (BIT_ARRAY*)malloc(sizeof(BIT_ARRAY));
// error if could not allocate enough memory
if(bitarr == NULL || bit_array_alloc(bitarr, nbits) == NULL)
{
if(bitarr != NULL) free(bitarr);
errno = ENOMEM;
return NULL;
}
DEBUG_PRINT("Creating BIT_ARRAY (bits: %lu; allocated words: %lu; "
"using words: %lu; WORD_SIZE: %i)\n",
(unsigned long)nbits, (unsigned long)bitarr->capacity_in_words,
(unsigned long)roundup_bits2words64(nbits), (int)WORD_SIZE);
DEBUG_VALIDATE(bitarr);
return bitarr;
}
//
// Destructor
//
void bit_array_free(BIT_ARRAY* bitarr)
{
if(bitarr->words != NULL)
free(bitarr->words);
free(bitarr);
}
bit_index_t bit_array_length(const BIT_ARRAY* bit_arr)
{
return bit_arr->num_of_bits;
}
// Change the size of a bit array. Enlarging an array will add zeros
// to the end of it. Returns 1 on success, 0 on failure (e.g. not enough memory)
char bit_array_resize(BIT_ARRAY* bitarr, bit_index_t new_num_of_bits)
{
word_addr_t old_num_of_words = bitarr->num_of_words;
word_addr_t new_num_of_words = roundup_bits2words64(new_num_of_bits);
bitarr->num_of_bits = new_num_of_bits;
bitarr->num_of_words = new_num_of_words;
DEBUG_PRINT("Resize: old_num_of_words: %i; new_num_of_words: %i capacity: %i\n",
(int)old_num_of_words, (int)new_num_of_words,
(int)bitarr->capacity_in_words);
if(new_num_of_words > bitarr->capacity_in_words)
{
// Need to change the amount of memory used
word_addr_t old_capacity_in_words = bitarr->capacity_in_words;
size_t old_capacity_in_bytes = old_capacity_in_words * sizeof(word_t);
bitarr->capacity_in_words = roundup2pow(new_num_of_words);
bitarr->capacity_in_words = MAX(8, bitarr->capacity_in_words);
size_t new_capacity_in_bytes = bitarr->capacity_in_words * sizeof(word_t);
bitarr->words = (word_t*)realloc(bitarr->words, new_capacity_in_bytes);
if(bitarr->words == NULL)
{
// error - could not allocate enough memory
perror("resize realloc");
errno = ENOMEM;
return 0;
}
// Need to zero new memory
size_t num_bytes_to_zero = new_capacity_in_bytes - old_capacity_in_bytes;
memset(bitarr->words + old_capacity_in_words, 0, num_bytes_to_zero);
DEBUG_PRINT("zeroing from word %i for %i bytes\n", (int)old_capacity_in_words,
(int)num_bytes_to_zero);
}
else if(new_num_of_words < old_num_of_words)
{
// Shrunk -- need to zero old memory
size_t num_bytes_to_zero = (old_num_of_words - new_num_of_words)*sizeof(word_t);
memset(bitarr->words + new_num_of_words, 0, num_bytes_to_zero);
}
// Mask top word
_mask_top_word(bitarr);
DEBUG_VALIDATE(bitarr);
return 1;
}
void bit_array_resize_critical(BIT_ARRAY* bitarr, bit_index_t num_of_bits)
{
bit_index_t old_num_of_bits = bitarr->num_of_bits;
if(!bit_array_resize(bitarr, num_of_bits))
{
fprintf(stderr, "Ran out of memory resizing [%lu -> %lu]",
(unsigned long)old_num_of_bits, (unsigned long)num_of_bits);
abort();
}
}
// If bitarr length < num_bits, resizes to num_bits
char bit_array_ensure_size(BIT_ARRAY* bitarr, bit_index_t ensure_num_of_bits)
{
if(bitarr->num_of_bits < ensure_num_of_bits)
{
return bit_array_resize(bitarr, ensure_num_of_bits);
}
return 1;
}
void bit_array_ensure_size_critical(BIT_ARRAY* bitarr, bit_index_t num_of_bits)
{
if(num_of_bits > bitarr->num_of_bits)
{
bit_array_resize_critical(bitarr, num_of_bits);
}
}
static inline
void _bit_array_ensure_nwords(BIT_ARRAY* bitarr, word_addr_t nwords,
const char *file, int lineno, const char *func)
{
size_t newmem, oldmem;
if(bitarr->capacity_in_words < nwords) {
oldmem = bitarr->capacity_in_words * sizeof(word_t);
bitarr->capacity_in_words = roundup2pow(nwords);
newmem = bitarr->capacity_in_words * sizeof(word_t);
bitarr->words = (word_t*)realloc(bitarr->words, newmem);
if(bitarr->words == NULL) {
fprintf(stderr, "[%s:%i:%s()] Ran out of memory resizing [%zu -> %zu]",
file, lineno, func, oldmem, newmem);
abort();
}
DEBUG_PRINT("Ensure nwords realloc %zu -> %zu\n", oldmem, newmem);
}
}
//
// Get, set, clear, assign and toggle individual bits
//
// Get the value of a bit (returns 0 or 1)
char bit_array_get_bit(const BIT_ARRAY* bitarr, bit_index_t b)
{
assert(b < bitarr->num_of_bits);
return bit_array_get(bitarr, b);
}
// set a bit (to 1) at position b
void bit_array_set_bit(BIT_ARRAY* bitarr, bit_index_t b)
{
assert(b < bitarr->num_of_bits);
bit_array_set(bitarr,b);
DEBUG_VALIDATE(bitarr);
}
// clear a bit (to 0) at position b
void bit_array_clear_bit(BIT_ARRAY* bitarr, bit_index_t b)
{
assert(b < bitarr->num_of_bits);
bit_array_clear(bitarr, b);
DEBUG_VALIDATE(bitarr);
}
// If bit is 0 -> 1, if bit is 1 -> 0. AKA 'flip'
void bit_array_toggle_bit(BIT_ARRAY* bitarr, bit_index_t b)
{
assert(b < bitarr->num_of_bits);
bit_array_toggle(bitarr, b);
DEBUG_VALIDATE(bitarr);
}
// If char c != 0, set bit; otherwise clear bit
void bit_array_assign_bit(BIT_ARRAY* bitarr, bit_index_t b, char c)
{
assert(b < bitarr->num_of_bits);
bit_array_assign(bitarr, b, c ? 1 : 0);
DEBUG_VALIDATE(bitarr);
}
//
// Get, set etc with resize
//
// Get the value of a bit (returns 0 or 1)
char bit_array_rget(BIT_ARRAY* bitarr, bit_index_t b)
{
bit_array_ensure_size_critical(bitarr, b+1);
return bit_array_get(bitarr, b);
}
// set a bit (to 1) at position b
void bit_array_rset(BIT_ARRAY* bitarr, bit_index_t b)
{
bit_array_ensure_size_critical(bitarr, b+1);
bit_array_set(bitarr,b);
DEBUG_VALIDATE(bitarr);
}
// clear a bit (to 0) at position b
void bit_array_rclear(BIT_ARRAY* bitarr, bit_index_t b)
{
bit_array_ensure_size_critical(bitarr, b+1);
bit_array_clear(bitarr, b);
DEBUG_VALIDATE(bitarr);
}
// If bit is 0 -> 1, if bit is 1 -> 0. AKA 'flip'
void bit_array_rtoggle(BIT_ARRAY* bitarr, bit_index_t b)
{
bit_array_ensure_size_critical(bitarr, b+1);
bit_array_toggle(bitarr, b);
DEBUG_VALIDATE(bitarr);
}
// If char c != 0, set bit; otherwise clear bit
void bit_array_rassign(BIT_ARRAY* bitarr, bit_index_t b, char c)
{
bit_array_ensure_size_critical(bitarr, b+1);
bit_array_assign(bitarr, b, c ? 1 : 0);
DEBUG_VALIDATE(bitarr);
}
//
// Get, set, clear and toggle several bits at once
//
// Get the offsets of the set bits (for offsets start<=offset<end)
// Returns the number of bits set
// It is assumed that dst is at least of length (end-start)
bit_index_t bit_array_get_bits(const BIT_ARRAY* bitarr,
bit_index_t start, bit_index_t end,
bit_index_t* dst)
{
bit_index_t i, n = 0;
assert(end <= bitarr->num_of_bits);
for(i = start; i < end; i++) {
if(bit_array_get(bitarr, i)) {
dst[n++] = i;
}
}
return n;
}
// Set multiple bits at once.
// e.g. set bits 1, 20 & 31: bit_array_set_bits(bitarr, 3, 1,20,31);
void bit_array_set_bits(BIT_ARRAY* bitarr, size_t n, ...)
{
size_t i;
va_list argptr;
va_start(argptr, n);
for(i = 0; i < n; i++)
{
unsigned int bit_index = va_arg(argptr, unsigned int);
bit_array_set_bit(bitarr, bit_index);
}
va_end(argptr);
DEBUG_VALIDATE(bitarr);
}
// Clear multiple bits at once.
// e.g. clear bits 1, 20 & 31: bit_array_clear_bits(bitarr, 3, 1,20,31);
void bit_array_clear_bits(BIT_ARRAY* bitarr, size_t n, ...)
{
size_t i;
va_list argptr;
va_start(argptr, n);
for(i = 0; i < n; i++)
{
unsigned int bit_index = va_arg(argptr, unsigned int);
bit_array_clear_bit(bitarr, bit_index);
}
va_end(argptr);
DEBUG_VALIDATE(bitarr);
}
// Toggle multiple bits at once
// e.g. toggle bits 1, 20 & 31: bit_array_toggle_bits(bitarr, 3, 1,20,31);
void bit_array_toggle_bits(BIT_ARRAY* bitarr, size_t n, ...)
{
size_t i;
va_list argptr;
va_start(argptr, n);
for(i = 0; i < n; i++)
{
unsigned int bit_index = va_arg(argptr, unsigned int);
bit_array_toggle_bit(bitarr, bit_index);
}
va_end(argptr);
DEBUG_VALIDATE(bitarr);
}
//
// Set, clear and toggle all bits in a region
//
// Set all the bits in a region
void bit_array_set_region(BIT_ARRAY* bitarr, bit_index_t start, bit_index_t len)
{
assert(start + len <= bitarr->num_of_bits);
SET_REGION(bitarr, start, len);
DEBUG_VALIDATE(bitarr);
}
// Clear all the bits in a region
void bit_array_clear_region(BIT_ARRAY* bitarr, bit_index_t start, bit_index_t len)
{
assert(start + len <= bitarr->num_of_bits);
CLEAR_REGION(bitarr, start, len);
DEBUG_VALIDATE(bitarr);
}
// Toggle all the bits in a region
void bit_array_toggle_region(BIT_ARRAY* bitarr, bit_index_t start, bit_index_t len)
{
assert(start + len <= bitarr->num_of_bits);
TOGGLE_REGION(bitarr, start, len);
DEBUG_VALIDATE(bitarr);
}
//
// Set, clear and toggle all bits at once
//
// set all elements of data to one
void bit_array_set_all(BIT_ARRAY* bitarr)
{
bit_index_t num_of_bytes = bitarr->num_of_words * sizeof(word_t);
memset(bitarr->words, 0xFF, num_of_bytes);
_mask_top_word(bitarr);
DEBUG_VALIDATE(bitarr);
}
// set all elements of data to zero
void bit_array_clear_all(BIT_ARRAY* bitarr)
{
memset(bitarr->words, 0, bitarr->num_of_words * sizeof(word_t));
DEBUG_VALIDATE(bitarr);
}
// Set all 1 bits to 0, and all 0 bits to 1. AKA flip
void bit_array_toggle_all(BIT_ARRAY* bitarr)
{
word_addr_t i;
for(i = 0; i < bitarr->num_of_words; i++)
{
bitarr->words[i] ^= WORD_MAX;
}
_mask_top_word(bitarr);
DEBUG_VALIDATE(bitarr);
}
//
// Get a word at a time
//
uint64_t bit_array_get_word64(const BIT_ARRAY* bitarr, bit_index_t start)
{
assert(start < bitarr->num_of_bits);
return (uint64_t)_get_word(bitarr, start);
}
uint32_t bit_array_get_word32(const BIT_ARRAY* bitarr, bit_index_t start)
{
assert(start < bitarr->num_of_bits);
return (uint32_t)_get_word(bitarr, start);
}
uint16_t bit_array_get_word16(const BIT_ARRAY* bitarr, bit_index_t start)
{
assert(start < bitarr->num_of_bits);
return (uint16_t)_get_word(bitarr, start);
}
uint8_t bit_array_get_word8(const BIT_ARRAY* bitarr, bit_index_t start)
{
assert(start < bitarr->num_of_bits);
return (uint8_t)_get_word(bitarr, start);
}
uint64_t bit_array_get_wordn(const BIT_ARRAY* bitarr, bit_index_t start, int n)
{
assert(start < bitarr->num_of_bits);
assert(n <= 64);
return (uint64_t)(_get_word(bitarr, start) & bitmask64(n));
}
//
// Set a word at a time
//
// Doesn't extend bit array. However it is safe to TRY to set bits beyond the
// end of the array, as long as: `start` is < `bit_array_length(arr)`
//
void bit_array_set_word64(BIT_ARRAY* bitarr, bit_index_t start, uint64_t word)
{
assert(start < bitarr->num_of_bits);
_set_word(bitarr, start, (word_t)word);
}
void bit_array_set_word32(BIT_ARRAY* bitarr, bit_index_t start, uint32_t word)
{
assert(start < bitarr->num_of_bits);
word_t w = _get_word(bitarr, start);
_set_word(bitarr, start, bitmask_merge(w, word, 0xffffffff00000000UL));
}
void bit_array_set_word16(BIT_ARRAY* bitarr, bit_index_t start, uint16_t word)
{
assert(start < bitarr->num_of_bits);
word_t w = _get_word(bitarr, start);
_set_word(bitarr, start, bitmask_merge(w, word, 0xffffffffffff0000UL));
}
void bit_array_set_word8(BIT_ARRAY* bitarr, bit_index_t start, uint8_t byte)
{
assert(start < bitarr->num_of_bits);
_set_byte(bitarr, start, byte);
}
void bit_array_set_wordn(BIT_ARRAY* bitarr, bit_index_t start, uint64_t word, int n)
{
assert(start < bitarr->num_of_bits);
assert(n <= 64);
word_t w = _get_word(bitarr, start), m = bitmask64(n);
_set_word(bitarr, start, bitmask_merge(word,w,m));
}
//
// Number of bits set
//
// Get the number of bits set (hamming weight)
bit_index_t bit_array_num_bits_set(const BIT_ARRAY* bitarr)
{
word_addr_t i;
bit_index_t num_of_bits_set = 0;
for(i = 0; i < bitarr->num_of_words; i++)
{
if(bitarr->words[i] > 0)
{
num_of_bits_set += POPCOUNT(bitarr->words[i]);
}
}
return num_of_bits_set;
}
// Get the number of bits not set (1 - hamming weight)
bit_index_t bit_array_num_bits_cleared(const BIT_ARRAY* bitarr)
{
return bitarr->num_of_bits - bit_array_num_bits_set(bitarr);
}
// Get the number of bits set in on array and not the other. This is equivalent
// to hamming weight of the XOR when the two arrays are the same length.
// e.g. 10101 vs 00111 => hamming distance 2 (XOR is 10010)
bit_index_t bit_array_hamming_distance(const BIT_ARRAY* arr1,
const BIT_ARRAY* arr2)
{
word_addr_t min_words = MIN(arr1->num_of_words, arr2->num_of_words);
word_addr_t max_words = MAX(arr1->num_of_words, arr2->num_of_words);
bit_index_t hamming_distance = 0;
word_addr_t i;
for(i = 0; i < min_words; i++)
{
hamming_distance += POPCOUNT(arr1->words[i] ^ arr2->words[i]);