95_bitfields.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #elif TEST == 2
  16. {
  17. struct M P __s
  18. {
  19. int x: 12;
  20. char y: 6;
  21. long long z:63;
  22. A char a:4;
  23. long long b:2;
  24. };
  25. TEST_STRUCT(3,30,0x123456789abcdef0LL,5,2);
  26. }
  27. /* ----------------------------------------------------------------------- */
  28. #elif TEST == 3
  29. {
  30. struct M P __s
  31. {
  32. unsigned x:5, y:5, :0, z:5; char a:5; A short b:5;
  33. };
  34. TEST_STRUCT(21,23,25,6,14);
  35. }
  36. /* ----------------------------------------------------------------------- */
  37. #elif TEST == 4
  38. {
  39. struct M P __s {
  40. int x : 3;
  41. int : 2;
  42. int y : 1;
  43. int : 0;
  44. int z : 5;
  45. int a : 7;
  46. unsigned int b : 7;
  47. };
  48. TEST_STRUCT(3,1,15,120,120);
  49. }
  50. /* ----------------------------------------------------------------------- */
  51. #elif TEST == 5
  52. {
  53. struct M P __s {
  54. long long x : 45;
  55. long long : 2;
  56. long long y : 30;
  57. unsigned long long z : 38;
  58. char a; short b;
  59. };
  60. TEST_STRUCT(0x123456789ULL, 120<<25, 120, 0x44, 0x77);
  61. }
  62. /* ----------------------------------------------------------------------- */
  63. #elif TEST == 6
  64. {
  65. struct M P __s {
  66. int a;
  67. signed char b;
  68. int x : 12, y : 4, : 0, : 4, z : 3;
  69. char d;
  70. };
  71. TEST_STRUCT(1,2,3,4,-3);
  72. }
  73. /* ----------------------------------------------------------------------- */
  74. #elif defined PACK
  75. #if PACK
  76. # pragma pack(push,1)
  77. # define P //_P
  78. #else
  79. # define P
  80. #endif
  81. printf("\n\n" + 2*top);
  82. #define TEST 1
  83. #include SELF
  84. top = 0;
  85. #define TEST 2
  86. #include SELF
  87. #define TEST 3
  88. #include SELF
  89. #define TEST 4
  90. #include SELF
  91. #define TEST 5
  92. #include SELF
  93. #define TEST 6
  94. #include SELF
  95. #if PACK
  96. # pragma pack(pop)
  97. #endif
  98. #undef P
  99. #undef PACK
  100. /* ----------------------------------------------------------------------- */
  101. #elif defined ALIGN
  102. #if ALIGN
  103. # define A _A(16)
  104. #else
  105. # define A
  106. #endif
  107. #define PACK 0
  108. #include SELF
  109. #define PACK 1
  110. #include SELF
  111. #undef A
  112. #undef ALIGN
  113. /* ----------------------------------------------------------------------- */
  114. #elif defined MS_BF
  115. #if MS_BF
  116. # ifdef __TINYC__
  117. # pragma comment(option, "-mms-bitfields")
  118. # elif defined __GNUC__
  119. # define M __attribute__((ms_struct))
  120. # endif
  121. #else
  122. # ifdef __TINYC__
  123. # pragma comment(option, "-mno-ms-bitfields")
  124. # elif defined __GNUC__
  125. # define M __attribute__((gcc_struct))
  126. # endif
  127. #endif
  128. #ifndef M
  129. # define M
  130. #endif
  131. #define ALIGN 0
  132. #include SELF
  133. #define ALIGN 1
  134. #include SELF
  135. #undef M
  136. #undef MS_BF
  137. /* ----------------------------------------------------------------------- */
  138. #else
  139. #include <stdio.h>
  140. #include <string.h>
  141. /* some gcc headers #define __attribute__ to empty if it's not gcc */
  142. #undef __attribute__
  143. void dump(void *p, int s)
  144. {
  145. int i;
  146. for (i = s; --i >= 0;)
  147. printf("%02X", ((unsigned char*)p)[i]);
  148. printf("\n");
  149. }
  150. #define pv(m) \
  151. printf(sizeof (s->m + 0) == 8 ? " %016llx" : " %02x", s->m)
  152. #define TEST_STRUCT(v1,v2,v3,v4,v5) { \
  153. struct __s _s, *s = & _s; \
  154. printf("\n---- TEST %d%s%s%s ----\n" + top, \
  155. TEST, MS_BF?" - MS-BITFIELDS":"", \
  156. PACK?" - PACKED":"", \
  157. ALIGN?" - WITH ALIGN":""); \
  158. memset(s, 0, sizeof *s); \
  159. s->x = -1, s->y = -1, s->z = -1, s->a = -1, s->b = -1; \
  160. printf("bits in use : "), dump(s, sizeof *s); \
  161. s->x = v1, s->y = v2, s->z = v3, s->a += v4, ++s->a, s->b = v5; \
  162. printf("bits as set : "), dump(s, sizeof *s); \
  163. printf("values :"), pv(x), pv(y), pv(z), pv(a), pv(b), printf("\n"); \
  164. printf("align/size : %d %d\n", alignof(struct __s),sizeof(struct __s)); \
  165. }
  166. #ifdef _MSC_VER
  167. # define _A(n) __declspec(align(n))
  168. # define _P
  169. # define alignof(x) __alignof(x)
  170. #else
  171. # define _A(n) __attribute__((aligned(n)))
  172. # define _P __attribute__((packed))
  173. # define alignof(x) __alignof__(x)
  174. #endif
  175. #ifndef MS_BITFIELDS
  176. # define MS_BITFIELDS 0
  177. #endif
  178. #define SELF "95_bitfields.c"
  179. int top = 1;
  180. int main()
  181. {
  182. #define MS_BF MS_BITFIELDS
  183. #include SELF
  184. return 0;
  185. }
  186. /* ----------------------------------------------------------------------- */
  187. #endif
  188. #undef TEST