utils: add bitfield inline ops
authorFelix Fietkau <nbd@openwrt.org>
Mon, 10 Jun 2013 11:37:14 +0000 (13:37 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 10 Jun 2013 11:37:14 +0000 (13:37 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
utils.h

diff --git a/utils.h b/utils.h
index 71a469225d5fbdff8694e4684f9d541d87ab0e8a..db6217ed849b53de35e5b8419e7ba89afc579795 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -22,6 +22,7 @@
 #include <sys/types.h>
 #include <sys/time.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <time.h>
 
 /*
@@ -151,4 +152,18 @@ void clock_gettime(int type, struct timespec *tv);
 #define __packed __attribute__((packed))
 #endif
 
+#ifndef BITS_PER_LONG
+#define BITS_PER_LONG (8 * sizeof(unsigned long))
+#endif
+
+static inline void bitfield_set(unsigned long *bits, int bit)
+{
+       bits[bit / BITS_PER_LONG] |= (1UL << (bit % BITS_PER_LONG));
+}
+
+static inline bool bitfield_test(unsigned long *bits, int bit)
+{
+       return !!(bits[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG)));
+}
+
 #endif