• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Byte order utilities
3  *
4  * Copyright (C) 1999-2013, Broadcom Corporation
5  *
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  *
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *      Notwithstanding the above, under no circumstances may you combine this
21  * software in any way with any other Broadcom software provided under a license
22  * other than the GPL, without Broadcom's express prior written consent.
23  *
24  *  $Id: bcmendian.h 241182 2011-02-17 21:50:03Z $
25  *
26  * This file by default provides proper behavior on little-endian architectures.
27  * On big-endian architectures, IL_BIGENDIAN should be defined.
28  */
29 
30 #ifndef _BCMENDIAN_H_
31 #define _BCMENDIAN_H_
32 
33 #include <typedefs.h>
34 
35 /* Reverse the bytes in a 16-bit value */
36 #define BCMSWAP16(val) \
37 	((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
38 		  (((uint16)(val) & (uint16)0xff00U) >> 8)))
39 
40 /* Reverse the bytes in a 32-bit value */
41 #define BCMSWAP32(val) \
42 	((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
43 		  (((uint32)(val) & (uint32)0x0000ff00U) <<  8) | \
44 		  (((uint32)(val) & (uint32)0x00ff0000U) >>  8) | \
45 		  (((uint32)(val) & (uint32)0xff000000U) >> 24)))
46 
47 /* Reverse the two 16-bit halves of a 32-bit value */
48 #define BCMSWAP32BY16(val) \
49 	((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
50 		  (((uint32)(val) & (uint32)0xffff0000U) >> 16)))
51 
52 /* Byte swapping macros
53  *    Host <=> Network (Big Endian) for 16- and 32-bit values
54  *    Host <=> Little-Endian for 16- and 32-bit values
55  */
56 #ifndef hton16
57 #define HTON16(i) BCMSWAP16(i)
58 #define	hton16(i) bcmswap16(i)
59 #define	HTON32(i) BCMSWAP32(i)
60 #define	hton32(i) bcmswap32(i)
61 #define	NTOH16(i) BCMSWAP16(i)
62 #define	ntoh16(i) bcmswap16(i)
63 #define	NTOH32(i) BCMSWAP32(i)
64 #define	ntoh32(i) bcmswap32(i)
65 #define LTOH16(i) (i)
66 #define ltoh16(i) (i)
67 #define LTOH32(i) (i)
68 #define ltoh32(i) (i)
69 #define HTOL16(i) (i)
70 #define htol16(i) (i)
71 #define HTOL32(i) (i)
72 #define htol32(i) (i)
73 #endif /* hton16 */
74 
75 #define ltoh16_buf(buf, i)
76 #define htol16_buf(buf, i)
77 
78 /* Unaligned loads and stores in host byte order */
79 #define load32_ua(a)		ltoh32_ua(a)
80 #define store32_ua(a, v)	htol32_ua_store(v, a)
81 #define load16_ua(a)		ltoh16_ua(a)
82 #define store16_ua(a, v)	htol16_ua_store(v, a)
83 
84 #define _LTOH16_UA(cp)	((cp)[0] | ((cp)[1] << 8))
85 #define _LTOH32_UA(cp)	((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
86 #define _NTOH16_UA(cp)	(((cp)[0] << 8) | (cp)[1])
87 #define _NTOH32_UA(cp)	(((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
88 
89 #define ltoh_ua(ptr) \
90 	(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
91 	 sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)(ptr)) : \
92 	 sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)(ptr)) : \
93 	 *(uint8 *)0)
94 
95 #define ntoh_ua(ptr) \
96 	(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
97 	 sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)(ptr)) : \
98 	 sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)(ptr)) : \
99 	 *(uint8 *)0)
100 
101 #ifdef __GNUC__
102 
103 /* GNU macro versions avoid referencing the argument multiple times, while also
104  * avoiding the -fno-inline used in ROM builds.
105  */
106 
107 #define bcmswap16(val) ({ \
108 	uint16 _val = (val); \
109 	BCMSWAP16(_val); \
110 })
111 
112 #define bcmswap32(val) ({ \
113 	uint32 _val = (val); \
114 	BCMSWAP32(_val); \
115 })
116 
117 #define bcmswap32by16(val) ({ \
118 	uint32 _val = (val); \
119 	BCMSWAP32BY16(_val); \
120 })
121 
122 #define bcmswap16_buf(buf, len) ({ \
123 	uint16 *_buf = (uint16 *)(buf); \
124 	uint _wds = (len) / 2; \
125 	while (_wds--) { \
126 		*_buf = bcmswap16(*_buf); \
127 		_buf++; \
128 	} \
129 })
130 
131 #define htol16_ua_store(val, bytes) ({ \
132 	uint16 _val = (val); \
133 	uint8 *_bytes = (uint8 *)(bytes); \
134 	_bytes[0] = _val & 0xff; \
135 	_bytes[1] = _val >> 8; \
136 })
137 
138 #define htol32_ua_store(val, bytes) ({ \
139 	uint32 _val = (val); \
140 	uint8 *_bytes = (uint8 *)(bytes); \
141 	_bytes[0] = _val & 0xff; \
142 	_bytes[1] = (_val >> 8) & 0xff; \
143 	_bytes[2] = (_val >> 16) & 0xff; \
144 	_bytes[3] = _val >> 24; \
145 })
146 
147 #define hton16_ua_store(val, bytes) ({ \
148 	uint16 _val = (val); \
149 	uint8 *_bytes = (uint8 *)(bytes); \
150 	_bytes[0] = _val >> 8; \
151 	_bytes[1] = _val & 0xff; \
152 })
153 
154 #define hton32_ua_store(val, bytes) ({ \
155 	uint32 _val = (val); \
156 	uint8 *_bytes = (uint8 *)(bytes); \
157 	_bytes[0] = _val >> 24; \
158 	_bytes[1] = (_val >> 16) & 0xff; \
159 	_bytes[2] = (_val >> 8) & 0xff; \
160 	_bytes[3] = _val & 0xff; \
161 })
162 
163 #define ltoh16_ua(bytes) ({ \
164 	const uint8 *_bytes = (const uint8 *)(bytes); \
165 	_LTOH16_UA(_bytes); \
166 })
167 
168 #define ltoh32_ua(bytes) ({ \
169 	const uint8 *_bytes = (const uint8 *)(bytes); \
170 	_LTOH32_UA(_bytes); \
171 })
172 
173 #define ntoh16_ua(bytes) ({ \
174 	const uint8 *_bytes = (const uint8 *)(bytes); \
175 	_NTOH16_UA(_bytes); \
176 })
177 
178 #define ntoh32_ua(bytes) ({ \
179 	const uint8 *_bytes = (const uint8 *)(bytes); \
180 	_NTOH32_UA(_bytes); \
181 })
182 
183 #else /* !__GNUC__ */
184 
185 /* Inline versions avoid referencing the argument multiple times */
186 static INLINE uint16
bcmswap16(uint16 val)187 bcmswap16(uint16 val)
188 {
189 	return BCMSWAP16(val);
190 }
191 
192 static INLINE uint32
bcmswap32(uint32 val)193 bcmswap32(uint32 val)
194 {
195 	return BCMSWAP32(val);
196 }
197 
198 static INLINE uint32
bcmswap32by16(uint32 val)199 bcmswap32by16(uint32 val)
200 {
201 	return BCMSWAP32BY16(val);
202 }
203 
204 /* Reverse pairs of bytes in a buffer (not for high-performance use) */
205 /* buf	- start of buffer of shorts to swap */
206 /* len  - byte length of buffer */
207 static INLINE void
bcmswap16_buf(uint16 * buf,uint len)208 bcmswap16_buf(uint16 *buf, uint len)
209 {
210 	len = len / 2;
211 
212 	while (len--) {
213 		*buf = bcmswap16(*buf);
214 		buf++;
215 	}
216 }
217 
218 /*
219  * Store 16-bit value to unaligned little-endian byte array.
220  */
221 static INLINE void
htol16_ua_store(uint16 val,uint8 * bytes)222 htol16_ua_store(uint16 val, uint8 *bytes)
223 {
224 	bytes[0] = val & 0xff;
225 	bytes[1] = val >> 8;
226 }
227 
228 /*
229  * Store 32-bit value to unaligned little-endian byte array.
230  */
231 static INLINE void
htol32_ua_store(uint32 val,uint8 * bytes)232 htol32_ua_store(uint32 val, uint8 *bytes)
233 {
234 	bytes[0] = val & 0xff;
235 	bytes[1] = (val >> 8) & 0xff;
236 	bytes[2] = (val >> 16) & 0xff;
237 	bytes[3] = val >> 24;
238 }
239 
240 /*
241  * Store 16-bit value to unaligned network-(big-)endian byte array.
242  */
243 static INLINE void
hton16_ua_store(uint16 val,uint8 * bytes)244 hton16_ua_store(uint16 val, uint8 *bytes)
245 {
246 	bytes[0] = val >> 8;
247 	bytes[1] = val & 0xff;
248 }
249 
250 /*
251  * Store 32-bit value to unaligned network-(big-)endian byte array.
252  */
253 static INLINE void
hton32_ua_store(uint32 val,uint8 * bytes)254 hton32_ua_store(uint32 val, uint8 *bytes)
255 {
256 	bytes[0] = val >> 24;
257 	bytes[1] = (val >> 16) & 0xff;
258 	bytes[2] = (val >> 8) & 0xff;
259 	bytes[3] = val & 0xff;
260 }
261 
262 /*
263  * Load 16-bit value from unaligned little-endian byte array.
264  */
265 static INLINE uint16
ltoh16_ua(const void * bytes)266 ltoh16_ua(const void *bytes)
267 {
268 	return _LTOH16_UA((const uint8 *)bytes);
269 }
270 
271 /*
272  * Load 32-bit value from unaligned little-endian byte array.
273  */
274 static INLINE uint32
ltoh32_ua(const void * bytes)275 ltoh32_ua(const void *bytes)
276 {
277 	return _LTOH32_UA((const uint8 *)bytes);
278 }
279 
280 /*
281  * Load 16-bit value from unaligned big-(network-)endian byte array.
282  */
283 static INLINE uint16
ntoh16_ua(const void * bytes)284 ntoh16_ua(const void *bytes)
285 {
286 	return _NTOH16_UA((const uint8 *)bytes);
287 }
288 
289 /*
290  * Load 32-bit value from unaligned big-(network-)endian byte array.
291  */
292 static INLINE uint32
ntoh32_ua(const void * bytes)293 ntoh32_ua(const void *bytes)
294 {
295 	return _NTOH32_UA((const uint8 *)bytes);
296 }
297 
298 #endif /* !__GNUC__ */
299 #endif /* !_BCMENDIAN_H_ */
300