These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / include / ipxe / bitops.h
1 #ifndef _IPXE_BITOPS_H
2 #define _IPXE_BITOPS_H
3
4 /*
5  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
6  *
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.
11  *
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.
16  *
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
20  * 02110-1301, USA.
21  *
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.
25  */
26
27 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28
29 /**
30  * @file
31  *
32  * Bit operations
33  *
34  */
35
36 #include <stdint.h>
37 #include <byteswap.h>
38
39 /* Endianness selection.
40  *
41  * This is a property of the NIC, not a property of the host CPU.
42  */
43 #ifdef BITOPS_LITTLE_ENDIAN
44 #define cpu_to_BIT64    cpu_to_le64
45 #define cpu_to_BIT32    cpu_to_le32
46 #define BIT64_to_cpu    le64_to_cpu
47 #define BIT32_to_cpu    le32_to_cpu
48 #endif
49 #ifdef BITOPS_BIG_ENDIAN
50 #define cpu_to_BIT64    cpu_to_be64
51 #define cpu_to_BIT32    cpu_to_be32
52 #define BIT64_to_cpu    be64_to_cpu
53 #define BIT32_to_cpu    be32_to_cpu
54 #endif
55
56 /** Datatype used to represent a bit in the pseudo-structures */
57 typedef unsigned char pseudo_bit_t;
58
59 /**
60  * Wrapper structure for pseudo_bit_t structures
61  *
62  * This structure provides a wrapper around pseudo_bit_t structures.
63  * It has the correct size, and also encapsulates type information
64  * about the underlying pseudo_bit_t-based structure, which allows the
65  * BIT_FILL() etc. macros to work without requiring explicit type
66  * information.
67  */
68 #define PSEUDO_BIT_STRUCT( _structure )                                       \
69         union {                                                               \
70                 uint8_t bytes[ sizeof ( _structure ) / 8 ];                   \
71                 uint32_t dwords[ sizeof ( _structure ) / 32 ];                \
72                 uint64_t qwords[ sizeof ( _structure ) / 64 ];                \
73                 _structure *dummy[0];                                         \
74         } __attribute__ (( packed )) u
75
76 /** Get pseudo_bit_t structure type from wrapper structure pointer */
77 #define PSEUDO_BIT_STRUCT_TYPE( _ptr )                                        \
78         typeof ( *((_ptr)->u.dummy[0]) )
79
80 /** Bit offset of a field within a pseudo_bit_t structure */
81 #define BIT_OFFSET( _ptr, _field )                                            \
82         offsetof ( PSEUDO_BIT_STRUCT_TYPE ( _ptr ), _field )
83
84 /** Bit width of a field within a pseudo_bit_t structure */
85 #define BIT_WIDTH( _ptr, _field )                                             \
86         sizeof ( ( ( PSEUDO_BIT_STRUCT_TYPE ( _ptr ) * ) NULL )->_field )
87
88 /** Qword offset of a field within a pseudo_bit_t structure */
89 #define QWORD_OFFSET( _ptr, _field )                                          \
90         ( BIT_OFFSET ( _ptr, _field ) / 64 )
91
92 /** Qword bit offset of a field within a pseudo_bit_t structure */
93 #define QWORD_BIT_OFFSET( _ptr, _index, _field )                              \
94         ( BIT_OFFSET ( _ptr, _field ) - ( 64 * (_index) ) )
95
96 /** Bit mask for a field within a pseudo_bit_t structure */
97 #define BIT_MASK( _ptr, _field )                                              \
98         ( ( ~( ( uint64_t ) 0 ) ) >>                                          \
99           ( 64 - BIT_WIDTH ( _ptr, _field ) ) )
100
101 /*
102  * Assemble native-endian qword from named fields and values
103  *
104  */
105
106 #define BIT_ASSEMBLE_1( _ptr, _index, _field, _value )                        \
107         ( ( ( uint64_t) (_value) ) <<                                         \
108           QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
109
110 #define BIT_ASSEMBLE_2( _ptr, _index, _field, _value, ... )                   \
111         ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
112           BIT_ASSEMBLE_1 ( _ptr, _index, __VA_ARGS__ ) )
113
114 #define BIT_ASSEMBLE_3( _ptr, _index, _field, _value, ... )                   \
115         ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
116           BIT_ASSEMBLE_2 ( _ptr, _index, __VA_ARGS__ ) )
117
118 #define BIT_ASSEMBLE_4( _ptr, _index, _field, _value, ... )                   \
119         ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
120           BIT_ASSEMBLE_3 ( _ptr, _index, __VA_ARGS__ ) )
121
122 #define BIT_ASSEMBLE_5( _ptr, _index, _field, _value, ... )                   \
123         ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
124           BIT_ASSEMBLE_4 ( _ptr, _index, __VA_ARGS__ ) )
125
126 #define BIT_ASSEMBLE_6( _ptr, _index, _field, _value, ... )                   \
127         ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
128           BIT_ASSEMBLE_5 ( _ptr, _index, __VA_ARGS__ ) )
129
130 #define BIT_ASSEMBLE_7( _ptr, _index, _field, _value, ... )                   \
131         ( BIT_ASSEMBLE_1 ( _ptr, _index, _field, _value ) |                   \
132           BIT_ASSEMBLE_6 ( _ptr, _index, __VA_ARGS__ ) )
133
134 /*
135  * Build native-endian (positive) qword bitmasks from named fields
136  *
137  */
138
139 #define BIT_MASK_1( _ptr, _index, _field )                                    \
140         ( BIT_MASK ( _ptr, _field ) <<                                        \
141           QWORD_BIT_OFFSET ( _ptr, _index, _field ) )
142
143 #define BIT_MASK_2( _ptr, _index, _field, ... )                               \
144         ( BIT_MASK_1 ( _ptr, _index, _field ) |                               \
145           BIT_MASK_1 ( _ptr, _index, __VA_ARGS__ ) )
146
147 #define BIT_MASK_3( _ptr, _index, _field, ... )                               \
148         ( BIT_MASK_1 ( _ptr, _index, _field ) |                               \
149           BIT_MASK_2 ( _ptr, _index, __VA_ARGS__ ) )
150
151 #define BIT_MASK_4( _ptr, _index, _field, ... )                               \
152         ( BIT_MASK_1 ( _ptr, _index, _field ) |                               \
153           BIT_MASK_3 ( _ptr, _index, __VA_ARGS__ ) )
154
155 #define BIT_MASK_5( _ptr, _index, _field, ... )                               \
156         ( BIT_MASK_1 ( _ptr, _index, _field ) |                               \
157           BIT_MASK_4 ( _ptr, _index, __VA_ARGS__ ) )
158
159 #define BIT_MASK_6( _ptr, _index, _field, ... )                               \
160         ( BIT_MASK_1 ( _ptr, _index, _field ) |                               \
161           BIT_MASK_5 ( _ptr, _index, __VA_ARGS__ ) )
162
163 #define BIT_MASK_7( _ptr, _index, _field, ... )                               \
164         ( BIT_MASK_1 ( _ptr, _index, _field ) |                               \
165           BIT_MASK_6 ( _ptr, _index, __VA_ARGS__ ) )
166
167 /*
168  * Populate little-endian qwords from named fields and values
169  *
170  */
171
172 #define BIT_FILL( _ptr, _index, _assembled ) do {                             \
173                 uint64_t *__ptr = &(_ptr)->u.qwords[(_index)];                \
174                 uint64_t __assembled = (_assembled);                          \
175                 *__ptr = cpu_to_BIT64 ( __assembled );                        \
176         } while ( 0 )
177
178 #define BIT_FILL_1( _ptr, _field1, ... )                                      \
179         BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),                      \
180                    BIT_ASSEMBLE_1 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),     \
181                                     _field1, __VA_ARGS__ ) )
182
183 #define BIT_FILL_2( _ptr, _field1, ... )                                      \
184         BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),                      \
185                    BIT_ASSEMBLE_2 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),     \
186                                     _field1, __VA_ARGS__ ) )
187
188 #define BIT_FILL_3( _ptr, _field1, ... )                                      \
189         BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),                      \
190                    BIT_ASSEMBLE_3 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),     \
191                                     _field1, __VA_ARGS__ ) )
192
193 #define BIT_FILL_4( _ptr, _field1, ... )                                      \
194         BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),                      \
195                    BIT_ASSEMBLE_4 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),     \
196                                     _field1, __VA_ARGS__ ) )
197
198 #define BIT_FILL_5( _ptr, _field1, ... )                                      \
199         BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),                      \
200                    BIT_ASSEMBLE_5 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),     \
201                                     _field1, __VA_ARGS__ ) )
202
203 #define BIT_FILL_6( _ptr, _field1, ... )                                      \
204         BIT_FILL ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),                      \
205                    BIT_ASSEMBLE_6 ( _ptr, QWORD_OFFSET ( _ptr, _field1 ),     \
206                                     _field1, __VA_ARGS__ ) )
207
208 /** Extract value of named field */
209 #define BIT_GET64( _ptr, _field )                                             \
210         ( {                                                                   \
211                 unsigned int __index = QWORD_OFFSET ( _ptr, _field );         \
212                 uint64_t *__ptr = &(_ptr)->u.qwords[__index];                 \
213                 uint64_t __value = BIT64_to_cpu ( *__ptr );                   \
214                 __value >>=                                                   \
215                     QWORD_BIT_OFFSET ( _ptr, __index, _field );               \
216                 __value &= BIT_MASK ( _ptr, _field );                         \
217                 __value;                                                      \
218         } )
219
220 /** Extract value of named field (for fields up to the size of a long) */
221 #define BIT_GET( _ptr, _field )                                               \
222         ( ( unsigned long ) BIT_GET64 ( _ptr, _field ) )
223
224 #define BIT_SET( _ptr, _field, _value ) do {                                  \
225                 unsigned int __index = QWORD_OFFSET ( _ptr, _field );         \
226                 uint64_t *__ptr = &(_ptr)->u.qwords[__index];                 \
227                 unsigned int __shift =                                        \
228                         QWORD_BIT_OFFSET ( _ptr, __index, _field );           \
229                 uint64_t __value = (_value);                                  \
230                 *__ptr &= cpu_to_BIT64 ( ~( BIT_MASK ( _ptr, _field ) <<      \
231                                             __shift ) );                      \
232                 *__ptr |= cpu_to_BIT64 ( __value << __shift );                \
233         } while ( 0 )
234
235 #endif /* _IPXE_BITOPS_H */