Tomato 1.28
[tomato-rt-n10.git] / release / src / include / bcmendian.h
blobae31617c94220ec257ba2a86606edd87706e7c03
1 /*
2 * local version of endian.h - byte order defines
4 * Copyright 2005, 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.1.1.8 2005/03/07 07:31:12 kanki 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 static INLINE uint16
35 bcmswap16(uint16 val)
37 return BCMSWAP16(val);
40 static INLINE uint32
41 bcmswap32(uint32 val)
43 return BCMSWAP32(val);
46 /* buf - start of buffer of shorts to swap */
47 /* len - byte length of buffer */
48 static INLINE void
49 bcmswap16_buf(uint16 *buf, uint len)
51 len = len/2;
53 while(len--){
54 *buf = bcmswap16(*buf);
55 buf++;
59 #ifndef hton16
60 #ifndef IL_BIGENDIAN
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)
66 #define ltoh16(i) (i)
67 #define ltoh32(i) (i)
68 #define htol16(i) (i)
69 #define htol32(i) (i)
70 #else
71 #define HTON16(i) (i)
72 #define hton16(i) (i)
73 #define hton32(i) (i)
74 #define ntoh16(i) (i)
75 #define ntoh32(i) (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)
80 #endif
81 #endif
83 #ifndef IL_BIGENDIAN
84 #define ltoh16_buf(buf, i)
85 #define htol16_buf(buf, i)
86 #else
87 #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
88 #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i)
89 #endif
92 * load 16-bit value from unaligned little endian byte array.
94 static INLINE uint16
95 ltoh16_ua(uint8 *bytes)
97 return (bytes[1]<<8)+bytes[0];
101 * load 32-bit value from unaligned little endian byte array.
103 static INLINE uint32
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.
112 static INLINE uint16
113 ntoh16_ua(uint8 *bytes)
115 return (bytes[0]<<8)+bytes[1];
119 * load 32-bit value from unaligned big(network) endian byte array.
121 static INLINE uint32
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 */
128 #ifdef IL_BIGENDIAN
129 #define get_ua(ptr) \
130 ({ \
131 __typeof__(*(ptr)) __val; \
133 switch (sizeof(*(ptr))) { \
134 case 1: \
135 __val = *(uint8 *)ptr; \
136 break; \
137 case 2: \
138 __val = ntoh16_ua((uint8 *)ptr); \
139 break; \
140 case 4: \
141 __val = ntoh32_ua((uint8 *)ptr); \
142 break; \
145 __val; \
147 #else
148 #define get_ua(ptr) \
149 ({ \
150 __typeof__(*(ptr)) __val; \
152 switch (sizeof(*(ptr))) { \
153 case 1: \
154 __val = *(uint8 *)ptr; \
155 break; \
156 case 2: \
157 __val = ltoh16_ua((uint8 *)ptr); \
158 break; \
159 case 4: \
160 __val = ltoh32_ua((uint8 *)ptr); \
161 break; \
164 __val; \
166 #endif
168 #endif /* _BCMENDIAN_H_ */