1 /* { dg-do run } */
2 /* { dg-require-effective-target stdint_types } */
3 /* { dg-options "-Wall" } */
4
5 #include <stdint.h>
6
7 #define MAKE_FUN(suffix, type) \
8 type my_bswap##suffix(type x) { \
9 type result = 0; \
10 int shift; \
11 for (shift = 0; shift < 8 * sizeof (type); shift += 8) \
12 { \
13 result <<= 8; \
14 result |= (x >> shift) & 0xff; \
15 } \
16 return result; \
17 } \
18
19 MAKE_FUN(16, uint16_t);
20 MAKE_FUN(32, uint32_t);
21 MAKE_FUN(64, uint64_t);
22
23 extern void abort (void);
24
25 #define NUMS16 \
26 { \
27 0x0000, \
28 0x1122, \
29 0xffff, \
30 }
31
32 #define NUMS32 \
33 { \
34 0x00000000UL, \
35 0x11223344UL, \
36 0xffffffffUL, \
37 }
38
39 #define NUMS64 \
40 { \
41 0x0000000000000000ULL, \
42 0x1122334455667788ULL, \
43 0xffffffffffffffffULL, \
44 }
45
46 uint16_t uint16_ts[] =
47 NUMS16;
48
49 uint32_t uint32_ts[] =
50 NUMS32;
51
52 uint64_t uint64_ts[] =
53 NUMS64;
54
55 #define N(table) (sizeof (table) / sizeof (table[0]))
56
57 int
main(void)58 main (void)
59 {
60 int i;
61
62 for (i = 0; i < N(uint16_ts); i++)
63 if (__builtin_bswap16 (uint16_ts[i]) != my_bswap16 (uint16_ts[i]))
64 abort ();
65
66 for (i = 0; i < N(uint32_ts); i++)
67 if (__builtin_bswap32 (uint32_ts[i]) != my_bswap32 (uint32_ts[i]))
68 abort ();
69
70 for (i = 0; i < N(uint64_ts); i++)
71 if (__builtin_bswap64 (uint64_ts[i]) != my_bswap64 (uint64_ts[i]))
72 abort ();
73
74 return 0;
75 }
76