5 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * NetXen bit operations
29 /** Datatype used to represent a bit in the pseudo-structures */
30 typedef unsigned char pseudo_bit_t;
33 * Wrapper structure for pseudo_bit_t structures
35 * This structure provides a wrapper around pseudo_bit_t structures.
36 * It has the correct size, and also encapsulates type information
37 * about the underlying pseudo_bit_t-based structure, which allows the
38 * NX_FILL etc. macros to work without requiring explicit type
41 #define NX_PSEUDO_BIT_STRUCT( _structure ) \
43 uint8_t bytes[ sizeof ( _structure ) / 8 ]; \
44 uint64_t qwords[ sizeof ( _structure ) / 64 ]; \
45 _structure *dummy[0]; \
48 /** Get pseudo_bit_t structure type from wrapper structure pointer */
49 #define NX_PSEUDO_STRUCT( _ptr ) \
50 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 )
56 /** Bit width of a field within a pseudo_bit_t structure */
57 #define NX_BIT_WIDTH( _ptr, _field ) \
58 sizeof ( ( ( NX_PSEUDO_STRUCT ( _ptr ) * ) NULL )->_field )
60 /** Qword offset of a field within a pseudo_bit_t structure */
61 #define NX_QWORD_OFFSET( _ptr, _field ) \
62 ( NX_BIT_OFFSET ( _ptr, _field ) / 64 )
64 /** Qword bit offset of a field within a pseudo_bit_t structure
66 * Yes, using mod-64 would work, but would lose the check for the
67 * error of specifying a mismatched field name and qword index.
69 #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field ) \
70 ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
72 /** Bit mask for a field within a pseudo_bit_t structure */
73 #define NX_BIT_MASK( _ptr, _field ) \
74 ( ( ~( ( uint64_t ) 0 ) ) >> \
75 ( 64 - NX_BIT_WIDTH ( _ptr, _field ) ) )
78 * Assemble native-endian qword from named fields and values
82 #define NX_ASSEMBLE_1( _ptr, _index, _field, _value ) \
83 ( ( ( uint64_t) (_value) ) << \
84 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
86 #define NX_ASSEMBLE_2( _ptr, _index, _field, _value, ... ) \
87 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
88 NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
90 #define NX_ASSEMBLE_3( _ptr, _index, _field, _value, ... ) \
91 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
92 NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
94 #define NX_ASSEMBLE_4( _ptr, _index, _field, _value, ... ) \
95 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
96 NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
98 #define NX_ASSEMBLE_5( _ptr, _index, _field, _value, ... ) \
99 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
100 NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
102 #define NX_ASSEMBLE_6( _ptr, _index, _field, _value, ... ) \
103 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
104 NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
106 #define NX_ASSEMBLE_7( _ptr, _index, _field, _value, ... ) \
107 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
108 NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
111 * Build native-endian (positive) qword bitmasks from named fields
115 #define NX_MASK_1( _ptr, _index, _field ) \
116 ( NX_BIT_MASK ( _ptr, _field ) << \
117 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
119 #define NX_MASK_2( _ptr, _index, _field, ... ) \
120 ( NX_MASK_1 ( _ptr, _index, _field ) | \
121 NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
123 #define NX_MASK_3( _ptr, _index, _field, ... ) \
124 ( NX_MASK_1 ( _ptr, _index, _field ) | \
125 NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
127 #define NX_MASK_4( _ptr, _index, _field, ... ) \
128 ( NX_MASK_1 ( _ptr, _index, _field ) | \
129 NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
131 #define NX_MASK_5( _ptr, _index, _field, ... ) \
132 ( NX_MASK_1 ( _ptr, _index, _field ) | \
133 NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
135 #define NX_MASK_6( _ptr, _index, _field, ... ) \
136 ( NX_MASK_1 ( _ptr, _index, _field ) | \
137 NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
139 #define NX_MASK_7( _ptr, _index, _field, ... ) \
140 ( NX_MASK_1 ( _ptr, _index, _field ) | \
141 NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
144 * Populate big-endian qwords from named fields and values
148 #define NX_FILL( _ptr, _index, _assembled ) \
150 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)]; \
151 uint64_t __assembled = (_assembled); \
152 *__ptr = cpu_to_le64 ( __assembled ); \
155 #define NX_FILL_1( _ptr, _index, ... ) \
156 NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
158 #define NX_FILL_2( _ptr, _index, ... ) \
159 NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
161 #define NX_FILL_3( _ptr, _index, ... ) \
162 NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
164 #define NX_FILL_4( _ptr, _index, ... ) \
165 NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
167 #define NX_FILL_5( _ptr, _index, ... ) \
168 NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
170 #define NX_FILL_6( _ptr, _index, ... ) \
171 NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
173 #define NX_FILL_7( _ptr, _index, ... ) \
174 NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) )
176 /** Extract value of named field */
177 #define NX_GET64( _ptr, _field ) \
179 unsigned int __index = NX_QWORD_OFFSET ( _ptr, _field ); \
180 uint64_t *__ptr = &(_ptr)->u.qwords[__index]; \
181 uint64_t __value = le64_to_cpu ( *__ptr ); \
183 NX_QWORD_BIT_OFFSET ( _ptr, __index, _field ); \
184 __value &= NX_BIT_MASK ( _ptr, _field ); \
188 /** Extract value of named field (for fields up to the size of a long) */
189 #define NX_GET( _ptr, _field ) \
190 ( ( unsigned long ) NX_GET64 ( _ptr, _field ) )
192 #endif /* _NX_BITOPS_H */