Logo AND Algorithmique Numérique Distribuée

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