This is the mail archive of the gsl-discuss@sources.redhat.com mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Byte ordering


Z F wrote:
> 
> Hello everybody,
> 
> Even though this question is not directly related to GSL, indirectly
> --- it is.
> 
> I have a scientific calculation program (which uses GSL) but most
> importantly it uses doubles. I need to transfer those data to another
> computer. The problem is that if the two computers have different byte
> ordering, I have to do something special about the data. I understand
> that there is a network standard for shorts and for ints to serve this
> purpose. I could not find any standards to transfer doubles/floats over
> net. I the past I could live with printf()-type things and convert all
> doubles to strings and pass strings since ASCII is more universal.
> This, however, increases the data size by a factor of three.
> 
> The current problem I am working on has data output rate of about
> 5-15MBytes/sec and increasing it by factor of three is not feasible.
> 
> Could someone, please, point me in the right direction? Should I
> give-up  on portability of my code and assume/hope that both ends use
> the same
> byte ordering?


I have in the past settled on a byte ordering (I use Big Endian) and 
write data in that format. It is quite easy to convert from one to
the other. The following is I'm sure not optimal, but will take an
8-byte double and convert it from one format to the other. 

/* This routine swaps data from the little-endian format to the big-endian 
format or visa-versa. */

void byteswap_doubles(double *a)
{
        unsigned char b[8],c[8];
        (void) memcpy(b,a,8); 
        c[0]=b[7]; /* swap data around */
        c[1]=b[6];
        c[2]=b[5];
        c[3]=b[4];
        c[4]=b[3];
        c[5]=b[2];
        c[6]=b[1];
        c[7]=b[0];
        (void) memcpy(a,c,8);
}



If you look at a program of mine called 'atlc' 

http://atlc.sourceforge.net/

you will see at one point I write bitmap files, which needs
2-byte integers in the little-endian format, as used on PC's.
The bitmap (bmp) standard calls for that. Yet with some effort
I have got that program to run on an old Cray Y-MP supercomputer
where

sizeof(char)=1
sizeof(short)=8
sizeof(int)=8
sizeof(long)=8

I think doubles are 8 bytes too, but are not sure about floats. 
Writing data in two byte integers was a real pain, but it was 
possible. The method writes the same format of data (little
endian) on a big or little endian machine. The code does not 
need to determine the byte ordering of the data. 

PS, I have used a 24-bit machine before - that was fun!!



-- 
"The day Microsoft makes something that doesn't suck is probably 
the day they start making vacuum cleaners." -Ernst Jan Plugge.

Dr. David Kirkby,
Senior Research Fellow,
Department of Medical Physics,
University College London,
11-20 Capper St, London, WC1E 6JA.
Website: http://www.medphys.ucl.ac.uk/~davek
Author of 'atlc' http://atlc.sourceforge.net/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]