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