import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / net / wan / 8253x / endian.h
1 /* -*- linux-c -*- */
2 /*
3  * Copyright (C) 2001 By Joachim Martillo, Telford Tools, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version
8  * 2 of the License, or (at your option) any later version.
9  *
10  */
11
12 /****************************************************/
13 /****************************************************/
14 /*          Begin header file "endian.h"            */
15 /****************************************************/
16 /****************************************************/
17
18 #if !defined(_ENDIAN_HP_)
19 #define _ENDIAN_HP_
20
21 /****************************************************/
22 /*                  header files                    */
23 /****************************************************/
24
25
26 /****************************************************/
27 /*  let's see if we know this is a big endian       */
28 /****************************************************/
29 #ifndef INLINE
30 #define INLINE inline
31 #endif
32
33 #define fnm_assert_stmt(a)
34
35 #ifndef BYTE_ORDER
36 #if defined(AMD29K) || defined(mc68000)
37
38 #define BIG_ENDIAN     4321
39
40 #endif
41
42 /****************************************************/
43 /*  global macro functions handling endian issues   */
44 /****************************************************/
45
46 #if !defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)
47 #define LITTLE_ENDIAN 1234
48 #endif
49 #endif
50
51 #define fnm_get_i_big_endian(p_uc, x)       fnm_get_ui_big_endian(p_uc, x)
52 #define fnm_get_s_big_endian(p_uc, x)       fnm_get_us_big_endian(p_uc, x)
53 #define fnm_get_i_little_endian(p_uc, x)    fnm_get_ui_little_endian(p_uc, x)
54 #define fnm_get_s_little_endian(p_uc, x)    fnm_get_us_little_endian(p_uc, x)
55
56 #define fnm_get_ui_big_endian(p_uc, x)                                      \
57 {                                                                           \
58         (x) = (((unsigned int)(*(p_uc)++)) << 24);                                              \
59         (x) += (((unsigned int)(*(p_uc)++)) << 16);                                             \
60         (x) += (((unsigned int)(*(p_uc)++)) << 8);                                              \
61         (x) += ((unsigned int)(*(p_uc)++));                                                     \
62 }
63
64 #define fnm_get_us_big_endian(p_uc, x)                                          \
65 {                                                                               \
66         (x) = (((unsigned short)(*(p_uc)++)) << 8);                                             \
67         (x) += ((unsigned short)(*(p_uc)++));                                                   \
68 }
69
70 #define fnm_get_ui_little_endian(p_uc, x)                                       \
71 {                                                                               \
72         (x) = ((unsigned int)(*(p_uc)++));                                                      \
73         (x) += (((unsigned int)(*(p_uc)++)) << 8);                                              \
74         (x) += (((unsigned int)(*(p_uc)++)) << 16);                                             \
75         (x) += (((unsigned int)(*(p_uc)++)) << 24);                                             \
76 }
77
78 #define fnm_get_us_little_endian(p_uc, x)                                       \
79 {                                                                               \
80         (x) = ((unsigned short)(*(p_uc)++));                                                    \
81         (x) += (((unsigned short)(*(p_uc)++)) << 8);                                            \
82 }
83
84 #define fnm_store_i_big_endian(p_uc, x)         fnm_store_ui_big_endian(p_uc, x)
85 #define fnm_store_s_big_endian(p_uc, x)         fnm_store_us_big_endian(p_uc, x)
86 #define fnm_store_i_little_endian(p_uc, x)  fnm_store_ui_little_endian(p_uc, x)
87 #define fnm_store_s_little_endian(p_uc, x)  fnm_store_us_little_endian(p_uc, x)
88
89 #define fnm_store_ui_big_endian(p_uc, x)                                        \
90 {                                                                               \
91         *(p_uc)++ = (((unsigned int)(x)) >> 24);                                                \
92         *(p_uc)++ = (((unsigned int)(x)) >> 16);                                                \
93         *(p_uc)++ = (((unsigned int)(x)) >> 8);                                         \
94         *(p_uc)++ = ((unsigned int)(x));                                                        \
95 }
96
97 #define fnm_store_us_big_endian(p_uc, x)                                        \
98 {                                                                               \
99         *(p_uc)++ = (unsigned char) (((unsigned short)(x)) >> 8);                               \
100         *(p_uc)++ = (unsigned char) ((unsigned short)(x));                                      \
101 }
102
103 #define fnm_store_ui_little_endian(p_uc, x)                                     \
104 {                                                                               \
105         *(p_uc)++ = ((unsigned int)(x));                                                        \
106         *(p_uc)++ = (((unsigned int)(x)) >> 8);                                         \
107         *(p_uc)++ = (((unsigned int)(x)) >> 16);                                                \
108         *(p_uc)++ = (((unsigned int)(x)) >> 24);                                                \
109 }
110
111 #define fnm_store_us_little_endian(p_uc, x)                                     \
112 {                                                                               \
113         *(p_uc)++ = ((unsigned short)(x));                                                      \
114         *(p_uc)++ = (((unsigned short)(x)) >> 8);                                               \
115 }
116
117 /* for now lets always use the macroes instead of the inline procedures
118    so that we are sure they work */
119
120 #if 1 || defined(AMD29K)
121
122 #define fnm_convert_us_endian(x)                                                \
123         ((unsigned short)((((unsigned short)(x)) << 8) + (((unsigned short)(x)) >> 8)))
124
125 #define fnm_convert_ui_endian(x)                                                \
126         ((unsigned int)((((unsigned int)(x)) >> 24) + ((((unsigned int)(x)) & 0x00ff0000) >> 8) +               \
127                  ((((unsigned int)(x)) & 0x0000ff00) << 8) + (((unsigned int)(x)) << 24)))
128
129 #define fnm_make_ui_from_2_us(us_high_part, us_low_part)                        \
130         ((unsigned int)((((unsigned int)(us_high_part)) << 16) + ((unsigned short)(us_low_part))))
131
132 #define fnm_make_ui_from_4_uc(p1, p2, p3, p4)                                   \
133         ((unsigned int)(((((((unsigned int)((t_uc)p1) << 8) + ((t_uc)p2)) << 8)                 \
134                 + ((t_uc)p3)) << 8) + ((t_uc)p4)))
135
136 #define fnm_make_us_from_2_uc(uc_high_part, uc_low_part)                        \
137         ((unsigned short)((((unsigned short)(uc_high_part)) << 8) + ((t_uc)(uc_low_part))))
138
139 #else
140
141 INLINE unsigned short fni_convert_us_endian(const unsigned short x)
142 {
143         return((x << 8) + (x >> 8));
144 }
145
146 INLINE unsigned int fni_convert_ui_endian(const unsigned int x)
147 {
148         return((x >> 24) + ((x & 0x00ff0000) >> 8)
149            + ((x & 0x0000ff00) << 8) + (x << 24));
150 }
151
152 INLINE unsigned int fni_make_ui_from_2_us(const unsigned short us_high_part,
153                                   const unsigned short us_low_part)
154 {
155         return((((unsigned int)us_high_part) << 16) + us_low_part);
156 }
157
158 INLINE unsigned int fni_make_ui_from_4_uc(const unsigned char p1, const unsigned char p2,
159                                   const unsigned char p3, const unsigned char p4)
160 {
161         return(((((((unsigned int)p1 << 8) + p2) << 8) + p3) << 8) + p4);
162 }
163
164 INLINE unsigned short fni_make_us_from_2_uc(const unsigned char uc_high_part,
165                                   const unsigned char uc_low_part)
166 {
167         return((((unsigned short)uc_high_part) << 8) + uc_low_part);
168 }
169
170 #define fnm_convert_us_endian(x)        fni_convert_us_endian(x)
171 #define fnm_convert_ui_endian(x)        fni_convert_ui_endian(x)
172
173 #define fnm_make_ui_from_2_us(us_high_part, us_low_part)                        \
174         fni_make_ui_from_2_us(us_high_part, us_low_part)
175
176 #define fnm_make_ui_from_4_uc(p1, p2, p3, p4)                                   \
177         fni_make_ui_from_4_uc(p1, p2, p3, p4)
178
179 #define fnm_make_us_from_2_uc(uc_high_part, uc_low_part)                        \
180         fni_make_us_from_2_uc(uc_high_part, uc_low_part)
181
182 #endif
183
184 #define fnm_convert_s_endian(x)         ((short)(fnm_convert_us_endian(x)))
185 #define fnm_convert_i_endian(x)         ((int)(fnm_convert_ui_endian(x)))
186
187 #if defined(BIG_ENDIAN)
188
189 #define fnm_convert_us_big_endian(x)            ((unsigned short)(x))
190 #define fnm_convert_s_big_endian(x)             ((short)(x))
191 #define fnm_convert_ui_big_endian(x)            ((unsigned int)(x))
192 #define fnm_convert_i_big_endian(x)             ((int)(x))
193
194 #define fnm_convert_us_little_endian(x)         fnm_convert_us_endian(x)
195 #define fnm_convert_s_little_endian(x)          fnm_convert_s_endian(x)
196 #define fnm_convert_ui_little_endian(x)         fnm_convert_ui_endian(x)
197 #define fnm_convert_i_little_endian(x)          fnm_convert_i_endian(x)
198
199 #else
200
201 #define fnm_convert_us_big_endian(x)            fnm_convert_us_endian(x)
202 #define fnm_convert_s_big_endian(x)             fnm_convert_s_endian(x)
203 #define fnm_convert_ui_big_endian(x)            fnm_convert_ui_endian(x)
204 #define fnm_convert_i_big_endian(x)             fnm_convert_i_endian(x)
205
206 #define fnm_convert_us_little_endian(x)         ((unsigned short)(x))
207 #define fnm_convert_s_little_endian(x)          ((short)(x))
208 #define fnm_convert_ui_little_endian(x)         ((unsigned int)(x))
209 #define fnm_convert_i_little_endian(x)          ((int)(x))
210
211 #endif
212
213 /****************************************************/
214 /*  test macro functions handling endian issues         */
215 /****************************************************/
216
217 #if defined(NDEBUG)
218
219 #define fnm_test_definitions()
220
221 #else
222
223 #define fnm_test_definitions()                                                  \
224 {                                                                               \
225         union                                                                   \
226         {                                                                               \
227         t_c     a_c[4];                                                         \
228         unsigned short  a_us[2];                                                        \
229         unsigned int    ul;                                                             \
230                                                                                 \
231         } t1 = { "\x01\x02\x03\x04" };                                          \
232                                                                                 \
233         unsigned char   *p;                                                                     \
234         unsigned short  us_one, us_two;                                                 \
235         unsigned int    ul_one;                                                         \
236                                                                                 \
237         fnm_assert_stmt((t1.a_c[0] == 1) && (t1.a_c[1] == 2) &&                 \
238                         (t1.a_c[2] == 3) && (t1.a_c[3] == 4));                  \
239                                                                                 \
240         fnm_assert_stmt(fnm_convert_ui_big_endian(t1.ul) == 0x01020304);                \
241         fnm_assert_stmt(fnm_convert_ui_little_endian(t1.ul) == 0x04030201);             \
242         fnm_assert_stmt(fnm_convert_us_big_endian(t1.a_us[0]) == 0x0102);               \
243         fnm_assert_stmt(fnm_convert_us_little_endian(t1.a_us[0]) == 0x0201);    \
244                                                                                 \
245         p = (unsigned char*)(&t1);                                                              \
246                                                                                 \
247         fnm_get_us_little_endian(p, us_one);                                    \
248         fnm_get_us_little_endian(p, us_two);                                    \
249                                                                                 \
250         fnm_assert_stmt((us_one == 0x0201) && (us_two == 0x0403));                      \
251         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
252                                                                                 \
253         p = (unsigned char*)(&t1);                                                              \
254                                                                                 \
255         fnm_get_us_big_endian(p, us_one);                                               \
256         fnm_get_us_big_endian(p, us_two);                                               \
257                                                                                 \
258         fnm_assert_stmt((us_one == 0x0102) && (us_two == 0x0304));                      \
259         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
260                                                                                 \
261         p = (unsigned char*)(&t1);                                                              \
262                                                                                 \
263         fnm_get_ui_little_endian(p, ul_one);                                    \
264                                                                                 \
265         fnm_assert_stmt(ul_one == 0x04030201);                                  \
266         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
267                                                                                 \
268         p = (unsigned char*)(&t1);                                                              \
269                                                                                 \
270         fnm_get_ui_big_endian(p, ul_one);                                               \
271                                                                                 \
272         fnm_assert_stmt(ul_one == 0x01020304);                                  \
273         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
274                                                                                 \
275         p = (unsigned char*)(&t1);                                                              \
276                                                                                 \
277         fnm_store_us_little_endian(p, 0x1234);                                  \
278         fnm_store_us_little_endian(p, 0x5678);                                  \
279         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
280                                                                                 \
281         p = (unsigned char*)(&t1);                                                              \
282                                                                                 \
283         fnm_get_us_little_endian(p, us_one);                                    \
284         fnm_get_us_little_endian(p, us_two);                                    \
285                                                                                 \
286         fnm_assert_stmt((us_one == 0x1234) && (us_two == 0x5678));                      \
287         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
288                                                                                 \
289         p = (unsigned char*)(&t1);                                                              \
290                                                                                 \
291         fnm_store_us_big_endian(p, 0x1234);                                             \
292         fnm_store_us_big_endian(p, 0x5678);                                             \
293         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
294                                                                                 \
295         p = (unsigned char*)(&t1);                                                              \
296                                                                                 \
297         fnm_get_us_big_endian(p, us_one);                                               \
298         fnm_get_us_big_endian(p, us_two);                                               \
299                                                                                 \
300         fnm_assert_stmt((us_one == 0x1234) && (us_two == 0x5678));                      \
301         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
302                                                                                 \
303         p = (unsigned char*)(&t1);                                                              \
304                                                                                 \
305         fnm_store_ui_little_endian(p, 0x12345678);                                      \
306         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
307                                                                                 \
308         p = (unsigned char*)(&t1);                                                              \
309                                                                                 \
310         fnm_get_ui_little_endian(p, ul_one);                                    \
311                                                                                 \
312         fnm_assert_stmt(ul_one == 0x12345678);                                  \
313         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
314                                                                                 \
315         p = (unsigned char*)(&t1);                                                              \
316                                                                                 \
317         fnm_store_ui_big_endian(p, 0x12345678);                                 \
318         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
319                                                                                 \
320         p = (unsigned char*)(&t1);                                                              \
321                                                                                 \
322         fnm_get_ui_big_endian(p, ul_one);                                               \
323                                                                                 \
324         fnm_assert_stmt(ul_one == 0x12345678);                                  \
325         fnm_assert_stmt((p-4) == ((unsigned char*)(&t1)));                                      \
326                                                                                 \
327         fnm_assert_stmt(fnm_make_ui_from_2_us(1, 2) == 0x00010002);                     \
328         fnm_assert_stmt(fnm_make_ui_from_4_uc(1, 2, 3, 4) == 0x01020304);               \
329         fnm_assert_stmt(fnm_make_us_from_2_uc(1, 2) == 0x0102);                 \
330                                                                                 \
331 }
332
333 #endif
334
335 #endif
336
337 /****************************************************/
338 /****************************************************/
339 /*              End header file "endian.h"                      */
340 /****************************************************/
341 /****************************************************/