comparison test/bitfield.c @ 336:d488b72254fb

bit-field done.
author kono
date Fri, 25 Jun 2004 03:56:48 +0900
parents
children 7fe7ce0a791f
comparison
equal deleted inserted replaced
335:4f98dc4b5fd8 336:d488b72254fb
1
2
3 typedef union {
4 struct _PTE {
5 unsigned long v:1;
6 unsigned long vsid:24;
7 unsigned long h:1;
8 unsigned long api:6;
9 unsigned long rpn:20;
10
11 unsigned long :3;
12 unsigned long r:1;
13 unsigned long c:1;
14 unsigned long w:1;
15 unsigned long i:1;
16 unsigned long m:1;
17 unsigned long g:1;
18 unsigned long :1;
19 unsigned long pp:2;
20 } pte;
21 struct {
22 unsigned char v:1;
23 signed long vsid:24;
24 unsigned long h:1;
25 signed char api:6;
26 signed long rpn:20;
27
28 signed long :3;
29 unsigned long r:1;
30 unsigned long c:1;
31 unsigned long w:1;
32 unsigned long i:1;
33 unsigned long m:1;
34 unsigned long g:1;
35 unsigned long :1;
36 signed long pp:2;
37 } sg;
38 unsigned long u[2];
39 } PTE;
40
41 PTE g;
42
43 main()
44 {
45 PTE a,*p;
46 int i = 0;
47 int j = 0;
48
49 a.pte.rpn = 55;
50 j = a.pte.rpn;
51 printf("% 3d: %d\n",i++,j);
52 a.pte.c = 1;
53 j = a.pte.c;
54 printf("% 3d: %d\n",i++,j);
55 a.pte.pp = -1;
56 j = a.pte.pp;
57 printf("% 3d: %d\n",i++,j);
58 a.sg.pp = -1;
59 j = a.sg.pp;
60 printf("% 3d: %d\n",i++,j);
61
62 p = &g;
63
64 g.u[0]=0;
65 g.u[1]=0;
66
67 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
68 g.pte. v = 3;
69 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
70 g.pte. vsid = -1;
71 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
72 g.pte. h = 3;
73 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
74 g.pte. api = 3;
75 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
76 g.pte. rpn = 3;
77 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
78
79 g.pte. r = 3;
80 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
81 g.pte. c = 3;
82 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
83 g.pte. w = 3;
84 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
85 g.pte. i = 3;
86 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
87 g.pte. m = 3;
88 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
89 g.pte. g = 3;
90 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
91 g.pte. pp = 3;
92 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
93
94 g.u[0]=-1;
95 g.u[1]=-1;
96
97 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
98 g.pte. v = 0;
99 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
100 g.pte. vsid = 0;
101 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
102 g.pte. h = 0;
103 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
104 g.pte. api = 0;
105 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
106 g.pte. rpn = 0;
107 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
108
109 g.pte. r = 0;
110 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
111 g.pte. c = 0;
112 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
113 g.pte. w = 0;
114 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
115 g.pte. i = 0;
116 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
117 g.pte. m = 0;
118 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
119 g.pte. g = 0;
120 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
121 g.pte. pp = 0;
122 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
123
124 g.u[0]=-1;
125 g.u[1]=-1;
126
127 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
128 g.pte. v = 0x5555555;
129 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
130 g.pte. vsid = 0x5555555;
131 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
132 g.pte. h = 0x5555555;
133 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
134 g.pte. api = 0x5555555;
135 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
136 g.pte. rpn = 0x5555555;
137 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
138
139 g.pte. r = 0x5555555;
140 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
141 g.pte. c = 0x5555555;
142 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
143 g.pte. w = 0x5555555;
144 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
145 g.pte. i = 0x5555555;
146 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
147 g.pte. m = 0x5555555;
148 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
149 g.pte. g = 0x5555555;
150 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
151 g.pte. pp = 0x5555555;
152 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
153
154 g.u[0]=-1;
155 g.u[1]=-1;
156
157 for(i=0;i<0x200;i++) {
158 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
159 g.pte. v = i;
160 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
161 g.pte. vsid = i;
162 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
163 g.pte. h = i;
164 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
165 g.pte. api = i;
166 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
167 g.pte. rpn = i;
168 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
169
170 g.pte. r = i;
171 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
172 g.pte. c = i;
173 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
174 g.pte. w = i;
175 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
176 g.pte. i = i;
177 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
178 g.pte. m = i;
179 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
180 g.pte. g = i;
181 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
182 g.pte. pp = i;
183 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
184 }
185
186 g = a;
187
188 printf("%d %d %d\n",a.pte.pp,g.pte.pp,p->pte.pp);
189 main2();
190 main5();
191 return 0;
192 }
193
194 main2()
195 {
196 int i = 0;
197
198 g.u[0]=0;
199 g.u[1]=0;
200
201 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
202 g.sg. v = 3;
203 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
204 g.sg. vsid = -1;
205 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
206 g.sg. h = 3;
207 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
208 g.sg. api = 3;
209 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
210 g.sg. rpn = 3;
211 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
212
213 g.sg. r = 3;
214 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
215 g.sg. c = 3;
216 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
217 g.sg. w = 3;
218 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
219 g.sg. i = 3;
220 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
221 g.sg. m = 3;
222 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
223 g.sg. g = 3;
224 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
225 g.sg. pp = 3;
226 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
227
228 g.u[0]=-1;
229 g.u[1]=-1;
230
231 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
232 g.sg. v = 0;
233 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
234 g.sg. vsid = 0;
235 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
236 g.sg. h = 0;
237 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
238 g.sg. api = 0;
239 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
240 g.sg. rpn = 0;
241 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
242
243 g.sg. r = 0;
244 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
245 g.sg. c = 0;
246 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
247 g.sg. w = 0;
248 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
249 g.sg. i = 0;
250 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
251 g.sg. m = 0;
252 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
253 g.sg. g = 0;
254 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
255 g.sg. pp = 0;
256 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
257
258 g.u[0]=-1;
259 g.u[1]=-1;
260
261 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
262 g.sg. v = 0x55;
263 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
264 g.sg. vsid = 0x5555555;
265 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
266 g.sg. h = 0x5555555;
267 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
268 g.sg. api = 0x55;
269 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
270 g.sg. rpn = 0x5555555;
271 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
272
273 g.sg. r = 0x5555555;
274 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
275 g.sg. c = 0x5555555;
276 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
277 g.sg. w = 0x5555555;
278 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
279 g.sg. i = 0x5555555;
280 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
281 g.sg. m = 0x5555555;
282 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
283 g.sg. g = 0x5555555;
284 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
285 g.sg. pp = 0x5555555;
286 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
287
288 g.u[0]=-1;
289 g.u[1]=-1;
290
291 for(i=-0x100;i<0x100;i++) {
292 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
293 g.sg. v = i;
294 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
295 g.sg. vsid = i;
296 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
297 g.sg. h = i;
298 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
299 g.sg. api = i;
300 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
301 g.sg. rpn = i;
302 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
303
304 g.sg. r = i;
305 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
306 g.sg. c = i;
307 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
308 g.sg. w = i;
309 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
310 g.sg. i = i;
311 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
312 g.sg. m = i;
313 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
314 g.sg. g = i;
315 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
316 g.sg. pp = i;
317 printf("% 3d: 0x%08x%08x\n",i++,g.u[0],g.u[1]);
318 }
319
320 // printf("size of %d\n",sizeof(g.sg.pp)); no size of for bit-field
321
322 }
323
324 // int :12 bit-field cannot be a return value;
325 // main3( int a:8 ) bit-field cannot be an argument;
326 main3( int a )
327 {
328 // int b:9; // nor local variable
329 int b;
330
331
332 g.pte.rpn--;
333 g.pte.rpn++;
334 --g.pte.rpn;
335 ++g.pte.rpn;
336 g.pte.rpn += 3;
337 g.pte.rpn -= 3;
338 g.pte.rpn *= 3;
339 g.pte.rpn /= 3;
340 g.pte.rpn %= 3;
341 g.pte.rpn |= 3;
342 g.pte.rpn &= 3;
343 g.pte.rpn ^= 3;
344
345 return --b;
346 }
347
348
349
350
351 union ll1 {
352 int a[8];
353 struct {
354 char a:4;
355 long long v:33;
356 long long w:33;
357 long long x:33;
358 } b;
359 } ll1;
360
361 union ll0 {
362 int a[8];
363 struct {
364 char a:4;
365 long long v:48;
366 long long w:48;
367 long long x:48;
368 } b;
369 } ll0;
370
371
372 union ll {
373 int a[8];
374 struct {
375 char a:4;
376 long long v:56;
377 long long w:56;
378 long long x:56;
379 } b;
380 } ll;
381
382 union cc {
383 int a;
384 struct {
385 char a:1;
386 char b:4;
387 char c:7;
388 char d:4;
389 char e:4;
390 char f:4;
391 } b;
392 } cc;
393
394 union ii {
395 int a;
396 struct {
397 int a:1;
398 int b:4;
399 int c:4;
400 int d:4;
401 int e:4;
402 int f:4;
403 } b;
404 } ii;
405
406 int m1 = -1;
407 int p1 = 1;
408 int zero = 0;
409
410 main5()
411 {
412 ii.b.a = -1;
413 printf("00:%d\n",ii.b.a);
414
415 ll1.b.v = m1;
416 printf("01:%llx\n",ll1.b.v);
417 printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
418 ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3],
419 ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7]
420 );
421 ll1.b.v = zero;
422 ll1.b.w = m1;
423 printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
424 ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3],
425 ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7]
426 );
427 ll1.b.w = zero;
428 ll1.b.x = m1;
429 printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
430 ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3],
431 ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7]
432 );
433
434 ll0.b.v = m1;
435 printf("01:%llx\n",ll0.b.v);
436 printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
437 ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3],
438 ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7]
439 );
440 ll0.b.v = zero;
441 ll0.b.w = m1;
442 printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
443 ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3],
444 ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7]
445 );
446 ll0.b.w = zero;
447 ll0.b.x = m1;
448 printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
449 ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3],
450 ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7]
451 );
452
453 ll.b.v = m1;
454 printf("1:%llx\n",ll.b.v);
455 printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n",
456 ll.a[0],ll.a[1],ll.a[2],ll.a[3],
457 ll.a[4],ll.a[5],ll.a[6],ll.a[7]
458 );
459 ll.b.v = zero;
460 ll.b.w = m1;
461 printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n",
462 ll.a[0],ll.a[1],ll.a[2],ll.a[3],
463 ll.a[4],ll.a[5],ll.a[6],ll.a[7]
464 );
465 ll.b.w = zero;
466 ll.b.x = m1;
467 printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n",
468 ll.a[0],ll.a[1],ll.a[2],ll.a[3],
469 ll.a[4],ll.a[5],ll.a[6],ll.a[7]
470 );
471
472 printf("char a:1; char b:4; char c:7; char d:4; char e:4; char f:4;\n");
473 cc.b.a = m1;
474 printf("a:%08x\n",cc.a);
475 cc.b.b = m1;
476 printf("b:%08x\n",cc.a);
477 cc.b.c = m1;
478 printf("c:%08x\n",cc.a);
479 cc.b.d = m1;
480 printf("d:%08x\n",cc.a);
481 cc.b.e = m1;
482 printf("e:%08x\n",cc.a);
483 cc.b.f = m1;
484 printf("f:%08x\n",cc.a);
485 printf("3:%d %d\n",cc.b.c,cc.b.d);
486 cc.a = m1;
487 printf("f:%08x\n",cc.a);
488 cc.b.a = zero;
489 printf("a:%08x\n",cc.a);
490 cc.b.b = zero;
491 printf("b:%08x\n",cc.a);
492 cc.b.c = zero;
493 printf("c:%08x\n",cc.a);
494 cc.b.d = zero;
495 printf("d:%08x\n",cc.a);
496 cc.b.e = zero;
497 printf("e:%08x\n",cc.a);
498 cc.b.f = zero;
499 printf("f:%08x\n",cc.a);
500 printf("3:%d %d\n",cc.b.c,cc.b.d);
501 cc.a = zero;
502 printf("f:%08x\n",cc.a);
503 cc.b.a = p1;
504 printf("a:%08x\n",cc.a);
505 cc.b.b = p1;
506 printf("b:%08x\n",cc.a);
507 cc.b.c = p1;
508 printf("c:%08x\n",cc.a);
509 cc.b.d = p1;
510 printf("d:%08x\n",cc.a);
511 cc.b.e = p1;
512 printf("e:%08x\n",cc.a);
513 cc.b.f = p1;
514 printf("f:%08x\n",cc.a);
515 printf("3:%d %d\n",cc.b.c,cc.b.d);
516 cc.a = m1;
517 printf("f:%08x\n",cc.a);
518 cc.b.a = p1;
519 printf("a:%08x\n",cc.a);
520 cc.b.b = p1;
521 printf("b:%08x\n",cc.a);
522 cc.b.c = p1;
523 printf("c:%08x\n",cc.a);
524 cc.b.d = p1;
525 printf("d:%08x\n",cc.a);
526 cc.b.e = p1;
527 printf("e:%08x\n",cc.a);
528 cc.b.f = p1;
529 printf("f:%08x\n",cc.a);
530 printf("3:%d %d\n",cc.b.c,cc.b.d);
531
532 ii.b.a = m1;
533 printf("6:%08x\n",ii.a);
534 ii.b.d = m1;
535 printf("6:%08x\n",ii.a);
536 printf("5:%d %d\n",ii.b.a,ii.b.d);
537 }
538
539