Subject: Re: Endianness conversion functions
To: Christian Biere <christianbiere@gmx.de>
From: Krister Walfridsson <cato@df.lth.se>
List: tech-misc
Date: 01/19/2007 18:20:45
On Thu, 18 Jan 2007, Christian Biere wrote:
> Krister Walfridsson wrote:
>> This breaks the C aliasing rules [*],
>
> I know that's not C but since there was prior art and due to the
> alignment check, I assumed it would be fine for this "implementation".
> Also shouldn't the "void *" invalidate any such aliasing assumptions?

No, aliasing is about the type of the memory access, and in a statement
like

    *(uint32_t *)buf = foo;

the memory is accessed as a uint32_t regardless of the type of buf.

Casting to void* makes the analysis harder for the compiler, so it
is often the case that code with aliasing issues start to "work" when
you sprinkle some void* over it, but it is no guarantee that it will
work with other versions of the compiler...
2B2B


> It's a bit sad that GCC doesn't recognize the shift/or construct though because
> I think it's cleanest version - considering that memcpy() might cause a huge
> penalty.

It does recognize some of the shift/or constructs, but that support
need to be improved.  That has been on my TODO list for a long time
now.  But I have not found the time yet... :(

    /Krister