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., 51 Franklin Street, Fifth Floor, Boston, MA
23 FILE_LICENCE ( GPL2_OR_LATER );
28 * NetXen bit operations
32 /** Datatype used to represent a bit in the pseudo-structures */
33 typedef unsigned char pseudo_bit_t;
36 * Wrapper structure for pseudo_bit_t structures
38 * This structure provides a wrapper around pseudo_bit_t structures.
39 * It has the correct size, and also encapsulates type information
40 * about the underlying pseudo_bit_t-based structure, which allows the
41 * NX_FILL etc. macros to work without requiring explicit type
44 #define NX_PSEUDO_BIT_STRUCT( _structure ) \
46 uint8_t bytes[ sizeof ( _structure ) / 8 ]; \
47 uint64_t qwords[ sizeof ( _structure ) / 64 ]; \
48 _structure *dummy[0]; \
51 /** Get pseudo_bit_t structure type from wrapper structure pointer */
52 #define NX_PSEUDO_STRUCT( _ptr ) \
53 typeof ( *((_ptr)->u.dummy[0]) )
55 /** Bit offset of a field within a pseudo_bit_t structure */
56 #define NX_BIT_OFFSET( _ptr, _field ) \
57 offsetof ( NX_PSEUDO_STRUCT ( _ptr ), _field )
59 /** Bit width of a field within a pseudo_bit_t structure */
60 #define NX_BIT_WIDTH( _ptr, _field ) \
61 sizeof ( ( ( NX_PSEUDO_STRUCT ( _ptr ) * ) NULL )->_field )
63 /** Qword offset of a field within a pseudo_bit_t structure */
64 #define NX_QWORD_OFFSET( _ptr, _field ) \
65 ( NX_BIT_OFFSET ( _ptr, _field ) / 64 )
67 /** Qword bit offset of a field within a pseudo_bit_t structure
69 * Yes, using mod-64 would work, but would lose the check for the
70 * error of specifying a mismatched field name and qword index.
72 #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field ) \
73 ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
75 /** Bit mask for a field within a pseudo_bit_t structure */
76 #define NX_BIT_MASK( _ptr, _field ) \
77 ( ( ~( ( uint64_t ) 0 ) ) >> \
78 ( 64 - NX_BIT_WIDTH ( _ptr, _field ) ) )
81 * Assemble native-endian qword from named fields and values
85 #define NX_ASSEMBLE_1( _ptr, _index, _field, _value ) \
86 ( ( ( uint64_t) (_value) ) << \
87 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
89 #define NX_ASSEMBLE_2( _ptr, _index, _field, _value, ... ) \
90 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
91 NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
93 #define NX_ASSEMBLE_3( _ptr, _index, _field, _value, ... ) \
94 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
95 NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
97 #define NX_ASSEMBLE_4( _ptr, _index, _field, _value, ... ) \
98 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
99 NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
101 #define NX_ASSEMBLE_5( _ptr, _index, _field, _value, ... ) \
102 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
103 NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
105 #define NX_ASSEMBLE_6( _ptr, _index, _field, _value, ... ) \
106 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
107 NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
109 #define NX_ASSEMBLE_7( _ptr, _index, _field, _value, ... ) \
110 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
111 NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
114 * Build native-endian (positive) qword bitmasks from named fields
118 #define NX_MASK_1( _ptr, _index, _field ) \
119 ( NX_BIT_MASK ( _ptr, _field ) << \
120 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
122 #define NX_MASK_2( _ptr, _index, _field, ... ) \
123 ( NX_MASK_1 ( _ptr, _index, _field ) | \
124 NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
126 #define NX_MASK_3( _ptr, _index, _field, ... ) \
127 ( NX_MASK_1 ( _ptr, _index, _field ) | \
128 NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
130 #define NX_MASK_4( _ptr, _index, _field, ... ) \
131 ( NX_MASK_1 ( _ptr, _index, _field ) | \
132 NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
134 #define NX_MASK_5( _ptr, _index, _field, ... ) \
135 ( NX_MASK_1 ( _ptr, _index, _field ) | \
136 NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
138 #define NX_MASK_6( _ptr, _index, _field, ... ) \
139 ( NX_MASK_1 ( _ptr, _index, _field ) | \
140 NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
142 #define NX_MASK_7( _ptr, _index, _field, ... ) \
143 ( NX_MASK_1 ( _ptr, _index, _field ) | \
144 NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
147 * Populate big-endian qwords from named fields and values
151 #define NX_FILL( _ptr, _index, _assembled ) \
153 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)]; \
154 uint64_t __assembled = (_assembled); \
155 *__ptr = cpu_to_le64 ( __assembled ); \
158 #define NX_FILL_1( _ptr, _index, ... ) \
159 NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
161 #define NX_FILL_2( _ptr, _index, ... ) \
162 NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
164 #define NX_FILL_3( _ptr, _index, ... ) \
165 NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
167 #define NX_FILL_4( _ptr, _index, ... ) \
168 NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
170 #define NX_FILL_5( _ptr, _index, ... ) \
171 NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
173 #define NX_FILL_6( _ptr, _index, ... ) \
174 NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
176 #define NX_FILL_7( _ptr, _index, ... ) \
177 NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) )
179 /** Extract value of named field */
180 #define NX_GET64( _ptr, _field ) \
182 unsigned int __index = NX_QWORD_OFFSET ( _ptr, _field ); \
183 uint64_t *__ptr = &(_ptr)->u.qwords[__index]; \
184 uint64_t __value = le64_to_cpu ( *__ptr ); \
186 NX_QWORD_BIT_OFFSET ( _ptr, __index, _field ); \
187 __value &= NX_BIT_MASK ( _ptr, _field ); \
191 /** Extract value of named field (for fields up to the size of a long) */
192 #define NX_GET( _ptr, _field ) \
193 ( ( unsigned long ) NX_GET64 ( _ptr, _field ) )
195 #endif /* _NX_BITOPS_H */