sbi_bitmap.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #ifndef __SBI_BITMAP_H__
  10. #define __SBI_BITMAP_H__
  11. #include <sbi/sbi_bitops.h>
  12. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
  13. #define BITMAP_LAST_WORD_MASK(nbits) \
  14. ( \
  15. ((nbits) % BITS_PER_LONG) ? \
  16. ((1UL << ((nbits) % BITS_PER_LONG)) - 1) : ~0UL \
  17. )
  18. #define small_const_nbits(nbits) \
  19. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  20. #define DECLARE_BITMAP(name, nbits) unsigned long name[BITS_TO_LONGS(nbits)]
  21. #define DEFINE_BITMAP(name) extern unsigned long name[]
  22. static inline unsigned long bitmap_estimate_size(int nbits)
  23. {
  24. return (BITS_TO_LONGS(nbits) * sizeof(unsigned long));
  25. }
  26. void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  27. const unsigned long *bitmap2, int bits);
  28. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  29. const unsigned long *bitmap2, int bits);
  30. void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  31. const unsigned long *bitmap2, int bits);
  32. static inline void bitmap_set(unsigned long *bmap, int start, int len)
  33. {
  34. int bit;
  35. for (bit = start; bit < (start + len); bit++)
  36. bmap[BIT_WORD(bit)] |= (0x1UL << BIT_WORD_OFFSET(bit));
  37. }
  38. static inline void bitmap_clear(unsigned long *bmap, int start, int len)
  39. {
  40. int bit;
  41. for (bit = start; bit < (start + len); bit++)
  42. bmap[BIT_WORD(bit)] &= ~(0x1UL << BIT_WORD_OFFSET(bit));
  43. }
  44. static inline void bitmap_zero(unsigned long *dst, int nbits)
  45. {
  46. if (small_const_nbits(nbits))
  47. *dst = 0UL;
  48. else {
  49. size_t i, len = BITS_TO_LONGS(nbits);
  50. for (i = 0; i < len; i++)
  51. dst[i] = 0;
  52. }
  53. }
  54. static inline void bitmap_zero_except(unsigned long *dst,
  55. int exception, int nbits)
  56. {
  57. if (small_const_nbits(nbits))
  58. *dst = 0UL;
  59. else {
  60. size_t i, len = BITS_TO_LONGS(nbits);
  61. for (i = 0; i < len; i++)
  62. dst[i] = 0;
  63. }
  64. if (exception < nbits)
  65. __set_bit(exception, dst);
  66. }
  67. static inline void bitmap_fill(unsigned long *dst, int nbits)
  68. {
  69. size_t i, nlongs = BITS_TO_LONGS(nbits);
  70. if (!small_const_nbits(nbits)) {
  71. for (i = 0; i < (nlongs - 1); i++)
  72. dst[i] = -1UL;
  73. }
  74. dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
  75. }
  76. static inline void bitmap_copy(unsigned long *dst,
  77. const unsigned long *src, int nbits)
  78. {
  79. if (small_const_nbits(nbits))
  80. *dst = *src;
  81. else {
  82. size_t i, len = BITS_TO_LONGS(nbits);
  83. for (i = 0; i < len; i++)
  84. dst[i] = src[i];
  85. }
  86. }
  87. static inline void bitmap_and(unsigned long *dst, const unsigned long *src1,
  88. const unsigned long *src2, int nbits)
  89. {
  90. if (small_const_nbits(nbits))
  91. *dst = *src1 & *src2;
  92. else
  93. __bitmap_and(dst, src1, src2, nbits);
  94. }
  95. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  96. const unsigned long *src2, int nbits)
  97. {
  98. if (small_const_nbits(nbits))
  99. *dst = *src1 | *src2;
  100. else
  101. __bitmap_or(dst, src1, src2, nbits);
  102. }
  103. static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
  104. const unsigned long *src2, int nbits)
  105. {
  106. if (small_const_nbits(nbits))
  107. *dst = *src1 ^ *src2;
  108. else
  109. __bitmap_xor(dst, src1, src2, nbits);
  110. }
  111. #endif