work on local extern declarations
[tinycc.git] / tests / tests2 / 95_bitfields.c
blobf025c575d0eab0c8b3e78bf53878004cbcfb6654
1 /* ----------------------------------------------------------------------- */
2 #if TEST == 1
4 struct M P A __s
6 unsigned x : 12;
7 unsigned char y : 7;
8 unsigned z : 28;
9 unsigned a: 4;
10 unsigned b: 5;
12 TEST_STRUCT(0x333,0x44,0x555555,6,7);
15 /* ----------------------------------------------------------------------- */
16 #elif TEST == 2
18 struct M P __s
20 int x: 12;
21 char y: 6;
22 long long z:63;
23 A char a:4;
24 long long b:2;
27 TEST_STRUCT(3,30,0x123456789abcdef0LL,5,2);
30 /* ----------------------------------------------------------------------- */
31 #elif TEST == 3
33 struct M P __s
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 /* ----------------------------------------------------------------------- */
41 #elif TEST == 4
43 struct M P __s {
44 int x : 3;
45 int : 2;
46 int y : 1;
47 int : 0;
48 int z : 5;
49 int a : 7;
50 unsigned int b : 7;
52 TEST_STRUCT(3,1,15,120,120);
55 /* ----------------------------------------------------------------------- */
56 #elif TEST == 5
58 struct M P __s {
59 long long x : 45;
60 long long : 2;
61 long long y : 30;
62 unsigned long long z : 38;
63 char a; short b;
65 TEST_STRUCT(0x123456789ULL, 120<<25, 120, 0x44, 0x77);
68 /* ----------------------------------------------------------------------- */
69 #elif TEST == 6
71 struct M P __s {
72 int a;
73 signed char b;
74 int x : 12, y : 4, : 0, : 4, z : 3;
75 char d;
77 TEST_STRUCT(1,2,3,4,-3);
80 /* ----------------------------------------------------------------------- */
81 #elif defined PACK
83 #if PACK
84 # pragma pack(push,1)
85 # define P //_P
86 #else
87 # define P
88 #endif
90 printf("\n\n" + 2*top);
91 #define TEST 1
92 #include SELF
93 top = 0;
94 #define TEST 2
95 #include SELF
96 #define TEST 3
97 #include SELF
98 #define TEST 4
99 #include SELF
100 #define TEST 5
101 #include SELF
102 #define TEST 6
103 #include SELF
105 #if PACK
106 # pragma pack(pop)
107 #endif
109 #undef P
110 #undef PACK
112 /* ----------------------------------------------------------------------- */
113 #elif defined ALIGN
115 #if ALIGN
116 # define A _A(16)
117 #else
118 # define A
119 #endif
121 #define PACK 0
122 #include SELF
123 #define PACK 1
124 #include SELF
126 #undef A
127 #undef ALIGN
129 /* ----------------------------------------------------------------------- */
130 #elif defined MS_BF
132 #if MS_BF
133 # ifdef __TINYC__
134 # pragma comment(option, "-mms-bitfields")
135 # elif defined __GNUC__
136 # define M __attribute__((ms_struct))
137 # endif
138 #else
139 # ifdef __TINYC__
140 # pragma comment(option, "-mno-ms-bitfields")
141 # elif defined __GNUC__
142 # define M __attribute__((gcc_struct))
143 # endif
144 #endif
145 #ifndef M
146 # define M
147 #endif
149 #define ALIGN 0
150 #include SELF
151 #define ALIGN 1
152 #include SELF
154 #undef M
155 #undef MS_BF
157 /* ----------------------------------------------------------------------- */
158 #else
160 #include <stdio.h>
161 #include <string.h>
162 /* some gcc headers #define __attribute__ to empty if it's not gcc */
163 #undef __attribute__
165 void dump(void *p, int s)
167 int i;
168 for (i = s; --i >= 0;)
169 printf("%02X", ((unsigned char*)p)[i]);
170 printf("\n");
173 #define pv(m) \
174 printf(sizeof (s->m + 0) == 8 ? " %016llx" : " %02x", s->m)
176 #define TEST_STRUCT(v1,v2,v3,v4,v5) { \
177 struct __s _s, *s = & _s; \
178 printf("\n---- TEST %d%s%s%s ----\n" + top, \
179 TEST, MS_BF?" - MS-BITFIELDS":"", \
180 PACK?" - PACKED":"", \
181 ALIGN?" - WITH ALIGN":""); \
182 memset(s, 0, sizeof *s); \
183 s->x = -1, s->y = -1, s->z = -1, s->a = -1, s->b = -1; \
184 printf("bits in use : "), dump(s, sizeof *s); \
185 s->x = v1, s->y = v2, s->z = v3, s->a += v4, ++s->a, s->b = v5; \
186 printf("bits as set : "), dump(s, sizeof *s); \
187 printf("values :"), pv(x), pv(y), pv(z), pv(a), pv(b), printf("\n"); \
188 printf("align/size : %d %d\n", alignof(struct __s),sizeof(struct __s)); \
191 #ifdef _MSC_VER
192 # define _A(n) __declspec(align(n))
193 # define _P
194 # define alignof(x) __alignof(x)
195 #else
196 # define _A(n) __attribute__((aligned(n)))
197 # define _P __attribute__((packed))
198 # define alignof(x) __alignof__(x)
199 #endif
201 #ifndef MS_BITFIELDS
202 # define MS_BITFIELDS 0
203 #endif
205 #define SELF "95_bitfields.c"
207 int top = 1;
209 int main()
211 #define MS_BF MS_BITFIELDS
212 #include SELF
213 return 0;
216 /* ----------------------------------------------------------------------- */
217 #endif
218 #undef TEST