-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitops.h
1108 lines (956 loc) · 26 KB
/
bitops.h
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
/* SPDX-License-Identifier: MIT */
/* Minimal Linux-like bit manipulation helper functions
*
* SPDX-FileCopyrightText: Sven Eckelmann <sven@narfation.org>
*/
#ifndef __LINUX_LIKE_BITOPS_H__
#define __LINUX_LIKE_BITOPS_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#if defined(__GNUC__)
#define BITOPS_BUILTIN_USE 1
#endif
#if defined(_MSC_VER)
#define __inline__ __inline
#endif
/**
* BITOPS_DIV_CEIL - calculate quotient of integer division (round up)
* @numerator: side effect free expression for numerator of division
* @denominator: side effect free expression for denominator of division
*
* numerator and denominator must be from a type which can store
* denominator + numerator without overflow. denominator must be larger than 0
* and numerator must be positive.
*
* WARNING @numerator expression must be side-effect free
*/
#define BITOPS_DIV_CEIL(numerator, denominator) \
(((numerator) + (denominator) - 1) / (denominator))
/**
* BITS_PER_BYTE - number of bits per byte/char
*/
#define BITS_PER_BYTE 8
/**
* BITS_PER_LONG - number of bits per long
*/
#define BITS_PER_LONG (sizeof(unsigned long) * BITS_PER_BYTE)
/**
* BIT - return unsigned long with a bit set
* @x: Bit which should be set
*/
#define BIT(x) (1UL << (x))
/**
* BITS_TO_LONGS - return number of longs to save at least bit 0..(bits - 1)
* @bits: number of required bits
*/
#define BITS_TO_LONGS(bits) \
BITOPS_DIV_CEIL(bits, BITS_PER_LONG)
/**
* DECLARE_BITMAP - declare bitmap to store at least bit 0..(bits -1)
* @bitmap: name for the new bitmap
* @bits: number of required bits
*/
#define DECLARE_BITMAP(bitmap, bits) \
unsigned long bitmap[BITS_TO_LONGS(bits)]
/**
* GENMASK - return unsigned long with a bits set in the range [@h, @l]
* @h: most significant bit which should be set
* @l: least significant bit it which should be set
*
* WARNING this macro cannot be used to set all bits to 1 via
* GENMASK(BITS_PER_LONG - 1, 0). Following must always be true:
* (@h - @l) < (BITS_PER_LONG - 1). Also @h must always be larger or equal to
* @l and never larger than (BITS_PER_LONG - 1). @l must always be larger than
* or equal to 0.
*
* WARNING @l expression must be side-effect free
*/
#define GENMASK(h, l) (((1UL << ((h) - (l) + 1)) - 1) << (l))
/**
* BITMAP_FIRST_WORD_MASK - return unsigned long mask for least significant long
* @start: offset to first bits
*
* All bits which can be modified in the least significant unsigned long for
* offset @start in the bitmap will be set to 1. All other bits will be set to
* zero
*/
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
/**
* BITMAP_LAST_WORD_MASK - return unsigned long mask for most significant long
* @bits: number of bits in complete bitmap
*
* All bits which can be modified in the most significant unsigned long in the
* bitmap will be set to 1. All other bits will be set to zero
*/
#define BITMAP_LAST_WORD_MASK(bits) (~0UL >> (-(bits) % BITS_PER_LONG))
/**
* bitops_ffs() - find (least significant) first set bit plus one
* @x: unsigned long to check
*
* Return: plus-one index of first set bit; zero when x is zero
*/
static __inline__ size_t bitops_ffs(unsigned long x)
{
#ifdef BITOPS_BUILTIN_USE
return __builtin_ffsl(x);
#else
size_t i = 1;
size_t shift = 0;
unsigned long t;
if (x == 0)
return 0;
t = ~0UL;
shift = BITS_PER_LONG;
shift /= 2;
t >>= shift;
while (shift) {
if ((t & x) == 0) {
i += shift;
x >>= shift;
}
shift /= 2;
t >>= shift;
}
return i;
#endif
}
/**
* bitops_ffz() - find (least significant) first zero bit plus one
* @x: unsigned long to check
*
* Return: plus-one index of first zero bit; zero when x is ULONG_MAX
*/
#define bitops_ffz(x) bitops_ffs(~(x))
/**
* hweight8() - number of set bits in an uint8_t
* @x: uint8_t to sum up
*
* Return: number of set bits
*/
static __inline__ unsigned int hweight8(uint8_t x)
{
static const uint8_t m1 = UINT8_C(0x55);
static const uint8_t m2 = UINT8_C(0x33);
/* x = (x & m1) + ((x >> 1) & m1); */
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x += x >> 4;
return x & 0x0f;
}
/**
* hweight16() - number of set bits in an uint16_t
* @x: uint16_t to sum up
*
* Return: number of set bits
*/
static __inline__ unsigned int hweight16(uint16_t x)
{
static const uint16_t m1 = UINT16_C(0x5555);
static const uint16_t m2 = UINT16_C(0x3333);
static const uint16_t m4 = UINT16_C(0x0f0f);
/* x = (x & m1) + ((x >> 1) & m1); */
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
x += x >> 8;
return x & 0x1f;
}
/**
* hweight32() - number of set bits in an uint32_t
* @x: uint32_t to sum up
*
* Return: number of set bits
*/
static __inline__ unsigned int hweight32(uint32_t x)
{
static const uint32_t m1 = UINT32_C(0x55555555);
static const uint32_t m2 = UINT32_C(0x33333333);
static const uint32_t m4 = UINT32_C(0x0f0f0f0f);
/* x = (x & m1) + ((x >> 1) & m1); */
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
x += x >> 8;
x += x >> 16;
return x & 0x3f;
}
/**
* hweight64() - number of set bits in an uint64_t
* @x: uint64_t to sum up
*
* Return: number of set bits
*/
static __inline__ unsigned int hweight64(uint64_t x)
{
if (BITS_PER_LONG >= 64) {
static const uint64_t m1 = UINT64_C(0x5555555555555555);
static const uint64_t m2 = UINT64_C(0x3333333333333333);
static const uint64_t m4 = UINT64_C(0x0f0f0f0f0f0f0f0f);
/* x = (x & m1) + ((x >> 1) & m1); */
x -= (x >> 1) & m1;
x = (x & m2) + ((x >> 2) & m2);
x = (x + (x >> 4)) & m4;
x += x >> 8;
x += x >> 16;
x += x >> 32;
return x & 0x7f;
} else {
return hweight32((uint32_t)x) + hweight32((uint32_t)(x >> 32));
}
}
/**
* hweight_long() - number of set bits in an unsigned long
* @x: unsigned long to sum up
*
* Return: number of set bits
*/
static __inline__ unsigned int hweight_long(unsigned long x)
{
#ifdef BITOPS_BUILTIN_USE
return __builtin_popcountl(x);
#else
size_t i;
if (BITS_PER_LONG == 64)
return hweight64((uint64_t)x);
if (BITS_PER_LONG == 32)
return hweight32((uint32_t)x);
for (i = 0; x; i++)
x &= x - 1;
return i;
#endif
}
/**
* bitops_rol8() - Rotate uint8_t to the left
* @x: uint8_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint8_t
*/
static __inline__ uint8_t bitops_rol8(uint8_t x, size_t n)
{
return (x << n) | (x >> ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_rol16() - Rotate uint16_t to the left
* @x: uint16_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint16_t
*/
static __inline__ uint16_t bitops_rol16(uint16_t x, size_t n)
{
return (x << n) | (x >> ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_rol32() - Rotate uint32_t to the left
* @x: uint32_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint32_t
*/
static __inline__ uint32_t bitops_rol32(uint32_t x, size_t n)
{
return (x << n) | (x >> ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_rol64() - Rotate uint64_t to the left
* @x: uint64_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint64_t
*/
static __inline__ uint64_t bitops_rol64(uint64_t x, size_t n)
{
return (x << n) | (x >> ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_rol_long() - Rotate unsigned long to the left
* @x: unsigned long to rotate
* @n: number of bits to rotate
*
* Return: rotated unsigned long
*/
static __inline__ unsigned long bitops_rol_long(unsigned long x, size_t n)
{
return (x << n) | (x >> ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_ror8() - Rotate uint8_t to the right
* @x: uint8_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint8_t
*/
static __inline__ uint8_t bitops_ror8(uint8_t x, size_t n)
{
return (x >> n) | (x << ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_ror16() - Rotate uint16_t to the right
* @x: uint16_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint16_t
*/
static __inline__ uint16_t bitops_ror16(uint16_t x, size_t n)
{
return (x >> n) | (x << ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_ror32() - Rotate uint32_t to the right
* @x: uint32_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint32_t
*/
static __inline__ uint32_t bitops_ror32(uint32_t x, size_t n)
{
return (x >> n) | (x << ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_ror64() - Rotate uint64_t to the right
* @x: uint64_t to rotate
* @n: number of bits to rotate
*
* Return: rotated uint64_t
*/
static __inline__ uint64_t bitops_ror64(uint64_t x, size_t n)
{
return (x >> n) | (x << ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitops_ror_long() - Rotate unsigned long to the right
* @x: unsigned long to rotate
* @n: number of bits to rotate
*
* Return: rotated unsigned long
*/
static __inline__ unsigned long bitops_ror_long(unsigned long x, size_t n)
{
return (x >> n) | (x << ((BITS_PER_BYTE * sizeof(x)) - n));
}
/**
* bitmap_zero() - Initializes bitmap with zero
* @bitmap: bitmap to modify
* @bits: number of bits
*
* Initializes all bits to zero. This also includes the overhead bits in the
* last unsigned long which will not be used.
*/
static __inline__ void bitmap_zero(unsigned long *bitmap, size_t bits)
{
memset(bitmap, 0, BITS_TO_LONGS(bits) * sizeof(unsigned long));
}
/**
* bitmap_fill() - Initializes bitmap with one
* @bitmap: bitmap to modify
* @bits: number of bits
*
* Initializes all modifiable bits to one. The overhead bits in the last
* unsigned long will be set to zero.
*/
static __inline__ void bitmap_fill(unsigned long *bitmap, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
if (l > 1)
memset(bitmap, 0xff, (l - 1) * sizeof(unsigned long));
bitmap[l - 1] = BITMAP_LAST_WORD_MASK(bits);
}
/**
* set_bit() - Set bit in bitmap to one
* @bit: address of bit to modify
* @bitmap: bitmap to modify
*/
static __inline__ void set_bit(size_t bit, unsigned long *bitmap)
{
size_t l = bit / BITS_PER_LONG;
size_t b = bit % BITS_PER_LONG;
bitmap[l] |= 1UL << b;
}
/**
* clear_bit() - Set bit in bitmap to zero
* @bit: address of bit to modify
* @bitmap: bitmap to modify
*/
static __inline__ void clear_bit(size_t bit, unsigned long *bitmap)
{
size_t l = bit / BITS_PER_LONG;
size_t b = bit % BITS_PER_LONG;
bitmap[l] &= ~(1UL << b);
}
/**
* change_bit() - Toggle bit in bitmap
* @bit: address of bit to modify
* @bitmap: bitmap to modify
*/
static __inline__ void change_bit(size_t bit, unsigned long *bitmap)
{
size_t l = bit / BITS_PER_LONG;
size_t b = bit % BITS_PER_LONG;
bitmap[l] ^= 1UL << b;
}
/**
* test_bit() - Get state of bit
* @bit: address of bit to test
* @bitmap: bitmap to test
*
* Return: true when bit is one and false when bit is zero
*/
static __inline__ bool test_bit(size_t bit, const unsigned long *bitmap)
{
size_t l = bit / BITS_PER_LONG;
size_t b = bit % BITS_PER_LONG;
return !!(bitmap[l] & (1UL << b));
}
/**
* test_and_set_bit() - Set bit in bitmap to one and return old state
* @bit: address of bit to modify
* @bitmap: bitmap to modify
*
* Return: true when bit was one and false when bit was zero
*/
static __inline__ bool test_and_set_bit(size_t bit, unsigned long *bitmap)
{
size_t l = bit / BITS_PER_LONG;
size_t b = bit % BITS_PER_LONG;
bool old;
old = !!(bitmap[l] & (1UL << b));
bitmap[l] |= 1UL << b;
return old;
}
/**
* test_and_clear_bit() - Set bit in bitmap to zero and return old state
* @bit: address of bit to modify
* @bitmap: bitmap to modify
*
* Return: true when bit was one and false when bit was zero
*/
static __inline__ bool test_and_clear_bit(size_t bit, unsigned long *bitmap)
{
size_t l = bit / BITS_PER_LONG;
size_t b = bit % BITS_PER_LONG;
bool old;
old = !!(bitmap[l] & (1UL << b));
bitmap[l] &= ~(1UL << b);
return old;
}
/**
* test_and_change_bit() - Toggle bit in bitmap and return old state
* @bit: address of bit to modify
* @bitmap: bitmap to modify
*
* Return: true when bit was one and false when bit was zero
*/
static __inline__ bool test_and_change_bit(size_t bit, unsigned long *bitmap)
{
size_t l = bit / BITS_PER_LONG;
size_t b = bit % BITS_PER_LONG;
bool old;
old = !!(bitmap[l] & (1UL << b));
bitmap[l] ^= 1UL << b;
return old;
}
/**
* bitmap_set() - Set bit range in bitmap
* @bitmap: bitmap to modify
* @start: start of bits to modify
* @bits: number of bits to modify
*
* Sets @bits number of bits in @bitmap starting at @start.
*/
static __inline__ void bitmap_set(unsigned long *bitmap, size_t start,
size_t bits)
{
size_t i;
size_t end = start + bits;
size_t l = end / BITS_PER_LONG;
unsigned long mask = BITMAP_FIRST_WORD_MASK(start);
size_t mask_bits = BITS_PER_LONG - (start % BITS_PER_LONG);
for (i = start / BITS_PER_LONG; i < l; i++) {
bitmap[i] |= mask;
bits -= mask_bits;
mask = ~0UL;
mask_bits = BITS_PER_LONG;
}
if (bits)
bitmap[l] |= mask & BITMAP_LAST_WORD_MASK(end);
}
/**
* bitmap_clear() - Clear bit range in bitmap
* @bitmap: bitmap to modify
* @start: start of bits to modify
* @bits: number of bits to modify
*
* Clears @bits number of bits in @bitmap starting at @start.
*/
static __inline__ void bitmap_clear(unsigned long *bitmap, size_t start,
size_t bits)
{
size_t i;
size_t end = start + bits;
size_t l = end / BITS_PER_LONG;
unsigned long mask = BITMAP_FIRST_WORD_MASK(start);
size_t mask_bits = BITS_PER_LONG - (start % BITS_PER_LONG);
for (i = start / BITS_PER_LONG; i < l; i++) {
bitmap[i] &= ~mask;
bits -= mask_bits;
mask = ~0UL;
mask_bits = BITS_PER_LONG;
}
if (bits)
bitmap[l] &= ~(mask & BITMAP_LAST_WORD_MASK(end));
}
/**
* find_next_bit() - Find next set bit in bitmap
* @bitmap: bitmap to check
* @bits: number of bits in @bitmap
* @start: start of bits to check
*
* Checks the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not be checked
*
* Return: bit position of next set bit, @bits when no set bit was found
*/
static __inline__ size_t find_next_bit(const unsigned long *bitmap, size_t bits,
size_t start)
{
size_t i;
size_t pos;
unsigned long t;
size_t l = BITS_TO_LONGS(bits);
size_t first_long = start / BITS_PER_LONG;
size_t long_lower = start - (start % BITS_PER_LONG);
if (start >= bits)
return bits;
t = bitmap[first_long] & BITMAP_FIRST_WORD_MASK(start);
for (i = first_long + 1; !t && i < l; i++) {
/* search until valid t is found */
long_lower += BITS_PER_LONG;
t = bitmap[i];
}
if (!t)
return bits;
pos = long_lower + bitops_ffs(t) - 1;
if (pos >= bits)
return bits;
return pos;
}
/**
* find_first_bit - Find first set bit in bitmap
* @bitmap: bitmap to check
* @bits: number of bits in @bitmap
*
* Checks the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not be checked
*
* Return: bit position of fist set bit, @bits when no set bit was found
*/
#define find_first_bit(bitmap, bits) find_next_bit(bitmap, bits, 0)
/**
* for_each_set_bit - iterate over set bits in bitmap
* @bit: current bit
* @bitmap: bitmap to iterate over
* @bits: number of bits in @bitmap
*
* WARNING expressions @bitmap and @bits must be side-effect free
*/
#define for_each_set_bit(bit, bitmap, bits) \
for (bit = find_first_bit(bitmap, bits); \
bit < (bits); \
bit = find_next_bit(bitmap, bits, bit + 1))
/**
* find_next_zero_bit() - Find next clear bit in bitmap
* @bitmap: bitmap to check
* @bits: number of bits in @bitmap
* @start: start of bits to check
*
* Checks the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not be checked
*
* Return: bit position of next clear bit, @bits when no clear bit was found
*/
static __inline__ size_t find_next_zero_bit(const unsigned long *bitmap,
size_t bits, size_t start)
{
size_t i;
size_t pos;
unsigned long t;
size_t l = BITS_TO_LONGS(bits);
size_t first_long = start / BITS_PER_LONG;
size_t long_lower = start - (start % BITS_PER_LONG);
if (start >= bits)
return bits;
t = bitmap[first_long] | ~BITMAP_FIRST_WORD_MASK(start);
t ^= ~0UL;
for (i = first_long + 1; !t && i < l; i++) {
/* search until valid t is found */
long_lower += BITS_PER_LONG;
t = bitmap[i];
t ^= ~0UL;
}
if (!t)
return bits;
pos = long_lower + bitops_ffs(t) - 1;
if (pos >= bits)
return bits;
return pos;
}
/**
* find_first_zero_bit - Find first clear bit in bitmap
* @bitmap: bitmap to check
* @bits: number of bits in @bitmap
*
* Checks the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not be checked
*
* Return: bit position of fist clear bit, @bits when no clear bit was found
*/
#define find_first_zero_bit(bitmap, bits) find_next_zero_bit(bitmap, bits, 0)
/**
* for_each_clear_bit - iterate over clear bits in bitmap
* @bit: current bit
* @bitmap: bitmap to iterate over
* @bits: number of bits in @bitmap
*
* WARNING expressions @bitmap and @bits must be side-effect free
*/
#define for_each_clear_bit(bit, bitmap, bits) \
for (bit = find_first_zero_bit(bitmap, bits); \
bit < (bits); \
bit = find_next_zero_bit(bitmap, bits, bit + 1))
/**
* bitmap_weight() - Calculate number of set bits in bitmap
* @bitmap: bitmap to sum up
* @bits: number of bits
*
* Sums the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not summed up
*
* Return: number of set bits
*/
static __inline__ size_t bitmap_weight(const unsigned long *bitmap, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
size_t sum = 0;
for (i = 0; i < l - 1; i++)
sum += hweight_long(bitmap[i]);
return sum + hweight_long(bitmap[l - 1] & BITMAP_LAST_WORD_MASK(bits));
}
/**
* bitmap_equal() - Compare usable bits of two bitmaps
* @bitmap1: bitmap to compare
* @bitmap2: bitmap to compare
* @bits: number of bits
*
* Compares the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not be compared
*
* Return: true when usable bits were equal and false otherwise
*/
static __inline__ bool bitmap_equal(const unsigned long *bitmap1,
const unsigned long *bitmap2, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
if (l > 1 &&
memcmp(bitmap1, bitmap2, (l - 1) * sizeof(unsigned long)) != 0)
return false;
return !((bitmap1[l - 1] ^ bitmap2[l - 1]) &
BITMAP_LAST_WORD_MASK(bits));
}
/**
* bitmap_intersects() - Check for common set bits in two bitmaps
* @bitmap1: bitmap to compare
* @bitmap2: bitmap to compare
* @bits: number of bits
*
* Compares the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not be compared
*
* Return: true when at least one bit is set in both bitmaps and false otherwise
*/
static __inline__ bool bitmap_intersects(const unsigned long *bitmap1,
const unsigned long *bitmap2,
size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l - 1; i++) {
if (bitmap1[i] & bitmap2[i])
return true;
}
return !!(bitmap1[l - 1] & bitmap2[l - 1] &
BITMAP_LAST_WORD_MASK(bits));
}
/**
* bitmap_subset() - Check bitmaps for subset/superset relationship
* @subset: potential subset bitmap
* @superset: potential superset bitmap
* @bits: number of bits
*
* Compares the modifiable bits in the bitmap. The overhead bits in the last
* unsigned long will not be compared
*
* Return: true when all set bits in @subset are also in @superset and false
* otherwise
*/
static __inline__ bool bitmap_subset(const unsigned long *subset,
const unsigned long *superset,
size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l - 1; i++) {
if (subset[i] & ~superset[i])
return false;
}
return !(subset[l - 1] & ~superset[l - 1] &
BITMAP_LAST_WORD_MASK(bits));
}
/**
* bitmap_empty() - Check if no bit is set in bitmap
* @bitmap: bitmap to test
* @bits: number of bits
*
* Check the modifiable bits in the bitmap for zero. The overhead bits in the
* last unsigned long will not be checked
*
* Return: true when usable bits were all zero and false otherwise
*/
static __inline__ bool bitmap_empty(const unsigned long *bitmap, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l - 1; i++) {
if (bitmap[i])
return false;
}
return !(bitmap[l - 1] & BITMAP_LAST_WORD_MASK(bits));
}
/**
* bitmap_full() - Check if all bits are set in bitmap
* @bitmap: bitmap to test
* @bits: number of bits
*
* Check the modifiable bits in the bitmap for one. The overhead bits in the
* last unsigned long will not be checked
*
* Return: true when usable bits were all one and false otherwise
*/
static __inline__ bool bitmap_full(const unsigned long *bitmap, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l - 1; i++) {
if (~bitmap[i])
return false;
}
return !(~bitmap[l - 1] & BITMAP_LAST_WORD_MASK(bits));
}
/**
* bitmap_copy() - Copy bits from one bitmap to another
* @bitmap: bitmap to modify
* @src: source bitmap
* @bits: number of bits
*/
static __inline__ void bitmap_copy(unsigned long *bitmap,
const unsigned long *src, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
memcpy(bitmap, src, l * sizeof(unsigned long));
}
/**
* bitmap_or() - Combines bits of two bitmaps using bitwise "or"
* @bitmap: bitmap to modify
* @src1: source bitmap 1
* @src2: source bitmap 2
* @bits: number of bits
*/
static __inline__ void bitmap_or(unsigned long *bitmap,
const unsigned long *src1,
const unsigned long *src2, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l; i++)
bitmap[i] = src1[i] | src2[i];
}
/**
* bitmap_and() - Combines bits of two bitmaps using bitwise "and"
* @bitmap: bitmap to modify
* @src1: source bitmap 1
* @src2: source bitmap 2
* @bits: number of bits
*/
static __inline__ void bitmap_and(unsigned long *bitmap,
const unsigned long *src1,
const unsigned long *src2, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l; i++)
bitmap[i] = src1[i] & src2[i];
}
/**
* bitmap_andnot() - Combines bits of two bitmaps using bitwise "andnot"
* @bitmap: bitmap to modify
* @src1: source bitmap 1
* @src2: source bitmap 2
* @bits: number of bits
*/
static __inline__ void bitmap_andnot(unsigned long *bitmap,
const unsigned long *src1,
const unsigned long *src2, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l - 1; i++)
bitmap[i] = src1[i] & ~src2[i];
bitmap[i] = (src1[i] & ~src2[i]) & BITMAP_LAST_WORD_MASK(bits);
}
/**
* bitmap_xor() - Combines bits of two bitmaps using bitwise "xor"
* @bitmap: bitmap to modify
* @src1: source bitmap 1
* @src2: source bitmap 2
* @bits: number of bits
*/
static __inline__ void bitmap_xor(unsigned long *bitmap,
const unsigned long *src1,
const unsigned long *src2, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l; i++)
bitmap[i] = src1[i] ^ src2[i];
}
/**
* bitmap_complement() - Return complement bitmap using bitwise "not"
* @bitmap: bitmap to modify
* @src: source bitmap
* @bits: number of bits
*/
static __inline__ void bitmap_complement(unsigned long *bitmap,
const unsigned long *src, size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t i;
for (i = 0; i < l - 1; i++)
bitmap[i] = ~src[i];
bitmap[i] = ~src[l - 1] & BITMAP_LAST_WORD_MASK(bits);
}
/**
* bitmap_shift_right() - Shift bits of bitmap from msb towards lsb
* @bitmap: bitmap to modify
* @src: source bitmap
* @n: number of bits to shift
* @bits: number of bits
*
* Only the modifiable bits in the bitmap will be shifted. The overhead bits in
* the last unsigned long will not be used. @n bits from msb onwards will
* be set to 0.
*/
static __inline__ void bitmap_shift_right(unsigned long *bitmap,
const unsigned long *src, size_t n,
size_t bits)
{
size_t l = BITS_TO_LONGS(bits);
size_t n_bytes = n / BITS_PER_LONG;
size_t n_bits = n % BITS_PER_LONG;
size_t i;
size_t src_i;
unsigned long high;
unsigned long low;
if (n > bits) {