PR tree-optimization/81384 - built-in form of strnlen missing
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / bswap-2.c
blob63e7807d3d92f317942da9b5b9eb516c4bdbac5d
1 /* { dg-require-effective-target int32plus } */
3 #ifdef __UINT32_TYPE__
4 typedef __UINT32_TYPE__ uint32_t;
5 #else
6 typedef __UINT32_TYPE__ unsigned;
7 #endif
9 struct bitfield {
10 unsigned char f0:7;
11 unsigned char :1;
12 unsigned char f1:7;
13 unsigned char :1;
14 unsigned char f2:7;
15 unsigned char :1;
16 unsigned char f3:7;
19 struct ok {
20 unsigned char f0;
21 unsigned char f1;
22 unsigned char f2;
23 unsigned char f3;
26 union bf_or_uint32 {
27 struct ok inval;
28 struct bitfield bfval;
31 __attribute__ ((noinline, noclone)) uint32_t
32 partial_read_le32 (union bf_or_uint32 in)
34 return in.bfval.f0 | (in.bfval.f1 << 8)
35 | (in.bfval.f2 << 16) | (in.bfval.f3 << 24);
38 __attribute__ ((noinline, noclone)) uint32_t
39 partial_read_be32 (union bf_or_uint32 in)
41 return in.bfval.f3 | (in.bfval.f2 << 8)
42 | (in.bfval.f1 << 16) | (in.bfval.f0 << 24);
45 __attribute__ ((noinline, noclone)) uint32_t
46 fake_read_le32 (char *x, char *y)
48 unsigned char c0, c1, c2, c3;
50 c0 = x[0];
51 c1 = x[1];
52 *y = 1;
53 c2 = x[2];
54 c3 = x[3];
55 return c0 | c1 << 8 | c2 << 16 | c3 << 24;
58 __attribute__ ((noinline, noclone)) uint32_t
59 fake_read_be32 (char *x, char *y)
61 unsigned char c0, c1, c2, c3;
63 c0 = x[0];
64 c1 = x[1];
65 *y = 1;
66 c2 = x[2];
67 c3 = x[3];
68 return c3 | c2 << 8 | c1 << 16 | c0 << 24;
71 __attribute__ ((noinline, noclone)) uint32_t
72 incorrect_read_le32 (char *x, char *y)
74 unsigned char c0, c1, c2, c3;
76 c0 = x[0];
77 c1 = x[1];
78 c2 = x[2];
79 c3 = x[3];
80 *y = 1;
81 return c0 | c1 << 8 | c2 << 16 | c3 << 24;
84 __attribute__ ((noinline, noclone)) uint32_t
85 incorrect_read_be32 (char *x, char *y)
87 unsigned char c0, c1, c2, c3;
89 c0 = x[0];
90 c1 = x[1];
91 c2 = x[2];
92 c3 = x[3];
93 *y = 1;
94 return c3 | c2 << 8 | c1 << 16 | c0 << 24;
97 int
98 main ()
100 union bf_or_uint32 bfin;
101 uint32_t out;
102 char cin[] = { 0x83, 0x85, 0x87, 0x89 };
104 if (sizeof (uint32_t) * __CHAR_BIT__ != 32)
105 return 0;
106 bfin.inval = (struct ok) { 0x83, 0x85, 0x87, 0x89 };
107 out = partial_read_le32 (bfin);
108 /* Test what bswap would do if its check are not strict enough instead of
109 what is the expected result as there is too many possible results with
110 bitfields. */
111 if (out == 0x89878583)
112 __builtin_abort ();
113 bfin.inval = (struct ok) { 0x83, 0x85, 0x87, 0x89 };
114 out = partial_read_be32 (bfin);
115 /* Test what bswap would do if its check are not strict enough instead of
116 what is the expected result as there is too many possible results with
117 bitfields. */
118 if (out == 0x83858789)
119 __builtin_abort ();
120 out = fake_read_le32 (cin, &cin[2]);
121 if (out != 0x89018583)
122 __builtin_abort ();
123 cin[2] = 0x87;
124 out = fake_read_be32 (cin, &cin[2]);
125 if (out != 0x83850189)
126 __builtin_abort ();
127 cin[2] = 0x87;
128 out = incorrect_read_le32 (cin, &cin[2]);
129 if (out != 0x89878583)
130 __builtin_abort ();
131 cin[2] = 0x87;
132 out = incorrect_read_be32 (cin, &cin[2]);
133 if (out != 0x83858789)
134 __builtin_abort ();
135 return 0;