bitfield.h: add FIELD_FIT() helper
authorJakub Kicinski <jakub.kicinski@netronome.com>
Thu, 9 Feb 2017 17:17:27 +0000 (09:17 -0800)
committerDavid S. Miller <davem@davemloft.net>
Fri, 10 Feb 2017 20:52:24 +0000 (15:52 -0500)
Add a helper for checking at runtime that a value will fit inside
a specified field/mask.

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/netronome/nfp/nfp_bpf.h
include/linux/bitfield.h

index 76a19f1796af71a338e0f47636ef70dcaa6ae8ba..9513c80f7be5c9bfa2ddc4802ebe6cc508f43c52 100644 (file)
@@ -39,8 +39,6 @@
 #include <linux/list.h>
 #include <linux/types.h>
 
-#define FIELD_FIT(mask, val)  (!((((u64)val) << __bf_shf(mask)) & ~(mask)))
-
 /* For branch fixup logic use up-most byte of branch instruction as scratch
  * area.  Remember to clear this before sending instructions to HW!
  */
index f6505d83069d4b0969688ee2e05e30bd08227fe7..8b9d6fff002db113cb2233a2d80b3186ff041ef0 100644 (file)
                                              (1ULL << __bf_shf(_mask))); \
        })
 
+/**
+ * FIELD_FIT() - check if value fits in the field
+ * @_mask: shifted mask defining the field's length and position
+ * @_val:  value to test against the field
+ *
+ * Return: true if @_val can fit inside @_mask, false if @_val is too big.
+ */
+#define FIELD_FIT(_mask, _val)                                         \
+       ({                                                              \
+               __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: ");     \
+               !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
+       })
+
 /**
  * FIELD_PREP() - prepare a bitfield element
  * @_mask: shifted mask defining the field's length and position