4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
33 * \brief Functions to convert integers to/from host byte-order.
37 * \author Bernie Innocenti <bernie@codewiz.org>
38 * \author Stefano Fedrigo <aleph@develer.com>
41 #ifndef MWARE_BYTEORDER_H
42 #define MWARE_BYTEORDER_H
44 #include <cfg/compiler.h>
48 * Swap upper and lower bytes in a 16-bit value.
50 INLINE uint16_t swab16(uint16_t x)
52 return ((x & (uint16_t)0x00FFU) << 8)
53 | ((x & (uint16_t)0xFF00U) >> 8);
57 * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
59 INLINE uint32_t swab32(uint32_t x)
61 return ((x & (uint32_t)0x000000FFUL) << 24)
62 | ((x & (uint32_t)0x0000FF00UL) << 8)
63 | ((x & (uint32_t)0x00FF0000UL) >> 8)
64 | ((x & (uint32_t)0xFF000000UL) >> 24);
68 * Reverse bytes in a 64-bit value.
70 INLINE uint64_t swab64(uint64_t x)
72 return (uint64_t)swab32(x >> 32)
73 | ((uint64_t)swab32(x & 0xFFFFFFFFUL) << 32);
77 * Reverse bytes in a float value.
79 INLINE float swab_float(float x)
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]);
91 INLINE uint16_t cpu_to_be16(uint16_t x)
93 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
96 INLINE uint16_t cpu_to_le16(uint16_t x)
98 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
101 INLINE uint32_t cpu_to_be32(uint32_t x)
103 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
106 INLINE uint32_t cpu_to_le32(uint32_t x)
108 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
111 INLINE uint64_t cpu_to_be64(uint64_t x)
113 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab64(x) : x;
116 INLINE uint64_t cpu_to_le64(uint64_t x)
118 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab64(x) : x;
121 INLINE float cpu_to_be_float(float x)
123 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
126 INLINE float cpu_to_le_float(float x)
128 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
131 INLINE uint16_t be16_to_cpu(uint16_t x)
133 return cpu_to_be16(x);
136 INLINE uint16_t le16_to_cpu(uint16_t x)
138 return cpu_to_le16(x);
141 INLINE uint32_t be32_to_cpu(uint32_t x)
143 return cpu_to_be32(x);
146 INLINE uint32_t le32_to_cpu(uint32_t x)
148 return cpu_to_le32(x);
151 INLINE uint64_t be64_to_cpu(uint64_t x)
153 return cpu_to_be64(x);
156 INLINE uint64_t le64_to_cpu(uint64_t x)
158 return cpu_to_le64(x);
161 INLINE float be_float_to_cpu(float x)
163 return cpu_to_be_float(x);
166 INLINE float le_float_to_cpu(float x)
168 return cpu_to_le_float(x);
171 INLINE uint16_t host_to_net16(uint16_t x)
173 return cpu_to_be16(x);
176 INLINE uint16_t net_to_host16(uint16_t x)
178 return be16_to_cpu(x);
181 INLINE uint32_t host_to_net32(uint32_t x)
183 return cpu_to_be32(x);
186 INLINE uint32_t net_to_host32(uint32_t x)
188 return be32_to_cpu(x);
191 INLINE uint64_t host_to_net64(uint64_t x)
193 return cpu_to_be64(x);
196 INLINE uint64_t net_to_host64(uint64_t x)
198 return be64_to_cpu(x);
201 INLINE float host_to_net_float(float x)
203 return cpu_to_be_float(x);
206 INLINE float net_to_host_float(float x)
208 return be_float_to_cpu(x);
213 /// Type generic byte swapping.
217 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
218 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
219 template<> INLINE uint64_t swab(uint64_t x) { return swab64(x); }
220 template<> INLINE int16_t swab(int16_t x) { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
221 template<> INLINE int32_t swab(int32_t x) { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
222 template<> INLINE int64_t swab(int64_t x) { return static_cast<int64_t>(swab64(static_cast<uint64_t>(x))); }
223 template<> INLINE float swab(float x) { return swab_float(x); }
225 /// Type generic conversion from CPU byte order to big-endian byte order.
227 INLINE T cpu_to_be(T x)
229 return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
232 /// Type generic conversion from CPU byte-order to little-endian.
234 INLINE T cpu_to_le(T x)
236 return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
239 /// Type generic conversion from big endian byte-order to CPU byte order.
241 INLINE T be_to_cpu(T x)
246 /// Type generic conversion from little-endian byte order to CPU byte order.
248 INLINE T le_to_cpu(T x)
253 /// Type generic conversion from network byte order to host byte order.
255 INLINE T net_to_host(T x)
260 /// Type generic conversion from host byte order to network byte order.
262 INLINE T host_to_net(T x)
264 return net_to_host(x);
267 #endif /* __cplusplus */
269 #endif /* MWARE_BYTEORDER_H */