Logo AND Algorithmique Numérique Distribuée

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