1 /* Test semantics of #pragma pack.
2 Contributed by Mike Coleman <mcoleman2@kc.rr.com> */
4 /* { dg-do compile { target { ! default_packed } } } */
6 /* Mainly we're just testing whether pushing and popping seem to be
7 working correctly, and verifying the (alignment == 1) case, which
8 is really the only reason anyone would use this pragma anyway. */
12 /* generalized compile-time test expression */
13 #define test(n, expr) int test_##n [(expr) ? 1 : -1]
15 /* Round V down to multiple of A */
16 #define floor(v,a) ((v) / (a) * (a))
18 /* Offset of field with alignment A in structure S after a field P of
20 #define offset(s,p,pt,a) \
21 floor ((offsetof(struct s, p) + sizeof (pt) + (a) - 1), a)
24 #define min(a,b) ((a) < (b) ? (a) : (b))
26 /* Check that field A (type AT) followed by field B (type BT) are
27 packed according to P */
28 #define test_pack(n, a, at, b, bt, p) \
29 test(n, offsetof (struct SNAME, b) \
30 == min (offset (SNAME,a,at,__alignof__(bt)), \
31 offset (SNAME,a,at,p)))
33 /* Test offset of field F in structs s1 and s2 are the same. */
34 #define test_offset(n, s1, s2, f) \
35 test (n, (offsetof(struct s1, f) == offsetof(struct s2, f)))
38 #include "pack-test-1.h"
42 #pragma pack(push, p1, 1)
43 #include "pack-test-1.h"
46 test_pack(0, f0
, char, f1
, double, 1);
47 test_pack(1, f2
, short, f3
, double, 1);
48 test_pack(2, f4
, int, f5
, double, 1);
53 #pragma pack(push, p2, 2)
54 #include "pack-test-1.h"
57 test_pack(0, f0
, char, f1
, double, 2);
58 test_pack(1, f2
, short, f3
, double, 2);
59 test_pack(2, f4
, int, f5
, double, 2);
64 #pragma pack(push, p3, 4)
65 #include "pack-test-1.h"
68 test_pack(0, f0
, char, f1
, double, 4);
69 test_pack(1, f2
, short, f3
, double, 4);
70 test_pack(2, f4
, int, f5
, double, 4);
76 #include "pack-test-1.h"
79 test_pack(0, f0
, char, f1
, double, 2);
80 test_pack(1, f2
, short, f3
, double, 2);
81 test_pack(2, f4
, int, f5
, double, 2);
87 #include "pack-test-1.h"
90 test_pack(0, f0
, char, f1
, double, 1);
91 test_pack(1, f2
, short, f3
, double, 1);
92 test_pack(2, f4
, int, f5
, double, 1);
98 #include "pack-test-1.h"
101 test_offset (0, s0
, SNAME
, f0
);
102 test_offset (1, s0
, SNAME
, f1
);
103 test_offset (2, s0
, SNAME
, f2
);
104 test_offset (3, s0
, SNAME
, f3
);
105 test_offset (4, s0
, SNAME
, f4
);
106 test_offset (5, s0
, SNAME
, f5
);
112 #include "pack-test-1.h"
115 test_pack(0, f0
, char, f1
, double, 1);
116 test_pack(1, f2
, short, f3
, double, 1);
117 test_pack(2, f4
, int, f5
, double, 1);
122 #pragma pack(push, p2, 2)
123 #include "pack-test-1.h"
126 test_pack(0, f0
, char, f1
, double, 2);
127 test_pack(1, f2
, short, f3
, double, 2);
128 test_pack(2, f4
, int, f5
, double, 2);
134 #include "pack-test-1.h"
137 test_pack(0, f0
, char, f1
, double, 1);
138 test_pack(1, f2
, short, f3
, double, 1);
139 test_pack(2, f4
, int, f5
, double, 1);
145 #include "pack-test-1.h"
148 test_offset (0, s0
, SNAME
, f0
);
149 test_offset (1, s0
, SNAME
, f1
);
150 test_offset (2, s0
, SNAME
, f2
);
151 test_offset (3, s0
, SNAME
, f3
);
152 test_offset (4, s0
, SNAME
, f4
);
153 test_offset (5, s0
, SNAME
, f5
);