2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Reverse the bytes in a 16-bit value */
21 #define BCMSWAP16(val) \
22 ((u16)((((u16)(val) & (u16)0x00ffU) << 8) | \
23 (((u16)(val) & (u16)0xff00U) >> 8)))
25 /* Reverse the bytes in a 32-bit value */
26 #define BCMSWAP32(val) \
27 ((u32)((((u32)(val) & (u32)0x000000ffU) << 24) | \
28 (((u32)(val) & (u32)0x0000ff00U) << 8) | \
29 (((u32)(val) & (u32)0x00ff0000U) >> 8) | \
30 (((u32)(val) & (u32)0xff000000U) >> 24)))
32 /* Reverse the two 16-bit halves of a 32-bit value */
33 #define BCMSWAP32BY16(val) \
34 ((u32)((((u32)(val) & (u32)0x0000ffffU) << 16) | \
35 (((u32)(val) & (u32)0xffff0000U) >> 16)))
37 /* Byte swapping macros
38 * Host <=> Network (Big Endian) for 16- and 32-bit values
39 * Host <=> Little-Endian for 16- and 32-bit values
43 #define HTON16(i) BCMSWAP16(i)
44 #define hton16(i) bcmswap16(i)
45 #define HTON32(i) BCMSWAP32(i)
46 #define hton32(i) bcmswap32(i)
47 #define NTOH16(i) BCMSWAP16(i)
48 #define ntoh16(i) bcmswap16(i)
49 #define NTOH32(i) BCMSWAP32(i)
50 #define ntoh32(i) bcmswap32(i)
59 #else /* IL_BIGENDIAN */
68 #define LTOH16(i) BCMSWAP16(i)
69 #define ltoh16(i) bcmswap16(i)
70 #define LTOH32(i) BCMSWAP32(i)
71 #define ltoh32(i) bcmswap32(i)
72 #define HTOL16(i) BCMSWAP16(i)
73 #define htol16(i) bcmswap16(i)
74 #define HTOL32(i) BCMSWAP32(i)
75 #define htol32(i) bcmswap32(i)
76 #endif /* IL_BIGENDIAN */
80 #define ltoh16_buf(buf, i)
81 #define htol16_buf(buf, i)
83 #define ltoh16_buf(buf, i) bcmswap16_buf((u16 *)(buf), (i))
84 #define htol16_buf(buf, i) bcmswap16_buf((u16 *)(buf), (i))
85 #endif /* IL_BIGENDIAN */
87 /* Unaligned loads and stores in host byte order */
89 #define load32_ua(a) ltoh32_ua(a)
90 #define store32_ua(a, v) htol32_ua_store(v, a)
91 #define load16_ua(a) ltoh16_ua(a)
92 #define store16_ua(a, v) htol16_ua_store(v, a)
94 #define load32_ua(a) ntoh32_ua(a)
95 #define store32_ua(a, v) hton32_ua_store(v, a)
96 #define load16_ua(a) ntoh16_ua(a)
97 #define store16_ua(a, v) hton16_ua_store(v, a)
98 #endif /* IL_BIGENDIAN */
100 #define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))
101 #define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
102 #define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])
103 #define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
105 #define ltoh_ua(ptr) \
106 (sizeof(*(ptr)) == sizeof(u8) ? *(const u8 *)(ptr) : \
107 sizeof(*(ptr)) == sizeof(u16) ? _LTOH16_UA((const u8 *)(ptr)) : \
108 sizeof(*(ptr)) == sizeof(u32) ? _LTOH32_UA((const u8 *)(ptr)) : \
111 #define ntoh_ua(ptr) \
112 (sizeof(*(ptr)) == sizeof(u8) ? *(const u8 *)(ptr) : \
113 sizeof(*(ptr)) == sizeof(u16) ? _NTOH16_UA((const u8 *)(ptr)) : \
114 sizeof(*(ptr)) == sizeof(u32) ? _NTOH32_UA((const u8 *)(ptr)) : \
119 /* GNU macro versions avoid referencing the argument multiple times, while also
120 * avoiding the -fno-inline used in ROM builds.
123 #define bcmswap16(val) ({ \
128 #define bcmswap32(val) ({ \
133 #define bcmswap32by16(val) ({ \
135 BCMSWAP32BY16(_val); \
138 #define bcmswap16_buf(buf, len) ({ \
139 u16 *_buf = (u16 *)(buf); \
140 uint _wds = (len) / 2; \
142 *_buf = bcmswap16(*_buf); \
147 #define htol16_ua_store(val, bytes) ({ \
149 u8 *_bytes = (u8 *)(bytes); \
150 _bytes[0] = _val & 0xff; \
151 _bytes[1] = _val >> 8; \
154 #define htol32_ua_store(val, bytes) ({ \
156 u8 *_bytes = (u8 *)(bytes); \
157 _bytes[0] = _val & 0xff; \
158 _bytes[1] = (_val >> 8) & 0xff; \
159 _bytes[2] = (_val >> 16) & 0xff; \
160 _bytes[3] = _val >> 24; \
163 #define hton16_ua_store(val, bytes) ({ \
165 u8 *_bytes = (u8 *)(bytes); \
166 _bytes[0] = _val >> 8; \
167 _bytes[1] = _val & 0xff; \
170 #define hton32_ua_store(val, bytes) ({ \
172 u8 *_bytes = (u8 *)(bytes); \
173 _bytes[0] = _val >> 24; \
174 _bytes[1] = (_val >> 16) & 0xff; \
175 _bytes[2] = (_val >> 8) & 0xff; \
176 _bytes[3] = _val & 0xff; \
179 #define ltoh16_ua(bytes) ({ \
180 const u8 *_bytes = (const u8 *)(bytes); \
181 _LTOH16_UA(_bytes); \
184 #define ltoh32_ua(bytes) ({ \
185 const u8 *_bytes = (const u8 *)(bytes); \
186 _LTOH32_UA(_bytes); \
189 #define ntoh16_ua(bytes) ({ \
190 const u8 *_bytes = (const u8 *)(bytes); \
191 _NTOH16_UA(_bytes); \
194 #define ntoh32_ua(bytes) ({ \
195 const u8 *_bytes = (const u8 *)(bytes); \
196 _NTOH32_UA(_bytes); \
199 #else /* !__GNUC__ */
201 /* Inline versions avoid referencing the argument multiple times */
202 static inline u16 bcmswap16(u16 val)
204 return BCMSWAP16(val);
207 static inline u32 bcmswap32(u32 val)
209 return BCMSWAP32(val);
212 static inline u32 bcmswap32by16(u32 val)
214 return BCMSWAP32BY16(val);
217 /* Reverse pairs of bytes in a buffer (not for high-performance use) */
218 /* buf - start of buffer of shorts to swap */
219 /* len - byte length of buffer */
220 static inline void bcmswap16_buf(u16 *buf, uint len)
225 *buf = bcmswap16(*buf);
231 * Store 16-bit value to unaligned little-endian byte array.
233 static inline void htol16_ua_store(u16 val, u8 *bytes)
235 bytes[0] = val & 0xff;
240 * Store 32-bit value to unaligned little-endian byte array.
242 static inline void htol32_ua_store(u32 val, u8 *bytes)
244 bytes[0] = val & 0xff;
245 bytes[1] = (val >> 8) & 0xff;
246 bytes[2] = (val >> 16) & 0xff;
247 bytes[3] = val >> 24;
251 * Store 16-bit value to unaligned network-(big-)endian byte array.
253 static inline void hton16_ua_store(u16 val, u8 *bytes)
256 bytes[1] = val & 0xff;
260 * Store 32-bit value to unaligned network-(big-)endian byte array.
262 static inline void hton32_ua_store(u32 val, u8 *bytes)
264 bytes[0] = val >> 24;
265 bytes[1] = (val >> 16) & 0xff;
266 bytes[2] = (val >> 8) & 0xff;
267 bytes[3] = val & 0xff;
271 * Load 16-bit value from unaligned little-endian byte array.
273 static inline u16 ltoh16_ua(const void *bytes)
275 return _LTOH16_UA((const u8 *)bytes);
279 * Load 32-bit value from unaligned little-endian byte array.
281 static inline u32 ltoh32_ua(const void *bytes)
283 return _LTOH32_UA((const u8 *)bytes);
287 * Load 16-bit value from unaligned big-(network-)endian byte array.
289 static inline u16 ntoh16_ua(const void *bytes)
291 return _NTOH16_UA((const u8 *)bytes);
295 * Load 32-bit value from unaligned big-(network-)endian byte array.
297 static inline u32 ntoh32_ua(const void *bytes)
299 return _NTOH32_UA((const u8 *)bytes);
302 #endif /* !__GNUC__ */
303 #endif /* !_BCMENDIAN_H_ */