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.9 2005/11/04 16:20:02 bernie
19 *#* Fix reference to README.devlib in header.
21 *#* Revision 1.8 2005/06/14 06:16:03 bernie
22 *#* Add all missing functions.
24 *#* Revision 1.7 2005/04/12 04:08:49 bernie
25 *#* host_to_net(16|32)(), net_to_host(16|32)(): New functions.
27 *#* Revision 1.6 2005/04/11 19:10:28 bernie
28 *#* Include top-level headers from cfg/ subdir.
30 *#* Revision 1.5 2004/08/25 14:12:09 rasky
31 *#* Aggiornato il comment block dei log RCS
33 *#* Revision 1.4 2004/07/22 01:08:43 bernie
34 *#* swab32(): Fix a very serious bug.
36 *#* Revision 1.3 2004/07/20 23:47:12 bernie
37 *#* Finally remove redundant protos.
39 *#* Revision 1.2 2004/07/20 17:09:11 bernie
40 *#* swab16(), swab32(), cpu_to_be32(), cpu_to_le32(): New functions.
42 *#* Revision 1.1 2004/07/20 16:26:15 bernie
43 *#* Import byte-order macros into DevLib.
47 #ifndef MWARE_BYTEORDER_H
48 #define MWARE_BYTEORDER_H
50 #include <cfg/compiler.h>
54 * Swap upper and lower bytes in a 16-bit value.
56 INLINE uint16_t swab16(uint16_t x)
58 return ((x & (uint16_t)0x00FFU) << 8)
59 | ((x & (uint16_t)0xFF00U) >> 8);
63 * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
65 INLINE uint32_t swab32(uint32_t x)
67 return ((x & (uint32_t)0x000000FFUL) << 24)
68 | ((x & (uint32_t)0x0000FF00UL) << 8)
69 | ((x & (uint32_t)0x00FF0000UL) >> 8)
70 | ((x & (uint32_t)0xFF000000UL) >> 24);
74 * Reverse bytes in a float value.
76 INLINE float swab_float(float x)
78 /* Avoid breaking strict aliasing rules. */
79 char *cx = (char *)(&x);
80 STATIC_ASSERT(sizeof(float) == 4);
81 #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b))
82 BYTEORDER_SWAP(cx[0], cx[3]);
83 BYTEORDER_SWAP(cx[1], cx[2]);
88 INLINE uint16_t cpu_to_be16(uint16_t x)
90 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
93 INLINE uint16_t cpu_to_le16(uint16_t x)
95 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
98 INLINE uint32_t cpu_to_be32(uint32_t x)
100 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
103 INLINE uint32_t cpu_to_le32(uint32_t x)
105 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
108 INLINE float cpu_to_be_float(float x)
110 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
113 INLINE float cpu_to_le_float(float x)
115 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
118 INLINE uint16_t be16_to_cpu(uint16_t x)
120 return cpu_to_be16(x);
123 INLINE uint16_t le16_to_cpu(uint16_t x)
125 return cpu_to_le16(x);
128 INLINE uint32_t be32_to_cpu(uint32_t x)
130 return cpu_to_be32(x);
133 INLINE uint32_t le32_to_cpu(uint32_t x)
135 return cpu_to_le32(x);
138 INLINE float be_float_to_cpu(float x)
140 return cpu_to_be_float(x);
143 INLINE float le_float_to_cpu(float x)
145 return cpu_to_le_float(x);
148 INLINE uint16_t host_to_net16(uint16_t x)
150 return cpu_to_be16(x);
153 INLINE uint16_t net_to_host16(uint16_t x)
155 return be16_to_cpu(x);
158 INLINE uint32_t host_to_net32(uint32_t x)
160 return cpu_to_be32(x);
163 INLINE uint32_t net_to_host32(uint32_t x)
165 return be32_to_cpu(x);
168 INLINE float host_to_net_float(float x)
170 return cpu_to_be_float(x);
173 INLINE float net_to_host_float(float x)
175 return be_float_to_cpu(x);
180 //! Type generic byte swapping.
184 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
185 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
186 template<> INLINE int16_t swab(int16_t x) { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
187 template<> INLINE int32_t swab(int32_t x) { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
188 template<> INLINE float swab(float x) { return swab_float(x); }
190 //! Type generic conversion from CPU byte order to big-endian byte order.
192 INLINE T cpu_to_be(T x)
194 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
197 //! Type generic conversion from CPU byte-order to little-endian.
199 INLINE T cpu_to_le(T x)
201 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
204 //! Type generic conversion from big endian byte-order to CPU byte order.
206 INLINE T be_to_cpu(T x)
211 //! Type generic conversion from little-endian byte order to CPU byte order.
213 INLINE T le_to_cpu(T x)
218 //! Type generic conversion from network byte order to host byte order.
220 INLINE T net_to_host(T x)
225 //! Type generic conversion from host byte order to network byte order.
227 INLINE T host_to_net(T x)
229 return net_to_host(x);
232 #endif /* __cplusplus */
234 #endif /* MWARE_BYTEORDER_H */