2 * local version of endian.h - byte order defines
4 * Copyright 2005, 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.1.1.8 2005/03/07 07:31:12 kanki 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) ))
37 return BCMSWAP16(val
);
43 return BCMSWAP32(val
);
46 /* buf - start of buffer of shorts to swap */
47 /* len - byte length of buffer */
49 bcmswap16_buf(uint16
*buf
, uint len
)
54 *buf
= bcmswap16(*buf
);
61 #define HTON16(i) BCMSWAP16(i)
62 #define hton16(i) bcmswap16(i)
63 #define hton32(i) bcmswap32(i)
64 #define ntoh16(i) bcmswap16(i)
65 #define ntoh32(i) bcmswap32(i)
76 #define ltoh16(i) bcmswap16(i)
77 #define ltoh32(i) bcmswap32(i)
78 #define htol16(i) bcmswap16(i)
79 #define htol32(i) bcmswap32(i)
84 #define ltoh16_buf(buf, i)
85 #define htol16_buf(buf, i)
87 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
88 #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
92 * load 16-bit value from unaligned little endian byte array.
95 ltoh16_ua(uint8
*bytes
)
97 return (bytes
[1]<<8)+bytes
[0];
101 * load 32-bit value from unaligned little endian byte array.
104 ltoh32_ua(uint8
*bytes
)
106 return (bytes
[3]<<24)+(bytes
[2]<<16)+(bytes
[1]<<8)+bytes
[0];
110 * load 16-bit value from unaligned big(network) endian byte array.
113 ntoh16_ua(uint8
*bytes
)
115 return (bytes
[0]<<8)+bytes
[1];
119 * load 32-bit value from unaligned big(network) endian byte array.
122 ntoh32_ua(uint8
*bytes
)
124 return (bytes
[0]<<24)+(bytes
[1]<<16)+(bytes
[2]<<8)+bytes
[3];
127 /* get_ua adapted from Linux asm-mips/unaligned.h */
129 #define get_ua(ptr) \
131 __typeof__(*(ptr)) __val; \
133 switch (sizeof(*(ptr))) { \
135 __val = *(uint8 *)ptr; \
138 __val = ntoh16_ua((uint8 *)ptr); \
141 __val = ntoh32_ua((uint8 *)ptr); \
148 #define get_ua(ptr) \
150 __typeof__(*(ptr)) __val; \
152 switch (sizeof(*(ptr))) { \
154 __val = *(uint8 *)ptr; \
157 __val = ltoh16_ua((uint8 *)ptr); \
160 __val = ltoh32_ua((uint8 *)ptr); \
168 #endif /* _BCMENDIAN_H_ */