bitarray.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * \file bitarray.h
  3. *
  4. * \brief This module defines interfaces to efficiently process
  5. * an array of booleans represented as bits in an array of 32-bit integers.
  6. */
  7. /*
  8. * Copyright (C) 2017. Mindtree Limited.
  9. * All rights reserved.
  10. */
  11. #ifndef _H_BITARRAY_
  12. #define _H_BITARRAY_
  13. /* --------------------------------------------- Header File Inclusion */
  14. #include "EM_os.h"
  15. /* --------------------------------------------- Global Definitions */
  16. /** Size of a single block in bitarray data structure */
  17. #define BITARRAY_BLOCK_SIZE 32u
  18. /** Number of blocks required to store a given number of bits */
  19. #define BITARRAY_NUM_BLOCKS(bit_count) \
  20. (((bit_count) + (BITARRAY_BLOCK_SIZE - 1)) / (BITARRAY_BLOCK_SIZE))
  21. /* --------------------------------------------- Structures/Data Types */
  22. /* --------------------------------------------- Macros */
  23. /* --------------------------------------------- API Declarations */
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * \brief Set boolean value of a specific bit position
  29. *
  30. * \par Description
  31. * This routine sets the boolean value of a specific bit position in a given bitarray.
  32. *
  33. * \param [in] bitarray The bitarray in which the bit position to be set.
  34. * \param [in] bit_position The bit position to be set.
  35. */
  36. void bitarray_set_bit
  37. (
  38. /* IN */ UINT32 * bitarray,
  39. /* IN */ UINT32 bit_position
  40. );
  41. /**
  42. * \brief Get boolean value of a specific bit position
  43. *
  44. * \par Description
  45. * This routine returns the boolean value of a specific bit position in a given bitarray.
  46. *
  47. * \param [in] bitarray The bitarray from which the bit position to be fetched.
  48. * \param [in] bit_position The bit position to be fetched.
  49. *
  50. * \return Boolean value 1 if bit is set, otherwise 0
  51. */
  52. UINT8 bitarray_get_bit
  53. (
  54. /* IN */ UINT32 * bitarray,
  55. /* IN */ UINT32 bit_position
  56. );
  57. /**
  58. * \brief Reset/clear boolean value of a specific bit position
  59. *
  60. * \par Description
  61. * This routine resets/clear the boolean value of a specific bit position in a given bitarray.
  62. *
  63. * \param [in] bitarray The bitarray in which the bit position to be reset.
  64. * \param [in] bit_position The bit position to be reset.
  65. */
  66. void bitarray_reset_bit
  67. (
  68. /* IN */ UINT32 * bitarray,
  69. /* IN */ UINT32 bit_position
  70. );
  71. /**
  72. * \brief Set boolean value of all bits
  73. *
  74. * \par Description
  75. * This routine sets the boolean value of all bits in a given bitarray.
  76. *
  77. * \param [in] bitarray The bitarray to be set.
  78. * \param [in] bit_count Number of bits in the bitarray.
  79. */
  80. void bitarray_set_all
  81. (
  82. /* IN */ UINT32 * bitarray,
  83. /* IN */ UINT32 bit_count
  84. );
  85. /**
  86. * \brief Reset/clear boolean value of all bits
  87. *
  88. * \par Description
  89. * This routine resets/clear the boolean value of all bits in a given bitarray.
  90. *
  91. * \param [in] bitarray The bitarray to be reset.
  92. * \param [in] bit_count Number of bits in the bitarray.
  93. */
  94. void bitarray_reset_all
  95. (
  96. /* IN */ UINT32 * bitarray,
  97. /* IN */ UINT32 bit_count
  98. );
  99. /**
  100. * \brief To check if all bits are set
  101. *
  102. * \par Description
  103. * This routine checks if all bits in a given bitarray are set.
  104. *
  105. * \param [in] bitarray The bitarray to be checked.
  106. * \param [in] bit_count Number of bits in the bitarray.
  107. *
  108. * \return Boolean value 1 if all bits are set, otherwise 0
  109. */
  110. UINT8 bitarray_is_all_set
  111. (
  112. /* IN */ UINT32 * bitarray,
  113. /* IN */ UINT32 bit_count
  114. );
  115. /**
  116. * \brief To check if all bits are reset/clear
  117. *
  118. * \par Description
  119. * This routine checks if all bits in a given bitarray are reset/clear.
  120. *
  121. * \param [in] bitarray The bitarray to be checked.
  122. * \param [in] bit_count Number of bits in the bitarray.
  123. *
  124. * \return Boolean value 1 if all bits are reset/clear, otherwise 0
  125. */
  126. UINT8 bitarray_is_all_reset
  127. (
  128. /* IN */ UINT32 * bitarray,
  129. /* IN */ UINT32 bit_count
  130. );
  131. /**
  132. * \brief Get the index of lowest bit that is set
  133. *
  134. * \par Description
  135. * This routine returns the index of the lowest bit set in a given bitarray.
  136. *
  137. * \param [in] bitarray The bitarray to be checked.
  138. * \param [in] bit_count Number of bits in the bitarray.
  139. * \param [in] start_index Index in the bitarray from where to start looking for.
  140. *
  141. * \return Index of the lowest bit that is set from the given start_index.
  142. * Return start_index itself, if no further bits are set.
  143. */
  144. UINT32 bitarray_get_lowest_bit_set
  145. (
  146. /* IN */ UINT32 * bitarray,
  147. /* IN */ UINT32 bit_count,
  148. /* IN */ UINT32 start_index
  149. );
  150. /**
  151. * \brief Get the index of highest bit that is set
  152. *
  153. * \par Description
  154. * This routine returns the index of the highest bit set in a given bitarray.
  155. *
  156. * \param [in] bitarray The bitarray to be checked.
  157. * \param [in] bit_count Number of bits in the bitarray.
  158. * \param [in] start_index Index in the bitarray from where to start looking for.
  159. *
  160. * \return Index of the highest bit that is set from the given start_index.
  161. * Return start_index itself, if no further bits are set.
  162. */
  163. UINT32 bitarray_get_highest_bit_set
  164. (
  165. /* IN */ UINT32 * bitarray,
  166. /* IN */ UINT32 bit_count,
  167. /* IN */ UINT32 start_index
  168. );
  169. #ifdef __cplusplus
  170. };
  171. #endif
  172. #endif /* _H_BITARRAY_ */