1 /* { dg-do compile { target arm*-*-* alpha*-*-* ia64*-*-* x86_64-*-* s390x-*-* powerpc*-*-* rs6000-*-* } } */
2 /* { dg-require-effective-target stdint_types } */
3 /* { dg-require-effective-target lp64 } */
4 /* { dg-options "-O2 -fdump-tree-bswap" } */
5
6 #include <stdint.h>
7 #define __const_swab64(x) ((uint64_t)( \
8 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
9 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
10 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
11 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
12 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
13 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
14 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
15 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
16
17
18 /* This byte swap implementation is used by the Linux kernel and the
19 GNU C library. */
20
21 uint64_t
swap64(uint64_t in)22 swap64 (uint64_t in)
23 {
24 return __const_swab64 (in);
25 }
26
27 /* This variant is currently used by libgcc. The difference is that
28 the bswap source and destination have a signed integer type which
29 requires a slightly higher search depth in order to dive through
30 the cast as well. */
31
32 typedef int DItype __attribute__ ((mode (DI)));
33 DItype
swap64_b(DItype u)34 swap64_b (DItype u)
35 {
36 return ((((u) & 0xff00000000000000ull) >> 56)
37 | (((u) & 0x00ff000000000000ull) >> 40)
38 | (((u) & 0x0000ff0000000000ull) >> 24)
39 | (((u) & 0x000000ff00000000ull) >> 8)
40 | (((u) & 0x00000000ff000000ull) << 8)
41 | (((u) & 0x0000000000ff0000ull) << 24)
42 | (((u) & 0x000000000000ff00ull) << 40)
43 | (((u) & 0x00000000000000ffull) << 56));
44 }
45
46 /* The OpenSSL variant. */
47
48 uint64_t
swap64_c(uint64_t x)49 swap64_c (uint64_t x)
50 {
51 uint32_t a = x >> 32;
52 uint32_t b = (uint32_t) x;
53 return ((uint64_t) ((((((b)) >> (8)) | (((b)) << (32 - (8)))) & 0xff00ff00L)
54 | (((((b)) << (8)) | (((b)) >> (32 - (8)))) & 0x00ff00ffL)) << 32)
55 | (uint64_t) ((((((a)) >> (8)) | (((a)) << (32 - (8)))) & 0xff00ff00L)
56 | (((((a)) << (8)) | (((a)) >> (32 - (8)))) & 0x00ff00ffL));
57 }
58
59
60 /* { dg-final { scan-tree-dump-times "64 bit bswap implementation found at" 3 "bswap" } } */
61 /* { dg-final { cleanup-tree-dump "bswap" } } */
62