Broadcom SDK and wireless driver: another attempt to update to ver. 5.10.147.0
[tomato.git] / release / src-rt / include / bcmendian.h
blob849dd939bb375f3848494251b3b50cbbc1626514
1 /*
2 * local version of endian.h - byte order defines
4 * Copyright (C) 2009, Broadcom Corporation
5 * All Rights Reserved.
6 *
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 $
15 #ifndef _BCMENDIAN_H_
16 #define _BCMENDIAN_H_
18 #include <typedefs.h>
20 /* Byte swap a 16 bit value */
21 #define BCMSWAP16(val) \
22 ((uint16)(\
23 (((uint16)(val) & (uint16)0x00ffU) << 8) | \
24 (((uint16)(val) & (uint16)0xff00U) >> 8)))
26 /* Byte swap a 32 bit value */
27 #define BCMSWAP32(val) \
28 ((uint32)(\
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) \
36 ((uint32)(\
37 (((uint32)(val) & (uint32)0x0000ffffUL) << 16) | \
38 (((uint32)(val) & (uint32)0xffff0000UL) >> 16)))
41 static INLINE uint16
42 bcmswap16(uint16 val)
44 return BCMSWAP16(val);
47 static INLINE uint32
48 bcmswap32(uint32 val)
50 return BCMSWAP32(val);
53 static INLINE uint32
54 bcmswap32by16(uint32 val)
56 return BCMSWAP32BY16(val);
59 /* buf - start of buffer of shorts to swap */
60 /* len - byte length of buffer */
61 static INLINE void
62 bcmswap16_buf(uint16 *buf, uint len)
64 len = len/2;
66 while (len--) {
67 *buf = bcmswap16(*buf);
68 buf++;
72 #ifndef hton16
73 #ifndef IL_BIGENDIAN
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)
82 #define ltoh16(i) (i)
83 #define ltoh32(i) (i)
84 #define htol16(i) (i)
85 #define htol32(i) (i)
86 #else
87 #define HTON16(i) (i)
88 #define NTOH16(i) (i)
89 #define HTON32(i) (i)
90 #define NTOH32(i) (i)
91 #define hton16(i) (i)
92 #define hton32(i) (i)
93 #define ntoh16(i) (i)
94 #define ntoh32(i) (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 */
100 #endif /* hton16 */
102 #ifndef IL_BIGENDIAN
103 #define ltoh16_buf(buf, i)
104 #define htol16_buf(buf, i)
105 #else
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.
113 static INLINE void
114 htol16_ua_store(uint16 val, uint8 *bytes)
116 bytes[0] = val&0xff;
117 bytes[1] = val>>8;
121 * Store 32-bit value to unaligned little endian byte array.
123 static INLINE void
124 htol32_ua_store(uint32 val, uint8 *bytes)
126 bytes[0] = val&0xff;
127 bytes[1] = (val>>8)&0xff;
128 bytes[2] = (val>>16)&0xff;
129 bytes[3] = val>>24;
133 * Store 16-bit value to unaligned network(big) endian byte array.
135 static INLINE void
136 hton16_ua_store(uint16 val, uint8 *bytes)
138 bytes[1] = val&0xff;
139 bytes[0] = val>>8;
143 * Store 32-bit value to unaligned network(big) endian byte array.
145 static INLINE void
146 hton32_ua_store(uint32 val, uint8 *bytes)
148 bytes[3] = val&0xff;
149 bytes[2] = (val>>8)&0xff;
150 bytes[1] = (val>>16)&0xff;
151 bytes[0] = val>>24;
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.
162 static INLINE uint16
163 ltoh16_ua(const void *bytes)
165 return _LTOH16_UA((const uint8 *)bytes);
169 * Load 32-bit value from unaligned little-endian byte array.
171 static INLINE uint32
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.
180 static INLINE uint16
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.
189 static INLINE uint32
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) : \
199 0xfeedf00d)
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) : \
205 0xfeedf00d)
208 #endif /* _BCMENDIAN_H_ */