blob: 8a7dabaa0a2ca7b9e729a0320048525e3e6fc993 [file] [log] [blame]
Greg Watson4f566ca2004-03-13 03:37:11 +00001#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 *
Martin Roth0cb07e32013-07-09 21:46:01 -060013 * See asm-i386/byteorder.h and such for examples of how to provide
Greg Watson4f566ca2004-03-13 03:37:11 +000014 * 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
Stefan Reinauer9ea33e92011-10-14 15:11:16 -070033#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
Greg Watson4f566ca2004-03-13 03:37:11 +000044#endif /* _SWAB_H */