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