Logo AND Algorithmique Numérique Distribuée

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