| 1 |
/* { dg-do run } */ |
| 2 |
|
| 3 |
typedef unsigned char uint8_t; |
| 4 |
typedef unsigned int uint32_t; |
| 5 |
|
| 6 |
#define __big_endian_attr__ scalar_storage_order("big-endian") |
| 7 |
#define __little_endian_attr__ scalar_storage_order("little-endian") |
| 8 |
|
| 9 |
typedef union |
| 10 |
{ |
| 11 |
uint32_t val; |
| 12 |
uint8_t v[4]; |
| 13 |
} __attribute__((__big_endian_attr__)) upal_u32be_t; |
| 14 |
|
| 15 |
typedef union |
| 16 |
{ |
| 17 |
uint32_t val; |
| 18 |
uint8_t v[4]; |
| 19 |
} __attribute__((__little_endian_attr__)) upal_u32le_t; |
| 20 |
|
| 21 |
static inline uint32_t native_to_big_endian(uint32_t t) |
| 22 |
{ |
| 23 |
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 24 |
return t; |
| 25 |
#else |
| 26 |
return __builtin_bswap32(t); |
| 27 |
#endif |
| 28 |
} |
| 29 |
static inline uint32_t native_to_little_endian(uint32_t t) |
| 30 |
{ |
| 31 |
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 32 |
return __builtin_bswap32(t); |
| 33 |
#else |
| 34 |
return t; |
| 35 |
#endif |
| 36 |
} |
| 37 |
#define test(p, p1, i) do { if (p[i] != p1[i]) __builtin_abort (); } while (0) |
| 38 |
|
| 39 |
#define tests(p, p1) do { test(p, p1, 0); test(p, p1, 1); \ |
| 40 |
test(p, p1, 2); test(p, p1, 3); } while (0) |
| 41 |
|
| 42 |
int main(void) |
| 43 |
{ |
| 44 |
const uint32_t u = 0x12345678; |
| 45 |
|
| 46 |
upal_u32be_t tempb; |
| 47 |
__builtin_memcpy (&tempb, &u, sizeof(uint32_t)); |
| 48 |
uint32_t bu = tempb.val; |
| 49 |
uint32_t b1u = native_to_big_endian(u); |
| 50 |
tests (((uint8_t*)&bu), ((uint8_t*)&b1u)); |
| 51 |
|
| 52 |
upal_u32le_t templ; |
| 53 |
__builtin_memcpy (&templ, &u, sizeof(uint32_t)); |
| 54 |
uint32_t lu = templ.val; |
| 55 |
uint32_t l1u = native_to_little_endian(u); |
| 56 |
tests (((uint8_t*)&lu), ((uint8_t*)&l1u)); |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |