Thread: [Dev-C++] Convert Little-Endian to Big-Endian (and vice versa)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Joey D. <jda...@ya...> - 2004-11-11 04:54:16
|
Hello, Does anybody know a faster algorithm to swap the bytes of an 'unsigned long' than this: // SWAP 32-BITS #define SWAP_LONG( A ) ((A << 24) | ((A & 0xff00L) << 8 ) | ((A & 0xff0000L) >> 8 ) | ((unsigned long)A >> 24)) Thank You. Joey --------------------------------- Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now |
|
From: Cary H. <xen...@gm...> - 2004-11-11 14:54:27
|
two things you should be considered: 1. In my experience, endian conversion is primarly used in porting code to a different platform. consequently, 2. You shouldn't make assumptions about how many bits are in an given type, as different compliers on the same platform, and even the same compiler on different platforms will used different sizes. An good example of this is in the WinAPI, Win32 says an unsigned long is 32 bits, while Win64 says its 64. On Thu, 11 Nov 2004 04:54:13 +0000 (GMT), Joey Dala <jda...@ya...> wrote: > > Hello, > > Does anybody know a faster algorithm to swap the bytes > of an 'unsigned long' than this: > > // SWAP 32-BITS > #define SWAP_LONG( A ) ((A << 24) | ((A & 0xff00L) << 8 ) | ((A & > 0xff0000L) >> 8 ) | ((unsigned long)A >> 24)) > > Thank You. > Joey > > ________________________________ > Yahoo! Messenger - Communicate instantly..."Ping" your friends today! > Download Messenger Now > > |
|
From: Daniel K. O. <dan...@ya...> - 2004-11-11 18:37:26
|
Joey Dala wrote:
> Does anybody know a faster algorithm to swap the bytes
> of an 'unsigned long' than this:
>
> // SWAP 32-BITS
> #define SWAP_LONG( A ) ((A << 24) | ((A & 0xff00L) << 8 ) | ((A &
> 0xff0000L) >> 8 ) | ((unsigned long)A >> 24))
Faster than this? Only if you use architecture-specific instructions.
The 486 (and higher) family has the "bswap" instruction. Here's an example:
---
#include <stdio.h>
#define SWAP32(x) asm volatile ("bswap %0" : "=r" (x) : "0" (x))
int main(void)
{
int foo = 1;
printf("%08x\n", foo);
asm volatile (
"bswap %0"
: "=r" (foo)
: "0" (foo)
);
printf("%08x\n", foo);
SWAP32(foo);
printf("%08x\n", foo);
getchar();
return 0;
}
---
Notice the use of the extended assembly syntax, so GCC can perform
better optimizations around the asm beast.
Also, the "volatile" keyword to make sure GCC won't move the assembly
statement around in the optimization proccess.
Before you ask, some information about using AT&T assembly syntax and
extended syntax is available here:
http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html
Daniel K. O.
|
|
From: William Mc C. <WIL...@ms...> - 2004-11-11 23:00:21
|
You might also want to look at using "exclusive OR" on each byte. ----- Original Message -----=20 From: Daniel K. O.<mailto:dan...@ya...>=20 To: Joey Dala<mailto:jda...@ya...>=20 Cc: = dev...@li...<mailto:dev...@li...= e.net>=20 Sent: Thursday, November 11, 2004 10:34 AM Subject: Re: [Dev-C++] Convert Little-Endian to Big-Endian (and vice = versa) Joey Dala wrote: > Does anybody know a faster algorithm to swap the bytes > of an 'unsigned long' than this: > =20 > // SWAP 32-BITS > #define SWAP_LONG( A ) ((A << 24) | ((A & 0xff00L) << 8 ) | ((A &=20 > 0xff0000L) >> 8 ) | ((unsigned long)A >> 24)) Faster than this? Only if you use architecture-specific instructions.=20 The 486 (and higher) family has the "bswap" instruction. Here's an = example: --- #include <stdio.h> #define SWAP32(x) asm volatile ("bswap %0" : "=3Dr" (x) : "0" = (x)) int main(void) { int foo =3D 1; =20 printf("%08x\n", foo); =20 asm volatile ( "bswap %0" : "=3Dr" (foo) : "0" (foo) ); =20 printf("%08x\n", foo); SWAP32(foo); printf("%08x\n", foo); getchar(); return 0; } --- Notice the use of the extended assembly syntax, so GCC can perform=20 better optimizations around the asm beast. Also, the "volatile" keyword to make sure GCC won't move the assembly=20 statement around in the optimization proccess. Before you ask, some information about using AT&T assembly syntax and=20 extended syntax is available here:=20 = http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html<ht= tp://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html> Daniel K. O. ------------------------------------------------------- This SF.Net email is sponsored by: Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux. = http://ads.osdn.com/?ad_id=3D5588&alloc_id=3D12065&op=3Dclick<http://ads.= osdn.com/?ad_id=3D5588&alloc_id=3D12065&op=3Dclick> _______________________________________________ Dev-cpp-users mailing list = Dev...@li...<mailto:Dev...@li...= e.net> TO UNSUBSCRIBE: = http://www23.brinkster.com/noicys/devcpp/ub.htm<http://www23.brinkster.co= m/noicys/devcpp/ub.htm> = https://lists.sourceforge.net/lists/listinfo/dev-cpp-users<https://lists.= sourceforge.net/lists/listinfo/dev-cpp-users> |
|
From: Joey D. <jda...@ya...> - 2004-11-12 00:39:01
|
can you please give me a code snippet.
i have no idea what's in your mind.
thanks.
--- William Mc Coy <WIL...@ms...> wrote:
> You might also want to look at using "exclusive OR"
> on each byte.
> ----- Original Message -----
> From: Daniel K.
> O.<mailto:dan...@ya...>
> To: Joey Dala<mailto:jda...@ya...>
> Cc:
>
dev...@li...<mailto:dev...@li...>
>
> Sent: Thursday, November 11, 2004 10:34 AM
> Subject: Re: [Dev-C++] Convert Little-Endian to
> Big-Endian (and vice versa)
>
>
> Joey Dala wrote:
>
> > Does anybody know a faster algorithm to swap the
> bytes
> > of an 'unsigned long' than this:
> >
> > // SWAP 32-BITS
> > #define SWAP_LONG( A ) ((A << 24) | ((A &
> 0xff00L) << 8 ) | ((A &
> > 0xff0000L) >> 8 ) | ((unsigned long)A >> 24))
>
>
> Faster than this? Only if you use
> architecture-specific instructions.
> The 486 (and higher) family has the "bswap"
> instruction. Here's an example:
>
> ---
> #include <stdio.h>
>
> #define SWAP32(x) asm volatile ("bswap %0" :
> "=r" (x) : "0" (x))
>
> int main(void)
> {
> int foo = 1;
>
> printf("%08x\n", foo);
>
> asm volatile (
> "bswap %0"
> : "=r" (foo)
> : "0" (foo)
> );
>
> printf("%08x\n", foo);
>
> SWAP32(foo);
>
> printf("%08x\n", foo);
>
> getchar();
> return 0;
> }
> ---
>
> Notice the use of the extended assembly syntax, so
> GCC can perform
> better optimizations around the asm beast.
> Also, the "volatile" keyword to make sure GCC
> won't move the assembly
> statement around in the optimization proccess.
>
>
> Before you ask, some information about using AT&T
> assembly syntax and
> extended syntax is available here:
>
>
http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html<http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html>
>
>
> Daniel K. O.
>
>
>
>
>
-------------------------------------------------------
> This SF.Net email is sponsored by:
> Sybase ASE Linux Express Edition - download now
> for FREE
> LinuxWorld Reader's Choice Award Winner for best
> database on Linux.
>
>
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click<http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click>
> _______________________________________________
> Dev-cpp-users mailing list
>
>
Dev...@li...<mailto:Dev...@li...>
> TO UNSUBSCRIBE:
>
http://www23.brinkster.com/noicys/devcpp/ub.htm<http://www23.brinkster.com/noicys/devcpp/ub.htm>
>
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users<https://lists.sourceforge.net/lists/listinfo/dev-cpp-users>
>
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
|