byteorder.h
Go to the documentation of this file.00001
00041 #ifndef MWARE_BYTEORDER_H
00042 #define MWARE_BYTEORDER_H
00043
00044 #include <cfg/compiler.h>
00045 #include <cpu/attr.h>
00046
00050 INLINE uint16_t swab16(uint16_t x)
00051 {
00052 return ((x & (uint16_t)0x00FFU) << 8)
00053 | ((x & (uint16_t)0xFF00U) >> 8);
00054 }
00055
00059 INLINE uint32_t swab32(uint32_t x)
00060 {
00061 return ((x & (uint32_t)0x000000FFUL) << 24)
00062 | ((x & (uint32_t)0x0000FF00UL) << 8)
00063 | ((x & (uint32_t)0x00FF0000UL) >> 8)
00064 | ((x & (uint32_t)0xFF000000UL) >> 24);
00065 }
00066
00070 INLINE uint64_t swab64(uint64_t x)
00071 {
00072 return (uint64_t)swab32(x >> 32)
00073 | ((uint64_t)swab32(x & 0xFFFFFFFFUL) << 32);
00074 }
00075
00079 INLINE float swab_float(float x)
00080 {
00081
00082 char *cx = (char *)(&x);
00083 STATIC_ASSERT(sizeof(float) == 4);
00084 #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b))
00085 BYTEORDER_SWAP(cx[0], cx[3]);
00086 BYTEORDER_SWAP(cx[1], cx[2]);
00087 #undef BYTEORDER_SWAP
00088 return x;
00089 }
00090
00091 INLINE uint16_t cpu_to_be16(uint16_t x)
00092 {
00093 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
00094 }
00095
00096 INLINE uint16_t cpu_to_le16(uint16_t x)
00097 {
00098 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
00099 }
00100
00101 INLINE uint32_t cpu_to_be32(uint32_t x)
00102 {
00103 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
00104 }
00105
00106 INLINE uint32_t cpu_to_le32(uint32_t x)
00107 {
00108 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
00109 }
00110
00111 INLINE uint64_t cpu_to_be64(uint64_t x)
00112 {
00113 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab64(x) : x;
00114 }
00115
00116 INLINE uint64_t cpu_to_le64(uint64_t x)
00117 {
00118 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab64(x) : x;
00119 }
00120
00121 INLINE float cpu_to_be_float(float x)
00122 {
00123 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
00124 }
00125
00126 INLINE float cpu_to_le_float(float x)
00127 {
00128 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
00129 }
00130
00131 INLINE uint16_t be16_to_cpu(uint16_t x)
00132 {
00133 return cpu_to_be16(x);
00134 }
00135
00136 INLINE uint16_t le16_to_cpu(uint16_t x)
00137 {
00138 return cpu_to_le16(x);
00139 }
00140
00141 INLINE uint32_t be32_to_cpu(uint32_t x)
00142 {
00143 return cpu_to_be32(x);
00144 }
00145
00146 INLINE uint32_t le32_to_cpu(uint32_t x)
00147 {
00148 return cpu_to_le32(x);
00149 }
00150
00151 INLINE uint64_t be64_to_cpu(uint64_t x)
00152 {
00153 return cpu_to_be64(x);
00154 }
00155
00156 INLINE uint64_t le64_to_cpu(uint64_t x)
00157 {
00158 return cpu_to_le64(x);
00159 }
00160
00161 INLINE float be_float_to_cpu(float x)
00162 {
00163 return cpu_to_be_float(x);
00164 }
00165
00166 INLINE float le_float_to_cpu(float x)
00167 {
00168 return cpu_to_le_float(x);
00169 }
00170
00171 INLINE uint16_t host_to_net16(uint16_t x)
00172 {
00173 return cpu_to_be16(x);
00174 }
00175
00176 INLINE uint16_t net_to_host16(uint16_t x)
00177 {
00178 return be16_to_cpu(x);
00179 }
00180
00181 INLINE uint32_t host_to_net32(uint32_t x)
00182 {
00183 return cpu_to_be32(x);
00184 }
00185
00186 INLINE uint32_t net_to_host32(uint32_t x)
00187 {
00188 return be32_to_cpu(x);
00189 }
00190
00191 INLINE uint64_t host_to_net64(uint64_t x)
00192 {
00193 return cpu_to_be64(x);
00194 }
00195
00196 INLINE uint64_t net_to_host64(uint64_t x)
00197 {
00198 return be64_to_cpu(x);
00199 }
00200
00201 INLINE float host_to_net_float(float x)
00202 {
00203 return cpu_to_be_float(x);
00204 }
00205
00206 INLINE float net_to_host_float(float x)
00207 {
00208 return be_float_to_cpu(x);
00209 }
00210
00211 #ifdef __cplusplus
00212
00214 template<typename T>
00215 INLINE T swab(T x);
00216
00217 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
00218 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
00219 template<> INLINE uint64_t swab(uint64_t x) { return swab64(x); }
00220 template<> INLINE int16_t swab(int16_t x) { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
00221 template<> INLINE int32_t swab(int32_t x) { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
00222 template<> INLINE int64_t swab(int64_t x) { return static_cast<int64_t>(swab64(static_cast<uint64_t>(x))); }
00223 template<> INLINE float swab(float x) { return swab_float(x); }
00224
00226 template<typename T>
00227 INLINE T cpu_to_be(T x)
00228 {
00229 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
00230 }
00231
00233 template<typename T>
00234 INLINE T cpu_to_le(T x)
00235 {
00236 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
00237 }
00238
00240 template<typename T>
00241 INLINE T be_to_cpu(T x)
00242 {
00243 return cpu_to_be(x);
00244 }
00245
00247 template<typename T>
00248 INLINE T le_to_cpu(T x)
00249 {
00250 return cpu_to_le(x);
00251 }
00252
00254 template<typename T>
00255 INLINE T net_to_host(T x)
00256 {
00257 return be_to_cpu(x);
00258 }
00259
00261 template<typename T>
00262 INLINE T host_to_net(T x)
00263 {
00264 return net_to_host(x);
00265 }
00266
00267 #endif
00268
00269 #endif