Skip to contentSkip to conversation

add equivalent of BIT(x) for bitfields

PATCH In review Sebastian Frias 3 participants 9 messages Dec 6, 2016 – Dec 7, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 add equivalent of BIT(x) for bitfields

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

---
 include/linux/bitops.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/bitops.h +25 -0
index a83c822..419529a0 100644
@@ -24,6 +24,31 @@
 #define GENMASK_ULL(h, l) \
 	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
 
+#ifdef	__KERNEL__
+/*
+ * Equivalent of BIT(x) but for contiguous bitfields
+ * GENVALUE(1, 0,0xff) = 0x00000003
+ * GENVALUE(3, 0,0xff) = 0x0000000f
+ * GENVALUE(15,8,0xff) = 0x0000ff00
+ * GENVALUE(6, 6,   1) = 0x00000040 == BIT(6)
+ */
+#define GENVALUE(msb, lsb, val)					\
+	(								\
+		/* BUILD_BUG_ON_ZERO() evaluates to 0 */		\
+		BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) |		\
+		BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) |		\
+		(((val) << (lsb)) & (GENMASK((msb), (lsb))))		\
+	)
+
+#define GENVALUE_ULL(msb, lsb, val)					\
+	(								\
+		/* BUILD_BUG_ON_ZERO() evaluates to 0 */		\
+		BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) |		\
+		BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) |		\
+		(((val) << (lsb)) & (GENMASK_ULL((msb), (lsb))))	\
+	)
+#endif
+
 extern unsigned int __sw_hweight8(unsigned int w);
 extern unsigned int __sw_hweight16(unsigned int w);
 extern unsigned int __sw_hweight32(unsigned int w);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 23 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Quoted 6 lines

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Adding Jakub to comment more.

Signature · 1 line
Kalle Valo
#2 lore raw
Quoted 32 lines · Kalle Valo
Quoted 23 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Quoted 6 lines

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

Quoted 4 lines

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Indeed, it appears to do the same :-)
Any reason why "include/linux/bitfield.h" is not included by default in bitops.h?

#3 lore raw
Quoted 33 lines · Sebastian Frias
Quoted 32 lines · Kalle Valo
Quoted 29 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

Quoted 8 lines
Quoted 4 lines

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Indeed, it appears to do the same :-)
Any reason why "include/linux/bitfield.h" is not included by default in bitops.h?

Hi!

The code is in a separate header because of circular dependencies (coming from bug.h including bitops.h, IIRC). You could possibly add an include of bitfield.h in bitops.h if you're very careful, I haven't tried TBH :)

#4 lore raw
Quoted 43 lines · Jakub Kicinski
Quoted 33 lines · Sebastian Frias
Quoted 32 lines · Kalle Valo
Quoted 29 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

Quoted 8 lines
Quoted 4 lines

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Indeed, it appears to do the same :-)
Any reason why "include/linux/bitfield.h" is not included by default in bitops.h?

Quoted 7 lines

Hi!

The code is in a separate header because of circular dependencies (coming from bug.h including bitops.h, IIRC). You could possibly add an include of bitfield.h in bitops.h if you're very careful, I haven't tried TBH :)

Well, the following seems to work just fine:

include/linux/bitfield.h +0 -2
index f6505d8..24c7480 100644
@@ -15,8 +15,6 @@
 #ifndef _LINUX_BITFIELD_H
 #define _LINUX_BITFIELD_H
 
-#include <linux/bug.h>
-
 /*
  * Bitfield access macros
  *
include/linux/bitops.h +2 -0
index a83c822..7e5fab8 100644
@@ -24,6 +24,8 @@
 #define GENMASK_ULL(h, l) \
        (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
 
+#include "bitfield.h"
+
 extern unsigned int __sw_hweight8(unsigned int w);
 extern unsigned int __sw_hweight16(unsigned int w);
 extern unsigned int __sw_hweight32(unsigned int w);

Is there a way to be sure it works in all cases? Otherwise I could just submit that, right?

#5 lore raw
Quoted 59 lines · Sebastian Frias
Quoted 43 lines · Jakub Kicinski
Quoted 33 lines · Sebastian Frias
Quoted 32 lines · Kalle Valo
Quoted 29 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

Quoted 8 lines
Quoted 4 lines

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Indeed, it appears to do the same :-)
Any reason why "include/linux/bitfield.h" is not included by default in bitops.h?

Quoted 7 lines

Hi!

The code is in a separate header because of circular dependencies (coming from bug.h including bitops.h, IIRC). You could possibly add an include of bitfield.h in bitops.h if you're very careful, I haven't tried TBH :)

Well, the following seems to work just fine:

include/linux/bitfield.h +0 -0
index f6505d8..24c7480 100644

include/linux/bitfield.h line 19

Quoted 5 lines
include/linux/bitfield.h +0 -2
@@ -15,8 +15,6 @@
 #ifndef _LINUX_BITFIELD_H
 #define _LINUX_BITFIELD_H
 
-#include <linux/bug.h>
-

That would break users who include bitfield.h directly and don't include bug.h, no?

Quoted 20 lines
include/linux/bitfield.h +0 -0
 /*
  * Bitfield access macros
  *
include/linux/bitops.h +2 -0
index a83c822..7e5fab8 100644
@@ -24,6 +24,8 @@
 #define GENMASK_ULL(h, l) \
        (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
 
+#include "bitfield.h"
+
 extern unsigned int __sw_hweight8(unsigned int w);
 extern unsigned int __sw_hweight16(unsigned int w);
 extern unsigned int __sw_hweight32(unsigned int w);

Is there a way to be sure it works in all cases? Otherwise I could just submit that, right?

#6 lore raw
Quoted 63 lines · Jakub Kicinski
Quoted 62 lines · Sebastian Frias
Quoted 43 lines · Jakub Kicinski
Quoted 33 lines · Sebastian Frias
Quoted 32 lines · Kalle Valo
Quoted 29 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

Quoted 8 lines
Quoted 4 lines

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Indeed, it appears to do the same :-)
Any reason why "include/linux/bitfield.h" is not included by default in bitops.h?

Quoted 7 lines

Hi!

The code is in a separate header because of circular dependencies (coming from bug.h including bitops.h, IIRC). You could possibly add an include of bitfield.h in bitops.h if you're very careful, I haven't tried TBH :)

Well, the following seems to work just fine:

include/linux/bitfield.h +0 -0
index f6505d8..24c7480 100644
@@ -15,8 +15,6 @@
 #ifndef _LINUX_BITFIELD_H
 #define _LINUX_BITFIELD_H
 
Quoted 5 lines

include/linux/bitfield.h line 19

Quoted 2 lines
include/linux/bitfield.h +0 -2
-#include <linux/bug.h>
-

That would break users who include bitfield.h directly and don't include bug.h, no?

That's why I was asking if there's a way to verify that I did not break anything, as it may be simpler to just modify the users.

Right now bitops.h does not include bug.h yet my patch on this thread used BUILD_BUG_ON_ZERO() inside bitops.h without issues.

So it seems like the "proper" include order is already in place. (even though I agree with you that ideally each header file should include headers required for it to work properly, regardless of include order)

Would the following files be the only two users we would need to worry about?

$ git grep "linux/bitfield\.h"
drivers/net/ethernet/netronome/nfp/nfp_bpf.h:#include <linux/bitfield.h>
drivers/net/wireless/mediatek/mt7601u/mt7601u.h:#include <linux/bitfield.h>

I can take a look at the underlying include order issue later.

Quoted 22 lines
Quoted 21 lines
include/linux/bitfield.h +0 -0
 /*
  * Bitfield access macros
  *
include/linux/bitops.h +2 -0
index a83c822..7e5fab8 100644
@@ -24,6 +24,8 @@
 #define GENMASK_ULL(h, l) \
        (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
 
+#include "bitfield.h"
+
 extern unsigned int __sw_hweight8(unsigned int w);
 extern unsigned int __sw_hweight16(unsigned int w);
 extern unsigned int __sw_hweight32(unsigned int w);

Is there a way to be sure it works in all cases? Otherwise I could just submit that, right?

#7 lore raw
Quoted 80 lines · Sebastian Frias
Quoted 63 lines · Jakub Kicinski
Quoted 62 lines · Sebastian Frias
Quoted 43 lines · Jakub Kicinski
Quoted 33 lines · Sebastian Frias
Quoted 32 lines · Kalle Valo
Quoted 29 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

Quoted 8 lines
Quoted 4 lines

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Indeed, it appears to do the same :-)
Any reason why "include/linux/bitfield.h" is not included by default in bitops.h?

Quoted 7 lines

Hi!

The code is in a separate header because of circular dependencies (coming from bug.h including bitops.h, IIRC). You could possibly add an include of bitfield.h in bitops.h if you're very careful, I haven't tried TBH :)

Well, the following seems to work just fine:

include/linux/bitfield.h +0 -0
index f6505d8..24c7480 100644
@@ -15,8 +15,6 @@
 #ifndef _LINUX_BITFIELD_H
 #define _LINUX_BITFIELD_H
 
Quoted 5 lines

include/linux/bitfield.h line 19

Quoted 2 lines
include/linux/bitfield.h +0 -2
-#include <linux/bug.h>
-  

That would break users who include bitfield.h directly and don't include bug.h, no?

That's why I was asking if there's a way to verify that I did not break anything, as it may be simpler to just modify the users.

Right now bitops.h does not include bug.h yet my patch on this thread used BUILD_BUG_ON_ZERO() inside bitops.h without issues.

So it seems like the "proper" include order is already in place. (even though I agree with you that ideally each header file should include headers required for it to work properly, regardless of include order)

Quoted 6 lines

Would the following files be the only two users we would need to worry about?

$ git grep "linux/bitfield\.h"
drivers/net/ethernet/netronome/nfp/nfp_bpf.h:#include <linux/bitfield.h>
drivers/net/wireless/mediatek/mt7601u/mt7601u.h:#include <linux/bitfield.h>

If you're proposing to require users to have to remember to include bug.h before bitfield.h then I don't think that's acceptable.

There are also out-of-tree users like LEDE/OpenWRT who such changes may disturb.

BTW I dug out the old conversation: https://lkml.org/lkml/2016/8/19/96

Quoted 1 line

I can take a look at the underlying include order issue later.

I think that would be the only way forward, but is not easy.

Is your concern that bitfield.h is hard to discover? Or that users need an extra #include beyond just bitops.h?

#8 lore raw
Quoted 84 lines · Jakub Kicinski
Quoted 83 lines · Sebastian Frias
Quoted 63 lines · Jakub Kicinski
Quoted 62 lines · Sebastian Frias
Quoted 43 lines · Jakub Kicinski
Quoted 33 lines · Sebastian Frias
Quoted 32 lines · Kalle Valo
Quoted 29 lines · Sebastian Frias

Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits.

GENVALUE_ULL(msb, lsb, value) macro is also added.

This is useful mostly for creating values to be packed together via OR operations, ex:

   u32 val = 0x11110000;
   val |= GENVALUE(19, 12, 0x5a);

now 'val = 0x1115a000'

---

Change in v2:
- rename the macro to GENVALUE as proposed by Linus
- longer comment attempts to show use case for the macro as proposed by Borislav

Change in v3:
- use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus.
Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency.

Quoted 8 lines
Quoted 4 lines

(I missed there was v3 already, but I'll repeat what I said in v1.)

Please check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same?

Indeed, it appears to do the same :-)
Any reason why "include/linux/bitfield.h" is not included by default in bitops.h?

Quoted 7 lines

Hi!

The code is in a separate header because of circular dependencies (coming from bug.h including bitops.h, IIRC). You could possibly add an include of bitfield.h in bitops.h if you're very careful, I haven't tried TBH :)

Well, the following seems to work just fine:

include/linux/bitfield.h +0 -0
index f6505d8..24c7480 100644
@@ -15,8 +15,6 @@
 #ifndef _LINUX_BITFIELD_H
 #define _LINUX_BITFIELD_H
 
Quoted 5 lines

include/linux/bitfield.h line 19

Quoted 2 lines
include/linux/bitfield.h +0 -2
-#include <linux/bug.h>
-  

That would break users who include bitfield.h directly and don't include bug.h, no?

That's why I was asking if there's a way to verify that I did not break anything, as it may be simpler to just modify the users.

Right now bitops.h does not include bug.h yet my patch on this thread used BUILD_BUG_ON_ZERO() inside bitops.h without issues.

So it seems like the "proper" include order is already in place. (even though I agree with you that ideally each header file should include headers required for it to work properly, regardless of include order)

Would the following files be the only two users we would need to worry about?

Quoted 7 lines
Quoted 3 lines
$ git grep "linux/bitfield\.h"
drivers/net/ethernet/netronome/nfp/nfp_bpf.h:#include <linux/bitfield.h>
drivers/net/wireless/mediatek/mt7601u/mt7601u.h:#include <linux/bitfield.h>

If you're proposing to require users to have to remember to include bug.h before bitfield.h then I don't think that's acceptable.

I was proposing a quick fix, and noted that "I agree with you that ideally each header file should include headers required for it to work properly, regardless of include order"

The reason is probably the same that you had when you submitted bitfield.h without including it in bitops.h: it seems that the "circular dependency" issue is very deep.

However, we have to keep in mind that after fixing the current users of bitfield.h, the rest won't have to do anything, because bitfield.h would come with bitops.h and work properly.

Quoted 3 lines

There are also out-of-tree users like LEDE/OpenWRT who such changes may disturb.

I thought, maybe incorrectly, that out-of-tree users are not to be worried about.

Quoted 6 lines

BTW I dug out the old conversation: https://lkml.org/lkml/2016/8/19/96

Quoted 1 line

I can take a look at the underlying include order issue later.

I think that would be the only way forward, but is not easy.

Right :-)

Quoted 3 lines

Is your concern that bitfield.h is hard to discover? Or that users need an extra #include beyond just bitops.h?

The thing is that there are so many files that it is indeed not easy to know that some facilities are already there.

I wrote the original macros in 4.7 which does not has bitfield.h, so it is not really the case here, but I think it would be a good idea to include these macros by default.