blob: 2b41fbac188eb3ba759aec56af6a27d79deb387f [file] [log] [blame]
wdenkb15cbc02000-08-21 15:05:47 +00001#ifndef _LINUX_BYTEORDER_SWAB_H
2#define _LINUX_BYTEORDER_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 ((__u16)( \
23 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
24 (((__u16)(x) & (__u16)0xff00U) >> 8) ))
25#define ___swab32(x) \
26 ((__u32)( \
27 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
28 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
29 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
30 (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
31#define ___swab64(x) \
32 ((__u64)( \
33 (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
34 (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
35 (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
36 (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
wdenk57b2d802003-06-27 21:31:46 +000037 (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
wdenkb15cbc02000-08-21 15:05:47 +000038 (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
39 (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
40 (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
41
Pali Rohár5dc7e672021-11-26 14:57:05 +010042#define ___constant_swab16(x) ___swab16(x)
43#define ___constant_swab32(x) ___swab32(x)
44#define ___constant_swab64(x) ___swab64(x)
45
wdenkb15cbc02000-08-21 15:05:47 +000046/*
47 * provide defaults when no architecture-specific optimization is detected
48 */
49#ifndef __arch__swab16
50# define __arch__swab16(x) ___swab16(x)
51#endif
52#ifndef __arch__swab32
53# define __arch__swab32(x) ___swab32(x)
54#endif
55#ifndef __arch__swab64
56# define __arch__swab64(x) ___swab64(x)
57#endif
58
59#ifndef __arch__swab16p
60# define __arch__swab16p(x) __swab16(*(x))
61#endif
62#ifndef __arch__swab32p
63# define __arch__swab32p(x) __swab32(*(x))
64#endif
65#ifndef __arch__swab64p
66# define __arch__swab64p(x) __swab64(*(x))
67#endif
68
69#ifndef __arch__swab16s
70# define __arch__swab16s(x) do { *(x) = __swab16p((x)); } while (0)
71#endif
72#ifndef __arch__swab32s
73# define __arch__swab32s(x) do { *(x) = __swab32p((x)); } while (0)
74#endif
75#ifndef __arch__swab64s
76# define __arch__swab64s(x) do { *(x) = __swab64p((x)); } while (0)
77#endif
78
wdenkb15cbc02000-08-21 15:05:47 +000079/*
80 * Allow constant folding
81 */
82#if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
83# define __swab16(x) \
84(__builtin_constant_p((__u16)(x)) ? \
85 ___swab16((x)) : \
86 __fswab16((x)))
87# define __swab32(x) \
88(__builtin_constant_p((__u32)(x)) ? \
89 ___swab32((x)) : \
90 __fswab32((x)))
91# define __swab64(x) \
92(__builtin_constant_p((__u64)(x)) ? \
93 ___swab64((x)) : \
94 __fswab64((x)))
95#else
96# define __swab16(x) __fswab16(x)
97# define __swab32(x) __fswab32(x)
98# define __swab64(x) __fswab64(x)
99#endif /* OPTIMIZE */
100
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200101static __inline__ __attribute__((const)) __u16 __fswab16(__u16 x)
wdenkb15cbc02000-08-21 15:05:47 +0000102{
103 return __arch__swab16(x);
104}
Kim Phillipsa1b457a2012-10-29 13:34:23 +0000105static __inline__ __u16 __swab16p(const __u16 *x)
wdenkb15cbc02000-08-21 15:05:47 +0000106{
107 return __arch__swab16p(x);
108}
wdenk5401de42004-02-07 01:27:10 +0000109static __inline__ void __swab16s(__u16 *addr)
wdenkb15cbc02000-08-21 15:05:47 +0000110{
111 __arch__swab16s(addr);
112}
113
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200114static __inline__ __attribute__((const)) __u32 __fswab32(__u32 x)
wdenkb15cbc02000-08-21 15:05:47 +0000115{
116 return __arch__swab32(x);
117}
Kim Phillipsa1b457a2012-10-29 13:34:23 +0000118static __inline__ __u32 __swab32p(const __u32 *x)
wdenkb15cbc02000-08-21 15:05:47 +0000119{
120 return __arch__swab32p(x);
121}
wdenk5401de42004-02-07 01:27:10 +0000122static __inline__ void __swab32s(__u32 *addr)
wdenkb15cbc02000-08-21 15:05:47 +0000123{
124 __arch__swab32s(addr);
125}
126
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200127static __inline__ __attribute__((const)) __u64 __fswab64(__u64 x)
wdenkb15cbc02000-08-21 15:05:47 +0000128{
129# ifdef __SWAB_64_THRU_32__
130 __u32 h = x >> 32;
wdenk57b2d802003-06-27 21:31:46 +0000131 __u32 l = x & ((1ULL<<32)-1);
132 return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
wdenkb15cbc02000-08-21 15:05:47 +0000133# else
134 return __arch__swab64(x);
135# endif
136}
Kim Phillipsa1b457a2012-10-29 13:34:23 +0000137static __inline__ __u64 __swab64p(const __u64 *x)
wdenkb15cbc02000-08-21 15:05:47 +0000138{
139 return __arch__swab64p(x);
140}
wdenk5401de42004-02-07 01:27:10 +0000141static __inline__ void __swab64s(__u64 *addr)
wdenkb15cbc02000-08-21 15:05:47 +0000142{
143 __arch__swab64s(addr);
144}
wdenkb15cbc02000-08-21 15:05:47 +0000145
146#if defined(__KERNEL__)
147#define swab16 __swab16
148#define swab32 __swab32
149#define swab64 __swab64
150#define swab16p __swab16p
151#define swab32p __swab32p
152#define swab64p __swab64p
153#define swab16s __swab16s
154#define swab32s __swab32s
155#define swab64s __swab64s
156#endif
157
158#endif /* _LINUX_BYTEORDER_SWAB_H */