-
-
Notifications
You must be signed in to change notification settings - Fork 39.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.
- Loading branch information
Showing
2 changed files
with
24 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,23 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
|
||
#pragma once | ||
|
||
#define BITS_PER_BYTE 8 | ||
#define BITS_PER_LONG 32 | ||
#define BITS_PER_LONG_LONG 64 | ||
|
||
#define BIT(nr) (UL(1) << (nr)) | ||
#define BIT_ULL(nr) (ULL(1) << (nr)) | ||
#define BIT_MASK(nr) (UL(1) << ((nr) % BITS_PER_LONG)) | ||
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG) | ||
#define BIT_ULL_MASK(nr) (ULL(1) << ((nr) % BITS_PER_LONG_LONG)) | ||
#define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG) | ||
|
||
/* | ||
* Create a contiguous bitmask starting at bit position @l and ending at | ||
* position @h. For example | ||
* GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000. | ||
*/ | ||
#define GENMASK(h, l) (((~UL(0)) - (UL(1) << (l)) + 1) & (~UL(0) >> (BITS_PER_LONG - 1 - (h)))) | ||
|
||
#define GENMASK_ULL(h, l) (((~ULL(0)) - (ULL(1) << (l)) + 1) & (~ULL(0) >> (BITS_PER_LONG_LONG - 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 | ||
|