Various fixes to cbfstool.
[coreboot.git] / util / cbfstool / swab.h
1 #ifndef _SWAB_H
2 #define _SWAB_H
3
4 /*
5  * linux/byteorder/swab.h
6  * Byte-swapping, independently from CPU endianness
7  *      swabXX[ps]?(foo)
8  *
9  * Francois-Rene Rideau <fare@tunes.org> 19971205
10  *    separated swab functions from cpu_to_XX,
11  *    to clean up support for bizarre-endian architectures.
12  *
13  * See asm-i386/byteorder.h and suches for examples of how to provide
14  * architecture-dependent optimized versions
15  *
16  */
17
18 /* casts are necessary for constants, because we never know how for sure
19  * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
20  */
21 #define swab16(x) \
22         ((unsigned short)( \
23                 (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
24                 (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
25
26 #define swab32(x) \
27         ((unsigned int)( \
28                 (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
29                 (((unsigned int)(x) & (unsigned int)0x0000ff00UL) <<  8) | \
30                 (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >>  8) | \
31                 (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
32
33 #define swab64(x) \
34         ((uint64_t)( \
35                 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
36                 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
37                 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
38                 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
39                 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
40                 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
41                 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
42                 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) ))
43
44 #endif /* _SWAB_H */