xref: /openbmc/qemu/pc-bios/s390-ccw/bswap.h (revision fbc8ea1ed0704b0cd393e5dc429816a36cb5ed05)
1*824df3b8SThomas Huth /*
2*824df3b8SThomas Huth  * Byte swap functions - taken from include/qemu/bswap.h
3*824df3b8SThomas Huth  *
4*824df3b8SThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or (at
5*824df3b8SThomas Huth  * your option) any later version. See the COPYING file in the top-level
6*824df3b8SThomas Huth  * directory.
7*824df3b8SThomas Huth  */
8*824df3b8SThomas Huth 
bswap16(uint16_t x)9*824df3b8SThomas Huth static inline uint16_t bswap16(uint16_t x)
10*824df3b8SThomas Huth {
11*824df3b8SThomas Huth     return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
12*824df3b8SThomas Huth }
13*824df3b8SThomas Huth 
bswap32(uint32_t x)14*824df3b8SThomas Huth static inline uint32_t bswap32(uint32_t x)
15*824df3b8SThomas Huth {
16*824df3b8SThomas Huth     return ((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) <<  8) |
17*824df3b8SThomas Huth            ((x & 0x00ff0000U) >>  8) | ((x & 0xff000000U) >> 24);
18*824df3b8SThomas Huth }
19*824df3b8SThomas Huth 
bswap64(uint64_t x)20*824df3b8SThomas Huth static inline uint64_t bswap64(uint64_t x)
21*824df3b8SThomas Huth {
22*824df3b8SThomas Huth     return ((x & 0x00000000000000ffULL) << 56) |
23*824df3b8SThomas Huth            ((x & 0x000000000000ff00ULL) << 40) |
24*824df3b8SThomas Huth            ((x & 0x0000000000ff0000ULL) << 24) |
25*824df3b8SThomas Huth            ((x & 0x00000000ff000000ULL) <<  8) |
26*824df3b8SThomas Huth            ((x & 0x000000ff00000000ULL) >>  8) |
27*824df3b8SThomas Huth            ((x & 0x0000ff0000000000ULL) >> 24) |
28*824df3b8SThomas Huth            ((x & 0x00ff000000000000ULL) >> 40) |
29*824df3b8SThomas Huth            ((x & 0xff00000000000000ULL) >> 56);
30*824df3b8SThomas Huth }
31