[MLton] Re: [MLton-commit] r7458

Matthew Fluet matthew.fluet at gmail.com
Mon May 10 11:19:29 PDT 2010


On Tue, May 4, 2010 at 1:19 PM, Wesley Terpstra <wesley at mlton.org> wrote:
> ... Wow.
>
> gcc 4.4.3 no longer allows this:
>  union X {
>    int x;
>    short y;
>  };
>  x = input;
>  reutrn y;
>
> According to the C99 standard, the result for y is undefined.
> For the longest time the union approach has been the recommended fix to the
> pre-C99 standard idiom of 'return *(short*)&input;'. Not any more!
>
> I really want to hurt whoever decided to take this idiom away. There are no
> alternatives except calling out to memcpy (or similar) via a char*. The cost
> to performance for a simple endian-swap is absurd.
>
> Since we're already gcc-specific, an easy fix is to mark these unions as
> __attribute__((__may_alias__)). This gets us past all regressions on 4.4.3.

Looking at the assembly code using memcpy vs. using a union with
__attribute__((__may_alias__)), they are nearly identical at all but
-O0.  Certainly, no explicit memcpy calls, and sometimes the code that
had memcpy is compiled to a shorter assembly sequence.



More information about the MLton mailing list