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

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