avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Efficient htons/swap16 or htonl/swap32 in place?


From: Peter N Lewis
Subject: [avr-gcc-list] Efficient htons/swap16 or htonl/swap32 in place?
Date: Fri, 17 Aug 2001 08:29:20 +0800

I'd like to do a whole bunch of int16 and int32 swaps on a struct to get the numbers in network byte order. I read through he assembly cookbook (thanks Harald!) and wrote thie macro:

#define htonuint16( n ) ({                                      \
        uint16_t __t = n;                                       \
        __asm__ (                                               \
                "mov __tmp_reg__, %A0" "\n\t"                       \
                "mov %A0, %B0" "\n\t"                               \
                "mov %B0, __tmp_reg__" "\n\t"                       \
                : "=r" (__t)                                  \
                : "0" (__t)                                   \
        );                                                      \
        __t;                                                    \
 })

and C code:

data.field = htonuint16( data.field )

but it generates this code:

        ldd r20,Y+1
        ldd r21,Y+2
/* #APP */
        mov __tmp_reg__, r20
        mov r20, r21
        mov r21, __tmp_reg__

/* #NOAPP */
        std Y+1,r20
        std Y+2,r21

with -Os or -O3.

Is there any way to convince the compiler to generate:

        ldd r20,Y+1
        ldd r21,Y+2
        std Y+1,r21
        std Y+2,r22

I would have thought a peephole optimizer would have little trouble generating this good code from the initial code?

Thanks for any ideas,
   Peter.

--
<http://www.interarchy.com/>  <ftp://ftp.interarchy.com/interarchy.hqx>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]