Rename algos directory to algo.
[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 #ifndef MWARE_BYTEORDER_H
42 #define MWARE_BYTEORDER_H
43
44 #include <cfg/compiler.h>
45 #include <cpu/attr.h>
46
47 /**
48  * Swap upper and lower bytes in a 16-bit value.
49  */
50 INLINE uint16_t swab16(uint16_t x)
51 {
52         return    ((x & (uint16_t)0x00FFU) << 8)
53                 | ((x & (uint16_t)0xFF00U) >> 8);
54 }
55
56 /**
57  * Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
58  */
59 INLINE uint32_t swab32(uint32_t x)
60 {
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);
65 }
66
67 /**
68  * Reverse bytes in a float value.
69  */
70 INLINE float swab_float(float x)
71 {
72         /* Avoid breaking strict aliasing rules.  */
73         char *cx = (char *)(&x);
74         STATIC_ASSERT(sizeof(float) == 4);
75         #define BYTEORDER_SWAP(a, b) ((a) ^= (b) ^= (a) ^= (b))
76         BYTEORDER_SWAP(cx[0], cx[3]);
77         BYTEORDER_SWAP(cx[1], cx[2]);
78         #undef BYTEORDER_SWAP
79         return x;
80 }
81
82 INLINE uint16_t cpu_to_be16(uint16_t x)
83 {
84         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab16(x) : x;
85 }
86
87 INLINE uint16_t cpu_to_le16(uint16_t x)
88 {
89         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab16(x) : x;
90 }
91
92 INLINE uint32_t cpu_to_be32(uint32_t x)
93 {
94         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab32(x) : x;
95 }
96
97 INLINE uint32_t cpu_to_le32(uint32_t x)
98 {
99         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab32(x) : x;
100 }
101
102 INLINE float cpu_to_be_float(float x)
103 {
104         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab_float(x) : x;
105 }
106
107 INLINE float cpu_to_le_float(float x)
108 {
109         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab_float(x) : x;
110 }
111
112 INLINE uint16_t be16_to_cpu(uint16_t x)
113 {
114         return cpu_to_be16(x);
115 }
116
117 INLINE uint16_t le16_to_cpu(uint16_t x)
118 {
119         return cpu_to_le16(x);
120 }
121
122 INLINE uint32_t be32_to_cpu(uint32_t x)
123 {
124         return cpu_to_be32(x);
125 }
126
127 INLINE uint32_t le32_to_cpu(uint32_t x)
128 {
129         return cpu_to_le32(x);
130 }
131
132 INLINE float be_float_to_cpu(float x)
133 {
134         return cpu_to_be_float(x);
135 }
136
137 INLINE float le_float_to_cpu(float x)
138 {
139         return cpu_to_le_float(x);
140 }
141
142 INLINE uint16_t host_to_net16(uint16_t x)
143 {
144         return cpu_to_be16(x);
145 }
146
147 INLINE uint16_t net_to_host16(uint16_t x)
148 {
149         return be16_to_cpu(x);
150 }
151
152 INLINE uint32_t host_to_net32(uint32_t x)
153 {
154         return cpu_to_be32(x);
155 }
156
157 INLINE uint32_t net_to_host32(uint32_t x)
158 {
159         return be32_to_cpu(x);
160 }
161
162 INLINE float host_to_net_float(float x)
163 {
164         return cpu_to_be_float(x);
165 }
166
167 INLINE float net_to_host_float(float x)
168 {
169         return be_float_to_cpu(x);
170 }
171
172 #ifdef __cplusplus
173
174 /// Type generic byte swapping.
175 template<typename T>
176 INLINE T swab(T x);
177
178 template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
179 template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
180 template<> INLINE int16_t  swab(int16_t x)  { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
181 template<> INLINE int32_t  swab(int32_t x)  { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
182 template<> INLINE float    swab(float x)    { return swab_float(x); }
183
184 /// Type generic conversion from CPU byte order to big-endian byte order.
185 template<typename T>
186 INLINE T cpu_to_be(T x)
187 {
188         return (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN) ? swab(x) : x;
189 }
190
191 /// Type generic conversion from CPU byte-order to little-endian.
192 template<typename T>
193 INLINE T cpu_to_le(T x)
194 {
195         return (CPU_BYTE_ORDER == CPU_BIG_ENDIAN) ? swab(x) : x;
196 }
197
198 /// Type generic conversion from big endian byte-order to CPU byte order.
199 template<typename T>
200 INLINE T be_to_cpu(T x)
201 {
202         return cpu_to_be(x);
203 }
204
205 /// Type generic conversion from little-endian byte order to CPU byte order.
206 template<typename T>
207 INLINE T le_to_cpu(T x)
208 {
209         return cpu_to_le(x);
210 }
211
212 /// Type generic conversion from network byte order to host byte order.
213 template<typename T>
214 INLINE T net_to_host(T x)
215 {
216         return be_to_cpu(x);
217 }
218
219 /// Type generic conversion from host byte order to network byte order.
220 template<typename T>
221 INLINE T host_to_net(T x)
222 {
223         return net_to_host(x);
224 }
225
226 #endif /* __cplusplus */
227
228 #endif /* MWARE_BYTEORDER_H */