Fix an off-by one printing error
[openwrt.git] / package / broadcom-57xx / src / bcmendian.h
1 /*
2 * local version of endian.h - byte order defines
3 *
4 * Copyright 2007, 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.
11 *
12 * $Id: bcmendian.h,v 1.1.1.11 2007/05/31 08:00:41 michael Exp $
13 */
14
15 #ifndef _BCMENDIAN_H_
16 #define _BCMENDIAN_H_
17
18 #include <typedefs.h>
19
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)))
25
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)))
33
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)))
39
40
41 static INLINE uint16
42 bcmswap16(uint16 val)
43 {
44 return BCMSWAP16(val);
45 }
46
47 static INLINE uint32
48 bcmswap32(uint32 val)
49 {
50 return BCMSWAP32(val);
51 }
52
53 static INLINE uint32
54 bcmswap32by16(uint32 val)
55 {
56 return BCMSWAP32BY16(val);
57 }
58
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)
63 {
64 len = len/2;
65
66 while (len--) {
67 *buf = bcmswap16(*buf);
68 buf++;
69 }
70 }
71
72 #ifndef hton16
73 #ifndef IL_BIGENDIAN
74 #define HTON16(i) BCMSWAP16(i)
75 #define hton16(i) bcmswap16(i)
76 #define hton32(i) bcmswap32(i)
77 #define ntoh16(i) bcmswap16(i)
78 #define ntoh32(i) bcmswap32(i)
79 #define ltoh16(i) (i)
80 #define ltoh32(i) (i)
81 #define htol16(i) (i)
82 #define htol32(i) (i)
83 #else
84 #define HTON16(i) (i)
85 #define hton16(i) (i)
86 #define hton32(i) (i)
87 #define ntoh16(i) (i)
88 #define ntoh32(i) (i)
89 #define ltoh16(i) bcmswap16(i)
90 #define ltoh32(i) bcmswap32(i)
91 #define htol16(i) bcmswap16(i)
92 #define htol32(i) bcmswap32(i)
93 #endif /* IL_BIGENDIAN */
94 #endif /* hton16 */
95
96 #ifndef IL_BIGENDIAN
97 #define ltoh16_buf(buf, i)
98 #define htol16_buf(buf, i)
99 #else
100 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
101 #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
102 #endif /* IL_BIGENDIAN */
103
104 /*
105 * store 16-bit value to unaligned little endian byte array.
106 */
107 static INLINE void
108 htol16_ua_store(uint16 val, uint8 *bytes)
109 {
110 bytes[0] = val&0xff;
111 bytes[1] = val>>8;
112 }
113
114 /*
115 * store 32-bit value to unaligned little endian byte array.
116 */
117 static INLINE void
118 htol32_ua_store(uint32 val, uint8 *bytes)
119 {
120 bytes[0] = val&0xff;
121 bytes[1] = (val>>8)&0xff;
122 bytes[2] = (val>>16)&0xff;
123 bytes[3] = val>>24;
124 }
125
126 /*
127 * store 16-bit value to unaligned network(big) endian byte array.
128 */
129 static INLINE void
130 hton16_ua_store(uint16 val, uint8 *bytes)
131 {
132 bytes[1] = val&0xff;
133 bytes[0] = val>>8;
134 }
135
136 /*
137 * store 32-bit value to unaligned network(big) endian byte array.
138 */
139 static INLINE void
140 hton32_ua_store(uint32 val, uint8 *bytes)
141 {
142 bytes[3] = val&0xff;
143 bytes[2] = (val>>8)&0xff;
144 bytes[1] = (val>>16)&0xff;
145 bytes[0] = val>>24;
146 }
147
148 /*
149 * load 16-bit value from unaligned little endian byte array.
150 */
151 static INLINE uint16
152 ltoh16_ua(void *bytes)
153 {
154 return (((uint8*)bytes)[1]<<8)+((uint8 *)bytes)[0];
155 }
156
157 /*
158 * load 32-bit value from unaligned little endian byte array.
159 */
160 static INLINE uint32
161 ltoh32_ua(void *bytes)
162 {
163 return (((uint8*)bytes)[3]<<24)+(((uint8*)bytes)[2]<<16)+
164 (((uint8*)bytes)[1]<<8)+((uint8*)bytes)[0];
165 }
166
167 /*
168 * load 16-bit value from unaligned big(network) endian byte array.
169 */
170 static INLINE uint16
171 ntoh16_ua(void *bytes)
172 {
173 return (((uint8*)bytes)[0]<<8)+((uint8*)bytes)[1];
174 }
175
176 /*
177 * load 32-bit value from unaligned big(network) endian byte array.
178 */
179 static INLINE uint32
180 ntoh32_ua(void *bytes)
181 {
182 return (((uint8*)bytes)[0]<<24)+(((uint8*)bytes)[1]<<16)+
183 (((uint8*)bytes)[2]<<8)+((uint8*)bytes)[3];
184 }
185
186 #define ltoh_ua(ptr) (\
187 sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
188 sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] : \
189 (((uint8 *)ptr)[3]<<24)+(((uint8 *)ptr)[2]<<16)+(((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] \
190 )
191
192 #define ntoh_ua(ptr) (\
193 sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
194 sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[0]<<8)+((uint8 *)ptr)[1] : \
195 (((uint8 *)ptr)[0]<<24)+(((uint8 *)ptr)[1]<<16)+(((uint8 *)ptr)[2]<<8)+((uint8 *)ptr)[3] \
196 )
197
198 #endif /* _BCMENDIAN_H_ */
This page took 0.048597 seconds and 5 git commands to generate.