Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2ab8d4859f3a7ca1da23efccce4442334d22e80d
[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
23 const gras_arch_desc_t gras_arches[gras_arch_count] = {
24   {"little32", 0,   {1,2,4,4,8,   4,4,   4,8}},
25   {"little64", 0,   {1,2,4,8,8,   8,8,   4.8}},
26   {"big32",    1,   {1,2,4,4,8,   4,4,   4,8}},
27   {"big64",    1,   {1,2,4,8,8,   8,8,   4,8}}
28 };
29
30 const char *gras_datadesc_arch_name(int code) {
31    if (code < 0 || code >= gras_arch_count)
32      return "[unknown arch]";
33    return gras_arches[code].name;
34 }
35
36 /**
37  * Local function doing the grunt work
38  */
39 static void
40 gras_dd_resize_int(const void *source,
41                    size_t sourceSize,
42                    void *destination,
43                    size_t destinationSize,
44                    int signedType,
45                    int lowOrderFirst);
46 static void
47 gras_dd_reverse_bytes(void *to,
48                       const void *from,
49                       size_t length);
50
51
52 /**
53  * gras_dd_convert_elm:
54  *
55  * Convert the element described by @type comming from architecture @r_arch.
56  * The data to be converted is stored in @src, and is to be stored in @dst.
57  * Both pointers may be the same location if no resizing is needed.
58  */
59 gras_error_t
60 gras_dd_convert_elm(gras_datadesc_type_t *type, int count,
61                     int r_arch, 
62                     void *src, void *dst) {
63   gras_dd_cat_scalar_t scal = type->category.scalar_data;
64   int cpt;
65   const void *r_data;
66   void *l_data;
67   size_t r_size, l_size;
68
69   gras_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
70
71
72   r_size = type->size[r_arch];
73   l_size = type->size[GRAS_THISARCH];
74   DEBUG4("r_size=%d l_size=%d,    src=%p dst=%p",
75          r_size,l_size,src,dst);
76
77   if (r_arch == GRAS_THISARCH) { 
78 //      || scal.encoding == e_gras_dd_scalar_encoding_float) {
79     DEBUG0("No conversion needed");
80     return no_error;
81   }
82
83   r_size = type->size[r_arch];
84   l_size = type->size[GRAS_THISARCH];
85
86   if(r_size != l_size) {
87     for(cpt = 0, r_data = src, l_data = dst; 
88         cpt < count; 
89         cpt++, 
90           r_data = (char *)r_data + r_size,
91           l_data = (char *)l_data + l_size) {
92
93       DEBUG5("Resize elm %d from %d @%p to %d @%p",cpt, r_size,r_data, l_size,l_data);
94       gras_dd_resize_int(r_data, r_size, l_data, l_size,
95                          scal.encoding == e_gras_dd_scalar_encoding_sint,
96                          gras_arches[r_arch].endian && 
97                          gras_arches[r_arch].endian 
98                          != gras_arches[GRAS_THISARCH].endian);*/
99
100     }
101     src=dst; /* Make sure to reverse bytes on the right data */
102   }
103
104   if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian && 
105      (l_size * count)  > 1) {
106
107     for(cpt = 0, r_data=src, l_data=dst;
108         cpt < count;
109         cpt++, 
110           r_data = (char *)r_data + l_size, /* resizing already done */
111           l_data = (char *)l_data + l_size) {                
112
113       DEBUG1("Flip elm %d",cpt);
114       gras_dd_reverse_bytes(l_data, r_data, l_size);
115     }
116   }
117
118   return no_error;
119 }
120
121 static void
122 gras_dd_reverse_bytes(void *to,
123                       const void *from,
124                       size_t length) {
125
126   char charBegin;
127   const char *fromBegin;
128   const char *fromEnd;
129   char *toBegin;
130   char *toEnd;
131
132   for(fromBegin = (const char *)from, 
133         fromEnd = fromBegin + length - 1,
134         toBegin = (char *)to,
135         toEnd = toBegin + length - 1;
136
137       fromBegin <= fromEnd; 
138
139       fromBegin++, fromEnd--, 
140         toBegin++, toEnd--) {
141
142     charBegin = *fromBegin;
143     *toBegin = *fromEnd;
144     *toEnd = charBegin;
145   }
146 }
147
148 /*
149  * Copies the integer value of size #sourceSize# stored in #source# to the
150  * #destinationSize#-long area #destination#.  #signedType# indicates whether
151  * or not the source integer is signed; #lowOrderFirst# whether or not the
152  * bytes run least-significant to most-significant.
153  *
154  * It should be thread safe (operates on local variables and calls mem*)
155  */
156 static void
157 gras_dd_resize_int(const void *r_data,
158                    size_t r_size,
159                    void *destination,
160                    size_t l_size,
161                    int signedType,
162                    int lowOrderFirst) {
163
164   unsigned char *destinationSign;
165   int padding;
166   int sizeChange = l_size - r_size;
167   unsigned char *r_dataSign;
168   
169   gras_assert0(sizeChange, "Nothing to resize");
170
171   if(sizeChange < 0) {
172     DEBUG1("Truncate %d bytes", -sizeChange);
173     /* Truncate high-order bytes. */
174     memcpy(destination, 
175            lowOrderFirst?r_data:((char*)r_data-sizeChange),
176            l_size);
177     if(signedType) {
178       DEBUG0("This is signed");
179       /* Make sure the high order bit of r_data and
180        * destination are the same */
181       destinationSign = lowOrderFirst ? ((unsigned char*)destination + l_size - 1) : (unsigned char*)destination;
182       r_dataSign = lowOrderFirst ? ((unsigned char*)r_data + r_size - 1) : (unsigned char*)r_data;
183       if((*r_dataSign > 127) != (*destinationSign > 127)) {
184         if(*r_dataSign > 127)
185           *destinationSign += 128;
186         else
187           *destinationSign -= 128;
188       }
189     }
190   } else {
191     DEBUG1("Extend %d bytes", sizeChange);
192     /* Pad with zeros or extend sign, as appropriate. */
193     if(!signedType)
194       padding = 0;
195     else {
196       r_dataSign = lowOrderFirst ? ((unsigned char*)r_data + r_size - 1)
197                                  : (unsigned char*)r_data;
198       padding = (*r_dataSign > 127) ? 0xff : 0;
199     }
200     memset(destination, padding, l_size);
201     memcpy(lowOrderFirst ? destination 
202                          : ((char *)destination + sizeChange),
203            r_data, r_size);
204   }
205 }
206
207
208 /**
209  * gras_arch_selfid:
210  *
211  * returns the ID of the architecture the process is running on
212  */
213 int
214 gras_arch_selfid(void) {
215   return GRAS_THISARCH;
216 }