Skip to content

Commit

Permalink
quantum: util: add bit and bitmask helpers
Browse files Browse the repository at this point in the history
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
KarlK90 committed Aug 2, 2024
1 parent 807ba71 commit fb46eda
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions quantum/bits.h
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))))
1 change: 1 addition & 0 deletions quantum/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include "bits.h"
#include "bitwise.h"

// convert to string
Expand Down

0 comments on commit fb46eda

Please sign in to comment.