Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new tracing mask TRACE_VOLUME to trace the msg tasks communication size and group...
[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 {
76   if (code < 0 || code >= gras_arch_count)
77     return "[unknown arch]";
78   return gras_arches[code].name;
79 }
80
81
82 /**
83  * Local function doing the grunt work
84  */
85 static void gras_dd_reverse_bytes(void *to, const void *from, size_t length);
86
87 /**
88  * gras_dd_convert_elm:
89  *
90  * Convert the element described by @type comming from architecture @r_arch.
91  * The data to be converted is stored in @src, and is to be stored in @dst.
92  * Both pointers may be the same location if no resizing is needed.
93  */
94 void
95 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
96                     int r_arch, void *src, void *dst)
97 {
98   gras_dd_cat_scalar_t scal = type->category.scalar_data;
99   int cpt;
100   const void *r_data;
101   void *l_data;
102   unsigned long r_size, l_size;
103   /* Hexadecimal displayer
104      union {
105      char c[sizeof(int)];
106      int i;
107      } tester;
108    */
109
110   xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
111   xbt_assert(r_arch != GRAS_THISARCH);
112
113   r_size = type->size[r_arch];
114   l_size = type->size[GRAS_THISARCH];
115   DEBUG4("r_size=%lu l_size=%lu,    src=%p dst=%p", r_size, l_size, src, dst);
116
117   DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian ? 'B' : 'l',
118          gras_arches[GRAS_THISARCH].endian ? 'B' : 'l');
119
120   if (r_size != l_size) {
121     for (cpt = 0, r_data = src, l_data = dst;
122          cpt < count;
123          cpt++,
124          r_data = (char *) r_data + r_size,
125          l_data = (char *) l_data + l_size) {
126
127       /*
128          fprintf(stderr,"r_data=");
129          for (cpt=0; cpt<r_size; cpt++) {
130          tester.i=0;
131          tester.c[0]= ((char*)r_data)[cpt];
132          fprintf(stderr,"\\%02x", tester.i);
133          }
134          fprintf(stderr,"\n");
135        */
136
137       /* Resize that damn integer, pal */
138
139       unsigned char *l_sign, *r_sign;
140       int padding;
141       int sizeChange = l_size - r_size;
142       int lowOrderFirst = !gras_arches[r_arch].endian ||
143         gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian;
144
145       DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
146              cpt, r_size, r_data, l_size, l_data);
147       xbt_assert0(r_data != l_data, "Impossible to resize in place");
148
149       if (sizeChange < 0) {
150         DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
151                lowOrderFirst ? "lowOrderFirst" : "bigOrderFirst",
152                scal.encoding ==
153                e_gras_dd_scalar_encoding_sint ? "signed" : "unsigned");
154         /* Truncate high-order bytes. */
155         memcpy(l_data,
156                gras_arches[r_arch].endian ? ((char *) r_data - sizeChange)
157                : r_data, l_size);
158
159         if (scal.encoding == e_gras_dd_scalar_encoding_sint) {
160           DEBUG0("This is signed");
161           /* Make sure the high order bit of r_data and l_data are the same */
162           l_sign = gras_arches[GRAS_THISARCH].endian
163             ? ((unsigned char *) l_data + l_size - 1)
164             : (unsigned char *) l_data;
165           r_sign = gras_arches[r_arch].endian
166             ? ((unsigned char *) r_data + r_size - 1)
167             : (unsigned char *) r_data;
168           DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign, *l_sign);
169
170           if ((*r_sign > 127) != (*l_sign > 127)) {
171             if (*r_sign > 127)
172               *l_sign += 128;
173             else
174               *l_sign -= 128;
175           }
176         }
177       } else {
178         DEBUG1("Extend %d bytes", sizeChange);
179         if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
180           DEBUG0("This is signed");
181           padding = 0;          /* pad unsigned with 0 */
182         } else {
183           /* extend sign */
184           r_sign =
185             gras_arches[r_arch].endian ? ((unsigned char *) r_data + r_size -
186                                           1)
187             : (unsigned char *) r_data;
188           padding = (*r_sign > 127) ? 0xff : 0;
189         }
190
191         memset(l_data, padding, l_size);
192         memcpy(!gras_arches[r_arch].endian ? l_data
193                : ((char *) l_data + sizeChange), r_data, r_size);
194
195         /*
196            fprintf(stderr,"r_data=");
197            for (cpt=0; cpt<r_size; cpt++) {
198            tester.i=0;
199            tester.c[0] = ((char*)r_data)[cpt];
200            fprintf(stderr,"\\%02x", tester.i);
201            }
202            fprintf(stderr,"\n");
203
204            fprintf(stderr,"l_data=");
205            for (cpt=0; cpt<l_size; cpt++) {
206            tester.i=0;
207            tester.c[0]= ((char*)l_data)[cpt];
208            fprintf(stderr,"\\%02x", tester.i);
209            } fprintf(stderr,"\n");
210          */
211       }
212     }
213   }
214
215   /* flip bytes if needed */
216   if (gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
217       (l_size * count) > 1) {
218
219     for (cpt = 0, r_data = dst, l_data = dst; cpt < count; cpt++, r_data = (char *) r_data + l_size,    /* resizing already done */
220          l_data = (char *) l_data + l_size) {
221
222       DEBUG1("Flip elm %d", cpt);
223       gras_dd_reverse_bytes(l_data, r_data, l_size);
224     }
225   }
226
227 }
228
229 static void gras_dd_reverse_bytes(void *to, const void *from, size_t length)
230 {
231
232   char charBegin;
233   const char *fromBegin;
234   const char *fromEnd;
235   char *toBegin;
236   char *toEnd;
237
238   for (fromBegin = (const char *) from,
239        fromEnd = fromBegin + length - 1,
240        toBegin = (char *) to,
241        toEnd = toBegin + length - 1;
242        fromBegin <= fromEnd; fromBegin++, fromEnd--, toBegin++, toEnd--) {
243
244     charBegin = *fromBegin;
245     *toBegin = *fromEnd;
246     *toEnd = charBegin;
247   }
248 }
249
250
251 /**
252  * gras_arch_selfid:
253  *
254  * returns the ID of the architecture the process is running on
255  */
256 int gras_arch_selfid(void)
257 {
258   return GRAS_THISARCH;
259 }