This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [avr-gcc-list] Re: AVR byte swap optimization


On 12/18/06, Anton Erasmus <antone@sentechsa.com> wrote:
Hi,

Not a macro, but the following seems to generate reasonable code.
...

Thanks Anton,

I came to the same conclusion.

Cheers,
Shaun

static inline uint16_t bswap_16_inline(uint16_t x)
{
	union {
		uint16_t x;
		struct {
			uint8_t a;
			uint8_t b;
		} s;
	} in, out;
	in.x = x;
	out.s.a = in.s.b;
	out.s.b = in.s.a;
	return out.x;
}

static inline uint32_t bswap_32_inline(uint32_t x)
{
	union {
		uint32_t x;
		struct {
			uint8_t a;
			uint8_t b;
			uint8_t c;
			uint8_t d;
		} s;
	} in, out;
	in.x = x;
	out.s.a = in.s.d;
	out.s.b = in.s.c;
	out.s.c = in.s.b;
	out.s.d = in.s.a;
	return out.x;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]