1 /*
2 * Byte order utilities
3 *
4 * Copyright (C) 1999-2009, Broadcom Corporation
5 *
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:
11 *
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.
19 *
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.
23 *
24 * $Id: bcmendian.h,v 1.31.302.1.16.1 2009/02/03 18:34:31 Exp $
25 *
26 * This file by default provides proper behavior on little-endian architectures.
27 * On big-endian architectures, IL_BIGENDIAN should be defined.
28 */
29
30
31 #ifndef _BCMENDIAN_H_
32 #define _BCMENDIAN_H_
33
34 #include <typedefs.h>
35
36
37 #define BCMSWAP16(val) \
38 ((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
39 (((uint16)(val) & (uint16)0xff00U) >> 8)))
40
41
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)))
47
48
49 #define BCMSWAP32BY16(val) \
50 ((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
51 (((uint32)(val) & (uint32)0xffff0000U) >> 16)))
52
53
54 static INLINE uint16
bcmswap16(uint16 val)55 bcmswap16(uint16 val)
56 {
57 return BCMSWAP16(val);
58 }
59
60 static INLINE uint32
bcmswap32(uint32 val)61 bcmswap32(uint32 val)
62 {
63 return BCMSWAP32(val);
64 }
65
66 static INLINE uint32
bcmswap32by16(uint32 val)67 bcmswap32by16(uint32 val)
68 {
69 return BCMSWAP32BY16(val);
70 }
71
72
73
74
75 static INLINE void
bcmswap16_buf(uint16 * buf,uint len)76 bcmswap16_buf(uint16 *buf, uint len)
77 {
78 len = len / 2;
79
80 while (len--) {
81 *buf = bcmswap16(*buf);
82 buf++;
83 }
84 }
85
86 #ifndef hton16
87 #ifndef IL_BIGENDIAN
88 #define HTON16(i) BCMSWAP16(i)
89 #define HTON32(i) BCMSWAP32(i)
90 #define hton16(i) bcmswap16(i)
91 #define hton32(i) bcmswap32(i)
92 #define ntoh16(i) bcmswap16(i)
93 #define ntoh32(i) bcmswap32(i)
94 #define HTOL16(i) (i)
95 #define HTOL32(i) (i)
96 #define ltoh16(i) (i)
97 #define ltoh32(i) (i)
98 #define htol16(i) (i)
99 #define htol32(i) (i)
100 #else
101 #define HTON16(i) (i)
102 #define HTON32(i) (i)
103 #define hton16(i) (i)
104 #define hton32(i) (i)
105 #define ntoh16(i) (i)
106 #define ntoh32(i) (i)
107 #define HTOL16(i) BCMSWAP16(i)
108 #define HTOL32(i) BCMSWAP32(i)
109 #define ltoh16(i) bcmswap16(i)
110 #define ltoh32(i) bcmswap32(i)
111 #define htol16(i) bcmswap16(i)
112 #define htol32(i) bcmswap32(i)
113 #endif
114 #endif
115
116 #ifndef IL_BIGENDIAN
117 #define ltoh16_buf(buf, i)
118 #define htol16_buf(buf, i)
119 #else
120 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16 *)buf, i)
121 #define htol16_buf(buf, i) bcmswap16_buf((uint16 *)buf, i)
122 #endif
123
124
125 static INLINE void
htol16_ua_store(uint16 val,uint8 * bytes)126 htol16_ua_store(uint16 val, uint8 *bytes)
127 {
128 bytes[0] = val & 0xff;
129 bytes[1] = val >> 8;
130 }
131
132
133 static INLINE void
htol32_ua_store(uint32 val,uint8 * bytes)134 htol32_ua_store(uint32 val, uint8 *bytes)
135 {
136 bytes[0] = val & 0xff;
137 bytes[1] = (val >> 8) & 0xff;
138 bytes[2] = (val >> 16) & 0xff;
139 bytes[3] = val >> 24;
140 }
141
142
143 static INLINE void
hton16_ua_store(uint16 val,uint8 * bytes)144 hton16_ua_store(uint16 val, uint8 *bytes)
145 {
146 bytes[0] = val >> 8;
147 bytes[1] = val & 0xff;
148 }
149
150
151 static INLINE void
hton32_ua_store(uint32 val,uint8 * bytes)152 hton32_ua_store(uint32 val, uint8 *bytes)
153 {
154 bytes[0] = val >> 24;
155 bytes[1] = (val >> 16) & 0xff;
156 bytes[2] = (val >> 8) & 0xff;
157 bytes[3] = val & 0xff;
158 }
159
160 #define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))
161 #define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
162 #define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])
163 #define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
164
165
166 static INLINE uint16
ltoh16_ua(const void * bytes)167 ltoh16_ua(const void *bytes)
168 {
169 return _LTOH16_UA((const uint8 *)bytes);
170 }
171
172
173 static INLINE uint32
ltoh32_ua(const void * bytes)174 ltoh32_ua(const void *bytes)
175 {
176 return _LTOH32_UA((const uint8 *)bytes);
177 }
178
179
180 static INLINE uint16
ntoh16_ua(const void * bytes)181 ntoh16_ua(const void *bytes)
182 {
183 return _NTOH16_UA((const uint8 *)bytes);
184 }
185
186
187 static INLINE uint32
ntoh32_ua(const void * bytes)188 ntoh32_ua(const void *bytes)
189 {
190 return _NTOH32_UA((const uint8 *)bytes);
191 }
192
193 #define ltoh_ua(ptr) \
194 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)ptr : \
195 sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)ptr) : \
196 sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)ptr) : \
197 0xfeedf00d)
198
199 #define ntoh_ua(ptr) \
200 (sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)ptr : \
201 sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)ptr) : \
202 sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)ptr) : \
203 0xfeedf00d)
204
205 #endif
206