Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Less debugging info
[simgrid.git] / src / gras / DataDesc / ddt_convert.c
1 /* $Id$ */
2
3 /* ddt_remote - Stuff needed to get datadescs about remote hosts            */
4
5 /* Copyright (c) 2003 Olivier Aumage.                                       */
6 /* Copyright (c) 2003, 2004 Martin Quinson.                                 */
7 /* All rights reserved.                                                     */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 /************************************************************************/
13 /* C combines the power of assembler with the portability of assembler. */
14 /************************************************************************/
15
16 #include "gras/DataDesc/datadesc_private.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_convert,gras_ddt,
19                 "Inter-architecture convertions");
20
21 /***
22  *** Table of all known architectures:
23  ***
24   l_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:) gras_arch=0; gras_arch_name=little32;;
25   l_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=1; gras_arch_name=little32_4;;
26
27   l_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=2; gras_arch_name=little64;;
28
29   B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8:) gras_arch=3; gras_arch_name=big32;;
30   B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=4; gras_arch_name=big32_8_4;;
31   B_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4:) gras_arch=5; gras_arch_name=big32_4;;
32   B_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2:) gras_arch=6; gras_arch_name=big32_2;;
33
34   B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8:) gras_arch=7; gras_arch_name=big64;;
35
36  ***/
37
38 const gras_arch_desc_t gras_arches[gras_arch_count] = {
39
40                 {"little32_1",  0, {1,2,4,4,8,   4,4,   4,8},   /* little endian, 1 byte alignement (win32) */
41                                 {1,1,1,1,1,   1,1,   1,1}},
42
43                                 {"little32_2",  0, {1,2,4,4,8,   4,4,   4,8},   /* little endian, 2 bytes alignements (win32) */
44                                                 {1,2,2,2,2,   2,2,   2,2}},
45
46                                                 {"little32_4",    0, {1,2,4,4,8,   4,4,   4,8},   /* little endian, 4 bytes alignements (win32 and linux x86) */
47                                                                 {1,2,4,4,4,   4,4,   4,4}},
48
49                                                                 {"little32_8",    0, {1,2,4,4,8,   4,4,   4,8},         /* little endian, 8 bytes alignement (win32) */
50                                                                                 {1,2,4,4,8,   4,4,   4,8}},
51
52                                                                                 {"little64",      0, {1,2,4,8,8,   8,8,   4,8}, /* alpha, ia64 */
53                                                                                                 {1,2,4,8,8,   8,8,   4,8}},
54
55                                                                                                 {"big32_8",       1, {1,2,4,4,8,   4,4,   4,8},
56                                                                                                                 {1,2,4,4,8,   4,4,   4,8}},
57
58                                                                                                                 {"big32_8_4",     1, {1,2,4,4,8,   4,4,   4,8}, /* AIX */
59                                                                                                                                 {1,2,4,4,8,   4,4,   4,4}},
60
61                                                                                                                                 {"big32_4",       1, {1,2,4,4,8,   4,4,   4,8}, /* G5 */
62                                                                                                                                                 {1,2,4,4,4,   4,4,   4,4}},
63
64                                                                                                                                                 {"big32_2",       1, {1,2,4,4,8,   4,4,   4,8}, /* ARM */
65                                                                                                                                                                 {1,2,2,2,2,   2,2,   2,2}},
66
67                                                                                                                                                                 {"big64",         1, {1,2,4,8,8,   8,8,   4,8}, /* sparc */
68                                                                                                                                                                                 {1,2,4,8,8,   8,8,   4,8}},
69
70                                                                                                                                                                                 {"big64_8_4",     1, {1,2,4,8,8,   8,8,   4,8}, /* aix with -maix64 */
71                                                                                                                                                                                                 {1,2,4,8,8,   8,8,   4,4}}
72 };
73
74 const char *gras_datadesc_arch_name(int code) {
75         if (code < 0 || code >= gras_arch_count)
76                 return "[unknown arch]";
77         return gras_arches[code].name;
78 }
79
80
81 /**
82  * Local function doing the grunt work
83  */
84 static void
85 gras_dd_reverse_bytes(void *to,
86                 const void *from,
87                 size_t length);
88
89 /**
90  * gras_dd_convert_elm:
91  *
92  * Convert the element described by @type comming from architecture @r_arch.
93  * The data to be converted is stored in @src, and is to be stored in @dst.
94  * Both pointers may be the same location if no resizing is needed.
95  */
96 void
97 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
98                 int r_arch,
99                 void *src, void *dst) {
100         gras_dd_cat_scalar_t scal = type->category.scalar_data;
101         int cpt;
102         const void *r_data;
103         void *l_data;
104         unsigned long r_size, l_size;
105         /* Hexadecimal displayer
106   union {
107     char c[sizeof(int)];
108     int i;
109   } tester;
110          */
111
112         xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
113         xbt_assert(r_arch != GRAS_THISARCH);
114
115         r_size = type->size[r_arch];
116         l_size = type->size[GRAS_THISARCH];
117         DEBUG4("r_size=%lu l_size=%lu,    src=%p dst=%p",
118                         r_size,l_size,src,dst);
119
120         DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
121                         gras_arches[GRAS_THISARCH].endian?'B':'l');
122
123         if(r_size != l_size) {
124                 for(cpt = 0, r_data = src, l_data = dst;
125                 cpt < count;
126                 cpt++,
127                 r_data = (char *)r_data + r_size,
128                 l_data = (char *)l_data + l_size) {
129
130                         /*
131       fprintf(stderr,"r_data=");
132       for (cpt=0; cpt<r_size; cpt++) {
133         tester.i=0;
134         tester.c[0]= ((char*)r_data)[cpt];
135         fprintf(stderr,"\\%02x", tester.i);
136       }
137       fprintf(stderr,"\n");
138                          */
139
140                         /* Resize that damn integer, pal */
141
142                         unsigned char *l_sign, *r_sign;
143                         int padding;
144                         int sizeChange = l_size - r_size;
145                         int lowOrderFirst = !gras_arches[r_arch].endian ||
146                         gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
147
148                         DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
149                                         cpt, r_size,r_data, l_size,l_data);
150                         xbt_assert0(r_data != l_data, "Impossible to resize in place");
151
152                         if(sizeChange < 0) {
153                                 DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
154                                                 lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
155                                                                 scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
156                                 /* Truncate high-order bytes. */
157                                 memcpy(l_data,
158                                                 gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
159                                                                 :         r_data,
160                                                                 l_size);
161
162                                 if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
163                                         DEBUG0("This is signed");
164                                         /* Make sure the high order bit of r_data and l_data are the same */
165                                         l_sign = gras_arches[GRAS_THISARCH].endian
166                                         ? ((unsigned char*)l_data + l_size - 1)
167                                                         :  (unsigned char*)l_data;
168                                         r_sign = gras_arches[r_arch].endian
169                                         ? ((unsigned char*)r_data + r_size - 1)
170                                                         :  (unsigned char*)r_data;
171                                         DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
172
173                                         if ((*r_sign > 127) != (*l_sign > 127)) {
174                                                 if(*r_sign > 127)
175                                                         *l_sign += 128;
176                                                 else
177                                                         *l_sign -= 128;
178                                         }
179                                 }
180                         } else {
181                                 DEBUG1("Extend %d bytes", sizeChange);
182                                 if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
183                                         DEBUG0("This is signed");
184                                         padding = 0; /* pad unsigned with 0 */
185                                 } else {
186                                         /* extend sign */
187                                         r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
188                                                         :  (unsigned char*)r_data;
189                                         padding = (*r_sign > 127) ? 0xff : 0;
190                                 }
191
192                                 memset(l_data, padding, l_size);
193                                 memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange),
194                                                 r_data, r_size);
195
196                                 /*
197         fprintf(stderr,"r_data=");
198         for (cpt=0; cpt<r_size; cpt++) {
199           tester.i=0;
200           tester.c[0] = ((char*)r_data)[cpt];
201           fprintf(stderr,"\\%02x", tester.i);
202         }
203         fprintf(stderr,"\n");
204
205         fprintf(stderr,"l_data=");
206         for (cpt=0; cpt<l_size; cpt++) {
207           tester.i=0;
208           tester.c[0]= ((char*)l_data)[cpt];
209           fprintf(stderr,"\\%02x", tester.i);
210         } fprintf(stderr,"\n");
211                                  */
212                         }
213                 }
214         }
215
216         /* flip bytes if needed */
217         if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
218                         (l_size * count)  > 1) {
219
220                 for(cpt = 0, r_data=dst, l_data=dst;
221                 cpt < count;
222                 cpt++,
223                 r_data = (char *)r_data + l_size, /* resizing already done */
224                 l_data = (char *)l_data + l_size) {
225
226                         DEBUG1("Flip elm %d",cpt);
227                         gras_dd_reverse_bytes(l_data, r_data, l_size);
228                 }
229         }
230
231 }
232
233 static void
234 gras_dd_reverse_bytes(void *to,
235                 const void *from,
236                 size_t length) {
237
238         char charBegin;
239         const char *fromBegin;
240         const char *fromEnd;
241         char *toBegin;
242         char *toEnd;
243
244         for(fromBegin = (const char *)from,
245                         fromEnd = fromBegin + length - 1,
246                         toBegin = (char *)to,
247                         toEnd = toBegin + length - 1;
248
249         fromBegin <= fromEnd;
250
251         fromBegin++, fromEnd--,
252         toBegin++, toEnd--) {
253
254                 charBegin = *fromBegin;
255                 *toBegin = *fromEnd;
256                 *toEnd = charBegin;
257         }
258 }
259
260
261 /**
262  * gras_arch_selfid:
263  *
264  * returns the ID of the architecture the process is running on
265  */
266 int
267 gras_arch_selfid(void) {
268         return GRAS_THISARCH;
269 }