view test/bitfield.c @ 336:d488b72254fb

bit-field done.
author kono
date Fri, 25 Jun 2004 03:56:48 +0900
parents
children 7fe7ce0a791f
line wrap: on
line source



typedef union {
    struct _PTE {
	    unsigned long v:1;
	    unsigned long vsid:24;
	    unsigned long h:1;
	    unsigned long api:6;
	    unsigned long rpn:20;

	    unsigned long :3;
	    unsigned long r:1;
	    unsigned long c:1;
	    unsigned long w:1;
	    unsigned long i:1;
	    unsigned long m:1;
	    unsigned long g:1;
	    unsigned long :1;
	    unsigned long pp:2;
    } pte;
    struct {
	    unsigned char v:1;
	    signed long vsid:24;
	    unsigned long h:1;
	    signed char api:6;
	    signed long rpn:20;

	    signed long :3;
	    unsigned long r:1;
	    unsigned long c:1;
	    unsigned long w:1;
	    unsigned long i:1;
	    unsigned long m:1;
	    unsigned long g:1;
	    unsigned long :1;
	    signed long pp:2;
    } sg;
    unsigned long u[2];
} PTE;

PTE g;

main()
{
       PTE a,*p;
      int i = 0;
      int j = 0;

      a.pte.rpn = 55;
      j = a.pte.rpn;
printf("% 3d:  %d\n",i++,j);
      a.pte.c = 1;
      j = a.pte.c;
printf("% 3d:  %d\n",i++,j);
      a.pte.pp = -1;
      j = a.pte.pp;
printf("% 3d:  %d\n",i++,j);
      a.sg.pp = -1;
      j = a.sg.pp;
printf("% 3d:  %d\n",i++,j);

      p = &g;

      g.u[0]=0;
      g.u[1]=0;
       
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. v = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. vsid = -1;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. h = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. api = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. rpn = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.pte. r = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. c = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. w = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. i = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. m = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. g = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. pp = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

      g.u[0]=-1;
      g.u[1]=-1;

printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. v = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. vsid = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. h = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. api = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. rpn = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.pte. r = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. c = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. w = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. i = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. m = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. g = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. pp = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

      g.u[0]=-1;
      g.u[1]=-1;

printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. v = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. vsid = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. h = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. api = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. rpn = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.pte. r = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. c = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. w = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. i = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. m = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. g = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. pp = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

      g.u[0]=-1;
      g.u[1]=-1;

for(i=0;i<0x200;i++) {
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. v = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. vsid = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. h = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. api = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. rpn = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.pte. r = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. c = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. w = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. i = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. m = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. g = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.pte. pp = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
}

      g = a;

      printf("%d %d %d\n",a.pte.pp,g.pte.pp,p->pte.pp);
     main2();
     main5();
    return 0;
}

main2()
{
      int i = 0;

      g.u[0]=0;
      g.u[1]=0;
       
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. v = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. vsid = -1;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. h = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. api = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. rpn = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.sg. r = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. c = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. w = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. i = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. m = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. g = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. pp = 3;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

      g.u[0]=-1;
      g.u[1]=-1;

printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. v = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. vsid = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. h = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. api = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. rpn = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.sg. r = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. c = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. w = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. i = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. m = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. g = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. pp = 0;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

      g.u[0]=-1;
      g.u[1]=-1;

printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. v = 0x55;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. vsid = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. h = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. api = 0x55;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. rpn = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.sg. r = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. c = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. w = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. i = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. m = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. g = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. pp = 0x5555555;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

      g.u[0]=-1;
      g.u[1]=-1;

for(i=-0x100;i<0x100;i++) {
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. v = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. vsid = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. h = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. api = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. rpn = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);

        g.sg. r = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. c = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. w = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. i = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. m = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. g = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
        g.sg. pp = i;
printf("% 3d:  0x%08x%08x\n",i++,g.u[0],g.u[1]);
}

// printf("size of %d\n",sizeof(g.sg.pp));    no size of for bit-field

}

//  int :12         bit-field cannot be a return value;
//  main3( int a:8 ) bit-field cannot be an argument;
main3( int a )
{
     // int b:9;       // nor local variable
     int b;


     g.pte.rpn--;
     g.pte.rpn++;
     --g.pte.rpn;
     ++g.pte.rpn;
     g.pte.rpn += 3;
     g.pte.rpn -= 3;
     g.pte.rpn *= 3;
     g.pte.rpn /= 3;
     g.pte.rpn %= 3;
     g.pte.rpn |= 3;
     g.pte.rpn &= 3;
     g.pte.rpn ^= 3;

     return --b;
}




union ll1 {
   int a[8];
   struct {
        char a:4;
	long long v:33;
	long long w:33;
	long long x:33;
   } b;
} ll1;

union ll0 {
   int a[8];
   struct {
        char a:4;
	long long v:48;
	long long w:48;
	long long x:48;
   } b;
} ll0;


union ll {
   int a[8];
   struct {
        char a:4;
	long long v:56;
	long long w:56;
	long long x:56;
   } b;
} ll;

union cc {
   int a;
   struct {
	char a:1;
	char b:4;
	char c:7;
	char d:4;
	char e:4;
	char f:4;
   } b;
} cc;

union ii {
   int a;
   struct {
	int a:1;
	int b:4;
	int c:4;
	int d:4;
	int e:4;
	int f:4;
    } b;
} ii;

int m1 = -1;
int p1 = 1;
int zero = 0;

main5()
{
     ii.b.a = -1;
     printf("00:%d\n",ii.b.a);

     ll1.b.v = m1;
     printf("01:%llx\n",ll1.b.v);
     printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3],
	ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7]
      );
     ll1.b.v = zero;
     ll1.b.w = m1;
     printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3],
	ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7]
      );
     ll1.b.w = zero;
     ll1.b.x = m1;
     printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll1.a[0],ll1.a[1],ll1.a[2],ll1.a[3],
	ll1.a[4],ll1.a[5],ll1.a[6],ll1.a[7]
      );

     ll0.b.v = m1;
     printf("01:%llx\n",ll0.b.v);
     printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3],
	ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7]
      );
     ll0.b.v = zero;
     ll0.b.w = m1;
     printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3],
	ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7]
      );
     ll0.b.w = zero;
     ll0.b.x = m1;
     printf("02:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll0.a[0],ll0.a[1],ll0.a[2],ll0.a[3],
	ll0.a[4],ll0.a[5],ll0.a[6],ll0.a[7]
      );

     ll.b.v = m1;
     printf("1:%llx\n",ll.b.v);
     printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll.a[0],ll.a[1],ll.a[2],ll.a[3],
	ll.a[4],ll.a[5],ll.a[6],ll.a[7]
      );
     ll.b.v = zero;
     ll.b.w = m1;
     printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll.a[0],ll.a[1],ll.a[2],ll.a[3],
	ll.a[4],ll.a[5],ll.a[6],ll.a[7]
      );
     ll.b.w = zero;
     ll.b.x = m1;
     printf("2:%08x %08x %08x %08x %08x %08x %08x %08x\n",
	ll.a[0],ll.a[1],ll.a[2],ll.a[3],
	ll.a[4],ll.a[5],ll.a[6],ll.a[7]
      );

     printf("char a:1; char b:4; char c:7; char d:4; char e:4; char f:4;\n");
     cc.b.a = m1;
     printf("a:%08x\n",cc.a);
     cc.b.b = m1;
     printf("b:%08x\n",cc.a);
     cc.b.c = m1;
     printf("c:%08x\n",cc.a);
     cc.b.d = m1;
     printf("d:%08x\n",cc.a);
     cc.b.e = m1;
     printf("e:%08x\n",cc.a);
     cc.b.f = m1;
     printf("f:%08x\n",cc.a);
     printf("3:%d %d\n",cc.b.c,cc.b.d);
     cc.a = m1;
     printf("f:%08x\n",cc.a);
     cc.b.a = zero;
     printf("a:%08x\n",cc.a);
     cc.b.b = zero;
     printf("b:%08x\n",cc.a);
     cc.b.c = zero;
     printf("c:%08x\n",cc.a);
     cc.b.d = zero;
     printf("d:%08x\n",cc.a);
     cc.b.e = zero;
     printf("e:%08x\n",cc.a);
     cc.b.f = zero;
     printf("f:%08x\n",cc.a);
     printf("3:%d %d\n",cc.b.c,cc.b.d);
     cc.a = zero;
     printf("f:%08x\n",cc.a);
     cc.b.a = p1;
     printf("a:%08x\n",cc.a);
     cc.b.b = p1;
     printf("b:%08x\n",cc.a);
     cc.b.c = p1;
     printf("c:%08x\n",cc.a);
     cc.b.d = p1;
     printf("d:%08x\n",cc.a);
     cc.b.e = p1;
     printf("e:%08x\n",cc.a);
     cc.b.f = p1;
     printf("f:%08x\n",cc.a);
     printf("3:%d %d\n",cc.b.c,cc.b.d);
     cc.a = m1;
     printf("f:%08x\n",cc.a);
     cc.b.a = p1;
     printf("a:%08x\n",cc.a);
     cc.b.b = p1;
     printf("b:%08x\n",cc.a);
     cc.b.c = p1;
     printf("c:%08x\n",cc.a);
     cc.b.d = p1;
     printf("d:%08x\n",cc.a);
     cc.b.e = p1;
     printf("e:%08x\n",cc.a);
     cc.b.f = p1;
     printf("f:%08x\n",cc.a);
     printf("3:%d %d\n",cc.b.c,cc.b.d);

     ii.b.a = m1;
     printf("6:%08x\n",ii.a);
     ii.b.d = m1;
     printf("6:%08x\n",ii.a);
     printf("5:%d %d\n",ii.b.a,ii.b.d);
}