Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
0002 #ifndef _UAPI_LINUX_SWAB_H
0003 #define _UAPI_LINUX_SWAB_H
0004 
0005 #include <linux/types.h>
0006 #include <linux/compiler.h>
0007 #include <asm/bitsperlong.h>
0008 #include <asm/swab.h>
0009 
0010 /*
0011  * casts are necessary for constants, because we never know how for sure
0012  * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
0013  */
0014 #define ___constant_swab16(x) ((__u16)(             \
0015     (((__u16)(x) & (__u16)0x00ffU) << 8) |          \
0016     (((__u16)(x) & (__u16)0xff00U) >> 8)))
0017 
0018 #define ___constant_swab32(x) ((__u32)(             \
0019     (((__u32)(x) & (__u32)0x000000ffUL) << 24) |        \
0020     (((__u32)(x) & (__u32)0x0000ff00UL) <<  8) |        \
0021     (((__u32)(x) & (__u32)0x00ff0000UL) >>  8) |        \
0022     (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
0023 
0024 #define ___constant_swab64(x) ((__u64)(             \
0025     (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) |   \
0026     (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) |   \
0027     (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) |   \
0028     (((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) |   \
0029     (((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) |   \
0030     (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) |   \
0031     (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) |   \
0032     (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
0033 
0034 #define ___constant_swahw32(x) ((__u32)(            \
0035     (((__u32)(x) & (__u32)0x0000ffffUL) << 16) |        \
0036     (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
0037 
0038 #define ___constant_swahb32(x) ((__u32)(            \
0039     (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) |     \
0040     (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
0041 
0042 /*
0043  * Implement the following as inlines, but define the interface using
0044  * macros to allow constant folding when possible:
0045  * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
0046  */
0047 
0048 static inline __attribute_const__ __u16 __fswab16(__u16 val)
0049 {
0050 #if defined (__arch_swab16)
0051     return __arch_swab16(val);
0052 #else
0053     return ___constant_swab16(val);
0054 #endif
0055 }
0056 
0057 static inline __attribute_const__ __u32 __fswab32(__u32 val)
0058 {
0059 #if defined(__arch_swab32)
0060     return __arch_swab32(val);
0061 #else
0062     return ___constant_swab32(val);
0063 #endif
0064 }
0065 
0066 static inline __attribute_const__ __u64 __fswab64(__u64 val)
0067 {
0068 #if defined (__arch_swab64)
0069     return __arch_swab64(val);
0070 #elif defined(__SWAB_64_THRU_32__)
0071     __u32 h = val >> 32;
0072     __u32 l = val & ((1ULL << 32) - 1);
0073     return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h)));
0074 #else
0075     return ___constant_swab64(val);
0076 #endif
0077 }
0078 
0079 static inline __attribute_const__ __u32 __fswahw32(__u32 val)
0080 {
0081 #ifdef __arch_swahw32
0082     return __arch_swahw32(val);
0083 #else
0084     return ___constant_swahw32(val);
0085 #endif
0086 }
0087 
0088 static inline __attribute_const__ __u32 __fswahb32(__u32 val)
0089 {
0090 #ifdef __arch_swahb32
0091     return __arch_swahb32(val);
0092 #else
0093     return ___constant_swahb32(val);
0094 #endif
0095 }
0096 
0097 /**
0098  * __swab16 - return a byteswapped 16-bit value
0099  * @x: value to byteswap
0100  */
0101 #ifdef __HAVE_BUILTIN_BSWAP16__
0102 #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
0103 #else
0104 #define __swab16(x)             \
0105     (__u16)(__builtin_constant_p(x) ?   \
0106     ___constant_swab16(x) :         \
0107     __fswab16(x))
0108 #endif
0109 
0110 /**
0111  * __swab32 - return a byteswapped 32-bit value
0112  * @x: value to byteswap
0113  */
0114 #ifdef __HAVE_BUILTIN_BSWAP32__
0115 #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
0116 #else
0117 #define __swab32(x)             \
0118     (__u32)(__builtin_constant_p(x) ?   \
0119     ___constant_swab32(x) :         \
0120     __fswab32(x))
0121 #endif
0122 
0123 /**
0124  * __swab64 - return a byteswapped 64-bit value
0125  * @x: value to byteswap
0126  */
0127 #ifdef __HAVE_BUILTIN_BSWAP64__
0128 #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x))
0129 #else
0130 #define __swab64(x)             \
0131     (__u64)(__builtin_constant_p(x) ?   \
0132     ___constant_swab64(x) :         \
0133     __fswab64(x))
0134 #endif
0135 
0136 static __always_inline unsigned long __swab(const unsigned long y)
0137 {
0138 #if __BITS_PER_LONG == 64
0139     return __swab64(y);
0140 #else /* __BITS_PER_LONG == 32 */
0141     return __swab32(y);
0142 #endif
0143 }
0144 
0145 /**
0146  * __swahw32 - return a word-swapped 32-bit value
0147  * @x: value to wordswap
0148  *
0149  * __swahw32(0x12340000) is 0x00001234
0150  */
0151 #define __swahw32(x)                \
0152     (__builtin_constant_p((__u32)(x)) ? \
0153     ___constant_swahw32(x) :        \
0154     __fswahw32(x))
0155 
0156 /**
0157  * __swahb32 - return a high and low byte-swapped 32-bit value
0158  * @x: value to byteswap
0159  *
0160  * __swahb32(0x12345678) is 0x34127856
0161  */
0162 #define __swahb32(x)                \
0163     (__builtin_constant_p((__u32)(x)) ? \
0164     ___constant_swahb32(x) :        \
0165     __fswahb32(x))
0166 
0167 /**
0168  * __swab16p - return a byteswapped 16-bit value from a pointer
0169  * @p: pointer to a naturally-aligned 16-bit value
0170  */
0171 static __always_inline __u16 __swab16p(const __u16 *p)
0172 {
0173 #ifdef __arch_swab16p
0174     return __arch_swab16p(p);
0175 #else
0176     return __swab16(*p);
0177 #endif
0178 }
0179 
0180 /**
0181  * __swab32p - return a byteswapped 32-bit value from a pointer
0182  * @p: pointer to a naturally-aligned 32-bit value
0183  */
0184 static __always_inline __u32 __swab32p(const __u32 *p)
0185 {
0186 #ifdef __arch_swab32p
0187     return __arch_swab32p(p);
0188 #else
0189     return __swab32(*p);
0190 #endif
0191 }
0192 
0193 /**
0194  * __swab64p - return a byteswapped 64-bit value from a pointer
0195  * @p: pointer to a naturally-aligned 64-bit value
0196  */
0197 static __always_inline __u64 __swab64p(const __u64 *p)
0198 {
0199 #ifdef __arch_swab64p
0200     return __arch_swab64p(p);
0201 #else
0202     return __swab64(*p);
0203 #endif
0204 }
0205 
0206 /**
0207  * __swahw32p - return a wordswapped 32-bit value from a pointer
0208  * @p: pointer to a naturally-aligned 32-bit value
0209  *
0210  * See __swahw32() for details of wordswapping.
0211  */
0212 static inline __u32 __swahw32p(const __u32 *p)
0213 {
0214 #ifdef __arch_swahw32p
0215     return __arch_swahw32p(p);
0216 #else
0217     return __swahw32(*p);
0218 #endif
0219 }
0220 
0221 /**
0222  * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
0223  * @p: pointer to a naturally-aligned 32-bit value
0224  *
0225  * See __swahb32() for details of high/low byteswapping.
0226  */
0227 static inline __u32 __swahb32p(const __u32 *p)
0228 {
0229 #ifdef __arch_swahb32p
0230     return __arch_swahb32p(p);
0231 #else
0232     return __swahb32(*p);
0233 #endif
0234 }
0235 
0236 /**
0237  * __swab16s - byteswap a 16-bit value in-place
0238  * @p: pointer to a naturally-aligned 16-bit value
0239  */
0240 static inline void __swab16s(__u16 *p)
0241 {
0242 #ifdef __arch_swab16s
0243     __arch_swab16s(p);
0244 #else
0245     *p = __swab16p(p);
0246 #endif
0247 }
0248 /**
0249  * __swab32s - byteswap a 32-bit value in-place
0250  * @p: pointer to a naturally-aligned 32-bit value
0251  */
0252 static __always_inline void __swab32s(__u32 *p)
0253 {
0254 #ifdef __arch_swab32s
0255     __arch_swab32s(p);
0256 #else
0257     *p = __swab32p(p);
0258 #endif
0259 }
0260 
0261 /**
0262  * __swab64s - byteswap a 64-bit value in-place
0263  * @p: pointer to a naturally-aligned 64-bit value
0264  */
0265 static __always_inline void __swab64s(__u64 *p)
0266 {
0267 #ifdef __arch_swab64s
0268     __arch_swab64s(p);
0269 #else
0270     *p = __swab64p(p);
0271 #endif
0272 }
0273 
0274 /**
0275  * __swahw32s - wordswap a 32-bit value in-place
0276  * @p: pointer to a naturally-aligned 32-bit value
0277  *
0278  * See __swahw32() for details of wordswapping
0279  */
0280 static inline void __swahw32s(__u32 *p)
0281 {
0282 #ifdef __arch_swahw32s
0283     __arch_swahw32s(p);
0284 #else
0285     *p = __swahw32p(p);
0286 #endif
0287 }
0288 
0289 /**
0290  * __swahb32s - high and low byteswap a 32-bit value in-place
0291  * @p: pointer to a naturally-aligned 32-bit value
0292  *
0293  * See __swahb32() for details of high and low byte swapping
0294  */
0295 static inline void __swahb32s(__u32 *p)
0296 {
0297 #ifdef __arch_swahb32s
0298     __arch_swahb32s(p);
0299 #else
0300     *p = __swahb32p(p);
0301 #endif
0302 }
0303 
0304 
0305 #endif /* _UAPI_LINUX_SWAB_H */