bitvec.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* sHT -- array of bits */
  4. #ifndef _sHT_BITVEC_H
  5. #define _sHT_BITVEC_H
  6. #include <stdint.h>
  7. #include <sHT/test.h>
  8. /** Test if a bit is set.
  9. @var{bits}: a bit array. Bits are numbered from the first byte to the last,
  10. from the least-significant bit to the most-significant.
  11. @var{index}: the index of the bit. It is within bounds.
  12. The bit should be set more often than it is not.
  13. No bounds checking is performed. This must be called in control position. */
  14. __attribute__((always_inline))
  15. __attribute__((pure))
  16. static inline _Bool
  17. sHT_bit_test(const uint8_t *bits, unsigned int index)
  18. {
  19. /* x86 has a special instruction for testing a bit (bt), which GCC
  20. doesn't generate automatically, but it is tricky to use -- as of
  21. now, to no avail. There are tests for the courageous, in
  22. <tests/bitvec.c>.
  23. There seems to be no code size difference in
  24. different choices of uintN_t. */
  25. return sHT_and_any(bits[index / 8], 1u << (index % 8));
  26. }
  27. #endif