modernc.org/ccgo/v3@v3.16.14/lib/testdata/tcc-0.9.27/tests/tests2/95_bitfields.c (about)

     1  /* ----------------------------------------------------------------------- */
     2  #if TEST == 1
     3  {
     4      struct M P A __s
     5      {
     6          unsigned x : 12;
     7          unsigned char y : 7;
     8          unsigned z : 28;
     9          unsigned a: 4;
    10          unsigned b: 5;
    11      };
    12      TEST_STRUCT(0x333,0x44,0x555555,6,7);
    13  }
    14  
    15  /* ----------------------------------------------------------------------- */
    16  #elif TEST == 2
    17  {
    18      struct M P __s
    19      {
    20          int x: 12;
    21          char y: 6;
    22          long long z:63;
    23          A char a:4;
    24          long long b:2;
    25  
    26      };
    27      TEST_STRUCT(3,30,0x123456789abcdef0LL,5,2);
    28  }
    29  
    30  /* ----------------------------------------------------------------------- */
    31  #elif TEST == 3
    32  {
    33      struct M P __s
    34      {
    35          unsigned x:5, y:5, :0, z:5; char a:5; A short b:5;
    36      };
    37      TEST_STRUCT(21,23,25,6,14);
    38  }
    39  
    40  /* ----------------------------------------------------------------------- */
    41  #elif TEST == 4
    42  {
    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;
    51      };
    52      TEST_STRUCT(3,1,15,120,120);
    53  }
    54  
    55  /* ----------------------------------------------------------------------- */
    56  #elif TEST == 5
    57  {
    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;
    64      };
    65      TEST_STRUCT(0x123456789ULL, 120<<25, 120, 0x44, 0x77);
    66  }
    67  
    68  /* ----------------------------------------------------------------------- */
    69  #elif TEST == 6
    70  {
    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;
    76      };
    77      TEST_STRUCT(1,2,3,4,-3);
    78  }
    79  
    80  /* ----------------------------------------------------------------------- */
    81  #elif defined PACK
    82  
    83  #if PACK
    84  # pragma pack(push,1)
    85  # define P //_P
    86  #else
    87  # define P
    88  #endif
    89  
    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
   104  
   105  #if PACK
   106  # pragma pack(pop)
   107  #endif
   108  
   109  #undef P
   110  #undef PACK
   111  
   112  /* ----------------------------------------------------------------------- */
   113  #elif defined ALIGN
   114  
   115  #if ALIGN
   116  # define A _A(16)
   117  #else
   118  # define A
   119  #endif
   120  
   121  #define PACK 0
   122  #include SELF
   123  #define PACK 1
   124  #include SELF
   125  
   126  #undef A
   127  #undef ALIGN
   128  
   129  /* ----------------------------------------------------------------------- */
   130  #elif defined MS_BF
   131  
   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
   148  
   149  #define ALIGN 0
   150  #include SELF
   151  #define ALIGN 1
   152  #include SELF
   153  
   154  #undef M
   155  #undef MS_BF
   156  
   157  /* ----------------------------------------------------------------------- */
   158  #else
   159  
   160  #include <stdio.h>
   161  #include <string.h>
   162  /* some gcc headers #define __attribute__ to empty if it's not gcc */
   163  #undef __attribute__
   164  
   165  void dump(void *p, int s)
   166  {
   167      int i;
   168      for (i = s; --i >= 0;)
   169          printf("%02X", ((unsigned char*)p)[i]);
   170      printf("\n");
   171  }
   172  
   173  #define pv(m) \
   174      printf(sizeof (s->m + 0) == 8 ? " %016llx" : " %02x", s->m)
   175  
   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)); \
   189      }
   190  
   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
   200  
   201  #ifndef MS_BITFIELDS
   202  # define MS_BITFIELDS 0
   203  #endif
   204  
   205  #define SELF "95_bitfields.c"
   206  
   207  int top = 1;
   208  
   209  int main()
   210  {
   211  #define MS_BF MS_BITFIELDS
   212  #include SELF
   213      return 0;
   214  }
   215  
   216  /* ----------------------------------------------------------------------- */
   217  #endif
   218  #undef TEST