Home | History | Annotate | Download | only in byteorder
      1 #ifndef _LINUX_BYTEORDER_SWAB_H
      2 #define _LINUX_BYTEORDER_SWAB_H
      3 
      4 /*
      5  * linux/byteorder/swab.h
      6  * Byte-swapping, independently from CPU endianness
      7  *	swabXX[ps]?(foo)
      8  *
      9  * Francois-Rene Rideau <fare (at) tunes.org> 19971205
     10  *    separated swab functions from cpu_to_XX,
     11  *    to clean up support for bizarre-endian architectures.
     12  *
     13  * Trent Piepho <xyzzy (at) speakeasy.org> 2007114
     14  *    make constant-folding work, provide C versions that
     15  *    gcc can optimize better, explain different versions
     16  *
     17  * See asm-i386/byteorder.h and suches for examples of how to provide
     18  * architecture-dependent optimized versions
     19  *
     20  */
     21 
     22 
     23 /* Functions/macros defined, there are a lot:
     24  *
     25  * ___swabXX
     26  *    Generic C versions of the swab functions.
     27  *
     28  * ___constant_swabXX
     29  *    C versions that gcc can fold into a compile-time constant when
     30  *    the argument is a compile-time constant.
     31  *
     32  * __arch__swabXX[sp]?
     33  *    Architecture optimized versions of all the swab functions
     34  *    (including the s and p versions).  These can be defined in
     35  *    asm-arch/byteorder.h.  Any which are not, are defined here.
     36  *    __arch__swabXXs() is defined in terms of __arch__swabXXp(), which
     37  *    is defined in terms of __arch__swabXX(), which is in turn defined
     38  *    in terms of ___swabXX(x).
     39  *    These must be macros.  They may be unsafe for arguments with
     40  *    side-effects.
     41  *
     42  * __fswabXX
     43  *    Inline function versions of the __arch__ macros.  These _are_ safe
     44  *    if the arguments have side-effects.  Note there are no s and p
     45  *    versions of these.
     46  *
     47  * __swabXX[sb]
     48  *    There are the ones you should actually use.  The __swabXX versions
     49  *    will be a constant given a constant argument and use the arch
     50  *    specific code (if any) for non-constant arguments.  The s and p
     51  *    versions always use the arch specific code (constant folding
     52  *    doesn't apply).  They are safe to use with arguments with
     53  *    side-effects.
     54  *
     55  * swabXX[sb]
     56  *    Nicknames for __swabXX[sb] to use in the kernel.
     57  */
     58 
     59 /* casts are necessary for constants, because we never know how for sure
     60  * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
     61  */
     62 
     63 static __inline__ __u16 ___swab16(__u16 x)
     64 {
     65 	return x<<8 | x>>8;
     66 }
     67 static __inline__ __u32 ___swab32(__u32 x)
     68 {
     69 	return x<<24 | x>>24 |
     70 		(x & (__u32)0x0000ff00UL)<<8 |
     71 		(x & (__u32)0x00ff0000UL)>>8;
     72 }
     73 static __inline__ __u64 ___swab64(__u64 x)
     74 {
     75 	return x<<56 | x>>56 |
     76 		(x & (__u64)0x000000000000ff00ULL)<<40 |
     77 		(x & (__u64)0x0000000000ff0000ULL)<<24 |
     78 		(x & (__u64)0x00000000ff000000ULL)<< 8 |
     79 	        (x & (__u64)0x000000ff00000000ULL)>> 8 |
     80 		(x & (__u64)0x0000ff0000000000ULL)>>24 |
     81 		(x & (__u64)0x00ff000000000000ULL)>>40;
     82 }
     83 
     84 #define ___constant_swab16(x) \
     85 	((__u16)( \
     86 		(((__u16)(x) & (__u16)0x00ffU) << 8) | \
     87 		(((__u16)(x) & (__u16)0xff00U) >> 8) ))
     88 #define ___constant_swab32(x) \
     89 	((__u32)( \
     90 		(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
     91 		(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
     92 		(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
     93 		(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
     94 #define ___constant_swab64(x) \
     95 	((__u64)( \
     96 		(__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
     97 		(__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
     98 		(__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
     99 		(__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) <<  8) | \
    100 	        (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >>  8) | \
    101 		(__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
    102 		(__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
    103 		(__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
    104 
    105 /*
    106  * provide defaults when no architecture-specific optimization is detected
    107  */
    108 #ifndef __arch__swab16
    109 #  define __arch__swab16(x) ___swab16(x)
    110 #endif
    111 #ifndef __arch__swab32
    112 #  define __arch__swab32(x) ___swab32(x)
    113 #endif
    114 #ifndef __arch__swab64
    115 #  define __arch__swab64(x) ___swab64(x)
    116 #endif
    117 
    118 #ifndef __arch__swab16p
    119 #  define __arch__swab16p(x) __arch__swab16(*(x))
    120 #endif
    121 #ifndef __arch__swab32p
    122 #  define __arch__swab32p(x) __arch__swab32(*(x))
    123 #endif
    124 #ifndef __arch__swab64p
    125 #  define __arch__swab64p(x) __arch__swab64(*(x))
    126 #endif
    127 
    128 #ifndef __arch__swab16s
    129 #  define __arch__swab16s(x) ((void)(*(x) = __arch__swab16p(x)))
    130 #endif
    131 #ifndef __arch__swab32s
    132 #  define __arch__swab32s(x) ((void)(*(x) = __arch__swab32p(x)))
    133 #endif
    134 #ifndef __arch__swab64s
    135 #  define __arch__swab64s(x) ((void)(*(x) = __arch__swab64p(x)))
    136 #endif
    137 
    138 
    139 /*
    140  * Allow constant folding
    141  */
    142 #if defined(__GNUC__) && defined(__OPTIMIZE__)
    143 #  define __swab16(x) \
    144 (__builtin_constant_p((__u16)(x)) ? \
    145  ___constant_swab16((x)) : \
    146  __fswab16((x)))
    147 #  define __swab32(x) \
    148 (__builtin_constant_p((__u32)(x)) ? \
    149  ___constant_swab32((x)) : \
    150  __fswab32((x)))
    151 #  define __swab64(x) \
    152 (__builtin_constant_p((__u64)(x)) ? \
    153  ___constant_swab64((x)) : \
    154  __fswab64((x)))
    155 #else
    156 #  define __swab16(x) __fswab16(x)
    157 #  define __swab32(x) __fswab32(x)
    158 #  define __swab64(x) __fswab64(x)
    159 #endif /* OPTIMIZE */
    160 
    161 
    162 static __inline__ __u16 __fswab16(__u16 x)
    163 {
    164 	return __arch__swab16(x);
    165 }
    166 static __inline__ __u16 __swab16p(const __u16 *x)
    167 {
    168 	return __arch__swab16p(x);
    169 }
    170 static __inline__ void __swab16s(__u16 *addr)
    171 {
    172 	__arch__swab16s(addr);
    173 }
    174 
    175 static __inline__ __u32 __fswab32(__u32 x)
    176 {
    177 	return __arch__swab32(x);
    178 }
    179 static __inline__ __u32 __swab32p(const __u32 *x)
    180 {
    181 	return __arch__swab32p(x);
    182 }
    183 static __inline__ void __swab32s(__u32 *addr)
    184 {
    185 	__arch__swab32s(addr);
    186 }
    187 
    188 #ifdef __BYTEORDER_HAS_U64__
    189 static __inline__ __u64 __fswab64(__u64 x)
    190 {
    191 #  ifdef __SWAB_64_THRU_32__
    192 	__u32 h = x >> 32;
    193         __u32 l = x & ((1ULL<<32)-1);
    194         return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
    195 #  else
    196 	return __arch__swab64(x);
    197 #  endif
    198 }
    199 static __inline__ __u64 __swab64p(const __u64 *x)
    200 {
    201 	return __arch__swab64p(x);
    202 }
    203 static __inline__ void __swab64s(__u64 *addr)
    204 {
    205 	__arch__swab64s(addr);
    206 }
    207 #endif /* __BYTEORDER_HAS_U64__ */
    208 
    209 
    210 #endif /* _LINUX_BYTEORDER_SWAB_H */
    211