4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib 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.10 2006/06/10 05:37:03 bernie
19 *#* Convert to new Doxygen comments.
21 *#* Revision 1.9 2005/11/04 16:20:02 bernie
22 *#* Fix reference to README.devlib in header.
24 *#* Revision 1.8 2005/06/14 06:16:03 bernie
25 *#* Add all missing functions.
27 *#* Revision 1.7 2005/04/12 04:08:49 bernie
28 *#* host_to_net(16|32)(), net_to_host(16|32)(): New functions.
30 *#* Revision 1.6 2005/04/11 19:10:28 bernie
31 *#* Include top-level headers from cfg/ subdir.
33 *#* Revision 1.5 2004/08/25 14:12:09 rasky
34 *#* Aggiornato il comment block dei log RCS
36 *#* Revision 1.4 2004/07/22 01:08:43 bernie
37 *#* swab32(): Fix a very serious bug.
39 *#* Revision 1.3 2004/07/20 23:47:12 bernie
40 *#* Finally remove redundant protos.
42 *#* Revision 1.2 2004/07/20 17:09:11 bernie
43 *#* swab16(), swab32(), cpu_to_be32(), cpu_to_le32(): New functions.
45 *#* Revision 1.1 2004/07/20 16:26:15 bernie
46 *#* Import byte-order macros into DevLib.
50 #ifndef MWARE_BYTEORDER_H
51 #define MWARE_BYTEORDER_H
53 #include <cfg/compiler.h>
57 * Swap upper and lower bytes in a 16-bit value.
59 INLINE uint16_t swab16(uint16_t x)
61 return ((x & (uint16_t)0x00FFU) << 8)
62 | ((x & (uint16_t)0xFF00U) >> 8);
66 * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
68 INLINE uint32_t swab32(uint32_t x)
70 return ((x & (uint32_t)0x000000FFUL) << 24)
71 | ((x & (uint32_t)0x0000FF00UL) << 8)
72 | ((x & (uint32_t)0x00FF0000UL) >> 8)
73 | ((x & (uint32_t)0xFF000000UL) >> 24);
77 * Reverse bytes in a float value.
79 INLINE float swab_float(float x)
81 /* Avoid breaking strict aliasing rules. */
82 char *cx = (char *)(&x);
83 STATIC_ASSERT(sizeof(float) == 4);
84 #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b))
85 BYTEORDER_SWAP(cx[0], cx[3]);
86 BYTEORDER_SWAP(cx[1], cx[2]);
91 INLINE uint16_t cpu_to_be16(uint16_t x)
93 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
96 INLINE uint16_t cpu_to_le16(uint16_t x)
98 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
101 INLINE uint32_t cpu_to_be32(uint32_t x)
103 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
106 INLINE uint32_t cpu_to_le32(uint32_t x)
108 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
111 INLINE float cpu_to_be_float(float x)
113 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
116 INLINE float cpu_to_le_float(float x)
118 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
121 INLINE uint16_t be16_to_cpu(uint16_t x)
123 return cpu_to_be16(x);
126 INLINE uint16_t le16_to_cpu(uint16_t x)
128 return cpu_to_le16(x);
131 INLINE uint32_t be32_to_cpu(uint32_t x)
133 return cpu_to_be32(x);
136 INLINE uint32_t le32_to_cpu(uint32_t x)
138 return cpu_to_le32(x);
141 INLINE float be_float_to_cpu(float x)
143 return cpu_to_be_float(x);
146 INLINE float le_float_to_cpu(float x)
148 return cpu_to_le_float(x);
151 INLINE uint16_t host_to_net16(uint16_t x)
153 return cpu_to_be16(x);
156 INLINE uint16_t net_to_host16(uint16_t x)
158 return be16_to_cpu(x);
161 INLINE uint32_t host_to_net32(uint32_t x)
163 return cpu_to_be32(x);
166 INLINE uint32_t net_to_host32(uint32_t x)
168 return be32_to_cpu(x);
171 INLINE float host_to_net_float(float x)
173 return cpu_to_be_float(x);
176 INLINE float net_to_host_float(float x)
178 return be_float_to_cpu(x);
183 /// Type generic byte swapping.
187 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
188 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
189 template<> INLINE int16_t swab(int16_t x) { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
190 template<> INLINE int32_t swab(int32_t x) { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
191 template<> INLINE float swab(float x) { return swab_float(x); }
193 /// Type generic conversion from CPU byte order to big-endian byte order.
195 INLINE T cpu_to_be(T x)
197 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
200 /// Type generic conversion from CPU byte-order to little-endian.
202 INLINE T cpu_to_le(T x)
204 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
207 /// Type generic conversion from big endian byte-order to CPU byte order.
209 INLINE T be_to_cpu(T x)
214 /// Type generic conversion from little-endian byte order to CPU byte order.
216 INLINE T le_to_cpu(T x)
221 /// Type generic conversion from network byte order to host byte order.
223 INLINE T net_to_host(T x)
228 /// Type generic conversion from host byte order to network byte order.
230 INLINE T host_to_net(T x)
232 return net_to_host(x);
235 #endif /* __cplusplus */
237 #endif /* MWARE_BYTEORDER_H */