bitfield.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2014 Felix Fietkau <[email protected]>
  4. * Copyright (C) 2004 - 2009 Ivo van Doorn <[email protected]>
  5. */
  6. #ifndef _LINUX_BITFIELD_H
  7. #define _LINUX_BITFIELD_H
  8. #include <linux/build_bug.h>
  9. #include <asm/byteorder.h>
  10. /*
  11. * Bitfield access macros
  12. *
  13. * FIELD_{GET,PREP} macros take as first parameter shifted mask
  14. * from which they extract the base mask and shift amount.
  15. * Mask must be a compilation time constant.
  16. *
  17. * Example:
  18. *
  19. * #include <linux/bitfield.h>
  20. * #include <linux/bits.h>
  21. *
  22. * #define REG_FIELD_A GENMASK(6, 0)
  23. * #define REG_FIELD_B BIT(7)
  24. * #define REG_FIELD_C GENMASK(15, 8)
  25. * #define REG_FIELD_D GENMASK(31, 16)
  26. *
  27. * Get:
  28. * a = FIELD_GET(REG_FIELD_A, reg);
  29. * b = FIELD_GET(REG_FIELD_B, reg);
  30. *
  31. * Set:
  32. * reg = FIELD_PREP(REG_FIELD_A, 1) |
  33. * FIELD_PREP(REG_FIELD_B, 0) |
  34. * FIELD_PREP(REG_FIELD_C, c) |
  35. * FIELD_PREP(REG_FIELD_D, 0x40);
  36. *
  37. * Modify:
  38. * reg &= ~REG_FIELD_C;
  39. * reg |= FIELD_PREP(REG_FIELD_C, c);
  40. */
  41. #define __bf_shf(x) (__builtin_ffsll(x) - 1)
  42. #define __scalar_type_to_unsigned_cases(type) \
  43. unsigned type: (unsigned type)0, \
  44. signed type: (unsigned type)0
  45. #define __unsigned_scalar_typeof(x) typeof( \
  46. _Generic((x), \
  47. char: (unsigned char)0, \
  48. __scalar_type_to_unsigned_cases(char), \
  49. __scalar_type_to_unsigned_cases(short), \
  50. __scalar_type_to_unsigned_cases(int), \
  51. __scalar_type_to_unsigned_cases(long), \
  52. __scalar_type_to_unsigned_cases(long long), \
  53. default: (x)))
  54. #define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
  55. #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
  56. ({ \
  57. BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
  58. _pfx "mask is not constant"); \
  59. BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \
  60. BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
  61. ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
  62. _pfx "value too large for the field"); \
  63. BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \
  64. __bf_cast_unsigned(_reg, ~0ull), \
  65. _pfx "type of reg too small for mask"); \
  66. __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
  67. (1ULL << __bf_shf(_mask))); \
  68. })
  69. /**
  70. * FIELD_MAX() - produce the maximum value representable by a field
  71. * @_mask: shifted mask defining the field's length and position
  72. *
  73. * FIELD_MAX() returns the maximum value that can be held in the field
  74. * specified by @_mask.
  75. */
  76. #define FIELD_MAX(_mask) \
  77. ({ \
  78. __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
  79. (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
  80. })
  81. /**
  82. * FIELD_FIT() - check if value fits in the field
  83. * @_mask: shifted mask defining the field's length and position
  84. * @_val: value to test against the field
  85. *
  86. * Return: true if @_val can fit inside @_mask, false if @_val is too big.
  87. */
  88. #define FIELD_FIT(_mask, _val) \
  89. ({ \
  90. __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \
  91. !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
  92. })
  93. /**
  94. * FIELD_PREP() - prepare a bitfield element
  95. * @_mask: shifted mask defining the field's length and position
  96. * @_val: value to put in the field
  97. *
  98. * FIELD_PREP() masks and shifts up the value. The result should
  99. * be combined with other fields of the bitfield using logical OR.
  100. */
  101. #define FIELD_PREP(_mask, _val) \
  102. ({ \
  103. __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \
  104. ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
  105. })
  106. /**
  107. * FIELD_GET() - extract a bitfield element
  108. * @_mask: shifted mask defining the field's length and position
  109. * @_reg: value of entire bitfield
  110. *
  111. * FIELD_GET() extracts the field specified by @_mask from the
  112. * bitfield passed in as @_reg by masking and shifting it down.
  113. */
  114. #define FIELD_GET(_mask, _reg) \
  115. ({ \
  116. __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
  117. (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
  118. })
  119. extern void __compiletime_error("value doesn't fit into mask")
  120. __field_overflow(void);
  121. extern void __compiletime_error("bad bitfield mask")
  122. __bad_mask(void);
  123. static __always_inline u64 field_multiplier(u64 field)
  124. {
  125. if ((field | (field - 1)) & ((field | (field - 1)) + 1))
  126. __bad_mask();
  127. return field & -field;
  128. }
  129. static __always_inline u64 field_mask(u64 field)
  130. {
  131. return field / field_multiplier(field);
  132. }
  133. #define field_max(field) ((typeof(field))field_mask(field))
  134. #define ____MAKE_OP(type,base,to,from) \
  135. static __always_inline __##type type##_encode_bits(base v, base field) \
  136. { \
  137. if (__builtin_constant_p(v) && (v & ~field_mask(field))) \
  138. __field_overflow(); \
  139. return to((v & field_mask(field)) * field_multiplier(field)); \
  140. } \
  141. static __always_inline __##type type##_replace_bits(__##type old, \
  142. base val, base field) \
  143. { \
  144. return (old & ~to(field)) | type##_encode_bits(val, field); \
  145. } \
  146. static __always_inline void type##p_replace_bits(__##type *p, \
  147. base val, base field) \
  148. { \
  149. *p = (*p & ~to(field)) | type##_encode_bits(val, field); \
  150. } \
  151. static __always_inline base type##_get_bits(__##type v, base field) \
  152. { \
  153. return (from(v) & field)/field_multiplier(field); \
  154. }
  155. #define __MAKE_OP(size) \
  156. ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
  157. ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \
  158. ____MAKE_OP(u##size,u##size,,)
  159. ____MAKE_OP(u8,u8,,)
  160. __MAKE_OP(16)
  161. __MAKE_OP(32)
  162. __MAKE_OP(64)
  163. #undef __MAKE_OP
  164. #undef ____MAKE_OP
  165. #endif