GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / include / bcmendian.h
blobe910885219741c9b41c41cbd253335752015da4b
1 /*
2 * Byte order utilities
4 * Copyright (C) 2012, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * $Id: bcmendian.h 358865 2012-09-25 19:04:27Z $
20 * This file by default provides proper behavior on little-endian architectures.
21 * On big-endian architectures, IL_BIGENDIAN should be defined.
24 #ifndef _BCMENDIAN_H_
25 #define _BCMENDIAN_H_
27 #include <typedefs.h>
29 /* Reverse the bytes in a 16-bit value */
30 #define BCMSWAP16(val) \
31 ((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
32 (((uint16)(val) & (uint16)0xff00U) >> 8)))
34 /* Reverse the bytes in a 32-bit value */
35 #define BCMSWAP32(val) \
36 ((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
37 (((uint32)(val) & (uint32)0x0000ff00U) << 8) | \
38 (((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \
39 (((uint32)(val) & (uint32)0xff000000U) >> 24)))
41 /* Reverse the two 16-bit halves of a 32-bit value */
42 #define BCMSWAP32BY16(val) \
43 ((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
44 (((uint32)(val) & (uint32)0xffff0000U) >> 16)))
46 /* Reverse the bytes in a 64-bit value */
47 #define BCMSWAP64(val) \
48 ((uint64)((((uint64)(val) & 0x00000000000000ffULL) << 56) | \
49 (((uint64)(val) & 0x000000000000ff00ULL) << 40) | \
50 (((uint64)(val) & 0x0000000000ff0000ULL) << 24) | \
51 (((uint64)(val) & 0x00000000ff000000ULL) << 8) | \
52 (((uint64)(val) & 0x000000ff00000000ULL) >> 8) | \
53 (((uint64)(val) & 0x0000ff0000000000ULL) >> 24) | \
54 (((uint64)(val) & 0x00ff000000000000ULL) >> 40) | \
55 (((uint64)(val) & 0xff00000000000000ULL) >> 56)))
57 /* Reverse the two 32-bit halves of a 64-bit value */
58 #define BCMSWAP64BY32(val) \
59 ((uint64)((((uint64)(val) & 0x00000000ffffffffULL) << 32) | \
60 (((uint64)(val) & 0xffffffff00000000ULL) >> 32)))
63 /* Byte swapping macros
64 * Host <=> Network (Big Endian) for 16- and 32-bit values
65 * Host <=> Little-Endian for 16- and 32-bit values
67 #ifndef hton16
68 #ifndef IL_BIGENDIAN
69 #define HTON16(i) BCMSWAP16(i)
70 #define hton16(i) bcmswap16(i)
71 #define HTON32(i) BCMSWAP32(i)
72 #define hton32(i) bcmswap32(i)
73 #define NTOH16(i) BCMSWAP16(i)
74 #define ntoh16(i) bcmswap16(i)
75 #define NTOH32(i) BCMSWAP32(i)
76 #define ntoh32(i) bcmswap32(i)
77 #define LTOH16(i) (i)
78 #define ltoh16(i) (i)
79 #define LTOH32(i) (i)
80 #define ltoh32(i) (i)
81 #define HTOL16(i) (i)
82 #define htol16(i) (i)
83 #define HTOL32(i) (i)
84 #define htol32(i) (i)
85 #define HTOL64(i) (i)
86 #define htol64(i) (i)
87 #else /* IL_BIGENDIAN */
88 #define HTON16(i) (i)
89 #define hton16(i) (i)
90 #define HTON32(i) (i)
91 #define hton32(i) (i)
92 #define NTOH16(i) (i)
93 #define ntoh16(i) (i)
94 #define NTOH32(i) (i)
95 #define ntoh32(i) (i)
96 #define LTOH16(i) BCMSWAP16(i)
97 #define ltoh16(i) bcmswap16(i)
98 #define LTOH32(i) BCMSWAP32(i)
99 #define ltoh32(i) bcmswap32(i)
100 #define HTOL16(i) BCMSWAP16(i)
101 #define htol16(i) bcmswap16(i)
102 #define HTOL32(i) BCMSWAP32(i)
103 #define htol32(i) bcmswap32(i)
104 #define HTOL64(i) BCMSWAP64(i)
105 #define htol64(i) bcmswap64(i)
106 #endif /* IL_BIGENDIAN */
107 #endif /* hton16 */
109 #ifndef IL_BIGENDIAN
110 #define ltoh16_buf(buf, i)
111 #define htol16_buf(buf, i)
112 #else
113 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))
114 #define htol16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))
115 #endif /* IL_BIGENDIAN */
117 /* Unaligned loads and stores in host byte order */
118 #ifndef IL_BIGENDIAN
119 #define load32_ua(a) ltoh32_ua(a)
120 #define store32_ua(a, v) htol32_ua_store(v, a)
121 #define load16_ua(a) ltoh16_ua(a)
122 #define store16_ua(a, v) htol16_ua_store(v, a)
123 #else
124 #define load32_ua(a) ntoh32_ua(a)
125 #define store32_ua(a, v) hton32_ua_store(v, a)
126 #define load16_ua(a) ntoh16_ua(a)
127 #define store16_ua(a, v) hton16_ua_store(v, a)
128 #endif /* IL_BIGENDIAN */
130 #define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))
131 #define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
132 #define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])
133 #define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
135 #define ltoh_ua(ptr) \
136 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
137 sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)(ptr)) : \
138 sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)(ptr)) : \
139 *(uint8 *)0)
141 #define ntoh_ua(ptr) \
142 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
143 sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)(ptr)) : \
144 sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)(ptr)) : \
145 *(uint8 *)0)
147 #ifdef __GNUC__
149 /* GNU macro versions avoid referencing the argument multiple times, while also
150 * avoiding the -fno-inline used in ROM builds.
153 #define bcmswap16(val) ({ \
154 uint16 _val = (val); \
155 BCMSWAP16(_val); \
158 #define bcmswap32(val) ({ \
159 uint32 _val = (val); \
160 BCMSWAP32(_val); \
163 #define bcmswap64(val) ({ \
164 uint64 _val = (val); \
165 BCMSWAP64(_val); \
168 #define bcmswap32by16(val) ({ \
169 uint32 _val = (val); \
170 BCMSWAP32BY16(_val); \
173 #define bcmswap16_buf(buf, len) ({ \
174 uint16 *_buf = (uint16 *)(buf); \
175 uint _wds = (len) / 2; \
176 while (_wds--) { \
177 *_buf = bcmswap16(*_buf); \
178 _buf++; \
182 #define htol16_ua_store(val, bytes) ({ \
183 uint16 _val = (val); \
184 uint8 *_bytes = (uint8 *)(bytes); \
185 _bytes[0] = _val & 0xff; \
186 _bytes[1] = _val >> 8; \
189 #define htol32_ua_store(val, bytes) ({ \
190 uint32 _val = (val); \
191 uint8 *_bytes = (uint8 *)(bytes); \
192 _bytes[0] = _val & 0xff; \
193 _bytes[1] = (_val >> 8) & 0xff; \
194 _bytes[2] = (_val >> 16) & 0xff; \
195 _bytes[3] = _val >> 24; \
198 #define hton16_ua_store(val, bytes) ({ \
199 uint16 _val = (val); \
200 uint8 *_bytes = (uint8 *)(bytes); \
201 _bytes[0] = _val >> 8; \
202 _bytes[1] = _val & 0xff; \
205 #define hton32_ua_store(val, bytes) ({ \
206 uint32 _val = (val); \
207 uint8 *_bytes = (uint8 *)(bytes); \
208 _bytes[0] = _val >> 24; \
209 _bytes[1] = (_val >> 16) & 0xff; \
210 _bytes[2] = (_val >> 8) & 0xff; \
211 _bytes[3] = _val & 0xff; \
214 #define ltoh16_ua(bytes) ({ \
215 const uint8 *_bytes = (const uint8 *)(bytes); \
216 _LTOH16_UA(_bytes); \
219 #define ltoh32_ua(bytes) ({ \
220 const uint8 *_bytes = (const uint8 *)(bytes); \
221 _LTOH32_UA(_bytes); \
224 #define ntoh16_ua(bytes) ({ \
225 const uint8 *_bytes = (const uint8 *)(bytes); \
226 _NTOH16_UA(_bytes); \
229 #define ntoh32_ua(bytes) ({ \
230 const uint8 *_bytes = (const uint8 *)(bytes); \
231 _NTOH32_UA(_bytes); \
234 #else /* !__GNUC__ */
236 /* Inline versions avoid referencing the argument multiple times */
237 static INLINE uint16
238 bcmswap16(uint16 val)
240 return BCMSWAP16(val);
243 static INLINE uint32
244 bcmswap32(uint32 val)
246 return BCMSWAP32(val);
249 static INLINE uint32
250 bcmswap32by16(uint32 val)
252 return BCMSWAP32BY16(val);
255 /* Reverse pairs of bytes in a buffer (not for high-performance use) */
256 /* buf - start of buffer of shorts to swap */
257 /* len - byte length of buffer */
258 static INLINE void
259 bcmswap16_buf(uint16 *buf, uint len)
261 len = len / 2;
263 while (len--) {
264 *buf = bcmswap16(*buf);
265 buf++;
270 * Store 16-bit value to unaligned little-endian byte array.
272 static INLINE void
273 htol16_ua_store(uint16 val, uint8 *bytes)
275 bytes[0] = val & 0xff;
276 bytes[1] = val >> 8;
280 * Store 32-bit value to unaligned little-endian byte array.
282 static INLINE void
283 htol32_ua_store(uint32 val, uint8 *bytes)
285 bytes[0] = val & 0xff;
286 bytes[1] = (val >> 8) & 0xff;
287 bytes[2] = (val >> 16) & 0xff;
288 bytes[3] = val >> 24;
292 * Store 16-bit value to unaligned network-(big-)endian byte array.
294 static INLINE void
295 hton16_ua_store(uint16 val, uint8 *bytes)
297 bytes[0] = val >> 8;
298 bytes[1] = val & 0xff;
302 * Store 32-bit value to unaligned network-(big-)endian byte array.
304 static INLINE void
305 hton32_ua_store(uint32 val, uint8 *bytes)
307 bytes[0] = val >> 24;
308 bytes[1] = (val >> 16) & 0xff;
309 bytes[2] = (val >> 8) & 0xff;
310 bytes[3] = val & 0xff;
314 * Load 16-bit value from unaligned little-endian byte array.
316 static INLINE uint16
317 ltoh16_ua(const void *bytes)
319 return _LTOH16_UA((const uint8 *)bytes);
323 * Load 32-bit value from unaligned little-endian byte array.
325 static INLINE uint32
326 ltoh32_ua(const void *bytes)
328 return _LTOH32_UA((const uint8 *)bytes);
332 * Load 16-bit value from unaligned big-(network-)endian byte array.
334 static INLINE uint16
335 ntoh16_ua(const void *bytes)
337 return _NTOH16_UA((const uint8 *)bytes);
341 * Load 32-bit value from unaligned big-(network-)endian byte array.
343 static INLINE uint32
344 ntoh32_ua(const void *bytes)
346 return _NTOH32_UA((const uint8 *)bytes);
349 #endif /* !__GNUC__ */
350 #endif /* !_BCMENDIAN_H_ */