[MLton-commit] r7458

Wesley Terpstra wesley at mlton.org
Tue May 4 10:19:39 PDT 2010


... 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.


----------------------------------------------------------------------

U   mlton/trunk/runtime/basis/Real/Real-ops.h
U   mlton/trunk/runtime/basis/Word/Word-ops.h
U   mlton/trunk/runtime/gdtoa-patch
U   mlton/trunk/runtime/util/endian.h

----------------------------------------------------------------------

Modified: mlton/trunk/runtime/basis/Real/Real-ops.h
===================================================================
--- mlton/trunk/runtime/basis/Real/Real-ops.h	2010-05-03 15:41:57 UTC (rev 7457)
+++ mlton/trunk/runtime/basis/Real/Real-ops.h	2010-05-04 17:19:38 UTC (rev 7458)
@@ -27,7 +27,7 @@
   typedef volatile union {                                              \
     Real##size##_t r;                                                   \
     Word32_t ws[sizeof(Real##size##_t) / sizeof(Word32_t)];             \
-  } Real##size##OrWord32s;                                              \
+  } __attribute__((__may_alias__)) Real##size##OrWord32s;               \
   MLTON_CODEGEN_STATIC_INLINE                                           \
   Real##size##_t Real##size##_fetch (Ref(Real##size##_t) rp) {          \
     Real##size##OrWord32s u;                                            \

Modified: mlton/trunk/runtime/basis/Word/Word-ops.h
===================================================================
--- mlton/trunk/runtime/basis/Word/Word-ops.h	2010-05-03 15:41:57 UTC (rev 7457)
+++ mlton/trunk/runtime/basis/Word/Word-ops.h	2010-05-04 17:19:38 UTC (rev 7458)
@@ -47,7 +47,7 @@
   typedef volatile union {                                              \
     Word##size##_t w;                                                   \
     Word32_t ws[sizeof(Word##size##_t) / sizeof(Word32_t)];             \
-  } Word##size##OrWord32s;                                              \
+  } __attribute__((__may_alias__)) Word##size##OrWord32s;               \
   MLTON_CODEGEN_STATIC_INLINE                                           \
   Word##size##_t Word##size##_fetch (Ref(Word##size##_t) wp) {          \
     Word##size##OrWord32s u;                                            \

Modified: mlton/trunk/runtime/gdtoa-patch
===================================================================
--- mlton/trunk/runtime/gdtoa-patch	2010-05-03 15:41:57 UTC (rev 7457)
+++ mlton/trunk/runtime/gdtoa-patch	2010-05-04 17:19:38 UTC (rev 7458)
@@ -311,6 +311,15 @@
 diff -u gdtoa.orig/gdtoaimp.h gdtoa/gdtoaimp.h
 --- gdtoa.orig/gdtoaimp.h	2000-11-02 15:09:01 +0000
 +++ gdtoa/gdtoaimp.h	2008-10-04 02:24:16 +0000
+@@ -267,7 +267,7 @@
+ Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
+ #endif
+ 
+-typedef union { double d; ULong L[2]; } U;
++typedef union { double d; ULong L[2]; } __attribute__((__may_alias__)) U;
+ 
+ #ifdef YES_ALIAS
+ #define dval(x) x
 @@ -502,6 +502,7 @@
  #define g__fmt g__fmt_D2A
  #define gethex gethex_D2A

Modified: mlton/trunk/runtime/util/endian.h
===================================================================
--- mlton/trunk/runtime/util/endian.h	2010-05-03 15:41:57 UTC (rev 7457)
+++ mlton/trunk/runtime/util/endian.h	2010-05-04 17:19:38 UTC (rev 7458)
@@ -10,7 +10,7 @@
   union {
     uint16_t x;
     uint8_t y;
-  } z;
+  } __attribute__((__may_alias__)) z;
 
   /* gcc optimizes the following code to just return the result. */
   z.x = 0xABCDU;




More information about the MLton-commit mailing list