1#ifndef _UAPI_LINUX_SWAB_H 2#define _UAPI_LINUX_SWAB_H 3 4#include <linux/types.h> 5#include <linux/compiler.h> 6#include <asm/swab.h> 7 8/* 9 * casts are necessary for constants, because we never know how for sure 10 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way. 11 */ 12#define ___constant_swab16(x) ((__u16)( \ 13 (((__u16)(x) & (__u16)0x00ffU) << 8) | \ 14 (((__u16)(x) & (__u16)0xff00U) >> 8))) 15 16#define ___constant_swab32(x) ((__u32)( \ 17 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \ 18 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \ 19 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \ 20 (((__u32)(x) & (__u32)0xff000000UL) >> 24))) 21 22#define ___constant_swab64(x) ((__u64)( \ 23 (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \ 24 (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \ 25 (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \ 26 (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \ 27 (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \ 28 (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \ 29 (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \ 30 (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56))) 31 32#define ___constant_swahw32(x) ((__u32)( \ 33 (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \ 34 (((__u32)(x) & (__u32)0xffff0000UL) >> 16))) 35 36#define ___constant_swahb32(x) ((__u32)( \ 37 (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \ 38 (((__u32)(x) & (__u32)0xff00ff00UL) >> 8))) 39 40/* 41 * Implement the following as inlines, but define the interface using 42 * macros to allow constant folding when possible: 43 * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32 44 */ 45 46static inline __attribute_const__ __u16 __fswab16(__u16 val) 47{ 48#ifdef __HAVE_BUILTIN_BSWAP16__ 49 return __builtin_bswap16(val); 50#elif defined (__arch_swab16) 51 return __arch_swab16(val); 52#else 53 return ___constant_swab16(val); 54#endif 55} 56 57static inline __attribute_const__ __u32 __fswab32(__u32 val) 58{ 59#ifdef __HAVE_BUILTIN_BSWAP32__ 60 return __builtin_bswap32(val); 61#elif defined(__arch_swab32) 62 return __arch_swab32(val); 63#else 64 return ___constant_swab32(val); 65#endif 66} 67 68static inline __attribute_const__ __u64 __fswab64(__u64 val) 69{ 70#ifdef __HAVE_BUILTIN_BSWAP64__ 71 return __builtin_bswap64(val); 72#elif defined (__arch_swab64) 73 return __arch_swab64(val); 74#elif defined(__SWAB_64_THRU_32__) 75 __u32 h = val >> 32; 76 __u32 l = val & ((1ULL << 32) - 1); 77 return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h))); 78#else 79 return ___constant_swab64(val); 80#endif 81} 82 83static inline __attribute_const__ __u32 __fswahw32(__u32 val) 84{ 85#ifdef __arch_swahw32 86 return __arch_swahw32(val); 87#else 88 return ___constant_swahw32(val); 89#endif 90} 91 92static inline __attribute_const__ __u32 __fswahb32(__u32 val) 93{ 94#ifdef __arch_swahb32 95 return __arch_swahb32(val); 96#else 97 return ___constant_swahb32(val); 98#endif 99} 100 101/** 102 * __swab16 - return a byteswapped 16-bit value 103 * @x: value to byteswap 104 */ 105#define __swab16(x) \ 106 (__builtin_constant_p((__u16)(x)) ? \ 107 ___constant_swab16(x) : \ 108 __fswab16(x)) 109 110/** 111 * __swab32 - return a byteswapped 32-bit value 112 * @x: value to byteswap 113 */ 114#define __swab32(x) \ 115 (__builtin_constant_p((__u32)(x)) ? \ 116 ___constant_swab32(x) : \ 117 __fswab32(x)) 118 119/** 120 * __swab64 - return a byteswapped 64-bit value 121 * @x: value to byteswap 122 */ 123#define __swab64(x) \ 124 (__builtin_constant_p((__u64)(x)) ? \ 125 ___constant_swab64(x) : \ 126 __fswab64(x)) 127 128/** 129 * __swahw32 - return a word-swapped 32-bit value 130 * @x: value to wordswap 131 * 132 * __swahw32(0x12340000) is 0x00001234 133 */ 134#define __swahw32(x) \ 135 (__builtin_constant_p((__u32)(x)) ? \ 136 ___constant_swahw32(x) : \ 137 __fswahw32(x)) 138 139/** 140 * __swahb32 - return a high and low byte-swapped 32-bit value 141 * @x: value to byteswap 142 * 143 * __swahb32(0x12345678) is 0x34127856 144 */ 145#define __swahb32(x) \ 146 (__builtin_constant_p((__u32)(x)) ? \ 147 ___constant_swahb32(x) : \ 148 __fswahb32(x)) 149 150/** 151 * __swab16p - return a byteswapped 16-bit value from a pointer 152 * @p: pointer to a naturally-aligned 16-bit value 153 */ 154static inline __u16 __swab16p(const __u16 *p) 155{ 156#ifdef __arch_swab16p 157 return __arch_swab16p(p); 158#else 159 return __swab16(*p); 160#endif 161} 162 163/** 164 * __swab32p - return a byteswapped 32-bit value from a pointer 165 * @p: pointer to a naturally-aligned 32-bit value 166 */ 167static inline __u32 __swab32p(const __u32 *p) 168{ 169#ifdef __arch_swab32p 170 return __arch_swab32p(p); 171#else 172 return __swab32(*p); 173#endif 174} 175 176/** 177 * __swab64p - return a byteswapped 64-bit value from a pointer 178 * @p: pointer to a naturally-aligned 64-bit value 179 */ 180static inline __u64 __swab64p(const __u64 *p) 181{ 182#ifdef __arch_swab64p 183 return __arch_swab64p(p); 184#else 185 return __swab64(*p); 186#endif 187} 188 189/** 190 * __swahw32p - return a wordswapped 32-bit value from a pointer 191 * @p: pointer to a naturally-aligned 32-bit value 192 * 193 * See __swahw32() for details of wordswapping. 194 */ 195static inline __u32 __swahw32p(const __u32 *p) 196{ 197#ifdef __arch_swahw32p 198 return __arch_swahw32p(p); 199#else 200 return __swahw32(*p); 201#endif 202} 203 204/** 205 * __swahb32p - return a high and low byteswapped 32-bit value from a pointer 206 * @p: pointer to a naturally-aligned 32-bit value 207 * 208 * See __swahb32() for details of high/low byteswapping. 209 */ 210static inline __u32 __swahb32p(const __u32 *p) 211{ 212#ifdef __arch_swahb32p 213 return __arch_swahb32p(p); 214#else 215 return __swahb32(*p); 216#endif 217} 218 219/** 220 * __swab16s - byteswap a 16-bit value in-place 221 * @p: pointer to a naturally-aligned 16-bit value 222 */ 223static inline void __swab16s(__u16 *p) 224{ 225#ifdef __arch_swab16s 226 __arch_swab16s(p); 227#else 228 *p = __swab16p(p); 229#endif 230} 231/** 232 * __swab32s - byteswap a 32-bit value in-place 233 * @p: pointer to a naturally-aligned 32-bit value 234 */ 235static inline void __swab32s(__u32 *p) 236{ 237#ifdef __arch_swab32s 238 __arch_swab32s(p); 239#else 240 *p = __swab32p(p); 241#endif 242} 243 244/** 245 * __swab64s - byteswap a 64-bit value in-place 246 * @p: pointer to a naturally-aligned 64-bit value 247 */ 248static inline void __swab64s(__u64 *p) 249{ 250#ifdef __arch_swab64s 251 __arch_swab64s(p); 252#else 253 *p = __swab64p(p); 254#endif 255} 256 257/** 258 * __swahw32s - wordswap a 32-bit value in-place 259 * @p: pointer to a naturally-aligned 32-bit value 260 * 261 * See __swahw32() for details of wordswapping 262 */ 263static inline void __swahw32s(__u32 *p) 264{ 265#ifdef __arch_swahw32s 266 __arch_swahw32s(p); 267#else 268 *p = __swahw32p(p); 269#endif 270} 271 272/** 273 * __swahb32s - high and low byteswap a 32-bit value in-place 274 * @p: pointer to a naturally-aligned 32-bit value 275 * 276 * See __swahb32() for details of high and low byte swapping 277 */ 278static inline void __swahb32s(__u32 *p) 279{ 280#ifdef __arch_swahb32s 281 __arch_swahb32s(p); 282#else 283 *p = __swahb32p(p); 284#endif 285} 286 287 288#endif /* _UAPI_LINUX_SWAB_H */ 289