From ad6959da916f5715693223fd972baf5a22d9bfa9 Mon Sep 17 00:00:00 2001 From: mquinson Date: Thu, 8 Jul 2004 06:11:00 +0000 Subject: [PATCH] Rename architectures, implement conversion;) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@226 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/gras/DataDesc/ddt_convert.c | 165 +++++++++++++++++++++++++++++--- 1 file changed, 154 insertions(+), 11 deletions(-) diff --git a/src/gras/DataDesc/ddt_convert.c b/src/gras/DataDesc/ddt_convert.c index 10c0d869d4..e7a5ca3996 100644 --- a/src/gras/DataDesc/ddt_convert.c +++ b/src/gras/DataDesc/ddt_convert.c @@ -21,10 +21,10 @@ GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(convert,DataDesc); ***/ const gras_arch_desc_t gras_arches[gras_arch_count] = { - {"i386", 0, {1,2,4,4,8, 4,4, 4,8}}, - {"alpha", 0, {1,2,4,8,8, 8,8, 4.8}}, - {"powerpc",1, {1,2,4,4,8, 4,4, 4,8}}, - {"sparc", 1, {1,2,4,8,8, 8,8, 4,8}} + {"little32", 0, {1,2,4,4,8, 4,4, 4,8}}, + {"little64", 0, {1,2,4,8,8, 8,8, 4.8}}, + {"big32", 1, {1,2,4,4,8, 4,4, 4,8}}, + {"big64", 1, {1,2,4,8,8, 8,8, 4,8}} }; const char *gras_datadesc_arch_name(int code) { @@ -33,6 +33,22 @@ const char *gras_datadesc_arch_name(int code) { return gras_arches[code].name; } +/** + * Local function doing the grunt work + */ +static void +gras_dd_resize_int(const void *source, + size_t sourceSize, + void *destination, + size_t destinationSize, + int signedType, + int lowOrderFirst); +static void +gras_dd_reverse_bytes(void *to, + const void *from, + size_t length); + + /** * gras_dd_convert_elm: * @@ -44,22 +60,149 @@ gras_error_t gras_dd_convert_elm(gras_datadesc_type_t *type, int count, int r_arch, void *src, void *dst) { - // gras_dd_cat_scalar_t scal_data = type->category.scal_data; + gras_dd_cat_scalar_t scal = type->category.scalar_data; + int cpt; + const void *r_data; + void *l_data; + size_t r_size, l_size; gras_assert(type->category_code == e_gras_datadesc_type_cat_scalar); - if (r_arch == GRAS_THISARCH) - return no_error; - if (gras_arches[r_arch].endian == gras_arches[GRAS_THISARCH].endian && - type->aligned_size[r_arch] == type->aligned_size[GRAS_THISARCH]) + r_size = type->size[r_arch]; + l_size = type->size[GRAS_THISARCH]; + DEBUG4("r_size=%d l_size=%d, src=%p dst=%p", + r_size,l_size,src,dst); + + if (r_arch == GRAS_THISARCH) { +// || scal.encoding == e_gras_dd_scalar_encoding_float) { + DEBUG0("No conversion needed"); return no_error; + } + + r_size = type->size[r_arch]; + l_size = type->size[GRAS_THISARCH]; + + if(r_size != l_size) { + for(cpt = 0, r_data = src, l_data = dst; + cpt < count; + cpt++, + r_data = (char *)r_data + r_size, + l_data = (char *)l_data + l_size) { + + DEBUG3("Resize elm %d from %p to %p",cpt,r_data, l_data); + gras_dd_resize_int(r_data, r_size, l_data, l_size, + scal.encoding == e_gras_dd_scalar_encoding_sint, + gras_arches[GRAS_THISARCH].endian); + /* && + gras_arches[r_arch].endian + != gras_arches[GRAS_THISARCH].endian);*/ + + } + src=dst; /* Make sure to reverse bytes on the right data */ + } + + if(gras_arches[r_arch].endian != gras_arches[GRAS_THISARCH].endian && + (l_size * count) > 1) { + + for(cpt = 0, r_data=src, l_data=dst; + cpt < count; + cpt++, + r_data = (char *)r_data + l_size, /* resizing already done */ + l_data = (char *)l_data + l_size) { + + DEBUG1("Flip elm %d",cpt); + gras_dd_reverse_bytes(l_data, r_data, l_size); + } + } + + return no_error; +} - +static void +gras_dd_reverse_bytes(void *to, + const void *from, + size_t length) { + + char charBegin; + const char *fromBegin; + const char *fromEnd; + char *toBegin; + char *toEnd; - RAISE_UNIMPLEMENTED; + for(fromBegin = (const char *)from, + fromEnd = fromBegin + length - 1, + toBegin = (char *)to, + toEnd = toBegin + length - 1; + + fromBegin <= fromEnd; + + fromBegin++, fromEnd--, + toBegin++, toEnd--) { + + charBegin = *fromBegin; + *toBegin = *fromEnd; + *toEnd = charBegin; + } } +/* + * Copies the integer value of size #sourceSize# stored in #source# to the + * #destinationSize#-long area #destination#. #signedType# indicates whether + * or not the source integer is signed; #lowOrderFirst# whether or not the + * bytes run least-significant to most-significant. + * + * It should be thread safe (operates on local variables and calls mem*) + */ +static void +gras_dd_resize_int(const void *source, + size_t sourceSize, + void *destination, + size_t destinationSize, + int signedType, + int lowOrderFirst) { + + unsigned char *destinationSign; + int padding; + int sizeChange = destinationSize - sourceSize; + unsigned char *sourceSign; + + if(sizeChange == 0) { + memcpy(destination, source, destinationSize); + } else if(sizeChange < 0) { + /* Truncate high-order bytes. */ + memcpy(destination, + lowOrderFirst?source:((char*)source-sizeChange), + destinationSize); + if(signedType) { + /* Make sure the high order bit of source and + * destination are the same */ + destinationSign = lowOrderFirst ? ((unsigned char*)destination + destinationSize - 1) : (unsigned char*)destination; + sourceSign = lowOrderFirst ? ((unsigned char*)source + sourceSize - 1) : (unsigned char*)source; + if((*sourceSign > 127) != (*destinationSign > 127)) { + if(*sourceSign > 127) + *destinationSign += 128; + else + *destinationSign -= 128; + } + } + } else { + /* Pad with zeros or extend sign, as appropriate. */ + if(!signedType) + padding = 0; + else { + sourceSign = lowOrderFirst ? ((unsigned char*)source + sourceSize - 1) + : (unsigned char*)source; + padding = (*sourceSign > 127) ? 0xff : 0; + } + memset(destination, padding, destinationSize); + memcpy(lowOrderFirst ? destination + : ((char *)destination + sizeChange), + source, sourceSize); + } +} + + /** * gras_arch_selfid: * -- 2.20.1