Server Help

Trash Talk - Endian Swappage

Cyan~Fire - Fri Jun 25, 2004 1:08 pm
Post subject: Endian Swappage
I'm writing some code for an app that reads mac datafiles (Macs are big-endian), and here's the endian swap func I just wrote for it. Of course, I want this to be lightning fast since it's used so much, so I was wondering if any of you could see any way to improve this. Thanks.

Code: Show/Hide
void SwapEndian(void *pData, int iBytes)
{
   if (iBytes % 2)   //even check
      return;

   char *pSwapped = (char*)malloc(iBytes);

   char *source = (char*)pData + iBytes;
   char *dest = pSwapped;

   while (iBytes--)
      *dest++ = *source--;

   memcpy(pData, pSwapped, iBytes);
   free(pSwapped);
}

Mr Ekted - Fri Jun 25, 2004 5:16 pm
Post subject:
OMG! That is the single most inefficient way to do it. You really couldn't make it any worse without Qndre's help...

Code: Show/Hide
void SwapEndian (void *pData, int iBytes)
{
char *p = (char *)pData;
char *q = p + iBytes - 1;

// don't even check for odd

while (iBytes--)
   *p++ = *q--;
}


If you want to be even more efficient, write SWAP2(), SWAP4(), SWAP8() macros that do it all inline...

Code: Show/Hide
#define SWAP2(x) ((x >> 8) | ((x & 0xff) << 8))

etc

Cyan~Fire - Fri Jun 25, 2004 5:36 pm
Post subject:
Umm wait is it just me or will that start overwriting data in pData as it copies? There has to be a temporary buffer or you get data loss.

And that's not the most inefficient way to do it, take a look at this code someone else wrote:
Code: Show/Hide
inline void SwapEndian64(char *pData)
{
   char cTemp;

   cTemp = pData[0];
   pData[0] = pData[7];
   pData[7] = cTemp;

   cTemp = pData[1];
   pData[1] = pData[6];
   pData[6] = cTemp;

   cTemp = pData[2];
   pData[2] = pData[5];
   pData[5] = cTemp;

   cTemp = pData[3];
   pData[3] = pData[4];
   pData[4] = cTemp;
}


Edit: Yep, just tested it.

Code: Show/Hide
void SwapEndian (void *pData, int iBytes)
{
char *p = (char *)pData;
char *q = p + iBytes - 1;

// don't even check for odd

while (iBytes--)
   *p++ = *q--;
}

int main(int argv, char **argc)
{
   char test[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };

   SwapEndian(test, 8);

   return 0;
}


In the memory debug window, test ended up being: 08 07 06 05 05 06 07 08. Cool little relfection thingy though icon_razz.gif
Cyan~Fire - Fri Jun 25, 2004 5:42 pm
Post subject:
Here, since this function shouldn't ever be used for anything above 10 bytes, I'll use a constant size.

Code: Show/Hide
void SwapEndian(void *pData, int iBytes)
{
   char swapped[10];   //should not be used above 80 bits

   if (iBytes % 2)   //even check
      return;

   char *source = (char*)pData + iBytes;
   char *dest = swapped;

   while (iBytes--)
      *dest++ = *source--;

   memcpy(pData, swapped, iBytes);
}

Mr Ekted - Fri Jun 25, 2004 6:21 pm
Post subject:
You are right, but this is best...

Code: Show/Hide
void SwapEndian (void *pData, int iBytes)
{
char *p = (char *)pData;
char *q = p + iBytes - 1;
char  ch;

// don't even check for odd

iBytes >>= 1;

while (iBytes--)
   {
   ch = *p;
   *p++ = *q;
   *q-- = ch;
   }
}

SuSE - Fri Jun 25, 2004 7:28 pm
Post subject:
Code: Show/Hide
void switchtomac, it's cool
{
la la la smurfs are fun
}

Dustpuppy - Fri Jun 25, 2004 7:53 pm
Post subject:
Code: Show/Hide
void switchtomac, it's cool
{
la la la smurfs are fun.
}


You forgot a period, that wouldn't compile
SuSE - Fri Jun 25, 2004 7:57 pm
Post subject:
LIES!! I HAVE CODED A PROGRAM IN ASM THAT WILL EXTRACT YOUR BRAIN!!
CypherJF - Fri Jun 25, 2004 8:35 pm
Post subject:
*confused* icon_sad.gif
D1st0rt - Sat Jun 26, 2004 12:49 am
Post subject:
Ekted lol, I was at a bookstore and I saw a book you could've written. "How NOT to program in C++ (119 Programs that don't work)"
Mr Ekted - Sat Jun 26, 2004 1:02 am
Post subject:
LOL!
Dustpuppy - Sat Jun 26, 2004 1:47 pm
Post subject:
SuSE wrote:
LIES!! I HAVE CODED A PROGRAM IN ASM THAT WILL EXTRACT YOUR BRAIN!!


Pfft, you can't extract my brain. It's tarballed and encrypted using an algorithm designed by Qndre himself.
Mr Ekted - Sat Jun 26, 2004 2:46 pm
Post subject:
The one with the 536,189 bit key? It's hopeless...
Dustpuppy - Sat Jun 26, 2004 5:36 pm
Post subject:
Mr Ekted wrote:
The one with the 536,189 bit key? It's hopeless...

What? But it has a 536,189 bit key. 536,189 BITS!!!!!!11111111111111
That's like, 50 bytes or something11111111111111
SuSE - Sun Jun 27, 2004 8:02 am
Post subject:
lol, god bless qndre sa_tongue.gif
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group