Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move datadesc and TCP sockets from GRAS to XBT.
[simgrid.git] / src / xbt / 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 "datadesc_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ddt_convert, xbt_ddt,
16                                 "Inter-architecture convertions");
17
18 /***
19  *** Table of all known architectures:
20  ***
21 l_C:1/1:_I:2/1:4/1:4/1:8/1:_P:4/1:4/1:_D:4/1:8/1: #xbt_arch=0;  xbt_size=32; xbt_arch_name=little32_1;
22 l_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2: #xbt_arch=1;  xbt_size=32; xbt_arch_name=little32_2;
23 l_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4: #xbt_arch=2;  xbt_size=32; xbt_arch_name=little32_4;
24 l_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8: #xbt_arch=3;  xbt_size=32; xbt_arch_name=little32_8;
25 l_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8: #xbt_arch=4;  xbt_size=64; xbt_arch_name=little64;
26 l_C:1/1:_I:2/2:4/4:4/4:8/8:_P:8/8:8/8:_D:4/4:8/8: #xbt_arch=5;  xbt_size=64; xbt_arch_name=little64_2;
27
28 B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/8: #xbt_arch=6;  xbt_size=32; xbt_arch_name=big32_8;
29 B_C:1/1:_I:2/2:4/4:4/4:8/8:_P:4/4:4/4:_D:4/4:8/4: #xbt_arch=7;  xbt_size=32; xbt_arch_name=big32_8_4;
30 B_C:1/1:_I:2/2:4/4:4/4:8/4:_P:4/4:4/4:_D:4/4:8/4: #xbt_arch=8;  xbt_size=32; xbt_arch_name=big32_4;
31 B_C:1/1:_I:2/2:4/2:4/2:8/2:_P:4/2:4/2:_D:4/2:8/2: #xbt_arch=9;  xbt_size=32; xbt_arch_name=big32_2;
32 B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/8: #xbt_arch=10; xbt_size=64; xbt_arch_name=big64;
33 B_C:1/1:_I:2/2:4/4:8/8:8/8:_P:8/8:8/8:_D:4/4:8/4: #xbt_arch=11; xbt_size=64; xbt_arch_name=big64_8_4;
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 xbt_arch_desc_t xbt_arches[xbt_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   {"little64_2", 0, {1, 2, 4, 4, 8, 8, 8, 4, 8},  /* win64 */
60    {1, 2, 4, 4, 8, 8, 8, 4, 8}},
61
62   {"big32_8", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8},
63    {1, 2, 4, 4, 8, 4, 4, 4, 8}},
64
65   {"big32_8_4", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8}, /* AIX */
66    {1, 2, 4, 4, 8, 4, 4, 4, 4}},
67
68   {"big32_4", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8},   /* G5 */
69    {1, 2, 4, 4, 4, 4, 4, 4, 4}},
70
71   {"big32_2", 1, {1, 2, 4, 4, 8, 4, 4, 4, 8},   /* ARM */
72    {1, 2, 2, 2, 2, 2, 2, 2, 2}},
73
74   {"big64", 1, {1, 2, 4, 8, 8, 8, 8, 4, 8},     /* sparc */
75    {1, 2, 4, 8, 8, 8, 8, 4, 8}},
76
77   {"big64_8_4", 1, {1, 2, 4, 8, 8, 8, 8, 4, 8}, /* aix with -maix64 */
78    {1, 2, 4, 8, 8, 8, 8, 4, 4}}
79 };
80
81 const char *xbt_datadesc_arch_name(int code)
82 {
83   if (code < 0 || code >= xbt_arch_count)
84     return "[unknown arch]";
85   return xbt_arches[code].name;
86 }
87
88
89 /**
90  * Local function doing the grunt work
91  */
92 static void xbt_dd_reverse_bytes(void *to, const void *from,
93                                   size_t length);
94
95 /**
96  * xbt_dd_convert_elm:
97  *
98  * Convert the element described by @type comming from architecture @r_arch.
99  * The data to be converted is stored in @src, and is to be stored in @dst.
100  * Both pointers may be the same location if no resizing is needed.
101  */
102 void
103 xbt_dd_convert_elm(xbt_datadesc_type_t type, int count,
104                     int r_arch, void *src, void *dst)
105 {
106   xbt_dd_cat_scalar_t scal = type->category.scalar_data;
107   int cpt;
108   const void *r_data;
109   void *l_data;
110   unsigned long r_size, l_size;
111   /* Hexadecimal displayer
112      union {
113      char c[sizeof(int)];
114      int i;
115      } tester;
116    */
117
118   xbt_assert(type->category_code == e_xbt_datadesc_type_cat_scalar);
119   xbt_assert(r_arch != GRAS_THISARCH);
120
121   r_size = type->size[r_arch];
122   l_size = type->size[GRAS_THISARCH];
123   XBT_DEBUG("r_size=%lu l_size=%lu,    src=%p dst=%p", r_size, l_size, src,
124          dst);
125
126   XBT_DEBUG("remote=%c local=%c", xbt_arches[r_arch].endian ? 'B' : 'l',
127          xbt_arches[GRAS_THISARCH].endian ? 'B' : 'l');
128
129   if (r_size != l_size) {
130     for (cpt = 0, r_data = src, l_data = dst;
131          cpt < count;
132          cpt++,
133          r_data = (char *) r_data + r_size,
134          l_data = (char *) l_data + l_size) {
135
136       /*
137          fprintf(stderr,"r_data=");
138          for (cpt=0; cpt<r_size; cpt++) {
139          tester.i=0;
140          tester.c[0]= ((char*)r_data)[cpt];
141          fprintf(stderr,"\\%02x", tester.i);
142          }
143          fprintf(stderr,"\n");
144        */
145
146       /* Resize that damn integer, pal */
147
148       unsigned char *l_sign, *r_sign;
149       int padding;
150       int sizeChange = l_size - r_size;
151       int lowOrderFirst = !xbt_arches[r_arch].endian ||
152           xbt_arches[r_arch].endian == xbt_arches[GRAS_THISARCH].endian;
153
154       XBT_DEBUG("Resize integer %d from %lu @%p to %lu @%p",
155              cpt, r_size, r_data, l_size, l_data);
156       xbt_assert(r_data != l_data, "Impossible to resize in place");
157
158       if (sizeChange < 0) {
159         XBT_DEBUG("Truncate %d bytes (%s,%s)", -sizeChange,
160                lowOrderFirst ? "lowOrderFirst" : "bigOrderFirst",
161                scal.encoding ==
162                e_xbt_dd_scalar_encoding_sint ? "signed" : "unsigned");
163         /* Truncate high-order bytes. */
164         memcpy(l_data,
165                xbt_arches[r_arch].endian ? ((char *) r_data - sizeChange)
166                : r_data, l_size);
167
168         if (scal.encoding == e_xbt_dd_scalar_encoding_sint) {
169           XBT_DEBUG("This is signed");
170           /* Make sure the high order bit of r_data and l_data are the same */
171           l_sign = xbt_arches[GRAS_THISARCH].endian
172               ? ((unsigned char *) l_data + l_size - 1)
173               : (unsigned char *) l_data;
174           r_sign = xbt_arches[r_arch].endian
175               ? ((unsigned char *) r_data + r_size - 1)
176               : (unsigned char *) r_data;
177           XBT_DEBUG("This is signed (r_sign=%c l_sign=%c", *r_sign, *l_sign);
178
179           if ((*r_sign > 127) != (*l_sign > 127)) {
180             if (*r_sign > 127)
181               *l_sign += 128;
182             else
183               *l_sign -= 128;
184           }
185         }
186       } else {
187         XBT_DEBUG("Extend %d bytes", sizeChange);
188         if (scal.encoding != e_xbt_dd_scalar_encoding_sint) {
189           XBT_DEBUG("This is signed");
190           padding = 0;          /* pad unsigned with 0 */
191         } else {
192           /* extend sign */
193           r_sign =
194               xbt_arches[r_arch].endian ? ((unsigned char *) r_data +
195                                             r_size - 1)
196               : (unsigned char *) r_data;
197           padding = (*r_sign > 127) ? 0xff : 0;
198         }
199
200         if (l_size != 0) {
201           memset(l_data, padding, l_size);
202         }
203         memcpy(!xbt_arches[r_arch].endian ? l_data
204                : ((char *) l_data + sizeChange), r_data, r_size);
205
206         /*
207            fprintf(stderr,"r_data=");
208            for (cpt=0; cpt<r_size; cpt++) {
209            tester.i=0;
210            tester.c[0] = ((char*)r_data)[cpt];
211            fprintf(stderr,"\\%02x", tester.i);
212            }
213            fprintf(stderr,"\n");
214
215            fprintf(stderr,"l_data=");
216            for (cpt=0; cpt<l_size; cpt++) {
217            tester.i=0;
218            tester.c[0]= ((char*)l_data)[cpt];
219            fprintf(stderr,"\\%02x", tester.i);
220            } fprintf(stderr,"\n");
221          */
222       }
223     }
224   }
225
226   /* flip bytes if needed */
227   if (xbt_arches[r_arch].endian != xbt_arches[GRAS_THISARCH].endian &&
228       (l_size * count) > 1) {
229
230     for (cpt = 0, r_data = dst, l_data = dst; cpt < count; cpt++, r_data = (char *) r_data + l_size,    /* resizing already done */
231          l_data = (char *) l_data + l_size) {
232
233       XBT_DEBUG("Flip elm %d", cpt);
234       xbt_dd_reverse_bytes(l_data, r_data, l_size);
235     }
236   }
237
238 }
239
240 static void xbt_dd_reverse_bytes(void *to, const void *from,
241                                   size_t length)
242 {
243
244   char charBegin;
245   const char *fromBegin;
246   const char *fromEnd;
247   char *toBegin;
248   char *toEnd;
249
250   for (fromBegin = (const char *) from,
251        fromEnd = fromBegin + length - 1,
252        toBegin = (char *) to,
253        toEnd = toBegin + length - 1;
254        fromBegin <= fromEnd; fromBegin++, fromEnd--, toBegin++, toEnd--) {
255
256     charBegin = *fromBegin;
257     *toBegin = *fromEnd;
258     *toEnd = charBegin;
259   }
260 }
261
262
263 /**
264  * xbt_arch_selfid:
265  *
266  * returns the ID of the architecture the process is running on
267  */
268 int xbt_arch_selfid(void)
269 {
270   return GRAS_THISARCH;
271 }