Logo AND Algorithmique Numérique Distribuée

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