2 * local version of endian.h - byte order defines
4 * Copyright (C) 2009, Broadcom Corporation
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
12 * $Id: bcmendian.h,v 1.31.296.2 2009/10/12 23:03:37 Exp $
20 /* Byte swap a 16 bit value */
21 #define BCMSWAP16(val) \
23 (((uint16)(val) & (uint16)0x00ffU) << 8) | \
24 (((uint16)(val) & (uint16)0xff00U) >> 8)))
26 /* Byte swap a 32 bit value */
27 #define BCMSWAP32(val) \
29 (((uint32)(val) & (uint32)0x000000ffUL) << 24) | \
30 (((uint32)(val) & (uint32)0x0000ff00UL) << 8) | \
31 (((uint32)(val) & (uint32)0x00ff0000UL) >> 8) | \
32 (((uint32)(val) & (uint32)0xff000000UL) >> 24)))
34 /* 2 Byte swap a 32 bit value */
35 #define BCMSWAP32BY16(val) \
37 (((uint32)(val) & (uint32)0x0000ffffUL) << 16) | \
38 (((uint32)(val) & (uint32)0xffff0000UL) >> 16)))
44 return BCMSWAP16(val
);
50 return BCMSWAP32(val
);
54 bcmswap32by16(uint32 val
)
56 return BCMSWAP32BY16(val
);
59 /* buf - start of buffer of shorts to swap */
60 /* len - byte length of buffer */
62 bcmswap16_buf(uint16
*buf
, uint len
)
67 *buf
= bcmswap16(*buf
);
74 #define HTON16(i) BCMSWAP16(i)
75 #define NTOH16(i) BCMSWAP16(i)
76 #define HTON32(i) BCMSWAP32(i)
77 #define NTOH32(i) BCMSWAP32(i)
78 #define hton16(i) bcmswap16(i)
79 #define hton32(i) bcmswap32(i)
80 #define ntoh16(i) bcmswap16(i)
81 #define ntoh32(i) bcmswap32(i)
95 #define ltoh16(i) bcmswap16(i)
96 #define ltoh32(i) bcmswap32(i)
97 #define htol16(i) bcmswap16(i)
98 #define htol32(i) bcmswap32(i)
99 #endif /* IL_BIGENDIAN */
103 #define ltoh16_buf(buf, i)
104 #define htol16_buf(buf, i)
106 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
107 #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
108 #endif /* IL_BIGENDIAN */
111 * Store 16-bit value to unaligned little endian byte array.
114 htol16_ua_store(uint16 val
, uint8
*bytes
)
121 * Store 32-bit value to unaligned little endian byte array.
124 htol32_ua_store(uint32 val
, uint8
*bytes
)
127 bytes
[1] = (val
>>8)&0xff;
128 bytes
[2] = (val
>>16)&0xff;
133 * Store 16-bit value to unaligned network(big) endian byte array.
136 hton16_ua_store(uint16 val
, uint8
*bytes
)
143 * Store 32-bit value to unaligned network(big) endian byte array.
146 hton32_ua_store(uint32 val
, uint8
*bytes
)
149 bytes
[2] = (val
>>8)&0xff;
150 bytes
[1] = (val
>>16)&0xff;
154 #define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))
155 #define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
156 #define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])
157 #define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
160 * Load 16-bit value from unaligned little-endian byte array.
163 ltoh16_ua(const void *bytes
)
165 return _LTOH16_UA((const uint8
*)bytes
);
169 * Load 32-bit value from unaligned little-endian byte array.
172 ltoh32_ua(const void *bytes
)
174 return _LTOH32_UA((const uint8
*)bytes
);
178 * Load 16-bit value from unaligned big-(network-)endian byte array.
181 ntoh16_ua(const void *bytes
)
183 return _NTOH16_UA((const uint8
*)bytes
);
187 * Load 32-bit value from unaligned big-(network-)endian byte array.
190 ntoh32_ua(const void *bytes
)
192 return _NTOH32_UA((const uint8
*)bytes
);
195 #define ltoh_ua(ptr) \
196 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)ptr : \
197 sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)ptr) : \
198 sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)ptr) : \
201 #define ntoh_ua(ptr) \
202 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)ptr : \
203 sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)ptr) : \
204 sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)ptr) : \
208 #endif /* _BCMENDIAN_H_ */