Logo AND Algorithmique Numérique Distribuée

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