add new macro BIT_S to include/linux/kernel.h
PATCH 1/1 add new macro BIT_S to include/linux/kernel.h
description:
adds a new macro for doing a bitshift. right now every subsystem defines its own macro (which all look the same) but this one also does overflow checking like it is already done in include/linux/input.h
patch is against latest git-tree from Linus
include/linux/kernel.h +5 -0
@@ -30,6 +30,11 @@ extern const char linux_banner[];#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0]))#define ALIGN (x,a) (((x)+(a)-1)&~((a)-1)) +/* +* BIT macro that also handles a possible overflow +*/ +#define BITS (x) (1UL << ((x) % BITS_PER_LONG)) +#define KERN_EMERG "<0>"/* system is unusable */ #define KERN_ALERT "<1>"/* action must be taken immediately */ #define KERN_CRIT "<2>"/* critical conditions */
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 3 lines · Michael Veeck
adds a new macro for doing a bitshift. right now every subsystem defines its own macro (which all look the same) but this one also does overflow checking
Checking?
$ cat -n test.c
1 #define BIT(x) (1ULL << (x))
2 #define BITS(x) (1ULL << ((x) % 64))
3
4 int main(void)
5 {
6 BIT(65);
7 BITS(65);
8 return 0;
9 }
$ gcc -o test test.c
test.c: In function `main':
test.c:6: warning: left shift count >= width of type
<=== ???_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 9 lines · Alexey Dobriyan
Quoted 3 lines · Michael Veeck
adds a new macro for doing a bitshift. right now every subsystem defines its own macro (which all look the same) but this one also does overflow checking
Checking?
Quoted 15 lines
$ cat -n test.c
1 #define BIT(x) (1ULL << (x))
2 #define BITS(x) (1ULL << ((x) % 64))
3
4 int main(void)
5 {
6 BIT(65);
7 BITS(65);
8 return 0;
9 }
$ gcc -o test test.c
test.c: In function `main':
test.c:6: warning: left shift count >= width of type
<== ???okay, good point, but does that work even when the parameter of BIT isnt known at compile time?
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 26 lines · Michael Veeck
Quoted 9 lines · Alexey Dobriyan
Quoted 3 lines · Michael Veeck
adds a new macro for doing a bitshift. right now every subsystem defines its own macro (which all look the same) but this one also does overflow checking
Checking?
Quoted 15 lines
$ cat -n test.c 1 #define BIT(x) (1ULL << (x)) 2 #define BITS(x) (1ULL << ((x) % 64)) 3 4 int main(void) 5 { 6 BIT(65); 7 BITS(65); 8 return 0; 9 } $ gcc -o test test.c test.c: In function `main': test.c:6: warning: left shift count >= width of type <=== ???
Quoted 2 lines
okay, good point, but does that work even when the parameter of BIT isnt known at compile time?
How about something like:
#define BITS(x) (1ULL << (__builtin_constant_p(x) ? (x) : (x) % 64)
Or maybe a WARN_ON would be nice, since (x >= 64) probably means there's a bug.
Domen
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors
Quoted 34 lines · Domen Puncer
Quoted 28 lines · Michael Veeck
Quoted 10 lines · Alexey Dobriyan
Quoted 3 lines · Michael Veeck
adds a new macro for doing a bitshift. right now every subsystem defines its own macro (which all look the same) but this one also does overflow checking
Checking?
Quoted 15 lines
$ cat -n test.c 1 #define BIT(x) (1ULL << (x)) 2 #define BITS(x) (1ULL << ((x) % 64)) 3 4 int main(void) 5 { 6 BIT(65); 7 BITS(65); 8 return 0; 9 } $ gcc -o test test.c test.c: In function `main': test.c:6: warning: left shift count >= width of type <== ???Quoted 2 lines
okay, good point, but does that work even when the parameter of BIT isnt known at compile time?
Quoted 8 lines
How about something like:
#define BITS(x) (1ULL << (__builtin_constant_p(x)? (x) : (x) % 64)Or maybe a
WARN_ONwould be nice, since (x >= 64) probably means there's a bug.Domen
I would put your warning around BIT. When it is used wrongly (x to high) then it really seems to be a bug. Since most definitions use this, it maybe unravels one or two bugs.
BIT_S does not check, that might be misleading word, but rather do a wrap. Everybody who will use BIT_S should know that. Maybe BIT_WRAP would be a better word for the macro then.
Veeck
_______________________________________________
Kernel-janitors mailing list
[email protected]
https://lists.osdl.org/mailman/listinfo/kernel-janitors