3 /* ddt_remote - Stuff needed to get datadescs about remote hosts */
5 /* Authors: Olivier Aumage, Martin Quinson */
6 /* Copyright (C) 2003, 2004 the GRAS posse. */
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. */
11 /************************************************************************/
12 /* C combines the power of assembler with the portability of assembler. */
13 /************************************************************************/
15 #include "DataDesc/datadesc_private.h"
17 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(convert,DataDesc);
20 *** Table of all known architectures.
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}}
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;
37 * Local function doing the grunt work
40 gras_dd_resize_int(const void *source,
43 size_t destinationSize,
47 gras_dd_reverse_bytes(void *to,
53 * gras_dd_convert_elm:
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.
60 gras_dd_convert_elm(gras_datadesc_type_t *type, int count,
62 void *src, void *dst) {
63 gras_dd_cat_scalar_t scal = type->category.scalar_data;
67 size_t r_size, l_size;
69 gras_assert(type->category_code == e_gras_datadesc_type_cat_scalar);
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);
77 if (r_arch == GRAS_THISARCH) {
78 // || scal.encoding == e_gras_dd_scalar_encoding_float) {
79 DEBUG0("No conversion needed");
83 r_size = type->size[r_arch];
84 l_size = type->size[GRAS_THISARCH];
86 if(r_size != l_size) {
87 for(cpt = 0, r_data = src, l_data = dst;
90 r_data = (char *)r_data + r_size,
91 l_data = (char *)l_data + l_size) {
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);*/
101 src=dst; /* Make sure to reverse bytes on the right data */
104 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
105 (l_size * count) > 1) {
107 for(cpt = 0, r_data=src, l_data=dst;
110 r_data = (char *)r_data + l_size, /* resizing already done */
111 l_data = (char *)l_data + l_size) {
113 DEBUG1("Flip elm %d",cpt);
114 gras_dd_reverse_bytes(l_data, r_data, l_size);
122 gras_dd_reverse_bytes(void *to,
127 const char *fromBegin;
132 for(fromBegin = (const char *)from,
133 fromEnd = fromBegin + length - 1,
134 toBegin = (char *)to,
135 toEnd = toBegin + length - 1;
137 fromBegin <= fromEnd;
139 fromBegin++, fromEnd--,
140 toBegin++, toEnd--) {
142 charBegin = *fromBegin;
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.
154 * It should be thread safe (operates on local variables and calls mem*)
157 gras_dd_resize_int(const void *r_data,
164 unsigned char *destinationSign;
166 int sizeChange = l_size - r_size;
167 unsigned char *r_dataSign;
169 gras_assert0(sizeChange, "Nothing to resize");
172 DEBUG1("Truncate %d bytes", -sizeChange);
173 /* Truncate high-order bytes. */
175 lowOrderFirst?r_data:((char*)r_data-sizeChange),
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;
187 *destinationSign -= 128;
191 DEBUG1("Extend %d bytes", sizeChange);
192 /* Pad with zeros or extend sign, as appropriate. */
196 r_dataSign = lowOrderFirst ? ((unsigned char*)r_data + r_size - 1)
197 : (unsigned char*)r_data;
198 padding = (*r_dataSign > 127) ? 0xff : 0;
200 memset(destination, padding, l_size);
201 memcpy(lowOrderFirst ? destination
202 : ((char *)destination + sizeChange),
211 * returns the ID of the architecture the process is running on
214 gras_arch_selfid(void) {
215 return GRAS_THISARCH;