This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Endian swapping with C code on ARM
On Fri, 2004-07-09 at 14:10, Joel Sherrill wrote:
> How about just using inline assembly like this from the RTEMS ARM port?
>
> unsigned int CPU_swap_u32(
> unsigned int value
> )
> {
> uint32_t tmp = value;
> asm volatile ("EOR %1, %0, %0, ROR #16\n"
> "BIC %1, %1, #0xff0000\n"
> "MOV %0, %0, ROR #8\n"
> "EOR %0, %0, %1, LSR #8\n"
> : "=r" (value), "=r" (tmp)
> : "0" (value), "1" (tmp));
>
> return value;
> }
Because for one instruction the loss of portability isn't really worth
it. Consider rebuilding your application in Thumb code. Even harder,
consider building it as a mix of ARM and Thumb code.
Inline ASM is generally Evil, especially when there's no specific reason
for doing it.
Having a builtin is OK, provided there's something to fall back on if it
doesn't exist for a particular port. I'm not aware of a generally
available (all OSes) library function that does a byte swap.
You also get into issues of word size. Which builtins do you provide?
32-bit? 16-bit? 64-bit? When is it best to stop trying to inline all
this and drop into a library call? The answer on ARM is probably > 32
bits, but for Thumb it might well be >= 32.
R.