You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nx_bitops.h 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef _NX_BITOPS_H
  2. #define _NX_BITOPS_H
  3. /*
  4. * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. *
  21. * You can also choose to distribute this program under the terms of
  22. * the Unmodified Binary Distribution Licence (as given in the file
  23. * COPYING.UBDL), provided that you have satisfied its requirements.
  24. */
  25. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  26. /**
  27. * @file
  28. *
  29. * NetXen bit operations
  30. *
  31. */
  32. /** Datatype used to represent a bit in the pseudo-structures */
  33. typedef unsigned char pseudo_bit_t;
  34. /**
  35. * Wrapper structure for pseudo_bit_t structures
  36. *
  37. * This structure provides a wrapper around pseudo_bit_t structures.
  38. * It has the correct size, and also encapsulates type information
  39. * about the underlying pseudo_bit_t-based structure, which allows the
  40. * NX_FILL etc. macros to work without requiring explicit type
  41. * information.
  42. */
  43. #define NX_PSEUDO_BIT_STRUCT( _structure ) \
  44. union { \
  45. uint8_t bytes[ sizeof ( _structure ) / 8 ]; \
  46. uint64_t qwords[ sizeof ( _structure ) / 64 ]; \
  47. _structure *dummy[0]; \
  48. } u;
  49. /** Get pseudo_bit_t structure type from wrapper structure pointer */
  50. #define NX_PSEUDO_STRUCT( _ptr ) \
  51. typeof ( *((_ptr)->u.dummy[0]) )
  52. /** Bit offset of a field within a pseudo_bit_t structure */
  53. #define NX_BIT_OFFSET( _ptr, _field ) \
  54. offsetof ( NX_PSEUDO_STRUCT ( _ptr ), _field )
  55. /** Bit width of a field within a pseudo_bit_t structure */
  56. #define NX_BIT_WIDTH( _ptr, _field ) \
  57. sizeof ( ( ( NX_PSEUDO_STRUCT ( _ptr ) * ) NULL )->_field )
  58. /** Qword offset of a field within a pseudo_bit_t structure */
  59. #define NX_QWORD_OFFSET( _ptr, _field ) \
  60. ( NX_BIT_OFFSET ( _ptr, _field ) / 64 )
  61. /** Qword bit offset of a field within a pseudo_bit_t structure
  62. *
  63. * Yes, using mod-64 would work, but would lose the check for the
  64. * error of specifying a mismatched field name and qword index.
  65. */
  66. #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field ) \
  67. ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
  68. /** Bit mask for a field within a pseudo_bit_t structure */
  69. #define NX_BIT_MASK( _ptr, _field ) \
  70. ( ( ~( ( uint64_t ) 0 ) ) >> \
  71. ( 64 - NX_BIT_WIDTH ( _ptr, _field ) ) )
  72. /*
  73. * Assemble native-endian qword from named fields and values
  74. *
  75. */
  76. #define NX_ASSEMBLE_1( _ptr, _index, _field, _value ) \
  77. ( ( ( uint64_t) (_value) ) << \
  78. NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
  79. #define NX_ASSEMBLE_2( _ptr, _index, _field, _value, ... ) \
  80. ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
  81. NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
  82. #define NX_ASSEMBLE_3( _ptr, _index, _field, _value, ... ) \
  83. ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
  84. NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
  85. #define NX_ASSEMBLE_4( _ptr, _index, _field, _value, ... ) \
  86. ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
  87. NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
  88. #define NX_ASSEMBLE_5( _ptr, _index, _field, _value, ... ) \
  89. ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
  90. NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
  91. #define NX_ASSEMBLE_6( _ptr, _index, _field, _value, ... ) \
  92. ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
  93. NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
  94. #define NX_ASSEMBLE_7( _ptr, _index, _field, _value, ... ) \
  95. ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
  96. NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
  97. /*
  98. * Build native-endian (positive) qword bitmasks from named fields
  99. *
  100. */
  101. #define NX_MASK_1( _ptr, _index, _field ) \
  102. ( NX_BIT_MASK ( _ptr, _field ) << \
  103. NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
  104. #define NX_MASK_2( _ptr, _index, _field, ... ) \
  105. ( NX_MASK_1 ( _ptr, _index, _field ) | \
  106. NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
  107. #define NX_MASK_3( _ptr, _index, _field, ... ) \
  108. ( NX_MASK_1 ( _ptr, _index, _field ) | \
  109. NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
  110. #define NX_MASK_4( _ptr, _index, _field, ... ) \
  111. ( NX_MASK_1 ( _ptr, _index, _field ) | \
  112. NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
  113. #define NX_MASK_5( _ptr, _index, _field, ... ) \
  114. ( NX_MASK_1 ( _ptr, _index, _field ) | \
  115. NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
  116. #define NX_MASK_6( _ptr, _index, _field, ... ) \
  117. ( NX_MASK_1 ( _ptr, _index, _field ) | \
  118. NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
  119. #define NX_MASK_7( _ptr, _index, _field, ... ) \
  120. ( NX_MASK_1 ( _ptr, _index, _field ) | \
  121. NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
  122. /*
  123. * Populate big-endian qwords from named fields and values
  124. *
  125. */
  126. #define NX_FILL( _ptr, _index, _assembled ) \
  127. do { \
  128. uint64_t *__ptr = &(_ptr)->u.qwords[(_index)]; \
  129. uint64_t __assembled = (_assembled); \
  130. *__ptr = cpu_to_le64 ( __assembled ); \
  131. } while ( 0 )
  132. #define NX_FILL_1( _ptr, _index, ... ) \
  133. NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
  134. #define NX_FILL_2( _ptr, _index, ... ) \
  135. NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
  136. #define NX_FILL_3( _ptr, _index, ... ) \
  137. NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
  138. #define NX_FILL_4( _ptr, _index, ... ) \
  139. NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
  140. #define NX_FILL_5( _ptr, _index, ... ) \
  141. NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
  142. #define NX_FILL_6( _ptr, _index, ... ) \
  143. NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
  144. #define NX_FILL_7( _ptr, _index, ... ) \
  145. NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) )
  146. /** Extract value of named field */
  147. #define NX_GET64( _ptr, _field ) \
  148. ( { \
  149. unsigned int __index = NX_QWORD_OFFSET ( _ptr, _field ); \
  150. uint64_t *__ptr = &(_ptr)->u.qwords[__index]; \
  151. uint64_t __value = le64_to_cpu ( *__ptr ); \
  152. __value >>= \
  153. NX_QWORD_BIT_OFFSET ( _ptr, __index, _field ); \
  154. __value &= NX_BIT_MASK ( _ptr, _field ); \
  155. __value; \
  156. } )
  157. /** Extract value of named field (for fields up to the size of a long) */
  158. #define NX_GET( _ptr, _field ) \
  159. ( ( unsigned long ) NX_GET64 ( _ptr, _field ) )
  160. #endif /* _NX_BITOPS_H */