pgm, byteorder: fix headers
[bertos.git] / bertos / cpu / 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  * \brief Functions to convert integers to/from host byte-order.
33  *
34  * \version $Id$
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  * \author Stefano Fedrigo <aleph@develer.com>
37  */
38
39 #ifndef MWARE_BYTEORDER_H
40 #define MWARE_BYTEORDER_H
41
42 #include <cfg/compiler.h>
43 #include <cpu/attr.h>
44
45 /**
46  * Swap upper and lower bytes in a 16-bit value.
47  */
48 INLINE uint16_t swab16(uint16_t x)
49 {
50         return    ((x & (uint16_t)0x00FFU) << 8)
51                 | ((x & (uint16_t)0xFF00U) >> 8);
52 }
53
54 /**
55  * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
56  */
57 INLINE uint32_t swab32(uint32_t x)
58 {
59         return    ((x & (uint32_t)0x000000FFUL) << 24)
60                 | ((x & (uint32_t)0x0000FF00UL) <<  8)
61                 | ((x & (uint32_t)0x00FF0000UL) >>  8)
62                 | ((x & (uint32_t)0xFF000000UL) >> 24);
63 }
64
65 /**
66  * Reverse bytes in a 64-bit value.
67  */
68 INLINE uint64_t swab64(uint64_t x)
69 {
70         return (uint64_t)swab32(x >> 32)
71                 | ((uint64_t)swab32(x & 0xFFFFFFFFUL) << 32);
72 }
73
74 /**
75  * Reverse bytes in a float value.
76  */
77 INLINE float swab_float(float x)
78 {
79         /* Avoid breaking strict aliasing rules.  */
80         char *cx = (char *)(&x);
81         STATIC_ASSERT(sizeof(float) == 4);
82         #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b))
83         BYTEORDER_SWAP(cx[0], cx[3]);
84         BYTEORDER_SWAP(cx[1], cx[2]);
85         #undef BYTEORDER_SWAP
86         return x;
87 }
88
89 INLINE uint16_t cpu_to_be16(uint16_t x)
90 {
91         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
92 }
93
94 INLINE uint16_t cpu_to_le16(uint16_t x)
95 {
96         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
97 }
98
99 INLINE uint32_t cpu_to_be32(uint32_t x)
100 {
101         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
102 }
103
104 INLINE uint32_t cpu_to_le32(uint32_t x)
105 {
106         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
107 }
108
109 INLINE uint64_t cpu_to_be64(uint64_t x)
110 {
111         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab64(x) : x;
112 }
113
114 INLINE uint64_t cpu_to_le64(uint64_t x)
115 {
116         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab64(x) : x;
117 }
118
119 INLINE float cpu_to_be_float(float x)
120 {
121         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
122 }
123
124 INLINE float cpu_to_le_float(float x)
125 {
126         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
127 }
128
129 INLINE uint16_t be16_to_cpu(uint16_t x)
130 {
131         return cpu_to_be16(x);
132 }
133
134 INLINE uint16_t le16_to_cpu(uint16_t x)
135 {
136         return cpu_to_le16(x);
137 }
138
139 INLINE uint32_t be32_to_cpu(uint32_t x)
140 {
141         return cpu_to_be32(x);
142 }
143
144 INLINE uint32_t le32_to_cpu(uint32_t x)
145 {
146         return cpu_to_le32(x);
147 }
148
149 INLINE uint64_t be64_to_cpu(uint64_t x)
150 {
151         return cpu_to_be64(x);
152 }
153
154 INLINE uint64_t le64_to_cpu(uint64_t x)
155 {
156         return cpu_to_le64(x);
157 }
158
159 INLINE float be_float_to_cpu(float x)
160 {
161         return cpu_to_be_float(x);
162 }
163
164 INLINE float le_float_to_cpu(float x)
165 {
166         return cpu_to_le_float(x);
167 }
168
169 INLINE uint16_t host_to_net16(uint16_t x)
170 {
171         return cpu_to_be16(x);
172 }
173
174 INLINE uint16_t net_to_host16(uint16_t x)
175 {
176         return be16_to_cpu(x);
177 }
178
179 INLINE uint32_t host_to_net32(uint32_t x)
180 {
181         return cpu_to_be32(x);
182 }
183
184 INLINE uint32_t net_to_host32(uint32_t x)
185 {
186         return be32_to_cpu(x);
187 }
188
189 INLINE uint64_t host_to_net64(uint64_t x)
190 {
191         return cpu_to_be64(x);
192 }
193
194 INLINE uint64_t net_to_host64(uint64_t x)
195 {
196         return be64_to_cpu(x);
197 }
198
199 INLINE float host_to_net_float(float x)
200 {
201         return cpu_to_be_float(x);
202 }
203
204 INLINE float net_to_host_float(float x)
205 {
206         return be_float_to_cpu(x);
207 }
208
209 #ifdef __cplusplus
210
211 /// Type generic byte swapping.
212 template<typename T>
213 INLINE T swab(T x);
214
215 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
216 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
217 template<> INLINE uint64_t swab(uint64_t x) { return swab64(x); }
218 template<> INLINE int16_t  swab(int16_t x)  { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
219 template<> INLINE int32_t  swab(int32_t x)  { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
220 template<> INLINE int64_t  swab(int64_t x)  { return static_cast<int64_t>(swab64(static_cast<uint64_t>(x))); }
221 template<> INLINE float    swab(float x)    { return swab_float(x); }
222
223 /// Type generic conversion from CPU byte order to big-endian byte order.
224 template<typename T>
225 INLINE T cpu_to_be(T x)
226 {
227         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
228 }
229
230 /// Type generic conversion from CPU byte-order to little-endian.
231 template<typename T>
232 INLINE T cpu_to_le(T x)
233 {
234         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
235 }
236
237 /// Type generic conversion from big endian byte-order to CPU byte order.
238 template<typename T>
239 INLINE T be_to_cpu(T x)
240 {
241         return cpu_to_be(x);
242 }
243
244 /// Type generic conversion from little-endian byte order to CPU byte order.
245 template<typename T>
246 INLINE T le_to_cpu(T x)
247 {
248         return cpu_to_le(x);
249 }
250
251 /// Type generic conversion from network byte order to host byte order.
252 template<typename T>
253 INLINE T net_to_host(T x)
254 {
255         return be_to_cpu(x);
256 }
257
258 /// Type generic conversion from host byte order to network byte order.
259 template<typename T>
260 INLINE T host_to_net(T x)
261 {
262         return net_to_host(x);
263 }
264
265 #endif /* __cplusplus */
266
267 #endif /* MWARE_BYTEORDER_H */