Skip to content

Commit

Permalink
bitfield.h: add FIELD_FIT() helper
Browse files Browse the repository at this point in the history
Add a helper for checking at runtime that a value will fit inside
a specified field/mask.

Signed-off-by: Jakub Kicinski <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Jakub Kicinski authored and davem330 committed Feb 10, 2017
1 parent 4f2bd6b commit 1697599
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 0 additions & 2 deletions drivers/net/ethernet/netronome/nfp/nfp_bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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!
*/
Expand Down
13 changes: 13 additions & 0 deletions include/linux/bitfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@
(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
Expand Down

0 comments on commit 1697599

Please sign in to comment.