1 #include <stdio.h>
2 #include <string.h>
3 
4 
5 void
dumpbits(void * p,int size)6 dumpbits(void *p, int size) {
7         unsigned char   *ucp = p;
8         int             i;
9 
10         for (i = 0; i < size; ++i) {
11                 printf("%02x ", *ucp++);
12         }
13         putchar('\n');
14 }
15 
16 /*
17  * 06/01/11: This test case exposes the shocking and very fundamental
18  * (and as of yet)  * unknown fact that bitfield storage is completely
19  * wrong for PowerPC and possibly all other big endian targets.
20  *
21  * "y" ends up being stored in bits that are already occupied by "x"
22  * on that (or those) platform(s). There are a few storage layout test
23  * cases in the test suite, but they probably weren't rigorous enough
24  * to detect this.
25  *
26  * What should happen in this test is that x comes first, followed by
27  * y - with endianness making a difference as to where the most
28  * significant bits of "x" are stored on little/big endian systems
29  * (y always lives in a byte, endianness-independently)
30  *
31  * Ideally all bitfield storage asssignment code should be rewritten
32  * because it is laughably complex for such a (supposedly) simple
33  * task
34  */
35 
36 int
main()37 main() {
38         struct foo {
39                 unsigned x:30;
40                 unsigned y:2;
41         } f;
42         unsigned z;
43 
44         memset(&f, 0, sizeof f);
45         f.x = 123;
46         dumpbits(&f, sizeof f);
47         memset(&f, 0, sizeof f);
48         f.y = 1;
49         dumpbits(&f, sizeof f);
50 }
51 
52