-
-
Notifications
You must be signed in to change notification settings - Fork 40.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quantum: util: add bit and bitmask helpers
These helpers are handy and can prevent off-by-one errors when working with registers and general low level bit manipulation tasks. The macros themself are inspired by the bits.h macros from the linux kernel source code. Signed-off-by: Stefan Kerkmann <[email protected]> Co-authored-by: Pascal Getreuer <[email protected]>
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
/** | ||
* @brief Mask for the little endian nth bit (0-31) in a 32-bit integer. | ||
*/ | ||
#define BIT32(n) (UINT32_C(1) << (n)) | ||
|
||
/** | ||
* @brief Mask for the little endian nth bit (0-63) in a 64-bit integer. | ||
*/ | ||
#define BIT64(n) (UINT64_C(1) << (n)) | ||
|
||
/** | ||
* @brief Create a contiguous 32-bit wide bitmask starting at bit position @l | ||
* and ending at position @h. The range is inclusive, meaning GENMASK32(20, 10) | ||
* gives us the 32-bit mask 0x001ffc00. | ||
*/ | ||
#define GENMASK32(h, l) (((~UINT32_C(0)) - (UINT32_C(1) << (l)) + 1) & (~UINT32_C(0) >> (UINT32_WIDTH - 1 - (h)))) | ||
|
||
/** | ||
* @brief Create a contiguous 64-bit wide bitmask starting at bit position @l | ||
* and ending at position @h. The range is inclusive, meaning GENMASK64(39, 21) | ||
* gives us the 64-bit mask 0x000000ffffe00000. | ||
*/ | ||
#define GENMASK64(h, l) (((~UINT64_C(0)) - (UINT64_C(1) << (l)) + 1) & (~UINT64_C(0) >> (UINT64_WIDTH - 1 - (h)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
#pragma once | ||
|
||
#include "bits.h" | ||
#include "bitwise.h" | ||
|
||
// convert to string | ||
|