Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Endian Swappage

 
Post new topic   Reply to topic Printable version
 View previous topic  S.H.I.T. - (My first zone) Post :: Post Ok who gave me the avatar?  View next topic  
Author Message
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Fri Jun 25, 2004 1:08 pm   Post maybe stupid    Post subject: Endian Swappage Reply to topic Reply with quote

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);
}

_________________
This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Fri Jun 25, 2004 5:16 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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

_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Fri Jun 25, 2004 5:36 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Fri Jun 25, 2004 5:42 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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);
}
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Fri Jun 25, 2004 6:21 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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;
   }
}
Back to top
View users profile Send private message Add User to Ignore List
SuSE
Me measures good


Joined: Dec 02 2002
Posts: 2307
Offline

PostPosted: Fri Jun 25, 2004 7:28 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Code: Show/Hide
void switchtomac, it's cool
{
la la la smurfs are fun
}
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
Dustpuppy
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Jan 23 2003
Posts: 215
Location: England
Offline

PostPosted: Fri Jun 25, 2004 7:53 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Code: Show/Hide
void switchtomac, it's cool
{
la la la smurfs are fun.
}


You forgot a period, that wouldn't compile
_________________
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
SuSE
Me measures good


Joined: Dec 02 2002
Posts: 2307
Offline

PostPosted: Fri Jun 25, 2004 7:57 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

LIES!! I HAVE CODED A PROGRAM IN ASM THAT WILL EXTRACT YOUR BRAIN!!
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
CypherJF
I gargle nitroglycerin


Gender:Gender:Male
Joined: Aug 14 2003
Posts: 2582
Location: USA
Offline

PostPosted: Fri Jun 25, 2004 8:35 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

*confused* icon_sad.gif
_________________
Performance is often the art of cheating carefully. - James Gosling
Back to top
View users profile Send private message Add User to Ignore List
D1st0rt
Miss Directed Wannabe


Age:37
Gender:Gender:Male
Joined: Aug 31 2003
Posts: 2247
Location: Blacksburg, VA
Offline

PostPosted: Sat Jun 26, 2004 12:49 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

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)"
_________________

Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat Jun 26, 2004 1:02 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

LOL!
Back to top
View users profile Send private message Add User to Ignore List
Dustpuppy
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Jan 23 2003
Posts: 215
Location: England
Offline

PostPosted: Sat Jun 26, 2004 1:47 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat Jun 26, 2004 2:46 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

The one with the 536,189 bit key? It's hopeless...
Back to top
View users profile Send private message Add User to Ignore List
Dustpuppy
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Jan 23 2003
Posts: 215
Location: England
Offline

PostPosted: Sat Jun 26, 2004 5:36 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

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
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
SuSE
Me measures good


Joined: Dec 02 2002
Posts: 2307
Offline

PostPosted: Sun Jun 27, 2004 8:02 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

lol, god bless qndre sa_tongue.gif
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 62 page(s) served in previous 5 minutes.

phpBB Created this page in 0.676482 seconds : 39 queries executed (81.2%): GZIP compression disabled