Logo AND Algorithmique Numérique Distribuée

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