Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenswab.h
Go to the documentation of this file.
00001 #ifndef _LINUX_BYTEORDER_SWAB_H 00002 #define _LINUX_BYTEORDER_SWAB_H 00003 00004 /* 00005 * linux/byteorder/swab.h 00006 * Byte-swapping, independently from CPU endianness 00007 * swabXX[ps]?(foo) 00008 * 00009 * Francois-Rene Rideau <fare@tunes.org> 19971205 00010 * separated swab functions from cpu_to_XX, 00011 * to clean up support for bizarre-endian architectures. 00012 * 00013 * See asm-i386/byteorder.h and suches for examples of how to provide 00014 * architecture-dependent optimized versions 00015 * 00016 */ 00017 00018 #include "compiler.h" 00019 00020 /* casts are necessary for constants, because we never know how for sure 00021 * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way. 00022 */ 00023 #define ___swab16(x) \ 00024 ({ \ 00025 __u16 __x = (x); \ 00026 ((__u16)( \ 00027 (((__u16)(__x) & (__u16)0x00ffU) << 8) | \ 00028 (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \ 00029 }) 00030 00031 #define ___swab32(x) \ 00032 ({ \ 00033 __u32 __x = (x); \ 00034 ((__u32)( \ 00035 (((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \ 00036 (((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \ 00037 (((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \ 00038 (((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \ 00039 }) 00040 00041 #define ___swab64(x) \ 00042 ({ \ 00043 __u64 __x = (x); \ 00044 ((__u64)( \ 00045 (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \ 00046 (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \ 00047 (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \ 00048 (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) << 8) | \ 00049 (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >> 8) | \ 00050 (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \ 00051 (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \ 00052 (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \ 00053 }) 00054 00055 #define ___constant_swab16(x) \ 00056 ((__u16)( \ 00057 (((__u16)(x) & (__u16)0x00ffU) << 8) | \ 00058 (((__u16)(x) & (__u16)0xff00U) >> 8) )) 00059 #define ___constant_swab32(x) \ 00060 ((__u32)( \ 00061 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \ 00062 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \ 00063 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \ 00064 (((__u32)(x) & (__u32)0xff000000UL) >> 24) )) 00065 #define ___constant_swab64(x) \ 00066 ((__u64)( \ 00067 (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \ 00068 (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \ 00069 (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \ 00070 (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \ 00071 (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \ 00072 (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \ 00073 (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \ 00074 (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) )) 00075 00076 /* 00077 * provide defaults when no architecture-specific optimization is detected 00078 */ 00079 #ifndef __arch__swab16 00080 # define __arch__swab16(x) ({ __u16 __tmp = (x) ; ___swab16(__tmp); }) 00081 #endif 00082 #ifndef __arch__swab32 00083 # define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); }) 00084 #endif 00085 #ifndef __arch__swab64 00086 # define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); }) 00087 #endif 00088 00089 #ifndef __arch__swab16p 00090 # define __arch__swab16p(x) __arch__swab16(*(x)) 00091 #endif 00092 #ifndef __arch__swab32p 00093 # define __arch__swab32p(x) __arch__swab32(*(x)) 00094 #endif 00095 #ifndef __arch__swab64p 00096 # define __arch__swab64p(x) __arch__swab64(*(x)) 00097 #endif 00098 00099 #ifndef __arch__swab16s 00100 # define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0) 00101 #endif 00102 #ifndef __arch__swab32s 00103 # define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0) 00104 #endif 00105 #ifndef __arch__swab64s 00106 # define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0) 00107 #endif 00108 00109 00110 /* 00111 * Allow constant folding 00112 */ 00113 #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__) 00114 # define __swab16(x) \ 00115 (__builtin_constant_p((__u16)(x)) ? \ 00116 ___swab16((x)) : \ 00117 __fswab16((x))) 00118 # define __swab32(x) \ 00119 (__builtin_constant_p((__u32)(x)) ? \ 00120 ___swab32((x)) : \ 00121 __fswab32((x))) 00122 # define __swab64(x) \ 00123 (__builtin_constant_p((__u64)(x)) ? \ 00124 ___swab64((x)) : \ 00125 __fswab64((x))) 00126 #else 00127 # define __swab16(x) __fswab16(x) 00128 # define __swab32(x) __fswab32(x) 00129 # define __swab64(x) __fswab64(x) 00130 #endif /* OPTIMIZE */ 00131 00132 00133 static __inline__ __attribute_const__ __u16 __fswab16(__u16 x) 00134 { 00135 return __arch__swab16(x); 00136 } 00137 static __inline__ __u16 __swab16p(const __u16 *x) 00138 { 00139 return __arch__swab16p(x); 00140 } 00141 static __inline__ void __swab16s(__u16 *addr) 00142 { 00143 __arch__swab16s(addr); 00144 } 00145 00146 static __inline__ __attribute_const__ __u32 __fswab32(__u32 x) 00147 { 00148 return __arch__swab32(x); 00149 } 00150 static __inline__ __u32 __swab32p(const __u32 *x) 00151 { 00152 return __arch__swab32p(x); 00153 } 00154 static __inline__ void __swab32s(__u32 *addr) 00155 { 00156 __arch__swab32s(addr); 00157 } 00158 00159 #ifdef __BYTEORDER_HAS_U64__ 00160 static __inline__ __attribute_const__ __u64 __fswab64(__u64 x) 00161 { 00162 # ifdef __SWAB_64_THRU_32__ 00163 __u32 h = x >> 32; 00164 __u32 l = x & ((1ULL<<32)-1); 00165 return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h))); 00166 # else 00167 return __arch__swab64(x); 00168 # endif 00169 } 00170 static __inline__ __u64 __swab64p(const __u64 *x) 00171 { 00172 return __arch__swab64p(x); 00173 } 00174 static __inline__ void __swab64s(__u64 *addr) 00175 { 00176 __arch__swab64s(addr); 00177 } 00178 #endif /* __BYTEORDER_HAS_U64__ */ 00179 00180 #if defined(__KERNEL__) 00181 #define swab16 __swab16 00182 #define swab32 __swab32 00183 #define swab64 __swab64 00184 #define swab16p __swab16p 00185 #define swab32p __swab32p 00186 #define swab64p __swab64p 00187 #define swab16s __swab16s 00188 #define swab32s __swab32s 00189 #define swab64s __swab64s 00190 #endif 00191 00192 #endif /* _LINUX_BYTEORDER_SWAB_H */ Generated on Sun May 27 2012 04:36:13 for ReactOS by
1.7.6.1
|