Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
486f2004f1e6f8cb8f5d5b34b35afb03b859eda2
[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(ddt_convert,datadesc,
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/4> P<4/4:4/4> D<4/4:8/4>
25  ***  l C<1/1> I<2/2:4/4:8/8:8/8> P<4/4:4/4> D<4/4:8/8>
26  ***  B C<1/1> I<2/2:4/4:4/8:8/8> P<4/4:4/4> D<4/4:8/4>
27  ***  B C<1/1> I<2/2:4/8:8/8:8/8> P<4/4:4/4> D<4/4:8/4>
28  ***  B C:1/1: I:2/2:4/4:4/4:8/8: P:4/4:4/4: D:4/4:8/4: AIX
29  ***  B C:1/1: I:2/2:4/2:4/2:8/2: P:4/2:4/2: D:4/2:8/2: ARM
30  ***  l C<1/1> I<2/2:4/4:4/4:8/8> P<4/4:4/4> D<4/4:8/8> win32 
31  ***/
32
33 const gras_arch_desc_t gras_arches[gras_arch_count] = {
34   {"little32", 0,   {1,2,4,4,8,   4,4,   4,8}, 
35                     {1,2,4,4,4,   4,4,   4,4}},
36
37   {"little64", 0,   {1,2,4,8,8,   8,8,   4,8}, 
38                     {1,2,4,8,8,   8,8,   4,8}},
39
40   {"big32",    1,   {1,2,4,4,8,   4,4,   4,8}, 
41                     {1,2,4,4,8,   4,4,   4,8}},
42
43   {"big64",    1,   {1,2,4,8,8,   8,8,   4,8}, 
44                     {1,2,4,8,8,   8,8,   4,8}},
45
46   {"aix",      1,   {1,2,4,4,8,   4,4,   4,8}, 
47                     {1,2,4,4,8,   4,4,   4,4}},
48
49   {"arm",      1,   {1,2,4,4,8,   4,4,   4,8}, 
50                     {1,2,2,2,2,   2,2,   2,2}},
51
52   {"win32",    0,   {1,2,4,4,8,   4,4,   4,8}, 
53                     {1,2,4,4,8,   4,4,   4,8}}
54 };
55
56 const char *gras_datadesc_arch_name(int code) {
57    if (code < 0 || code >= gras_arch_count)
58      return "[unknown arch]";
59    return gras_arches[code].name;
60 }
61
62
63 /**
64  * Local function doing the grunt work
65  */
66 static void
67 gras_dd_reverse_bytes(void *to,
68                       const void *from,
69                       size_t length);
70
71 /**
72  * gras_dd_convert_elm:
73  *
74  * Convert the element described by @type comming from architecture @r_arch.
75  * The data to be converted is stored in @src, and is to be stored in @dst.
76  * Both pointers may be the same location if no resizing is needed.
77  */
78 xbt_error_t
79 gras_dd_convert_elm(gras_datadesc_type_t type, int count,
80                     int r_arch, 
81                     void *src, void *dst) {
82   gras_dd_cat_scalar_t scal = type->category.scalar_data;
83   int cpt;
84   const void *r_data;
85   void *l_data;
86   unsigned long r_size, l_size;
87   /* Hexadecimal displayer
88   union {
89     char c[sizeof(int)];
90     int i;
91   } tester;
92   */
93
94   xbt_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
95   xbt_assert(r_arch != GRAS_THISARCH);
96   
97   r_size = type->size[r_arch];
98   l_size = type->size[GRAS_THISARCH];
99   DEBUG4("r_size=%lu l_size=%lu,    src=%p dst=%p",
100          r_size,l_size,src,dst);
101
102   DEBUG2("remote=%c local=%c", gras_arches[r_arch].endian?'B':'l',
103          gras_arches[GRAS_THISARCH].endian?'B':'l');
104
105   if(r_size != l_size) {
106     for(cpt = 0, r_data = src, l_data = dst; 
107         cpt < count; 
108         cpt++, 
109           r_data = (char *)r_data + r_size,
110           l_data = (char *)l_data + l_size) {
111
112       /*
113       fprintf(stderr,"r_data=");
114       for (cpt=0; cpt<r_size; cpt++) {
115         tester.i=0;
116         tester.c[0]= ((char*)r_data)[cpt];
117         fprintf(stderr,"\\%02x", tester.i);
118       }
119       fprintf(stderr,"\n");
120       */
121
122       /* Resize that damn integer, pal */
123
124       unsigned char *l_sign, *r_sign;
125       int padding;
126       int sizeChange = l_size - r_size;
127       int lowOrderFirst = !gras_arches[r_arch].endian ||
128         gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian; 
129
130       DEBUG5("Resize integer %d from %lu @%p to %lu @%p",
131              cpt, r_size,r_data, l_size,l_data);
132       xbt_assert0(r_data != l_data, "Impossible to resize in place");
133
134       if(sizeChange < 0) {
135         DEBUG3("Truncate %d bytes (%s,%s)", -sizeChange,
136                lowOrderFirst?"lowOrderFirst":"bigOrderFirst",
137                scal.encoding == e_gras_dd_scalar_encoding_sint?"signed":"unsigned");
138         /* Truncate high-order bytes. */
139         memcpy(l_data, 
140                gras_arches[r_arch].endian ? ((char*)r_data-sizeChange)
141                                           :         r_data,
142                l_size);
143
144         if(scal.encoding == e_gras_dd_scalar_encoding_sint) {
145           DEBUG0("This is signed");
146           /* Make sure the high order bit of r_data and l_data are the same */
147           l_sign = gras_arches[GRAS_THISARCH].endian
148                  ? ((unsigned char*)l_data + l_size - 1)
149                  :  (unsigned char*)l_data;
150           r_sign = gras_arches[r_arch].endian
151                  ? ((unsigned char*)r_data + r_size - 1)
152                  :  (unsigned char*)r_data;
153           DEBUG2("This is signed (r_sign=%c l_sign=%c", *r_sign,*l_sign);
154
155           if ((*r_sign > 127) != (*l_sign > 127)) {
156             if(*r_sign > 127)
157               *l_sign += 128;
158             else
159               *l_sign -= 128;
160           }
161         }
162       } else {
163         DEBUG1("Extend %d bytes", sizeChange);
164         if (scal.encoding != e_gras_dd_scalar_encoding_sint) {
165           DEBUG0("This is signed");
166           padding = 0; /* pad unsigned with 0 */
167         } else {
168           /* extend sign */
169           r_sign = gras_arches[r_arch].endian ? ((unsigned char*)r_data + r_size - 1)
170                                               :  (unsigned char*)r_data;
171           padding = (*r_sign > 127) ? 0xff : 0;
172         }
173
174         memset(l_data, padding, l_size);
175         memcpy(!gras_arches[r_arch].endian ? l_data : ((char *)l_data + sizeChange), 
176                r_data, r_size);
177
178         /*
179         fprintf(stderr,"r_data=");
180         for (cpt=0; cpt<r_size; cpt++) {
181           tester.i=0;
182           tester.c[0] = ((char*)r_data)[cpt];
183           fprintf(stderr,"\\%02x", tester.i);
184         }
185         fprintf(stderr,"\n");
186
187         fprintf(stderr,"l_data=");
188         for (cpt=0; cpt<l_size; cpt++) {
189           tester.i=0;
190           tester.c[0]= ((char*)l_data)[cpt];
191           fprintf(stderr,"\\%02x", tester.i);
192         } fprintf(stderr,"\n");
193         */
194       }
195     }
196   }
197
198   /* flip bytes if needed */
199   if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian && 
200      (l_size * count)  > 1) {
201
202     for(cpt = 0, r_data=dst, l_data=dst;
203         cpt < count;
204         cpt++, 
205           r_data = (char *)r_data + l_size, /* resizing already done */
206           l_data = (char *)l_data + l_size) {                
207
208       DEBUG1("Flip elm %d",cpt);
209       gras_dd_reverse_bytes(l_data, r_data, l_size);
210     }
211   }
212
213   return no_error;
214 }
215
216 static void
217 gras_dd_reverse_bytes(void *to,
218                       const void *from,
219                       size_t length) {
220
221   char charBegin;
222   const char *fromBegin;
223   const char *fromEnd;
224   char *toBegin;
225   char *toEnd;
226
227   for(fromBegin = (const char *)from, 
228         fromEnd = fromBegin + length - 1,
229         toBegin = (char *)to,
230         toEnd = toBegin + length - 1;
231
232       fromBegin <= fromEnd; 
233
234       fromBegin++, fromEnd--, 
235         toBegin++, toEnd--) {
236
237     charBegin = *fromBegin;
238     *toBegin = *fromEnd;
239     *toEnd = charBegin;
240   }
241 }
242
243
244 /**
245  * gras_arch_selfid:
246  *
247  * returns the ID of the architecture the process is running on
248  */
249 int
250 gras_arch_selfid(void) {
251   return GRAS_THISARCH;
252 }