cpu/byteorder.h

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