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