logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

bits.h (1084B)


  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #pragma once
  3. #include <stdint.h>
  4. /* Remove these once we transitioned to C23 across all platforms */
  5. #define UINT32_WIDTH 32
  6. #define UINT64_WIDTH 64
  7. /**
  8. * @brief Mask for the little endian nth bit (0-31) in a 32-bit integer.
  9. */
  10. #define BIT32(n) (UINT32_C(1) << (n))
  11. /**
  12. * @brief Mask for the little endian nth bit (0-63) in a 64-bit integer.
  13. */
  14. #define BIT64(n) (UINT64_C(1) << (n))
  15. /**
  16. * @brief Create a contiguous 32-bit wide bitmask starting at bit position @l
  17. * and ending at position @h. The range is inclusive, meaning GENMASK32(20, 10)
  18. * gives us the 32-bit mask 0x001ffc00.
  19. */
  20. #define GENMASK32(h, l) (((~UINT32_C(0)) - (UINT32_C(1) << (l)) + 1) & (~UINT32_C(0) >> (UINT32_WIDTH - 1 - (h))))
  21. /**
  22. * @brief Create a contiguous 64-bit wide bitmask starting at bit position @l
  23. * and ending at position @h. The range is inclusive, meaning GENMASK64(39, 21)
  24. * gives us the 64-bit mask 0x000000ffffe00000.
  25. */
  26. #define GENMASK64(h, l) (((~UINT64_C(0)) - (UINT64_C(1) << (l)) + 1) & (~UINT64_C(0) >> (UINT64_WIDTH - 1 - (h))))