Harvey Harrison | 40c9f22 | 2008-08-12 15:09:04 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_SWAB_H |
| 2 | #define _LINUX_SWAB_H |
| 3 | |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/compiler.h> |
| 6 | #include <asm/byteorder.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 __const_swab16(x) ((__u16)( \ |
| 13 | (((__u16)(x) & (__u16)0x00ffU) << 8) | \ |
| 14 | (((__u16)(x) & (__u16)0xff00U) >> 8))) |
| 15 | |
| 16 | #define __const_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 __const_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 __const_swahw32(x) ((__u32)( \ |
| 33 | (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \ |
| 34 | (((__u32)(x) & (__u32)0xffff0000UL) >> 16))) |
| 35 | |
| 36 | #define __const_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 | |
| 46 | static inline __attribute_const__ __u16 ___swab16(__u16 val) |
| 47 | { |
| 48 | #ifdef __arch_swab16 |
| 49 | return __arch_swab16(val); |
| 50 | #elif defined(__arch_swab16p) |
| 51 | return __arch_swab16p(&val); |
| 52 | #else |
| 53 | return __const_swab16(val); |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | static inline __attribute_const__ __u32 ___swab32(__u32 val) |
| 58 | { |
| 59 | #ifdef __arch_swab32 |
| 60 | return __arch_swab32(val); |
| 61 | #elif defined(__arch_swab32p) |
| 62 | return __arch_swab32p(&val); |
| 63 | #else |
| 64 | return __const_swab32(val); |
| 65 | #endif |
| 66 | } |
| 67 | |
| 68 | static inline __attribute_const__ __u64 ___swab64(__u64 val) |
| 69 | { |
| 70 | #ifdef __arch_swab64 |
| 71 | return __arch_swab64(val); |
| 72 | #elif defined(__arch_swab64p) |
| 73 | return __arch_swab64p(&val); |
| 74 | #elif defined(__SWAB_64_THRU_32__) |
| 75 | __u32 h = val >> 32; |
| 76 | __u32 l = val & ((1ULL << 32) - 1); |
| 77 | return (((__u64)___swab32(l)) << 32) | ((__u64)(___swab32(h))); |
| 78 | #else |
| 79 | return __const_swab64(val); |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | static inline __attribute_const__ __u32 ___swahw32(__u32 val) |
| 84 | { |
| 85 | #ifdef __arch_swahw32 |
| 86 | return __arch_swahw32(val); |
| 87 | #elif defined(__arch_swahw32p) |
| 88 | return __arch_swahw32p(&val); |
| 89 | #else |
| 90 | return __const_swahw32(val); |
| 91 | #endif |
| 92 | } |
| 93 | |
| 94 | static inline __attribute_const__ __u32 ___swahb32(__u32 val) |
| 95 | { |
| 96 | #ifdef __arch_swahb32 |
| 97 | return __arch_swahb32(val); |
| 98 | #elif defined(__arch_swahb32p) |
| 99 | return __arch_swahb32p(&val); |
| 100 | #else |
| 101 | return __const_swahb32(val); |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * __swab16 - return a byteswapped 16-bit value |
| 107 | * @x: value to byteswap |
| 108 | */ |
| 109 | #define __swab16(x) \ |
| 110 | (__builtin_constant_p((__u16)(x)) ? \ |
| 111 | __const_swab16((x)) : \ |
| 112 | ___swab16((x))) |
| 113 | |
| 114 | /** |
| 115 | * __swab32 - return a byteswapped 32-bit value |
| 116 | * @x: value to byteswap |
| 117 | */ |
| 118 | #define __swab32(x) \ |
| 119 | (__builtin_constant_p((__u32)(x)) ? \ |
| 120 | __const_swab32((x)) : \ |
| 121 | ___swab32((x))) |
| 122 | |
| 123 | /** |
| 124 | * __swab64 - return a byteswapped 64-bit value |
| 125 | * @x: value to byteswap |
| 126 | */ |
| 127 | #define __swab64(x) \ |
| 128 | (__builtin_constant_p((__u64)(x)) ? \ |
| 129 | __const_swab64((x)) : \ |
| 130 | ___swab64((x))) |
| 131 | |
| 132 | /** |
| 133 | * __swahw32 - return a word-swapped 32-bit value |
| 134 | * @x: value to wordswap |
| 135 | * |
| 136 | * __swahw32(0x12340000) is 0x00001234 |
| 137 | */ |
| 138 | #define __swahw32(x) \ |
| 139 | (__builtin_constant_p((__u32)(x)) ? \ |
| 140 | __const_swahw32((x)) : \ |
| 141 | ___swahw32((x))) |
| 142 | |
| 143 | /** |
| 144 | * __swahb32 - return a high and low byte-swapped 32-bit value |
| 145 | * @x: value to byteswap |
| 146 | * |
| 147 | * __swahb32(0x12345678) is 0x34127856 |
| 148 | */ |
| 149 | #define __swahb32(x) \ |
| 150 | (__builtin_constant_p((__u32)(x)) ? \ |
| 151 | __const_swahb32((x)) : \ |
| 152 | ___swahb32((x))) |
| 153 | |
| 154 | /** |
| 155 | * __swab16p - return a byteswapped 16-bit value from a pointer |
| 156 | * @p: pointer to a naturally-aligned 16-bit value |
| 157 | */ |
| 158 | static inline __u16 __swab16p(const __u16 *p) |
| 159 | { |
| 160 | #ifdef __arch_swab16p |
| 161 | return __arch_swab16p(p); |
| 162 | #else |
| 163 | return __swab16(*p); |
| 164 | #endif |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * __swab32p - return a byteswapped 32-bit value from a pointer |
| 169 | * @p: pointer to a naturally-aligned 32-bit value |
| 170 | */ |
| 171 | static inline __u32 __swab32p(const __u32 *p) |
| 172 | { |
| 173 | #ifdef __arch_swab32p |
| 174 | return __arch_swab32p(p); |
| 175 | #else |
| 176 | return __swab32(*p); |
| 177 | #endif |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * __swab64p - return a byteswapped 64-bit value from a pointer |
| 182 | * @p: pointer to a naturally-aligned 64-bit value |
| 183 | */ |
| 184 | static inline __u64 __swab64p(const __u64 *p) |
| 185 | { |
| 186 | #ifdef __arch_swab64p |
| 187 | return __arch_swab64p(p); |
| 188 | #else |
| 189 | return __swab64(*p); |
| 190 | #endif |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * __swahw32p - return a wordswapped 32-bit value from a pointer |
| 195 | * @p: pointer to a naturally-aligned 32-bit value |
| 196 | * |
| 197 | * See __swahw32() for details of wordswapping. |
| 198 | */ |
| 199 | static inline __u32 __swahw32p(const __u32 *p) |
| 200 | { |
| 201 | #ifdef __arch_swahw32p |
| 202 | return __arch_swahw32p(p); |
| 203 | #else |
| 204 | return __swahw32(*p); |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * __swahb32p - return a high and low byteswapped 32-bit value from a pointer |
| 210 | * @p: pointer to a naturally-aligned 32-bit value |
| 211 | * |
| 212 | * See __swahb32() for details of high/low byteswapping. |
| 213 | */ |
| 214 | static inline __u32 __swahb32p(const __u32 *p) |
| 215 | { |
| 216 | #ifdef __arch_swahb32p |
| 217 | return __arch_swahb32p(p); |
| 218 | #else |
| 219 | return __swahb32(*p); |
| 220 | #endif |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * __swab16s - byteswap a 16-bit value in-place |
| 225 | * @p: pointer to a naturally-aligned 16-bit value |
| 226 | */ |
| 227 | static inline void __swab16s(__u16 *p) |
| 228 | { |
| 229 | #ifdef __arch_swab16s |
| 230 | __arch_swab16s(p); |
| 231 | #else |
| 232 | *p = __swab16p(p); |
| 233 | #endif |
| 234 | } |
| 235 | /** |
| 236 | * __swab32s - byteswap a 32-bit value in-place |
| 237 | * @p: pointer to a naturally-aligned 32-bit value |
| 238 | */ |
| 239 | static inline void __swab32s(__u32 *p) |
| 240 | { |
| 241 | #ifdef __arch_swab32s |
| 242 | __arch_swab32s(p); |
| 243 | #else |
| 244 | *p = __swab32p(p); |
| 245 | #endif |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * __swab64s - byteswap a 64-bit value in-place |
| 250 | * @p: pointer to a naturally-aligned 64-bit value |
| 251 | */ |
| 252 | static inline void __swab64s(__u64 *p) |
| 253 | { |
| 254 | #ifdef __arch_swab64s |
| 255 | __arch_swab64s(p); |
| 256 | #else |
| 257 | *p = __swab64p(p); |
| 258 | #endif |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * __swahw32s - wordswap a 32-bit value in-place |
| 263 | * @p: pointer to a naturally-aligned 32-bit value |
| 264 | * |
| 265 | * See __swahw32() for details of wordswapping |
| 266 | */ |
| 267 | static inline void __swahw32s(__u32 *p) |
| 268 | { |
| 269 | #ifdef __arch_swahw32s |
| 270 | __arch_swahw32s(p); |
| 271 | #else |
| 272 | *p = __swahw32p(p); |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * __swahb32s - high and low byteswap a 32-bit value in-place |
| 278 | * @p: pointer to a naturally-aligned 32-bit value |
| 279 | * |
| 280 | * See __swahb32() for details of high and low byte swapping |
| 281 | */ |
| 282 | static inline void __swahb32s(__u32 *p) |
| 283 | { |
| 284 | #ifdef __arch_swahb32s |
| 285 | __arch_swahb32s(p); |
| 286 | #else |
| 287 | *p = __swahb32p(p); |
| 288 | #endif |
| 289 | } |
| 290 | |
| 291 | #ifdef __KERNEL__ |
| 292 | # define swab16 __swab16 |
| 293 | # define swab32 __swab32 |
| 294 | # define swab64 __swab64 |
| 295 | # define swahw32 __swahw32 |
| 296 | # define swahb32 __swahb32 |
| 297 | # define swab16p __swab16p |
| 298 | # define swab32p __swab32p |
| 299 | # define swab64p __swab64p |
| 300 | # define swahw32p __swahw32p |
| 301 | # define swahb32p __swahb32p |
| 302 | # define swab16s __swab16s |
| 303 | # define swab32s __swab32s |
| 304 | # define swab64s __swab64s |
| 305 | # define swahw32s __swahw32s |
| 306 | # define swahb32s __swahb32s |
| 307 | #endif /* __KERNEL__ */ |
| 308 | |
| 309 | #endif /* _LINUX_SWAB_H */ |