Logo AND Algorithmique Numérique Distribuée

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