3 * Copyright (C) 2001 By Joachim Martillo, Telford Tools, Inc.
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.
12 /****************************************************/
13 /****************************************************/
14 /* Begin header file "endian.h" */
15 /****************************************************/
16 /****************************************************/
18 #if !defined(_ENDIAN_HP_)
21 /****************************************************/
23 /****************************************************/
26 /****************************************************/
27 /* let's see if we know this is a big endian */
28 /****************************************************/
33 #define fnm_assert_stmt(a)
36 #if defined(AMD29K) || defined(mc68000)
38 #define BIG_ENDIAN 4321
42 /****************************************************/
43 /* global macro functions handling endian issues */
44 /****************************************************/
46 #if !defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)
47 #define LITTLE_ENDIAN 1234
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)
56 #define fnm_get_ui_big_endian(p_uc, x) \
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)++)); \
64 #define fnm_get_us_big_endian(p_uc, x) \
66 (x) = (((unsigned short)(*(p_uc)++)) << 8); \
67 (x) += ((unsigned short)(*(p_uc)++)); \
70 #define fnm_get_ui_little_endian(p_uc, x) \
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); \
78 #define fnm_get_us_little_endian(p_uc, x) \
80 (x) = ((unsigned short)(*(p_uc)++)); \
81 (x) += (((unsigned short)(*(p_uc)++)) << 8); \
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)
89 #define fnm_store_ui_big_endian(p_uc, x) \
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)); \
97 #define fnm_store_us_big_endian(p_uc, x) \
99 *(p_uc)++ = (unsigned char) (((unsigned short)(x)) >> 8); \
100 *(p_uc)++ = (unsigned char) ((unsigned short)(x)); \
103 #define fnm_store_ui_little_endian(p_uc, x) \
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); \
111 #define fnm_store_us_little_endian(p_uc, x) \
113 *(p_uc)++ = ((unsigned short)(x)); \
114 *(p_uc)++ = (((unsigned short)(x)) >> 8); \
117 /* for now lets always use the macroes instead of the inline procedures
118 so that we are sure they work */
120 #if 1 || defined(AMD29K)
122 #define fnm_convert_us_endian(x) \
123 ((unsigned short)((((unsigned short)(x)) << 8) + (((unsigned short)(x)) >> 8)))
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)))
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))))
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)))
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))))
141 INLINE unsigned short fni_convert_us_endian(const unsigned short x)
143 return((x << 8) + (x >> 8));
146 INLINE unsigned int fni_convert_ui_endian(const unsigned int x)
148 return((x >> 24) + ((x & 0x00ff0000) >> 8)
149 + ((x & 0x0000ff00) << 8) + (x << 24));
152 INLINE unsigned int fni_make_ui_from_2_us(const unsigned short us_high_part,
153 const unsigned short us_low_part)
155 return((((unsigned int)us_high_part) << 16) + us_low_part);
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)
161 return(((((((unsigned int)p1 << 8) + p2) << 8) + p3) << 8) + p4);
164 INLINE unsigned short fni_make_us_from_2_uc(const unsigned char uc_high_part,
165 const unsigned char uc_low_part)
167 return((((unsigned short)uc_high_part) << 8) + uc_low_part);
170 #define fnm_convert_us_endian(x) fni_convert_us_endian(x)
171 #define fnm_convert_ui_endian(x) fni_convert_ui_endian(x)
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)
176 #define fnm_make_ui_from_4_uc(p1, p2, p3, p4) \
177 fni_make_ui_from_4_uc(p1, p2, p3, p4)
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)
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)))
187 #if defined(BIG_ENDIAN)
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))
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)
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)
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))
213 /****************************************************/
214 /* test macro functions handling endian issues */
215 /****************************************************/
219 #define fnm_test_definitions()
223 #define fnm_test_definitions() \
228 unsigned short a_us[2]; \
231 } t1 = { "\x01\x02\x03\x04" }; \
234 unsigned short us_one, us_two; \
235 unsigned int ul_one; \
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)); \
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); \
245 p = (unsigned char*)(&t1); \
247 fnm_get_us_little_endian(p, us_one); \
248 fnm_get_us_little_endian(p, us_two); \
250 fnm_assert_stmt((us_one == 0x0201) && (us_two == 0x0403)); \
251 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
253 p = (unsigned char*)(&t1); \
255 fnm_get_us_big_endian(p, us_one); \
256 fnm_get_us_big_endian(p, us_two); \
258 fnm_assert_stmt((us_one == 0x0102) && (us_two == 0x0304)); \
259 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
261 p = (unsigned char*)(&t1); \
263 fnm_get_ui_little_endian(p, ul_one); \
265 fnm_assert_stmt(ul_one == 0x04030201); \
266 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
268 p = (unsigned char*)(&t1); \
270 fnm_get_ui_big_endian(p, ul_one); \
272 fnm_assert_stmt(ul_one == 0x01020304); \
273 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
275 p = (unsigned char*)(&t1); \
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))); \
281 p = (unsigned char*)(&t1); \
283 fnm_get_us_little_endian(p, us_one); \
284 fnm_get_us_little_endian(p, us_two); \
286 fnm_assert_stmt((us_one == 0x1234) && (us_two == 0x5678)); \
287 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
289 p = (unsigned char*)(&t1); \
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))); \
295 p = (unsigned char*)(&t1); \
297 fnm_get_us_big_endian(p, us_one); \
298 fnm_get_us_big_endian(p, us_two); \
300 fnm_assert_stmt((us_one == 0x1234) && (us_two == 0x5678)); \
301 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
303 p = (unsigned char*)(&t1); \
305 fnm_store_ui_little_endian(p, 0x12345678); \
306 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
308 p = (unsigned char*)(&t1); \
310 fnm_get_ui_little_endian(p, ul_one); \
312 fnm_assert_stmt(ul_one == 0x12345678); \
313 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
315 p = (unsigned char*)(&t1); \
317 fnm_store_ui_big_endian(p, 0x12345678); \
318 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
320 p = (unsigned char*)(&t1); \
322 fnm_get_ui_big_endian(p, ul_one); \
324 fnm_assert_stmt(ul_one == 0x12345678); \
325 fnm_assert_stmt((p-4) == ((unsigned char*)(&t1))); \
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); \
337 /****************************************************/
338 /****************************************************/
339 /* End header file "endian.h" */
340 /****************************************************/
341 /****************************************************/