82bd9241cbd9ba07e26445936c8b2932b200450d
[bertos.git] / mware / byteorder.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Functions to convert integers to/from host byte-order.
34  *
35  * \version $Id$
36  *
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  */
40
41 /*#*
42  *#* $Log$
43  *#* Revision 1.10  2006/06/10 05:37:03  bernie
44  *#* Convert to new Doxygen comments.
45  *#*
46  *#* Revision 1.9  2005/11/04 16:20:02  bernie
47  *#* Fix reference to README.devlib in header.
48  *#*
49  *#* Revision 1.8  2005/06/14 06:16:03  bernie
50  *#* Add all missing functions.
51  *#*
52  *#* Revision 1.7  2005/04/12 04:08:49  bernie
53  *#* host_to_net(16|32)(), net_to_host(16|32)(): New functions.
54  *#*
55  *#* Revision 1.6  2005/04/11 19:10:28  bernie
56  *#* Include top-level headers from cfg/ subdir.
57  *#*
58  *#* Revision 1.5  2004/08/25 14:12:09  rasky
59  *#* Aggiornato il comment block dei log RCS
60  *#*
61  *#* Revision 1.4  2004/07/22 01:08:43  bernie
62  *#* swab32(): Fix a very serious bug.
63  *#*
64  *#* Revision 1.3  2004/07/20 23:47:12  bernie
65  *#* Finally remove redundant protos.
66  *#*
67  *#* Revision 1.2  2004/07/20 17:09:11  bernie
68  *#* swab16(), swab32(), cpu_to_be32(), cpu_to_le32(): New functions.
69  *#*
70  *#* Revision 1.1  2004/07/20 16:26:15  bernie
71  *#* Import byte-order macros into DevLib.
72  *#*
73  *#*/
74
75 #ifndef MWARE_BYTEORDER_H
76 #define MWARE_BYTEORDER_H
77
78 #include <cfg/compiler.h>
79 #include <cfg/cpu.h>
80
81 /**
82  * Swap upper and lower bytes in a 16-bit value.
83  */
84 INLINE uint16_t swab16(uint16_t x)
85 {
86         return    ((x & (uint16_t)0x00FFU) << 8)
87                 | ((x & (uint16_t)0xFF00U) >> 8);
88 }
89
90 /**
91  * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
92  */
93 INLINE uint32_t swab32(uint32_t x)
94 {
95         return    ((x & (uint32_t)0x000000FFUL) << 24)
96                 | ((x & (uint32_t)0x0000FF00UL) <<  8)
97                 | ((x & (uint32_t)0x00FF0000UL) >>  8)
98                 | ((x & (uint32_t)0xFF000000UL) >> 24);
99 }
100
101 /**
102  * Reverse bytes in a float value.
103  */
104 INLINE float swab_float(float x)
105 {
106         /* Avoid breaking strict aliasing rules.  */
107         char *cx = (char *)(&x);
108         STATIC_ASSERT(sizeof(float) == 4);
109         #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b))
110         BYTEORDER_SWAP(cx[0], cx[3]);
111         BYTEORDER_SWAP(cx[1], cx[2]);
112         #undef BYTEORDER_SWAP
113         return x;
114 }
115
116 INLINE uint16_t cpu_to_be16(uint16_t x)
117 {
118         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
119 }
120
121 INLINE uint16_t cpu_to_le16(uint16_t x)
122 {
123         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
124 }
125
126 INLINE uint32_t cpu_to_be32(uint32_t x)
127 {
128         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
129 }
130
131 INLINE uint32_t cpu_to_le32(uint32_t x)
132 {
133         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
134 }
135
136 INLINE float cpu_to_be_float(float x)
137 {
138         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
139 }
140
141 INLINE float cpu_to_le_float(float x)
142 {
143         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
144 }
145
146 INLINE uint16_t be16_to_cpu(uint16_t x)
147 {
148         return cpu_to_be16(x);
149 }
150
151 INLINE uint16_t le16_to_cpu(uint16_t x)
152 {
153         return cpu_to_le16(x);
154 }
155
156 INLINE uint32_t be32_to_cpu(uint32_t x)
157 {
158         return cpu_to_be32(x);
159 }
160
161 INLINE uint32_t le32_to_cpu(uint32_t x)
162 {
163         return cpu_to_le32(x);
164 }
165
166 INLINE float be_float_to_cpu(float x)
167 {
168         return cpu_to_be_float(x);
169 }
170
171 INLINE float le_float_to_cpu(float x)
172 {
173         return cpu_to_le_float(x);
174 }
175
176 INLINE uint16_t host_to_net16(uint16_t x)
177 {
178         return cpu_to_be16(x);
179 }
180
181 INLINE uint16_t net_to_host16(uint16_t x)
182 {
183         return be16_to_cpu(x);
184 }
185
186 INLINE uint32_t host_to_net32(uint32_t x)
187 {
188         return cpu_to_be32(x);
189 }
190
191 INLINE uint32_t net_to_host32(uint32_t x)
192 {
193         return be32_to_cpu(x);
194 }
195
196 INLINE float host_to_net_float(float x)
197 {
198         return cpu_to_be_float(x);
199 }
200
201 INLINE float net_to_host_float(float x)
202 {
203         return be_float_to_cpu(x);
204 }
205
206 #ifdef __cplusplus
207
208 /// Type generic byte swapping.
209 template<typename T>
210 INLINE T swab(T x);
211
212 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
213 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
214 template<> INLINE int16_t  swab(int16_t x)  { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
215 template<> INLINE int32_t  swab(int32_t x)  { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
216 template<> INLINE float    swab(float x)    { return swab_float(x); }
217
218 /// Type generic conversion from CPU byte order to big-endian byte order.
219 template<typename T>
220 INLINE T cpu_to_be(T x)
221 {
222         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
223 }
224
225 /// Type generic conversion from CPU byte-order to little-endian.
226 template<typename T>
227 INLINE T cpu_to_le(T x)
228 {
229         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
230 }
231
232 /// Type generic conversion from big endian byte-order to CPU byte order.
233 template<typename T>
234 INLINE T be_to_cpu(T x)
235 {
236         return cpu_to_be(x);
237 }
238
239 /// Type generic conversion from little-endian byte order to CPU byte order.
240 template<typename T>
241 INLINE T le_to_cpu(T x)
242 {
243         return cpu_to_le(x);
244 }
245
246 /// Type generic conversion from network byte order to host byte order.
247 template<typename T>
248 INLINE T net_to_host(T x)
249 {
250         return be_to_cpu(x);
251 }
252
253 /// Type generic conversion from host byte order to network byte order.
254 template<typename T>
255 INLINE T host_to_net(T x)
256 {
257         return net_to_host(x);
258 }
259
260 #endif /* __cplusplus */
261
262 #endif /* MWARE_BYTEORDER_H */