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