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 6.8KB

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