Skip to content

Commit 5b572e8

Browse files
lucasdemarchiYuryNorov
authored andcommitted
bits: introduce fixed-type BIT_U*()
Implement fixed-type BIT_U*() to help drivers add stricter checks, like it was done for GENMASK_U*(). Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com> Acked-by: Jani Nikula <jani.nikula@intel.com> Co-developed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr> Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Yury Norov <yury.norov@gmail.com>
1 parent 1940820 commit 5b572e8

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

include/linux/bits.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/*
2525
* Missing asm support
2626
*
27-
* GENMASK_U*() depend on BITS_PER_TYPE() which relies on sizeof(),
27+
* GENMASK_U*() and BIT_U*() depend on BITS_PER_TYPE() which relies on sizeof(),
2828
* something not available in asm. Nevertheless, fixed width integers is a C
2929
* concept. Assembly code can rely on the long and long long versions instead.
3030
*/
@@ -55,6 +55,24 @@
5555
#define GENMASK_U32(h, l) GENMASK_TYPE(u32, h, l)
5656
#define GENMASK_U64(h, l) GENMASK_TYPE(u64, h, l)
5757

58+
/*
59+
* Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The
60+
* following examples generate compiler warnings due to -Wshift-count-overflow:
61+
*
62+
* - BIT_U8(8)
63+
* - BIT_U32(-1)
64+
* - BIT_U32(40)
65+
*/
66+
#define BIT_INPUT_CHECK(type, nr) \
67+
BUILD_BUG_ON_ZERO(const_true((nr) >= BITS_PER_TYPE(type)))
68+
69+
#define BIT_TYPE(type, nr) ((type)(BIT_INPUT_CHECK(type, nr) + BIT_ULL(nr)))
70+
71+
#define BIT_U8(nr) BIT_TYPE(u8, nr)
72+
#define BIT_U16(nr) BIT_TYPE(u16, nr)
73+
#define BIT_U32(nr) BIT_TYPE(u32, nr)
74+
#define BIT_U64(nr) BIT_TYPE(u64, nr)
75+
5876
#else /* defined(__ASSEMBLY__) */
5977

6078
/*

0 commit comments

Comments
 (0)