befc15664fbf49647461d9b994dccac43b2399e4
[bertos.git] / mware / byteorder.h
1 /**
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Functions to convert integers to/from host byte-order.
9  *
10  * \version $Id$
11  *
12  * \author Bernardo Innocenti <bernie@develer.com>
13  * \author Stefano Fedrigo <aleph@develer.com>
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.10  2006/06/10 05:37:03  bernie
19  *#* Convert to new Doxygen comments.
20  *#*
21  *#* Revision 1.9  2005/11/04 16:20:02  bernie
22  *#* Fix reference to README.devlib in header.
23  *#*
24  *#* Revision 1.8  2005/06/14 06:16:03  bernie
25  *#* Add all missing functions.
26  *#*
27  *#* Revision 1.7  2005/04/12 04:08:49  bernie
28  *#* host_to_net(16|32)(), net_to_host(16|32)(): New functions.
29  *#*
30  *#* Revision 1.6  2005/04/11 19:10:28  bernie
31  *#* Include top-level headers from cfg/ subdir.
32  *#*
33  *#* Revision 1.5  2004/08/25 14:12:09  rasky
34  *#* Aggiornato il comment block dei log RCS
35  *#*
36  *#* Revision 1.4  2004/07/22 01:08:43  bernie
37  *#* swab32(): Fix a very serious bug.
38  *#*
39  *#* Revision 1.3  2004/07/20 23:47:12  bernie
40  *#* Finally remove redundant protos.
41  *#*
42  *#* Revision 1.2  2004/07/20 17:09:11  bernie
43  *#* swab16(), swab32(), cpu_to_be32(), cpu_to_le32(): New functions.
44  *#*
45  *#* Revision 1.1  2004/07/20 16:26:15  bernie
46  *#* Import byte-order macros into DevLib.
47  *#*
48  *#*/
49
50 #ifndef MWARE_BYTEORDER_H
51 #define MWARE_BYTEORDER_H
52
53 #include <cfg/compiler.h>
54 #include <cfg/cpu.h>
55
56 /**
57  * Swap upper and lower bytes in a 16-bit value.
58  */
59 INLINE uint16_t swab16(uint16_t x)
60 {
61         return    ((x & (uint16_t)0x00FFU) << 8)
62                 | ((x & (uint16_t)0xFF00U) >> 8);
63 }
64
65 /**
66  * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
67  */
68 INLINE uint32_t swab32(uint32_t x)
69 {
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);
74 }
75
76 /**
77  * Reverse bytes in a float value.
78  */
79 INLINE float swab_float(float x)
80 {
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]);
87         #undef BYTEORDER_SWAP
88         return x;
89 }
90
91 INLINE uint16_t cpu_to_be16(uint16_t x)
92 {
93         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
94 }
95
96 INLINE uint16_t cpu_to_le16(uint16_t x)
97 {
98         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
99 }
100
101 INLINE uint32_t cpu_to_be32(uint32_t x)
102 {
103         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
104 }
105
106 INLINE uint32_t cpu_to_le32(uint32_t x)
107 {
108         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
109 }
110
111 INLINE float cpu_to_be_float(float x)
112 {
113         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
114 }
115
116 INLINE float cpu_to_le_float(float x)
117 {
118         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
119 }
120
121 INLINE uint16_t be16_to_cpu(uint16_t x)
122 {
123         return cpu_to_be16(x);
124 }
125
126 INLINE uint16_t le16_to_cpu(uint16_t x)
127 {
128         return cpu_to_le16(x);
129 }
130
131 INLINE uint32_t be32_to_cpu(uint32_t x)
132 {
133         return cpu_to_be32(x);
134 }
135
136 INLINE uint32_t le32_to_cpu(uint32_t x)
137 {
138         return cpu_to_le32(x);
139 }
140
141 INLINE float be_float_to_cpu(float x)
142 {
143         return cpu_to_be_float(x);
144 }
145
146 INLINE float le_float_to_cpu(float x)
147 {
148         return cpu_to_le_float(x);
149 }
150
151 INLINE uint16_t host_to_net16(uint16_t x)
152 {
153         return cpu_to_be16(x);
154 }
155
156 INLINE uint16_t net_to_host16(uint16_t x)
157 {
158         return be16_to_cpu(x);
159 }
160
161 INLINE uint32_t host_to_net32(uint32_t x)
162 {
163         return cpu_to_be32(x);
164 }
165
166 INLINE uint32_t net_to_host32(uint32_t x)
167 {
168         return be32_to_cpu(x);
169 }
170
171 INLINE float host_to_net_float(float x)
172 {
173         return cpu_to_be_float(x);
174 }
175
176 INLINE float net_to_host_float(float x)
177 {
178         return be_float_to_cpu(x);
179 }
180
181 #ifdef __cplusplus
182
183 /// Type generic byte swapping.
184 template<typename T>
185 INLINE T swab(T x);
186
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); }
192
193 /// Type generic conversion from CPU byte order to big-endian byte order.
194 template<typename T>
195 INLINE T cpu_to_be(T x)
196 {
197         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
198 }
199
200 /// Type generic conversion from CPU byte-order to little-endian.
201 template<typename T>
202 INLINE T cpu_to_le(T x)
203 {
204         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
205 }
206
207 /// Type generic conversion from big endian byte-order to CPU byte order.
208 template<typename T>
209 INLINE T be_to_cpu(T x)
210 {
211         return cpu_to_be(x);
212 }
213
214 /// Type generic conversion from little-endian byte order to CPU byte order.
215 template<typename T>
216 INLINE T le_to_cpu(T x)
217 {
218         return cpu_to_le(x);
219 }
220
221 /// Type generic conversion from network byte order to host byte order.
222 template<typename T>
223 INLINE T net_to_host(T x)
224 {
225         return be_to_cpu(x);
226 }
227
228 /// Type generic conversion from host byte order to network byte order.
229 template<typename T>
230 INLINE T host_to_net(T x)
231 {
232         return net_to_host(x);
233 }
234
235 #endif /* __cplusplus */
236
237 #endif /* MWARE_BYTEORDER_H */