Logo AND Algorithmique Numérique Distribuée

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