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
22 * You can also choose to distribute this program under the terms of
23 * the Unmodified Binary Distribution Licence (as given in the file
24 * COPYING.UBDL), provided that you have satisfied its requirements.
27 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 * NetXen bit operations
36 /** Datatype used to represent a bit in the pseudo-structures */
37 typedef unsigned char pseudo_bit_t;
40 * Wrapper structure for pseudo_bit_t structures
42 * This structure provides a wrapper around pseudo_bit_t structures.
43 * It has the correct size, and also encapsulates type information
44 * about the underlying pseudo_bit_t-based structure, which allows the
45 * NX_FILL etc. macros to work without requiring explicit type
48 #define NX_PSEUDO_BIT_STRUCT( _structure ) \
50 uint8_t bytes[ sizeof ( _structure ) / 8 ]; \
51 uint64_t qwords[ sizeof ( _structure ) / 64 ]; \
52 _structure *dummy[0]; \
55 /** Get pseudo_bit_t structure type from wrapper structure pointer */
56 #define NX_PSEUDO_STRUCT( _ptr ) \
57 typeof ( *((_ptr)->u.dummy[0]) )
59 /** Bit offset of a field within a pseudo_bit_t structure */
60 #define NX_BIT_OFFSET( _ptr, _field ) \
61 offsetof ( NX_PSEUDO_STRUCT ( _ptr ), _field )
63 /** Bit width of a field within a pseudo_bit_t structure */
64 #define NX_BIT_WIDTH( _ptr, _field ) \
65 sizeof ( ( ( NX_PSEUDO_STRUCT ( _ptr ) * ) NULL )->_field )
67 /** Qword offset of a field within a pseudo_bit_t structure */
68 #define NX_QWORD_OFFSET( _ptr, _field ) \
69 ( NX_BIT_OFFSET ( _ptr, _field ) / 64 )
71 /** Qword bit offset of a field within a pseudo_bit_t structure
73 * Yes, using mod-64 would work, but would lose the check for the
74 * error of specifying a mismatched field name and qword index.
76 #define NX_QWORD_BIT_OFFSET( _ptr, _index, _field ) \
77 ( NX_BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
79 /** Bit mask for a field within a pseudo_bit_t structure */
80 #define NX_BIT_MASK( _ptr, _field ) \
81 ( ( ~( ( uint64_t ) 0 ) ) >> \
82 ( 64 - NX_BIT_WIDTH ( _ptr, _field ) ) )
85 * Assemble native-endian qword from named fields and values
89 #define NX_ASSEMBLE_1( _ptr, _index, _field, _value ) \
90 ( ( ( uint64_t) (_value) ) << \
91 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
93 #define NX_ASSEMBLE_2( _ptr, _index, _field, _value, ... ) \
94 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
95 NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
97 #define NX_ASSEMBLE_3( _ptr, _index, _field, _value, ... ) \
98 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
99 NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
101 #define NX_ASSEMBLE_4( _ptr, _index, _field, _value, ... ) \
102 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
103 NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
105 #define NX_ASSEMBLE_5( _ptr, _index, _field, _value, ... ) \
106 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
107 NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
109 #define NX_ASSEMBLE_6( _ptr, _index, _field, _value, ... ) \
110 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
111 NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
113 #define NX_ASSEMBLE_7( _ptr, _index, _field, _value, ... ) \
114 ( NX_ASSEMBLE_1 ( _ptr, _index, _field, _value ) | \
115 NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
118 * Build native-endian (positive) qword bitmasks from named fields
122 #define NX_MASK_1( _ptr, _index, _field ) \
123 ( NX_BIT_MASK ( _ptr, _field ) << \
124 NX_QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
126 #define NX_MASK_2( _ptr, _index, _field, ... ) \
127 ( NX_MASK_1 ( _ptr, _index, _field ) | \
128 NX_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
130 #define NX_MASK_3( _ptr, _index, _field, ... ) \
131 ( NX_MASK_1 ( _ptr, _index, _field ) | \
132 NX_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
134 #define NX_MASK_4( _ptr, _index, _field, ... ) \
135 ( NX_MASK_1 ( _ptr, _index, _field ) | \
136 NX_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
138 #define NX_MASK_5( _ptr, _index, _field, ... ) \
139 ( NX_MASK_1 ( _ptr, _index, _field ) | \
140 NX_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
142 #define NX_MASK_6( _ptr, _index, _field, ... ) \
143 ( NX_MASK_1 ( _ptr, _index, _field ) | \
144 NX_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
146 #define NX_MASK_7( _ptr, _index, _field, ... ) \
147 ( NX_MASK_1 ( _ptr, _index, _field ) | \
148 NX_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
151 * Populate big-endian qwords from named fields and values
155 #define NX_FILL( _ptr, _index, _assembled ) \
157 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)]; \
158 uint64_t __assembled = (_assembled); \
159 *__ptr = cpu_to_le64 ( __assembled ); \
162 #define NX_FILL_1( _ptr, _index, ... ) \
163 NX_FILL ( _ptr, _index, NX_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
165 #define NX_FILL_2( _ptr, _index, ... ) \
166 NX_FILL ( _ptr, _index, NX_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
168 #define NX_FILL_3( _ptr, _index, ... ) \
169 NX_FILL ( _ptr, _index, NX_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
171 #define NX_FILL_4( _ptr, _index, ... ) \
172 NX_FILL ( _ptr, _index, NX_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
174 #define NX_FILL_5( _ptr, _index, ... ) \
175 NX_FILL ( _ptr, _index, NX_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
177 #define NX_FILL_6( _ptr, _index, ... ) \
178 NX_FILL ( _ptr, _index, NX_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
180 #define NX_FILL_7( _ptr, _index, ... ) \
181 NX_FILL ( _ptr, _index, NX_ASSEMBLE_7 ( _ptr, _index, __VA_ARGS__ ) )
183 /** Extract value of named field */
184 #define NX_GET64( _ptr, _field ) \
186 unsigned int __index = NX_QWORD_OFFSET ( _ptr, _field ); \
187 uint64_t *__ptr = &(_ptr)->u.qwords[__index]; \
188 uint64_t __value = le64_to_cpu ( *__ptr ); \
190 NX_QWORD_BIT_OFFSET ( _ptr, __index, _field ); \
191 __value &= NX_BIT_MASK ( _ptr, _field ); \
195 /** Extract value of named field (for fields up to the size of a long) */
196 #define NX_GET( _ptr, _field ) \
197 ( ( unsigned long ) NX_GET64 ( _ptr, _field ) )
199 #endif /* _NX_BITOPS_H */