2 * local version of endian.h - byte order defines
4 * Copyright 2007, 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.
19 /* Byte swap a 16 bit value */
20 #define BCMSWAP16(val) \
22 (((uint16)(val) & (uint16)0x00ffU) << 8) | \
23 (((uint16)(val) & (uint16)0xff00U) >> 8)))
25 /* Byte swap a 32 bit value */
26 #define BCMSWAP32(val) \
28 (((uint32)(val) & (uint32)0x000000ffUL) << 24) | \
29 (((uint32)(val) & (uint32)0x0000ff00UL) << 8) | \
30 (((uint32)(val) & (uint32)0x00ff0000UL) >> 8) | \
31 (((uint32)(val) & (uint32)0xff000000UL) >> 24)))
33 /* 2 Byte swap a 32 bit value */
34 #define BCMSWAP32BY16(val) \
36 (((uint32)(val) & (uint32)0x0000ffffUL) << 16) | \
37 (((uint32)(val) & (uint32)0xffff0000UL) >> 16)))
43 return BCMSWAP16(val);
49 return BCMSWAP32(val);
53 bcmswap32by16(uint32 val)
55 return BCMSWAP32BY16(val);
58 /* buf - start of buffer of shorts to swap */
59 /* len - byte length of buffer */
61 bcmswap16_buf(uint16 *buf, uint len)
66 *buf = bcmswap16(*buf);
73 #define HTON16(i) BCMSWAP16(i)
74 #define hton16(i) bcmswap16(i)
75 #define hton32(i) bcmswap32(i)
76 #define ntoh16(i) bcmswap16(i)
77 #define ntoh32(i) bcmswap32(i)
88 #define ltoh16(i) bcmswap16(i)
89 #define ltoh32(i) bcmswap32(i)
90 #define htol16(i) bcmswap16(i)
91 #define htol32(i) bcmswap32(i)
92 #endif /* IL_BIGENDIAN */
96 #define ltoh16_buf(buf, i)
97 #define htol16_buf(buf, i)
99 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
100 #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
101 #endif /* IL_BIGENDIAN */
104 * store 16-bit value to unaligned little endian byte array.
107 htol16_ua_store(uint16 val, uint8 *bytes)
114 * store 32-bit value to unaligned little endian byte array.
117 htol32_ua_store(uint32 val, uint8 *bytes)
120 bytes[1] = (val>>8)&0xff;
121 bytes[2] = (val>>16)&0xff;
126 * store 16-bit value to unaligned network(big) endian byte array.
129 hton16_ua_store(uint16 val, uint8 *bytes)
136 * store 32-bit value to unaligned network(big) endian byte array.
139 hton32_ua_store(uint32 val, uint8 *bytes)
142 bytes[2] = (val>>8)&0xff;
143 bytes[1] = (val>>16)&0xff;
148 * load 16-bit value from unaligned little endian byte array.
151 ltoh16_ua(void *bytes)
153 return (((uint8*)bytes)[1]<<8)+((uint8 *)bytes)[0];
157 * load 32-bit value from unaligned little endian byte array.
160 ltoh32_ua(void *bytes)
162 return (((uint8*)bytes)[3]<<24)+(((uint8*)bytes)[2]<<16)+
163 (((uint8*)bytes)[1]<<8)+((uint8*)bytes)[0];
167 * load 16-bit value from unaligned big(network) endian byte array.
170 ntoh16_ua(void *bytes)
172 return (((uint8*)bytes)[0]<<8)+((uint8*)bytes)[1];
176 * load 32-bit value from unaligned big(network) endian byte array.
179 ntoh32_ua(void *bytes)
181 return (((uint8*)bytes)[0]<<24)+(((uint8*)bytes)[1]<<16)+
182 (((uint8*)bytes)[2]<<8)+((uint8*)bytes)[3];
185 #define ltoh_ua(ptr) (\
186 sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
187 sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] : \
188 (((uint8 *)ptr)[3]<<24)+(((uint8 *)ptr)[2]<<16)+(((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] \
191 #define ntoh_ua(ptr) (\
192 sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
193 sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[0]<<8)+((uint8 *)ptr)[1] : \
194 (((uint8 *)ptr)[0]<<24)+(((uint8 *)ptr)[1]<<16)+(((uint8 *)ptr)[2]<<8)+((uint8 *)ptr)[3] \
197 #endif /* _BCMENDIAN_H_ */