]> Savannah Git Hosting - gnulib.git/commitdiff
stddef: port to pre-C11 GCC on x86
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 2 Apr 2015 17:35:53 +0000 (10:35 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 2 Apr 2015 17:36:37 +0000 (10:36 -0700)
On this platform, max_align_t should have an alignment of 8 even
though the storage alignments of double, long, etc. max out at 4.
Inspired by a comment of Andreas Schwab's here:
https://sourceware.org/ml/libc-alpha/2015-04/msg00017.html
* lib/stddef.in.h (_GL_STDDEF_ALIGNAS) [!HAVE_MAX_ALIGN_T]: New macro.
(max_align_t) [!HAVE_MAX_ALIGN_T]: Use it.
* tests/test-stddef.c: Test __alignof__ too, if available.

ChangeLog
lib/stddef.in.h
tests/test-stddef.c

index 41dd857bad556652ecf1baf91daa1326bff24dff..6d690cc1a24ede999016263b5e934b24e7ae05e2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2015-04-02  Paul Eggert  <eggert@cs.ucla.edu>
+
+       stddef: port to pre-C11 GCC on x86
+       On this platform, max_align_t should have an alignment of 8 even
+       though the storage alignments of double, long, etc. max out at 4.
+       Inspired by a comment of Andreas Schwab's here:
+       https://sourceware.org/ml/libc-alpha/2015-04/msg00017.html
+       * lib/stddef.in.h (_GL_STDDEF_ALIGNAS) [!HAVE_MAX_ALIGN_T]: New macro.
+       (max_align_t) [!HAVE_MAX_ALIGN_T]: Use it.
+       * tests/test-stddef.c: Test __alignof__ too, if available.
+
 2015-03-24  Pádraig Brady  <P@draigBrady.com>
 
        quotearg-simple-tests: add missing gl_FUNC_MMAP_ANON dependency
index 535132a4e24a910bc83eb50a3e7ab638667f48bb..d6b81fac829249eb490ed507b4d3d05afc3cada3 100644 (file)
 
 /* Some platforms lack max_align_t.  */
 #if !@HAVE_MAX_ALIGN_T@
+/* On the x86, the maximum storage alignment of double, long, etc. is 4,
+   but GCC's C11 ABI for x86 says that max_align_t has an alignment of 8,
+   and the C11 standard allows this.  Work around this problem by
+   using __alignof__ (which returns 8 for double) rather than _Alignof
+   (which returns 4), and align each union member accordingly.  */
+# ifdef __GNUC__
+#  define _GL_STDDEF_ALIGNAS(type) \
+     __attribute__ ((__aligned__ (__alignof__ (type))))
+# else
+#  define _GL_STDDEF_ALIGNAS(type) /* */
+# endif
 typedef union
 {
-  char *__p;
-  double __d;
-  long double __ld;
-  long int __i;
+  char *__p _GL_STDDEF_ALIGNAS (char *);
+  double __d _GL_STDDEF_ALIGNAS (double);
+  long double __ld _GL_STDDEF_ALIGNAS (long double);
+  long int __i _GL_STDDEF_ALIGNAS (long int);
 } max_align_t;
 #endif
 
index 317949655e96eafe57847365067a46237b81ebd1..aa6ae549cc7eca28339134b930db78a96ef4d1ef 100644 (file)
@@ -55,6 +55,16 @@ verify (alignof (ptrdiff_t) <= alignof (max_align_t));
 verify (alignof (size_t) <= alignof (max_align_t));
 verify (alignof (wchar_t) <= alignof (max_align_t));
 verify (alignof (struct d) <= alignof (max_align_t));
+#if defined __GNUC__ || defined __IBM__ALIGNOF__
+verify (__alignof__ (double) <= __alignof__ (max_align_t));
+verify (__alignof__ (int) <= __alignof__ (max_align_t));
+verify (__alignof__ (long double) <= __alignof__ (max_align_t));
+verify (__alignof__ (long int) <= __alignof__ (max_align_t));
+verify (__alignof__ (ptrdiff_t) <= __alignof__ (max_align_t));
+verify (__alignof__ (size_t) <= __alignof__ (max_align_t));
+verify (__alignof__ (wchar_t) <= __alignof__ (max_align_t));
+verify (__alignof__ (struct d) <= __alignof__ (max_align_t));
+#endif
 
 int
 main (void)