4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README for information.
8 * \brief Functions to convert integers to/from host byte-order.
12 * \author Bernardo Innocenti <bernie@develer.com>
13 * \author Stefano Fedrigo <aleph@develer.com>
18 *#* Revision 1.8 2005/06/14 06:16:03 bernie
19 *#* Add all missing functions.
21 *#* Revision 1.7 2005/04/12 04:08:49 bernie
22 *#* host_to_net(16|32)(), net_to_host(16|32)(): New functions.
24 *#* Revision 1.6 2005/04/11 19:10:28 bernie
25 *#* Include top-level headers from cfg/ subdir.
27 *#* Revision 1.5 2004/08/25 14:12:09 rasky
28 *#* Aggiornato il comment block dei log RCS
30 *#* Revision 1.4 2004/07/22 01:08:43 bernie
31 *#* swab32(): Fix a very serious bug.
33 *#* Revision 1.3 2004/07/20 23:47:12 bernie
34 *#* Finally remove redundant protos.
36 *#* Revision 1.2 2004/07/20 17:09:11 bernie
37 *#* swab16(), swab32(), cpu_to_be32(), cpu_to_le32(): New functions.
39 *#* Revision 1.1 2004/07/20 16:26:15 bernie
40 *#* Import byte-order macros into DevLib.
44 #ifndef MWARE_BYTEORDER_H
45 #define MWARE_BYTEORDER_H
47 #include <cfg/compiler.h>
51 * Swap upper and lower bytes in a 16-bit value.
53 INLINE uint16_t swab16(uint16_t x)
55 return ((x & (uint16_t)0x00FFU) << 8)
56 | ((x & (uint16_t)0xFF00U) >> 8);
60 * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
62 INLINE uint32_t swab32(uint32_t x)
64 return ((x & (uint32_t)0x000000FFUL) << 24)
65 | ((x & (uint32_t)0x0000FF00UL) << 8)
66 | ((x & (uint32_t)0x00FF0000UL) >> 8)
67 | ((x & (uint32_t)0xFF000000UL) >> 24);
71 * Reverse bytes in a float value.
73 INLINE float swab_float(float x)
75 /* Avoid breaking strict aliasing rules. */
76 char *cx = (char *)(&x);
77 STATIC_ASSERT(sizeof(float) == 4);
78 #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b))
79 BYTEORDER_SWAP(cx[0], cx[3]);
80 BYTEORDER_SWAP(cx[1], cx[2]);
85 INLINE uint16_t cpu_to_be16(uint16_t x)
87 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
90 INLINE uint16_t cpu_to_le16(uint16_t x)
92 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
95 INLINE uint32_t cpu_to_be32(uint32_t x)
97 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
100 INLINE uint32_t cpu_to_le32(uint32_t x)
102 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
105 INLINE float cpu_to_be_float(float x)
107 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
110 INLINE float cpu_to_le_float(float x)
112 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
115 INLINE uint16_t be16_to_cpu(uint16_t x)
117 return cpu_to_be16(x);
120 INLINE uint16_t le16_to_cpu(uint16_t x)
122 return cpu_to_le16(x);
125 INLINE uint32_t be32_to_cpu(uint32_t x)
127 return cpu_to_be32(x);
130 INLINE uint32_t le32_to_cpu(uint32_t x)
132 return cpu_to_le32(x);
135 INLINE float be_float_to_cpu(float x)
137 return cpu_to_be_float(x);
140 INLINE float le_float_to_cpu(float x)
142 return cpu_to_le_float(x);
145 INLINE uint16_t host_to_net16(uint16_t x)
147 return cpu_to_be16(x);
150 INLINE uint16_t net_to_host16(uint16_t x)
152 return be16_to_cpu(x);
155 INLINE uint32_t host_to_net32(uint32_t x)
157 return cpu_to_be32(x);
160 INLINE uint32_t net_to_host32(uint32_t x)
162 return be32_to_cpu(x);
165 INLINE float host_to_net_float(float x)
167 return cpu_to_be_float(x);
170 INLINE float net_to_host_float(float x)
172 return be_float_to_cpu(x);
177 //! Type generic byte swapping.
181 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
182 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
183 template<> INLINE int16_t swab(int16_t x) { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
184 template<> INLINE int32_t swab(int32_t x) { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
185 template<> INLINE float swab(float x) { return swab_float(x); }
187 //! Type generic conversion from CPU byte order to big-endian byte order.
189 INLINE T cpu_to_be(T x)
191 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
194 //! Type generic conversion from CPU byte-order to little-endian.
196 INLINE T cpu_to_le(T x)
198 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
201 //! Type generic conversion from big endian byte-order to CPU byte order.
203 INLINE T be_to_cpu(T x)
208 //! Type generic conversion from little-endian byte order to CPU byte order.
210 INLINE T le_to_cpu(T x)
215 //! Type generic conversion from network byte order to host byte order.
217 INLINE T net_to_host(T x)
222 //! Type generic conversion from host byte order to network byte order.
224 INLINE T host_to_net(T x)
226 return net_to_host(x);
229 #endif /* __cplusplus */
231 #endif /* MWARE_BYTEORDER_H */