blob: b189fa5a45e42c20ceee888714d8c72b5c35f41b [file] [log] [blame]
Larry Finger94a79942011-08-23 19:00:42 -05001#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
Larry Fingera44325f2011-08-25 11:48:23 -050017#define EF1Byte(_val) ((u8)(_val))
18#define EF2Byte(_val) ((u16)(_val))
19#define EF4Byte(_val) ((u32)(_val))
Larry Finger94a79942011-08-23 19:00:42 -050020
21#else
Larry Fingera44325f2011-08-25 11:48:23 -050022#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))
Larry Finger94a79942011-08-23 19:00:42 -050030#endif
31
32#define ReadEF1Byte(_ptr) EF1Byte(*((u8 *)(_ptr)))
33#define ReadEF2Byte(_ptr) EF2Byte(*((u16 *)(_ptr)))
34#define ReadEF4Byte(_ptr) EF4Byte(*((u32 *)(_ptr)))
35
Andreas Frembs6e925e12013-12-20 11:29:08 +010036#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))
Larry Finger94a79942011-08-23 19:00:42 -050039#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)))
Larry Fingera44325f2011-08-25 11:48:23 -050068#define BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen) \
69 (BIT_LEN_MASK_32(__BitLen) << (__BitOffset))
Larry Finger94a79942011-08-23 19:00:42 -050070
71#define LE_P4BYTE_TO_HOST_4BYTE(__pStart) (EF4Byte(*((u32 *)(__pStart))))
72
73#define LE_BITS_TO_4BYTE(__pStart, __BitOffset, __BitLen) \
74 ( \
Larry Fingera44325f2011-08-25 11:48:23 -050075 (LE_P4BYTE_TO_HOST_4BYTE(__pStart) >> (__BitOffset)) \
Larry Finger94a79942011-08-23 19:00:42 -050076 & \
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 & \
Larry Fingera44325f2011-08-25 11:48:23 -050084 (~BIT_OFFSET_LEN_MASK_32(__BitOffset, __BitLen)) \
Larry Finger94a79942011-08-23 19:00:42 -050085 )
86
Larry Finger94a79942011-08-23 19:00:42 -050087#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 ( \
Larry Fingera44325f2011-08-25 11:48:23 -050098 (LE_P2BYTE_TO_HOST_2BYTE(__pStart) >> (__BitOffset)) \
Larry Finger94a79942011-08-23 19:00:42 -050099 & \
100 BIT_LEN_MASK_16(__BitLen) \
101 )
102
Larry Finger94a79942011-08-23 19:00:42 -0500103#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 ( \
Larry Fingera44325f2011-08-25 11:48:23 -0500114 (LE_P1BYTE_TO_HOST_1BYTE(__pStart) >> (__BitOffset)) \
Larry Finger94a79942011-08-23 19:00:42 -0500115 & \
116 BIT_LEN_MASK_8(__BitLen) \
117 )
118
Larry Fingera44325f2011-08-25 11:48:23 -0500119#define N_BYTE_ALIGMENT(__Value, __Aligment) \
120 ((__Aligment == 1) ? (__Value) : (((__Value + __Aligment - 1) / \
121 __Aligment) * __Aligment))
Larry Finger94a79942011-08-23 19:00:42 -0500122#endif