• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Byte order utilities
3  *
4  * Copyright (C) 1999-2011, Broadcom Corporation
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.
17  *
18  *  $Id: bcmendian.h,v 1.36 2009-11-09 05:29:43 Exp $
19  *
20  * This file by default provides proper behavior on little-endian architectures.
21  * On big-endian architectures, IL_BIGENDIAN should be defined.
22  */
23 
24 
25 #ifndef _BCMENDIAN_H_
26 #define _BCMENDIAN_H_
27 
28 #include <typedefs.h>
29 
30 
31 #define BCMSWAP16(val) \
32 	((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
33 		  (((uint16)(val) & (uint16)0xff00U) >> 8)))
34 
35 
36 #define BCMSWAP32(val) \
37 	((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
38 		  (((uint32)(val) & (uint32)0x0000ff00U) <<  8) | \
39 		  (((uint32)(val) & (uint32)0x00ff0000U) >>  8) | \
40 		  (((uint32)(val) & (uint32)0xff000000U) >> 24)))
41 
42 
43 #define BCMSWAP32BY16(val) \
44 	((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
45 		  (((uint32)(val) & (uint32)0xffff0000U) >> 16)))
46 
47 
48 #ifndef hton16
49 #define HTON16(i) BCMSWAP16(i)
50 #define	hton16(i) bcmswap16(i)
51 #define	HTON32(i) BCMSWAP32(i)
52 #define	hton32(i) bcmswap32(i)
53 #define	NTOH16(i) BCMSWAP16(i)
54 #define	ntoh16(i) bcmswap16(i)
55 #define	NTOH32(i) BCMSWAP32(i)
56 #define	ntoh32(i) bcmswap32(i)
57 #define LTOH16(i) (i)
58 #define ltoh16(i) (i)
59 #define LTOH32(i) (i)
60 #define ltoh32(i) (i)
61 #define HTOL16(i) (i)
62 #define htol16(i) (i)
63 #define HTOL32(i) (i)
64 #define htol32(i) (i)
65 #endif
66 
67 #define ltoh16_buf(buf, i)
68 #define htol16_buf(buf, i)
69 
70 
71 #define load32_ua(a)		ltoh32_ua(a)
72 #define store32_ua(a, v)	htol32_ua_store(v, a)
73 #define load16_ua(a)		ltoh16_ua(a)
74 #define store16_ua(a, v)	htol16_ua_store(v, a)
75 
76 #define _LTOH16_UA(cp)	((cp)[0] | ((cp)[1] << 8))
77 #define _LTOH32_UA(cp)	((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
78 #define _NTOH16_UA(cp)	(((cp)[0] << 8) | (cp)[1])
79 #define _NTOH32_UA(cp)	(((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
80 
81 #define ltoh_ua(ptr) \
82 	(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
83 	 sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)(ptr)) : \
84 	 sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)(ptr)) : \
85 	 *(uint8 *)0)
86 
87 #define ntoh_ua(ptr) \
88 	(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
89 	 sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)(ptr)) : \
90 	 sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)(ptr)) : \
91 	 *(uint8 *)0)
92 
93 #ifdef __GNUC__
94 
95 
96 
97 #define bcmswap16(val) ({ \
98 	uint16 _val = (val); \
99 	BCMSWAP16(_val); \
100 })
101 
102 #define bcmswap32(val) ({ \
103 	uint32 _val = (val); \
104 	BCMSWAP32(_val); \
105 })
106 
107 #define bcmswap32by16(val) ({ \
108 	uint32 _val = (val); \
109 	BCMSWAP32BY16(_val); \
110 })
111 
112 #define bcmswap16_buf(buf, len) ({ \
113 	uint16 *_buf = (uint16 *)(buf); \
114 	uint _wds = (len) / 2; \
115 	while (_wds--) { \
116 		*_buf = bcmswap16(*_buf); \
117 		_buf++; \
118 	} \
119 })
120 
121 #define htol16_ua_store(val, bytes) ({ \
122 	uint16 _val = (val); \
123 	uint8 *_bytes = (uint8 *)(bytes); \
124 	_bytes[0] = _val & 0xff; \
125 	_bytes[1] = _val >> 8; \
126 })
127 
128 #define htol32_ua_store(val, bytes) ({ \
129 	uint32 _val = (val); \
130 	uint8 *_bytes = (uint8 *)(bytes); \
131 	_bytes[0] = _val & 0xff; \
132 	_bytes[1] = (_val >> 8) & 0xff; \
133 	_bytes[2] = (_val >> 16) & 0xff; \
134 	_bytes[3] = _val >> 24; \
135 })
136 
137 #define hton16_ua_store(val, bytes) ({ \
138 	uint16 _val = (val); \
139 	uint8 *_bytes = (uint8 *)(bytes); \
140 	_bytes[0] = _val >> 8; \
141 	_bytes[1] = _val & 0xff; \
142 })
143 
144 #define hton32_ua_store(val, bytes) ({ \
145 	uint32 _val = (val); \
146 	uint8 *_bytes = (uint8 *)(bytes); \
147 	_bytes[0] = _val >> 24; \
148 	_bytes[1] = (_val >> 16) & 0xff; \
149 	_bytes[2] = (_val >> 8) & 0xff; \
150 	_bytes[3] = _val & 0xff; \
151 })
152 
153 #define ltoh16_ua(bytes) ({ \
154 	const uint8 *_bytes = (const uint8 *)(bytes); \
155 	_LTOH16_UA(_bytes); \
156 })
157 
158 #define ltoh32_ua(bytes) ({ \
159 	const uint8 *_bytes = (const uint8 *)(bytes); \
160 	_LTOH32_UA(_bytes); \
161 })
162 
163 #define ntoh16_ua(bytes) ({ \
164 	const uint8 *_bytes = (const uint8 *)(bytes); \
165 	_NTOH16_UA(_bytes); \
166 })
167 
168 #define ntoh32_ua(bytes) ({ \
169 	const uint8 *_bytes = (const uint8 *)(bytes); \
170 	_NTOH32_UA(_bytes); \
171 })
172 
173 #else
174 
175 
176 static INLINE uint16
bcmswap16(uint16 val)177 bcmswap16(uint16 val)
178 {
179 	return BCMSWAP16(val);
180 }
181 
182 static INLINE uint32
bcmswap32(uint32 val)183 bcmswap32(uint32 val)
184 {
185 	return BCMSWAP32(val);
186 }
187 
188 static INLINE uint32
bcmswap32by16(uint32 val)189 bcmswap32by16(uint32 val)
190 {
191 	return BCMSWAP32BY16(val);
192 }
193 
194 
195 
196 
197 static INLINE void
bcmswap16_buf(uint16 * buf,uint len)198 bcmswap16_buf(uint16 *buf, uint len)
199 {
200 	len = len / 2;
201 
202 	while (len--) {
203 		*buf = bcmswap16(*buf);
204 		buf++;
205 	}
206 }
207 
208 
209 static INLINE void
htol16_ua_store(uint16 val,uint8 * bytes)210 htol16_ua_store(uint16 val, uint8 *bytes)
211 {
212 	bytes[0] = val & 0xff;
213 	bytes[1] = val >> 8;
214 }
215 
216 
217 static INLINE void
htol32_ua_store(uint32 val,uint8 * bytes)218 htol32_ua_store(uint32 val, uint8 *bytes)
219 {
220 	bytes[0] = val & 0xff;
221 	bytes[1] = (val >> 8) & 0xff;
222 	bytes[2] = (val >> 16) & 0xff;
223 	bytes[3] = val >> 24;
224 }
225 
226 
227 static INLINE void
hton16_ua_store(uint16 val,uint8 * bytes)228 hton16_ua_store(uint16 val, uint8 *bytes)
229 {
230 	bytes[0] = val >> 8;
231 	bytes[1] = val & 0xff;
232 }
233 
234 
235 static INLINE void
hton32_ua_store(uint32 val,uint8 * bytes)236 hton32_ua_store(uint32 val, uint8 *bytes)
237 {
238 	bytes[0] = val >> 24;
239 	bytes[1] = (val >> 16) & 0xff;
240 	bytes[2] = (val >> 8) & 0xff;
241 	bytes[3] = val & 0xff;
242 }
243 
244 
245 static INLINE uint16
ltoh16_ua(const void * bytes)246 ltoh16_ua(const void *bytes)
247 {
248 	return _LTOH16_UA((const uint8 *)bytes);
249 }
250 
251 
252 static INLINE uint32
ltoh32_ua(const void * bytes)253 ltoh32_ua(const void *bytes)
254 {
255 	return _LTOH32_UA((const uint8 *)bytes);
256 }
257 
258 
259 static INLINE uint16
ntoh16_ua(const void * bytes)260 ntoh16_ua(const void *bytes)
261 {
262 	return _NTOH16_UA((const uint8 *)bytes);
263 }
264 
265 
266 static INLINE uint32
ntoh32_ua(const void * bytes)267 ntoh32_ua(const void *bytes)
268 {
269 	return _NTOH32_UA((const uint8 *)bytes);
270 }
271 
272 #endif
273 #endif
274