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