What's new

Byteswap

NecroRomancist

New member
Does anyone here know any fast swap routines for byteswapping doubles.I had a hand coded slow one that work but then i decided to work with SDL_Swap64 and it crashed things for me.Its this supposed to work it just with 64 bit's integers.
Thanks :bouncy:
 
OP
NecroRomancist

NecroRomancist

New member
union

One other thing

union fp
{
float regs[16];
double dr[8];
};

this is bound to explode right? Considering that a ieee 754 float has some special bits.Any ideas how i can solve this?
 

smcd

Active member
This page has some examples that should help
http://www.codeproject.com/cpp/endianness.asp

Finally, we can write a more general function that can deal with any atomic data type (e.g. int, float, double, etc) with automatic size detection:

Code:
#include <algorithm> //required for std::swap

#define ByteSwap5(x) ByteSwap((unsigned char *) &x,sizeof(x))

void ByteSwap(unsigned char * b, int n)
{
   register int i = 0;
   register int j = n-1;
   while (i<j)
   {
      std::swap(b[i], b[j]);
      i++, j--;
   }
}
For example, the next code snippet shows how to convert a data array of doubles from one format (e.g. Big-Endian) to the other (e.g. Little-Endian):
Code:
double* dArray; //array in big-endian format
int n; //Number of elements

for (register int i = 0; i <n; i++) 
   ByteSwap5(dArray[i]);
 

Top