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