4 * Copyright (C) 1999-2011, Broadcom Corporation
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:
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.
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.
24 * $Id: bcmendian.h 277737 2011-08-16 17:54:59Z $
26 * This file by default provides proper behavior on little-endian architectures.
27 * On big-endian architectures, IL_BIGENDIAN should be defined.
37 #define BCMSWAP16(val) \
38 ((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
39 (((uint16)(val) & (uint16)0xff00U) >> 8)))
42 #define BCMSWAP32(val) \
43 ((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
44 (((uint32)(val) & (uint32)0x0000ff00U) << 8) | \
45 (((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \
46 (((uint32)(val) & (uint32)0xff000000U) >> 24)))
49 #define BCMSWAP32BY16(val) \
50 ((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
51 (((uint32)(val) & (uint32)0xffff0000U) >> 16)))
55 #define HTON16(i) BCMSWAP16(i)
56 #define hton16(i) bcmswap16(i)
57 #define HTON32(i) BCMSWAP32(i)
58 #define hton32(i) bcmswap32(i)
59 #define NTOH16(i) BCMSWAP16(i)
60 #define ntoh16(i) bcmswap16(i)
61 #define NTOH32(i) BCMSWAP32(i)
62 #define ntoh32(i) bcmswap32(i)
73 #define ltoh16_buf(buf, i)
74 #define htol16_buf(buf, i)
77 #define load32_ua(a) ltoh32_ua(a)
78 #define store32_ua(a, v) htol32_ua_store(v, a)
79 #define load16_ua(a) ltoh16_ua(a)
80 #define store16_ua(a, v) htol16_ua_store(v, a)
82 #define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))
83 #define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
84 #define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])
85 #define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
87 #define ltoh_ua(ptr) \
88 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
89 sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)(ptr)) : \
90 sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)(ptr)) : \
93 #define ntoh_ua(ptr) \
94 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \
95 sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)(ptr)) : \
96 sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)(ptr)) : \
103 #define bcmswap16(val) ({ \
104 uint16 _val = (val); \
108 #define bcmswap32(val) ({ \
109 uint32 _val = (val); \
113 #define bcmswap32by16(val) ({ \
114 uint32 _val = (val); \
115 BCMSWAP32BY16(_val); \
118 #define bcmswap16_buf(buf, len) ({ \
119 uint16 *_buf = (uint16 *)(buf); \
120 uint _wds = (len) / 2; \
122 *_buf = bcmswap16(*_buf); \
127 #define htol16_ua_store(val, bytes) ({ \
128 uint16 _val = (val); \
129 uint8 *_bytes = (uint8 *)(bytes); \
130 _bytes[0] = _val & 0xff; \
131 _bytes[1] = _val >> 8; \
134 #define htol32_ua_store(val, bytes) ({ \
135 uint32 _val = (val); \
136 uint8 *_bytes = (uint8 *)(bytes); \
137 _bytes[0] = _val & 0xff; \
138 _bytes[1] = (_val >> 8) & 0xff; \
139 _bytes[2] = (_val >> 16) & 0xff; \
140 _bytes[3] = _val >> 24; \
143 #define hton16_ua_store(val, bytes) ({ \
144 uint16 _val = (val); \
145 uint8 *_bytes = (uint8 *)(bytes); \
146 _bytes[0] = _val >> 8; \
147 _bytes[1] = _val & 0xff; \
150 #define hton32_ua_store(val, bytes) ({ \
151 uint32 _val = (val); \
152 uint8 *_bytes = (uint8 *)(bytes); \
153 _bytes[0] = _val >> 24; \
154 _bytes[1] = (_val >> 16) & 0xff; \
155 _bytes[2] = (_val >> 8) & 0xff; \
156 _bytes[3] = _val & 0xff; \
159 #define ltoh16_ua(bytes) ({ \
160 const uint8 *_bytes = (const uint8 *)(bytes); \
161 _LTOH16_UA(_bytes); \
164 #define ltoh32_ua(bytes) ({ \
165 const uint8 *_bytes = (const uint8 *)(bytes); \
166 _LTOH32_UA(_bytes); \
169 #define ntoh16_ua(bytes) ({ \
170 const uint8 *_bytes = (const uint8 *)(bytes); \
171 _NTOH16_UA(_bytes); \
174 #define ntoh32_ua(bytes) ({ \
175 const uint8 *_bytes = (const uint8 *)(bytes); \
176 _NTOH32_UA(_bytes); \
183 bcmswap16(uint16 val)
185 return BCMSWAP16(val);
189 bcmswap32(uint32 val)
191 return BCMSWAP32(val);
195 bcmswap32by16(uint32 val)
197 return BCMSWAP32BY16(val);
204 bcmswap16_buf(uint16 *buf, uint len)
209 *buf = bcmswap16(*buf);
216 htol16_ua_store(uint16 val, uint8 *bytes)
218 bytes[0] = val & 0xff;
224 htol32_ua_store(uint32 val, uint8 *bytes)
226 bytes[0] = val & 0xff;
227 bytes[1] = (val >> 8) & 0xff;
228 bytes[2] = (val >> 16) & 0xff;
229 bytes[3] = val >> 24;
234 hton16_ua_store(uint16 val, uint8 *bytes)
237 bytes[1] = val & 0xff;
242 hton32_ua_store(uint32 val, uint8 *bytes)
244 bytes[0] = val >> 24;
245 bytes[1] = (val >> 16) & 0xff;
246 bytes[2] = (val >> 8) & 0xff;
247 bytes[3] = val & 0xff;
252 ltoh16_ua(const void *bytes)
254 return _LTOH16_UA((const uint8 *)bytes);
259 ltoh32_ua(const void *bytes)
261 return _LTOH32_UA((const uint8 *)bytes);
266 ntoh16_ua(const void *bytes)
268 return _NTOH16_UA((const uint8 *)bytes);
273 ntoh32_ua(const void *bytes)
275 return _NTOH32_UA((const uint8 *)bytes);