1 /* ----------------------------------------------------------------------- */
12 TEST_STRUCT(0x333,0x44,0x555555,6,7);
15 /* ----------------------------------------------------------------------- */
27 TEST_STRUCT(3,30,0x123456789abcdef0LL
,5,2);
30 /* ----------------------------------------------------------------------- */
35 unsigned x
:5, y
:5, :0, z
:5; char a
:5; A
short b
:5;
37 TEST_STRUCT(21,23,25,6,14);
40 /* ----------------------------------------------------------------------- */
52 TEST_STRUCT(3,1,15,120,120);
55 /* ----------------------------------------------------------------------- */
62 unsigned long long z
: 38;
65 TEST_STRUCT(0x123456789ULL
, 120<<25, 120, 0x44, 0x77);
68 /* ----------------------------------------------------------------------- */
74 int x
: 12, y
: 4, : 0, : 4, z
: 3;
77 TEST_STRUCT(1,2,3,4,-3);
80 /* ----------------------------------------------------------------------- */
84 typedef long long int ll
;
98 TEST_STRUCT(1,2,3,4,5);
101 /* ----------------------------------------------------------------------- */
105 # pragma pack(push,1)
111 printf("\n\n" + 2*top
);
135 /* ----------------------------------------------------------------------- */
152 /* ----------------------------------------------------------------------- */
157 # pragma comment(option, "-mms-bitfields")
158 # elif defined __GNUC__
159 # define M __attribute__((ms_struct))
163 # pragma comment(option, "-mno-ms-bitfields")
164 # elif defined __GNUC__
165 # define M __attribute__((gcc_struct))
180 /* ----------------------------------------------------------------------- */
185 /* some gcc headers #define __attribute__ to empty if it's not gcc */
188 void dump(void *p
, int s
)
191 for (i
= s
; --i
>= 0;)
192 printf("%02X", ((unsigned char*)p
)[i
]);
197 printf(sizeof (s->m + 0) == 8 ? " %016llx" : " %02x", s->m)
199 #define TEST_STRUCT(v1,v2,v3,v4,v5) { \
200 struct __s _s, *s = & _s; \
201 printf("\n---- TEST %d%s%s%s ----\n" + top, \
202 TEST, MS_BF?" - MS-BITFIELDS":"", \
203 PACK?" - PACKED":"", \
204 ALIGN?" - WITH ALIGN":""); \
205 memset(s, 0, sizeof *s); \
206 s->x = -1, s->y = -1, s->z = -1, s->a = -1, s->b = -1; \
207 printf("bits in use : "), dump(s, sizeof *s); \
208 s->x = v1, s->y = v2, s->z = v3, s->a += v4, ++s->a, s->b = v5; \
209 printf("bits as set : "), dump(s, sizeof *s); \
210 printf("values :"), pv(x), pv(y), pv(z), pv(a), pv(b), printf("\n"); \
211 printf("align/size : %d %d\n", alignof(struct __s),sizeof(struct __s)); \
215 # define _A(n) __declspec(align(n))
217 # define alignof(x) __alignof(x)
219 # define _A(n) __attribute__((aligned(n)))
220 # define _P __attribute__((packed))
221 # define alignof(x) __alignof__(x)
225 # define MS_BITFIELDS 0
228 #define SELF "95_bitfields.c"
234 #define MS_BF MS_BITFIELDS
239 /* ----------------------------------------------------------------------- */