Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e7a5ca39964b98eca2a89203558a84fddd4cebf8
[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       DEBUG3("Resize elm %d from %p to %p",cpt,r_data, 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[GRAS_THISARCH].endian);
97       /*      && 
98                          gras_arches[r_arch].endian 
99                          != gras_arches[GRAS_THISARCH].endian);*/
100
101     }
102     src=dst; /* Make sure to reverse bytes on the right data */
103   }
104
105   if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian && 
106      (l_size * count)  > 1) {
107
108     for(cpt = 0, r_data=src, l_data=dst;
109         cpt < count;
110         cpt++, 
111           r_data = (char *)r_data + l_size, /* resizing already done */
112           l_data = (char *)l_data + l_size) {                
113
114       DEBUG1("Flip elm %d",cpt);
115       gras_dd_reverse_bytes(l_data, r_data, l_size);
116     }
117   }
118
119   return no_error;
120 }
121
122 static void
123 gras_dd_reverse_bytes(void *to,
124                       const void *from,
125                       size_t length) {
126
127   char charBegin;
128   const char *fromBegin;
129   const char *fromEnd;
130   char *toBegin;
131   char *toEnd;
132
133   for(fromBegin = (const char *)from, 
134         fromEnd = fromBegin + length - 1,
135         toBegin = (char *)to,
136         toEnd = toBegin + length - 1;
137
138       fromBegin <= fromEnd; 
139
140       fromBegin++, fromEnd--, 
141         toBegin++, toEnd--) {
142
143     charBegin = *fromBegin;
144     *toBegin = *fromEnd;
145     *toEnd = charBegin;
146   }
147 }
148
149 /*
150  * Copies the integer value of size #sourceSize# stored in #source# to the
151  * #destinationSize#-long area #destination#.  #signedType# indicates whether
152  * or not the source integer is signed; #lowOrderFirst# whether or not the
153  * bytes run least-significant to most-significant.
154  *
155  * It should be thread safe (operates on local variables and calls mem*)
156  */
157 static void
158 gras_dd_resize_int(const void *source,
159                    size_t sourceSize,
160                    void *destination,
161                    size_t destinationSize,
162                    int signedType,
163                    int lowOrderFirst) {
164
165   unsigned char *destinationSign;
166   int padding;
167   int sizeChange = destinationSize - sourceSize;
168   unsigned char *sourceSign;
169   
170   if(sizeChange == 0) {
171     memcpy(destination, source, destinationSize);
172   } else if(sizeChange < 0) {
173     /* Truncate high-order bytes. */
174     memcpy(destination, 
175            lowOrderFirst?source:((char*)source-sizeChange),
176            destinationSize);
177     if(signedType) {
178       /* Make sure the high order bit of source and
179        * destination are the same */
180       destinationSign = lowOrderFirst ? ((unsigned char*)destination + destinationSize - 1) : (unsigned char*)destination;
181       sourceSign = lowOrderFirst ? ((unsigned char*)source + sourceSize - 1) : (unsigned char*)source;
182       if((*sourceSign > 127) != (*destinationSign > 127)) {
183         if(*sourceSign > 127)
184           *destinationSign += 128;
185         else
186           *destinationSign -= 128;
187       }
188     }
189   } else {
190     /* Pad with zeros or extend sign, as appropriate. */
191     if(!signedType)
192       padding = 0;
193     else {
194       sourceSign = lowOrderFirst ? ((unsigned char*)source + sourceSize - 1)
195                                  : (unsigned char*)source;
196       padding = (*sourceSign > 127) ? 0xff : 0;
197     }
198     memset(destination, padding, destinationSize);
199     memcpy(lowOrderFirst ? destination 
200                          : ((char *)destination + sizeChange),
201            source, sourceSize);
202   }
203 }
204
205
206 /**
207  * gras_arch_selfid:
208  *
209  * returns the ID of the architecture the process is running on
210  */
211 int
212 gras_arch_selfid(void) {
213   return GRAS_THISARCH;
214 }