Re: [MLton-commit] r7458
Matthew Fluet <[email protected]>
| Newsgroups | gmane.comp.lang.ml.mlton.devel |
|---|---|
| Message-ID | <[email protected]> |
On Tue, May 4, 2010 at 1:19 PM, Wesley Terpstra <[email protected]> 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.