Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

mlx_bitops.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef _MLX_BITOPS_H
  2. #define _MLX_BITOPS_H
  3. /*
  4. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /**
  21. * @file
  22. *
  23. * Mellanox bit operations
  24. *
  25. */
  26. /* Datatype used to represent a bit in the Mellanox autogenerated headers */
  27. typedef unsigned char pseudo_bit_t;
  28. /**
  29. * Wrapper structure for pseudo_bit_t structures
  30. *
  31. * This structure provides a wrapper around the autogenerated
  32. * pseudo_bit_t structures. It has the correct size, and also
  33. * encapsulates type information about the underlying pseudo_bit_t
  34. * structure, which allows the MLX_FILL etc. macros to work without
  35. * requiring explicit type information.
  36. */
  37. #define MLX_DECLARE_STRUCT( _structure ) \
  38. _structure { \
  39. union { \
  40. uint8_t bytes[ sizeof ( struct _structure ## _st ) / 8 ]; \
  41. uint32_t dwords[ sizeof ( struct _structure ## _st ) / 32 ]; \
  42. struct _structure ## _st *dummy[0]; \
  43. } u; \
  44. }
  45. /** Get pseudo_bit_t structure type from wrapper structure pointer */
  46. #define MLX_PSEUDO_STRUCT( _ptr ) \
  47. typeof ( *((_ptr)->u.dummy[0]) )
  48. /** Bit offset of a field within a pseudo_bit_t structure */
  49. #define MLX_BIT_OFFSET( _structure_st, _field ) \
  50. offsetof ( _structure_st, _field )
  51. /** Dword offset of a field within a pseudo_bit_t structure */
  52. #define MLX_DWORD_OFFSET( _structure_st, _field ) \
  53. ( MLX_BIT_OFFSET ( _structure_st, _field ) / 32 )
  54. /** Dword bit offset of a field within a pseudo_bit_t structure
  55. *
  56. * Yes, using mod-32 would work, but would lose the check for the
  57. * error of specifying a mismatched field name and dword index.
  58. */
  59. #define MLX_DWORD_BIT_OFFSET( _structure_st, _index, _field ) \
  60. ( MLX_BIT_OFFSET ( _structure_st, _field ) - ( 32 * (_index) ) )
  61. /** Bit width of a field within a pseudo_bit_t structure */
  62. #define MLX_BIT_WIDTH( _structure_st, _field ) \
  63. sizeof ( ( ( _structure_st * ) NULL )->_field )
  64. /** Bit mask for a field within a pseudo_bit_t structure */
  65. #define MLX_BIT_MASK( _structure_st, _field ) \
  66. ( ( ~( ( uint32_t ) 0 ) ) >> \
  67. ( 32 - MLX_BIT_WIDTH ( _structure_st, _field ) ) )
  68. /*
  69. * Assemble native-endian dword from named fields and values
  70. *
  71. */
  72. #define MLX_ASSEMBLE_1( _structure_st, _index, _field, _value ) \
  73. ( (_value) << MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  74. #define MLX_ASSEMBLE_2( _structure_st, _index, _field, _value, ... ) \
  75. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  76. MLX_ASSEMBLE_1 ( _structure_st, _index, __VA_ARGS__ ) )
  77. #define MLX_ASSEMBLE_3( _structure_st, _index, _field, _value, ... ) \
  78. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  79. MLX_ASSEMBLE_2 ( _structure_st, _index, __VA_ARGS__ ) )
  80. #define MLX_ASSEMBLE_4( _structure_st, _index, _field, _value, ... ) \
  81. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  82. MLX_ASSEMBLE_3 ( _structure_st, _index, __VA_ARGS__ ) )
  83. #define MLX_ASSEMBLE_5( _structure_st, _index, _field, _value, ... ) \
  84. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  85. MLX_ASSEMBLE_4 ( _structure_st, _index, __VA_ARGS__ ) )
  86. #define MLX_ASSEMBLE_6( _structure_st, _index, _field, _value, ... ) \
  87. ( MLX_ASSEMBLE_1 ( _structure_st, _index, _field, _value ) | \
  88. MLX_ASSEMBLE_5 ( _structure_st, _index, __VA_ARGS__ ) )
  89. /*
  90. * Build native-endian (positive) dword bitmasks from named fields
  91. *
  92. */
  93. #define MLX_MASK_1( _structure_st, _index, _field ) \
  94. ( MLX_BIT_MASK ( _structure_st, _field ) << \
  95. MLX_DWORD_BIT_OFFSET ( _structure_st, _index, _field ) )
  96. #define MLX_MASK_2( _structure_st, _index, _field, ... ) \
  97. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  98. MLX_MASK_1 ( _structure_st, _index, __VA_ARGS__ ) )
  99. #define MLX_MASK_3( _structure_st, _index, _field, ... ) \
  100. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  101. MLX_MASK_2 ( _structure_st, _index, __VA_ARGS__ ) )
  102. #define MLX_MASK_4( _structure_st, _index, _field, ... ) \
  103. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  104. MLX_MASK_3 ( _structure_st, _index, __VA_ARGS__ ) )
  105. #define MLX_MASK_5( _structure_st, _index, _field, ... ) \
  106. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  107. MLX_MASK_4 ( _structure_st, _index, __VA_ARGS__ ) )
  108. #define MLX_MASK_6( _structure_st, _index, _field, ... ) \
  109. ( MLX_MASK_1 ( _structure_st, _index, _field ) | \
  110. MLX_MASK_5 ( _structure_st, _index, __VA_ARGS__ ) )
  111. /*
  112. * Populate big-endian dwords from named fields and values
  113. *
  114. */
  115. #define MLX_FILL( _ptr, _index, _assembled ) \
  116. do { \
  117. uint32_t *__ptr = &(_ptr)->u.dwords[(_index)]; \
  118. uint32_t __assembled = (_assembled); \
  119. *__ptr = cpu_to_be32 ( __assembled ); \
  120. } while ( 0 )
  121. #define MLX_FILL_1( _ptr, _index, ... ) \
  122. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  123. _index, __VA_ARGS__ ) )
  124. #define MLX_FILL_2( _ptr, _index, ... ) \
  125. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_2 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  126. _index, __VA_ARGS__ ) )
  127. #define MLX_FILL_3( _ptr, _index, ... ) \
  128. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_3 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  129. _index, __VA_ARGS__ ) )
  130. #define MLX_FILL_4( _ptr, _index, ... ) \
  131. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_4 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  132. _index, __VA_ARGS__ ) )
  133. #define MLX_FILL_5( _ptr, _index, ... ) \
  134. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_5 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  135. _index, __VA_ARGS__ ) )
  136. #define MLX_FILL_6( _ptr, _index, ... ) \
  137. MLX_FILL ( _ptr, _index, MLX_ASSEMBLE_6 ( MLX_PSEUDO_STRUCT ( _ptr ),\
  138. _index, __VA_ARGS__ ) )
  139. /*
  140. * Modify big-endian dword using named field and value
  141. *
  142. */
  143. #define MLX_SET( _ptr, _field, _value ) \
  144. do { \
  145. unsigned int __index = \
  146. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  147. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  148. uint32_t __value = be32_to_cpu ( *__ptr ); \
  149. __value &= ~( MLX_MASK_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  150. __index, _field ) ); \
  151. __value |= MLX_ASSEMBLE_1 ( MLX_PSEUDO_STRUCT ( _ptr ), \
  152. __index, _field, _value ); \
  153. *__ptr = cpu_to_be32 ( __value ); \
  154. } while ( 0 )
  155. /*
  156. * Extract value of named field
  157. *
  158. */
  159. #define MLX_GET( _ptr, _field ) \
  160. ( { \
  161. unsigned int __index = \
  162. MLX_DWORD_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  163. uint32_t *__ptr = &(_ptr)->u.dwords[__index]; \
  164. uint32_t __value = be32_to_cpu ( *__ptr ); \
  165. __value >>= \
  166. MLX_DWORD_BIT_OFFSET ( MLX_PSEUDO_STRUCT ( _ptr ), \
  167. __index, _field ); \
  168. __value &= \
  169. MLX_BIT_MASK ( MLX_PSEUDO_STRUCT ( _ptr ), _field ); \
  170. __value; \
  171. } )
  172. #endif /* _MLX_BITOPS_H */