| 1 |
// PR tree-optimization/94403 |
| 2 |
// Only test on some 64-bit targets which do have bswap{si,di}2 patterns and |
| 3 |
// are either big or little endian (not pdp endian). |
| 4 |
// { dg-do compile { target { lp64 && { i?86-*-* x86_64-*-* powerpc*-*-* aarch64*-*-* } } } } |
| 5 |
// { dg-require-effective-target store_merge } |
| 6 |
// { dg-options "-O2 -fno-tree-vectorize -fdump-tree-store-merging -std=c++17" } |
| 7 |
|
| 8 |
namespace std { |
| 9 |
template <typename T> |
| 10 |
void swap (T& t1, T& t2) { T tmp (t1); t1 = t2; t2 = tmp; } |
| 11 |
enum class byte : unsigned char {}; |
| 12 |
} |
| 13 |
|
| 14 |
unsigned |
| 15 |
bswap32 (unsigned int x) |
| 16 |
{ |
| 17 |
unsigned int y = x; |
| 18 |
std::byte *bytes = reinterpret_cast<std::byte*> (&y); |
| 19 |
std::swap (bytes[0], bytes[3]); |
| 20 |
std::swap (bytes[1], bytes[2]); |
| 21 |
return y; |
| 22 |
} |
| 23 |
|
| 24 |
unsigned long long |
| 25 |
bswap64 (unsigned long long x) |
| 26 |
{ |
| 27 |
unsigned long long y = x; |
| 28 |
std::byte *bytes = reinterpret_cast<std::byte*> (&y); |
| 29 |
std::swap (bytes[0], bytes[7]); |
| 30 |
std::swap (bytes[1], bytes[6]); |
| 31 |
std::swap (bytes[2], bytes[5]); |
| 32 |
std::swap (bytes[3], bytes[4]); |
| 33 |
return y; |
| 34 |
} |
| 35 |
|
| 36 |
/* { dg-final { scan-tree-dump-times "__builtin_bswap64" 1 "store-merging" } } */ |
| 37 |
/* { dg-final { scan-tree-dump-times "__builtin_bswap32" 1 "store-merging" } } */ |