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 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);
98 gras_arches[r_arch].endian
99 != gras_arches[GRAS_THISARCH].endian);*/
102 src=dst; /* Make sure to reverse bytes on the right data */
105 if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian &&
106 (l_size * count) > 1) {
108 for(cpt = 0, r_data=src, l_data=dst;
111 r_data = (char *)r_data + l_size, /* resizing already done */
112 l_data = (char *)l_data + l_size) {
114 DEBUG1("Flip elm %d",cpt);
115 gras_dd_reverse_bytes(l_data, r_data, l_size);
123 gras_dd_reverse_bytes(void *to,
128 const char *fromBegin;
133 for(fromBegin = (const char *)from,
134 fromEnd = fromBegin + length - 1,
135 toBegin = (char *)to,
136 toEnd = toBegin + length - 1;
138 fromBegin <= fromEnd;
140 fromBegin++, fromEnd--,
141 toBegin++, toEnd--) {
143 charBegin = *fromBegin;
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.
155 * It should be thread safe (operates on local variables and calls mem*)
158 gras_dd_resize_int(const void *source,
161 size_t destinationSize,
165 unsigned char *destinationSign;
167 int sizeChange = destinationSize - sourceSize;
168 unsigned char *sourceSign;
170 if(sizeChange == 0) {
171 memcpy(destination, source, destinationSize);
172 } else if(sizeChange < 0) {
173 /* Truncate high-order bytes. */
175 lowOrderFirst?source:((char*)source-sizeChange),
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;
186 *destinationSign -= 128;
190 /* Pad with zeros or extend sign, as appropriate. */
194 sourceSign = lowOrderFirst ? ((unsigned char*)source + sourceSize - 1)
195 : (unsigned char*)source;
196 padding = (*sourceSign > 127) ? 0xff : 0;
198 memset(destination, padding, destinationSize);
199 memcpy(lowOrderFirst ? destination
200 : ((char *)destination + sizeChange),
209 * returns the ID of the architecture the process is running on
212 gras_arch_selfid(void) {
213 return GRAS_THISARCH;