Logo AND Algorithmique Numérique Distribuée

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