• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __INC_ENDIANFREE_H
2 #define __INC_ENDIANFREE_H
3 
4 /*
5  *	Call endian free function when
6  *		1. Read/write packet content.
7  *		2. Before write integer to IO.
8  *		3. After read integer from IO.
9  */
10 
11 #define __MACHINE_LITTLE_ENDIAN 1234    /* LSB first: i386, vax */
12 #define __MACHINE_BIG_ENDIAN    4321    /* MSB first: 68000, ibm, net, ppc */
13 
14 #define BYTE_ORDER __MACHINE_LITTLE_ENDIAN
15 
16 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
17 #define EF1Byte(_val)			((u8)(_val))
18 #define EF2Byte(_val)			((u16)(_val))
19 #define EF4Byte(_val)			((u32)(_val))
20 
21 #else
22 #define EF1Byte(_val)			((u8)(_val))
23 #define EF2Byte(_val)			\
24 	(((((u16)(_val))&0x00ff)<<8)|((((u16)(_val))&0xff00)>>8))
25 #define EF4Byte(_val)			\
26 	(((((u32)(_val))&0x000000ff)<<24)|\
27 	((((u32)(_val))&0x0000ff00)<<8)|\
28 	((((u32)(_val))&0x00ff0000)>>8)|\
29 	((((u32)(_val))&0xff000000)>>24))
30 #endif
31 
32 #define ReadEF1Byte(_ptr)		EF1Byte(*((u8 *)(_ptr)))
33 #define ReadEF2Byte(_ptr)		EF2Byte(*((u16 *)(_ptr)))
34 #define ReadEF4Byte(_ptr)		EF4Byte(*((u32 *)(_ptr)))
35 
36 #define WriteEF1Byte(_ptr, _val)	((*((u8 *)(_ptr))) = EF1Byte(_val))
37 #define WriteEF2Byte(_ptr, _val)	((*((u16 *)(_ptr))) = EF2Byte(_val))
38 #define WriteEF4Byte(_ptr, _val)	((*((u32 *)(_ptr))) = EF4Byte(_val))
39 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
40 #define H2N1BYTE(_val)	((u8)(_val))
41 #define H2N2BYTE(_val)	(((((u16)(_val))&0x00ff)<<8)|\
42 			((((u16)(_val))&0xff00)>>8))
43 #define H2N4BYTE(_val)	(((((u32)(_val))&0x000000ff)<<24)|\
44 			((((u32)(_val))&0x0000ff00)<<8)	|\
45 			((((u32)(_val))&0x00ff0000)>>8)	|\
46 			((((u32)(_val))&0xff000000)>>24))
47 #else
48 #define H2N1BYTE(_val)			((u8)(_val))
49 #define H2N2BYTE(_val)			((u16)(_val))
50 #define H2N4BYTE(_val)			((u32)(_val))
51 #endif
52 
53 #if BYTE_ORDER == __MACHINE_LITTLE_ENDIAN
54 #define N2H1BYTE(_val)	((u8)(_val))
55 #define N2H2BYTE(_val)	(((((u16)(_val))&0x00ff)<<8)|\
56 			((((u16)(_val))&0xff00)>>8))
57 #define N2H4BYTE(_val)	(((((u32)(_val))&0x000000ff)<<24)|\
58 			((((u32)(_val))&0x0000ff00)<<8)	|\
59 			((((u32)(_val))&0x00ff0000)>>8)	|\
60 			((((u32)(_val))&0xff000000)>>24))
61 #else
62 #define N2H1BYTE(_val)			((u8)(_val))
63 #define N2H2BYTE(_val)			((u16)(_val))
64 #define N2H4BYTE(_val)			((u32)(_val))
65 #endif
66 
67 #define BIT_LEN_MASK_32(__BitLen) (0xFFFFFFFF >> (32 - (__BitLen)))
68 #define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen)			\
69 	(BIT_LEN_MASK_32(__BitLen) << (__BitOffset))
70 
71 #define LE_P4BYTE_TO_HOST_4BYTE(__pStart) (EF4Byte(*((u32 *)(__pStart))))
72 
73 #define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
74 	( \
75 	  (LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset)) \
76 	  & \
77 	  BIT_LEN_MASK_32(__BitLen) \
78 	)
79 
80 #define LE_BITS_CLEARED_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
81 	( \
82 	  LE_P4BYTE_TO_HOST_4BYTE(__pStart) \
83 	  & \
84 	  (~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen)) \
85 	)
86 
87 #define BIT_LEN_MASK_16(__BitLen) \
88 	(0xFFFF >> (16 - (__BitLen)))
89 
90 #define BIT_OFFSET_LEN_MASK_16(__BitOffset, __BitLen) \
91 	(BIT_LEN_MASK_16(__BitLen) << (__BitOffset))
92 
93 #define LE_P2BYTE_TO_HOST_2BYTE(__pStart) \
94 	(EF2Byte(*((u16 *)(__pStart))))
95 
96 #define LE_BITS_TO_2BYTE(__pStart, __BitOffset, __BitLen) \
97 	( \
98 	  (LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset)) \
99 	  & \
100 	  BIT_LEN_MASK_16(__BitLen) \
101 	)
102 
103 #define BIT_LEN_MASK_8(__BitLen) \
104 	(0xFF >> (8 - (__BitLen)))
105 
106 #define BIT_OFFSET_LEN_MASK_8(__BitOffset, __BitLen) \
107 	(BIT_LEN_MASK_8(__BitLen) << (__BitOffset))
108 
109 #define LE_P1BYTE_TO_HOST_1BYTE(__pStart) \
110 	(EF1Byte(*((u8 *)(__pStart))))
111 
112 #define LE_BITS_TO_1BYTE(__pStart, __BitOffset, __BitLen) \
113 	( \
114 	  (LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset)) \
115 	  & \
116 	  BIT_LEN_MASK_8(__BitLen) \
117 	)
118 
119 #define	N_BYTE_ALIGMENT(__Value, __Aligment)			\
120 	 ((__Aligment == 1) ? (__Value) : (((__Value + __Aligment - 1) / \
121 	__Aligment) * __Aligment))
122 #endif
123