Logo AND Algorithmique Numérique Distribuée

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