-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbitfields.c
417 lines (369 loc) · 12 KB
/
tbitfields.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
/*
* Bit-fields and their order in structs.
*
* From C99:
*
* An implementation may allocate any addressable storage unit large
* enough to hold a bit-field.
*
* If enough space remains, a bit-field that immediately follows another
* bit-field in a structure shall be packed into adjacent bits of the same
* unit.
*
* If insufficient space remains, whether a bit-field that does not fit is
* put into the next unit or overlaps adjacent units is
* implementation-defined.
*
* Some architectures like SPARC and Motorola 68000 use "LSB 0" bit numbering,
* while S/390, PowerPC and PA-RISC use "MSB 0". Dunno about their bit-fields
* though.
*
* GCC's manual discusses the layout of bit-fields in structures, mentioning
* the sections of the relevant C standards which say that this aspect of C is
* "implementation defined", and then says how GCC implements the feature:
*
* * `The order of allocation of bit-fields within a unit (C90 6.5.2.1,
* C99 6.7.2.1).'
*
* Determined by ABI.
*
* Some choices depend on the externally determined ABI for the platform
* which GCC follows
*
* Some compilers, e.g. reportedly Metrowerks CodeWarrior, support a "#pragma
* reverse_bitfields" directive that will reverse the order of the bits in a
* bit-field from the order that would otherwise have applied.
*
* The COSMIC compiler for HC12/HCS12 microcontrollers defines __CSMC__ to a
* value with 6 or more bits, of which bit #4 is set if the "reverse bitfield
* option (was) specified (+rev)". (Apparently it normally uses LtoH
* ordering.)
*
* The LCC 4.x Code-Generation Interface Manual by Microsoft for MIPS says that
* for bit field layout effectively ignores any platform ABI requirements:
*
* bit numbers start at zero with the least significant bit. This bit
* field representation does not depend on the target's endiannes; the
* same representation is used for both big and little endians.
*
* Apparently Microsoft C allocates bit fields opposite to byte endian-ness?
*
* The SH-4 Generic and C Specific ABI says:
*
* In the little-endian ABI, bit-fields are allocated from right to left
* (least to most significant) within a storage unit. In the big-endian
* ABI, bit-fields are allocated from left to right (most to least
* significant) within a storage unit.
*
* The UNIX System V ABI for PowerPC says that the CPU may be used in either
* Little Endian mode or Big Endian mode, and with respect to bit-fields:
*
* Bit-fields are allocated from right to left (least to most significant)
* on Little-Endian implementations and from left to right (most to least
* significant) on Big-Endian implementations.
*
* For i386, the UNIX System V ABI says:
*
* Bit-fields are allocated from right to left (least to most significant).
*
* The UNIX System V ABI for MIPS gives only one valid bit-field layout:
*
* Bit-fields are allocated from left to right (most to least significant).
*
* The UNIX System V AMD64 ABI says:
*
* bit-fields are allocated from right to left
*
* The LINUX ABI for IA64 syas:
*
* Bit-fields are allocated from right to left (least to most significant).
*
* For all Sun Sparc:
*
* The C compiler assigns bit fields from the high to the low end of an
* int (most to least significant).
*
* However for all Sun systems we can get the official value directly:
*/
#if defined(__sun__)
# include <sys/isa_defs.h>
#endif
#if !(defined(_BIT_FIELDS_LTOH) || defined(_BIT_FIELDS_HTOL))
/*
* First try by CPU based on compiler defines....
*/
# if defined(sun3) || defined(mc68000) || \
defined(sun4) || defined(__sparc) || defined(sparc) || \
defined(__hppa) || defined(_ARCH_PPC) || defined(_IBMR2)
# define _BIT_FIELDS_HTOL
# endif
# if defined(__sgi) && defined(__mips)
# define _BIT_FIELDS_HTOL
# endif
# if defined(mips) && defined(__LCC__) /* xxx not sure "mips" is correct */
# define _BIT_FIELDS_LTOH
# endif
# if defined(__i386__) || defined(__i386) || defined(i386) || \
defined(__alpha__) || defined(__alpha) || defined(alpha) || \
defined(__arm__) || defined(__arm) || defined(arm)
# define _BIT_FIELDS_LTOH
# endif
#endif /* !(_BIT_FIELDS_LTOH || _BIT_FIELDS_HTOL) */
#if 1 /* !(defined(_BIT_FIELDS_LTOH) || defined(_BIT_FIELDS_HTOL)) */ /* endian hacks */
/*
* If that didn't work then try using platform-specific compile-time endian
* discovery is a last-ditch effort. This will likely only work on hosted
* implementations.
*/
/* newer GCC & Clang will already have these defined... */
# if !(defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__))
# if !(defined(__BYTE_ORDER__) && \
defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__))
# if defined(linux) || defined(__linux__) || \
defined (__gnu_linux__) || defined(__linux)
# include <endian.h>
# endif
# if defined(BSD) || \
defined(__NetBSD__) || \
defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__Darwin__) || \
defined(__DragonFly__)
# include <sys/param.h> /* also <sys/types.h>??? */
# include <machine/endian.h>
# endif
# ifdef __SCO__
# include <sys/types.h>
# include <sys/byteorder.h>
# endif
# ifdef __aix__
# include <sys/machine.h>
# endif
# ifdef __hpux
# include <machine/param.h>
# endif
# ifdef OS2
# include <machine/endian.h>
# endif
# ifdef SVR4
# if defined(NCR) || defined(Mips) || defined(__sgi)
# include <sys/endian.h>
# else
# if !defined(sun)
# include <sys/byteorder.h>
# endif
# endif
# endif
/* the above #includes should define one of the following... */
# if !defined(__ORDER_LITTLE_ENDIAN__) && defined(_LITTLE_ENDIAN)
# define __ORDER_LITTLE_ENDIAN__ _LITTLE_ENDIAN
# endif
# if !defined(__ORDER_BIG_ENDIAN__) && defined(_BIG_ENDIAN)
# define __ORDER_BIG_ENDIAN__ _BIG_ENDIAN
# endif
# if !defined(__BYTE_ORDER__) && defined(_BYTE_ORDER)
# define __BYTE_ORDER__ _BYTE_ORDER
# endif
# if !defined(__ORDER_LITTLE_ENDIAN__) && defined(__LITTLE_ENDIAN)
# define __ORDER_LITTLE_ENDIAN__ __LITTLE_ENDIAN
# endif
# if !defined(__ORDER_BIG_ENDIAN__) && defined(__BIG_ENDIAN)
# define __ORDER_BIG_ENDIAN__ __BIG_ENDIAN
# endif
# if !defined(__BYTE_ORDER__) && defined(__BYTE_ORDER)
# define __BYTE_ORDER__ __BYTE_ORDER
# endif
/*
* If we still dont know the target byte order, then we try one last effort.
*
* This is based on compiler CPU macros, so for bit-field determination it's no
* better than those above, but for code wanting to know the word endianness...
*/
# ifndef __BYTE_ORDER__
/*
* note: these are the values GCC & Clang use....
*/
# ifndef __ORDER_LITTLE_ENDIAN__
# define __ORDER_LITTLE_ENDIAN__ 1234
# endif
# ifndef __ORDER_BIG_ENDIAN__
# define __ORDER_BIG_ENDIAN__ 4321
# endif
# if defined(__QNX__) && !defined(__QNXNTO__)
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
# endif /* __QNX__ */
# if defined(__QNXNTO__)
# if defined(i386) || defined(__i386__) || defined(__x86__)
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
# else
# define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
# endif
# endif /* __QNXNTO__ */
# ifdef Lynx
# if defined(i386) || defined(__i386__) || defined(__x86__)
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
# else
# define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
# endif
# endif /* Lynx */
# if !defined(__BYTE_ORDER__) && defined(_LITTLE_ENDIAN)
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
# endif
# if !defined(__BYTE_ORDER__) && defined(_BIG_ENDIAN)
# define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
# endif
# endif /* __BYTE_ORDER__ */
# endif /* !(__BYTE_ORDER__ && __ORDER_LITTLE_ENDIAN__ && __ORDER_BIG_ENDIAN__)) */
# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define __BIG_ENDIAN__
# elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define __LITTLE_ENDIAN__
# endif
# endif /* !(__LITTLE_ENDIAN__) || __BIG_ENDIAN__) */
/* XXX just make some assumptions */
# if defined(__LITTLE_ENDIAN__)
# define _BIT_FIELDS_LTOH
# elif defined(__BIG_ENDIAN__)
# define _BIT_FIELS_HTOL
# endif
# if !(defined(_BIT_FIELDS_LTOH) || defined(_BIT_FIELDS_HTOL))
# include "one of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined"
# endif
#endif /* endian hacks */
/*
* now a simple mnemonic name to represent the first bit's position....
*
* It must use the same ordinal numbering scheme as ffs(3), and this allows an
* undefined value to be represented by 0.
*/
enum {
#ifdef _BIT_FIELDS_LTOH
BITFIELDS_FIRST_BIT = 1
#elif defined(_BIT_FIELDS_HTOL)
BITFIELDS_FIRST_BIT = WORD_BIT
#else
BITFIELDS_FIRST_BIT = 0
#endif
};
union bitfield_test {
struct a_bit {
unsigned int first_bit:1;
unsigned int other_bits:(WORD_BIT-2);
unsigned int last_bit:1;
} the_bits;
unsigned int the_int;
};
static volatile uint32_t l = 0x01020304UL;
volatile uint8_t *p = (volatile uint8_t *) &l;
int main(void);
int
main()
{
union bitfield_test tb;
bool little_bit_first = false;
bool big_bit_first = false;
unsigned int i;
assert(WORD_BIT == sizeof(unsigned int) * CHAR_BIT);
tb.the_int = 0;
tb.the_bits.first_bit = 1;
putchar('\n');
printf("first_bit = %d (int = %u, aka 0x%8.8x)\n",
ffs((int) tb.the_int), tb.the_int, tb.the_int);
if (ffs((int) tb.the_int) != BITFIELDS_FIRST_BIT) {
printf("Compile-time detection says it should be %d\n", BITFIELDS_FIRST_BIT);
}
little_bit_first = (ffs((int) tb.the_int) == 1);
tb.the_int = 0;
tb.the_bits.last_bit = 1;
printf("last_bit = %d (int = %u, aka 0x%8.8x)\n",
ffs((int) tb.the_int), tb.the_int, tb.the_int);
big_bit_first = (ffs((int) tb.the_int) == 1);
if (little_bit_first && big_bit_first) {
printf("Houston, we have a problem.... (PDP-11?)\n");
}
if (!little_bit_first && !big_bit_first) {
printf("Houston, we have an even bigger problem....\n");
}
if (tb.the_bits.other_bits != 0) {
printf("It's full of stars....\n");
}
printf("LSB = %d\n", ffs(1));
i = 1U << (WORD_BIT - 1);
printf("MSB = %d\n", ffs((int) i));
if (little_bit_first) {
printf("#define _BIT_FIELDS_LTOH\t1\n");
#ifndef _BIT_FIELDS_LTOH
printf("Hmmm... but that was _not_ seen at compile time!\n");
#endif
} else if (big_bit_first) {
printf("#define _BIT_FIELDS_HTOL\t1\n");
#ifndef _BIT_FIELDS_HTOL
printf("Hmmm... but that was _not_ seen at compile time!\n");
#endif
}
putchar('\n');
switch (*p) {
case 0x01:
printf("BIG-Endian\n");
if (little_bit_first) {
printf("hmmmmm... bits & bytes don't match!\n");
}
#ifndef __BIG_ENDIAN__
printf("Hmmm... BIG-Endian _not_ detected at compile time!\n");
#endif
#ifdef __LITTLE_ENDIAN__
printf("OH OH! LITTLE-Endian detected at compile time!!!\n");
#endif
break;
case 0x04:
printf("LITTLE-Endian\n");
if (big_bit_first) {
printf("hmmmmm... bits & bytes don't match!\n");
}
#ifndef __LITTLE_ENDIAN__
printf("Hmmm... LITTLE-Endian _not_ detected at compile time!\n");
#endif
#ifdef __BIG_ENDIAN__
printf("OH OH! BIG-Endian detected at compile time!!!\n");
#endif
break;
case 0x02:
printf("PDP-11???? Impossible?\n");
#ifdef __BIG_ENDIAN__
printf("OH OH! BIG-Endian detected at compile time!!!\n");
#endif
#ifdef __LITTLE_ENDIAN__
printf("OH OH! LITTLE-Endian detected at compile time!!!\n");
#endif
break;
default:
printf("WTF!?!?!?\n");
break;
}
/*
* and now for comparison, print the bytes in the integer 'l' via *p
*
* remember, from the global scope:
*
* static volatile uint32_t l = 0x01020304UL;
* volatile uint8_t *p = (volatile uint8_t *) &l;
*/
printf("l = 0x%8.8x\n", l);
for (i = 0; i < (int) sizeof(l); i++) {
printf("l[%i] = 0x%02x%s", i, *p++, i == sizeof(l)-1 ? "\n" : ", ");
}
exit(0);
}
/*
* Local Variables:
* eval: (make-local-variable 'compile-command)
* compile-command: (let ((fn (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))) (concat "rm -f " fn "; " (default-value 'compile-command) " " fn " && ./" fn))
* End:
*/