<div dir="ltr">Is there a compelling reason to use __BIGGEST_ALIGNMENT__ over max_align_t?</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 13, 2014 at 9:51 AM, mats petersson <span dir="ltr"><<a href="mailto:mats@planetcatfish.com" target="_blank">mats@planetcatfish.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This is my first attempt for a patch to Clang, so please accept my<br>
apologies for any misconduct with regards to formatting, styling,<br>
content, culture, etc - and I hope that gmail doesn't munge the<br>
patch...<br>
<br>
The idea for this came about when at work (ARM) we discussed some code<br>
that had accidentally aligned it's sub-allocated block to 4 bytes for<br>
a 64-bit type, and got a unaligned access trap. So "how do you know",<br>
and someone found __BIGGEST_ALIGNMENT__ in gcc - but not available for<br>
Clang. So I thought I could add it, to make it a little more<br>
consistent between compilers...<br>
<br>
--<br>
Mats<br>
<br>
Add a predefined macro reflecting the largest alignment required by<br>
the archiecture, in bytes. This is useful for allocator designs that<br>
need to know the system alignment requirements. It reflects the<br>
TargetInfo.SuitableAlignment, converted to bytes (using CharWidth).<br>
---<br>
 lib/Frontend/InitPreprocessor.cpp |    4 ++++<br>
 test/Preprocessor/init.c          |   33 +++++++++++++++++++++++++++++++++<br>
 2 files changed, 37 insertions(+), 0 deletions(-)<br>
<br>
diff --git a/lib/Frontend/InitPreprocessor.cpp<br>
b/lib/Frontend/InitPreprocessor.cpp<br>
index f4241a9..896277a 100644<br>
--- a/lib/Frontend/InitPreprocessor.cpp<br>
+++ b/lib/Frontend/InitPreprocessor.cpp<br>
@@ -710,6 +710,10 @@ static void InitializePredefinedMacros(const<br>
TargetInfo &TI,<br>
   Builder.defineMacro("__POINTER_WIDTH__",<br>
                       Twine((int)TI.getPointerWidth(0)));<br>
<br>
+  // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc.<br>
+  Builder.defineMacro("__BIGGEST_ALIGNMENT__",<br>
+                      Twine(TI.getSuitableAlign() / TI.getCharWidth()) );<br>
+<br>
   if (!LangOpts.CharIsSigned)<br>
     Builder.defineMacro("__CHAR_UNSIGNED__");<br>
<br>
diff --git a/test/Preprocessor/init.c b/test/Preprocessor/init.c<br>
index 4f32114..924aa5b 100644<br>
--- a/test/Preprocessor/init.c<br>
+++ b/test/Preprocessor/init.c<br>
@@ -241,6 +241,7 @@<br>
 // AARCH64:#define __ARM_ARCH 8<br>
 // AARCH64:#define __ARM_ARCH_ISA_A64 1<br>
 // AARCH64-NOT:#define __ARM_BIG_ENDIAN 1<br>
+// AARCH64:#define __BIGGEST_ALIGNMENT__ 8<br>
 // AARCH64:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // AARCH64:#define __CHAR16_TYPE__ unsigned short<br>
 // AARCH64:#define __CHAR32_TYPE__ unsigned int<br>
@@ -431,6 +432,7 @@<br>
 // AARCH64-BE:#define __ARM_ARCH 8<br>
 // AARCH64-BE:#define __ARM_ARCH_ISA_A64 1<br>
 // AARCH64-BE:#define __ARM_BIG_ENDIAN 1<br>
+// AARCH64-BE:#define __BIGGEST_ALIGNMENT__ 8<br>
 // AARCH64-BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // AARCH64-BE:#define __CHAR16_TYPE__ unsigned short<br>
 // AARCH64-BE:#define __CHAR32_TYPE__ unsigned int<br>
@@ -621,6 +623,7 @@<br>
 // AARCH64-NETBSD:#define __ARM_ARCH 8<br>
 // AARCH64-NETBSD:#define __ARM_ARCH_ISA_A64 1<br>
 // AARCH64-NETBSD-NOT:#define __ARM_BIG_ENDIAN 1<br>
+// AARCH64-NETBSD:#define __BIGGEST_ALIGNMENT__ 8<br>
 // AARCH64-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // AARCH64-NETBSD:#define __CHAR16_TYPE__ unsigned short<br>
 // AARCH64-NETBSD:#define __CHAR32_TYPE__ unsigned int<br>
@@ -812,6 +815,7 @@<br>
 // AARCH64-FREEBSD:#define __ARM_ARCH 8<br>
 // AARCH64-FREEBSD:#define __ARM_ARCH_ISA_A64 1<br>
 // AARCH64-FREEBSD-NOT:#define __ARM_BIG_ENDIAN 1<br>
+// AARCH64-FREEBSD:#define __BIGGEST_ALIGNMENT__ 8<br>
 // AARCH64-FREEBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // AARCH64-FREEBSD:#define __CHAR16_TYPE__ unsigned short<br>
 // AARCH64-FREEBSD:#define __CHAR32_TYPE__ unsigned int<br>
@@ -1003,6 +1007,7 @@<br>
 // ARM:#define __ARMEL__ 1<br>
 // ARM:#define __ARM_ARCH_6J__ 1<br>
 // ARM-NOT:#define __ARM_BIG_ENDIAN 1<br>
+// ARM:#define __BIGGEST_ALIGNMENT__ 8<br>
 // ARM:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // ARM:#define __CHAR16_TYPE__ unsigned short<br>
 // ARM:#define __CHAR32_TYPE__ unsigned int<br>
@@ -1193,6 +1198,7 @@<br>
 // ARM-BE-NOT:#define __ARMEL__ 1<br>
 // ARM-BE:#define __ARM_ARCH_6J__ 1<br>
 // ARM-BE:#define __ARM_BIG_ENDIAN 1<br>
+// ARM-BE:#define __BIGGEST_ALIGNMENT__ 8<br>
 // ARM-BE:#define __BIG_ENDIAN__ 1<br>
 // ARM-BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // ARM-BE:#define __CHAR16_TYPE__ unsigned short<br>
@@ -1387,6 +1393,7 @@<br>
 // ARMEABISOFTFP:#define __ARM_EABI__ 1<br>
 // ARMEABISOFTFP:#define __ARM_PCS 1<br>
 // ARMEABISOFTFP-NOT:#define __ARM_PCS_VFP 1<br>
+// ARMEABISOFTFP:#define __BIGGEST_ALIGNMENT__ 8<br>
 // ARMEABISOFTFP:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // ARMEABISOFTFP:#define __CHAR16_TYPE__ unsigned short<br>
 // ARMEABISOFTFP:#define __CHAR32_TYPE__ unsigned int<br>
@@ -1582,6 +1589,7 @@<br>
 // ARMEABIHARDFP:#define __ARM_EABI__ 1<br>
 // ARMEABIHARDFP:#define __ARM_PCS 1<br>
 // ARMEABIHARDFP:#define __ARM_PCS_VFP 1<br>
+// ARMEABIHARDFP:#define __BIGGEST_ALIGNMENT__ 8<br>
 // ARMEABIHARDFP:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // ARMEABIHARDFP:#define __CHAR16_TYPE__ unsigned short<br>
 // ARMEABIHARDFP:#define __CHAR32_TYPE__ unsigned int<br>
@@ -1775,6 +1783,7 @@<br>
 // ARM-NETBSD:#define __ARM_DWARF_EH__ 1<br>
 // ARM-NETBSD:#define __ARM_EABI__ 1<br>
 // ARM-NETBSD-NOT:#define __ARM_BIG_ENDIAN 1<br>
+// ARM-NETBSD:#define __BIGGEST_ALIGNMENT__ 8<br>
 // ARM-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // ARM-NETBSD:#define __CHAR16_TYPE__ unsigned short<br>
 // ARM-NETBSD:#define __CHAR32_TYPE__ unsigned int<br>
@@ -2021,6 +2030,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=i386-none-none <<br>
/dev/null | FileCheck -check-prefix I386 %s<br>
 //<br>
 // I386-NOT:#define _LP64<br>
+// I386:#define __BIGGEST_ALIGNMENT__ 16<br>
 // I386:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // I386:#define __CHAR16_TYPE__ unsigned short<br>
 // I386:#define __CHAR32_TYPE__ unsigned int<br>
@@ -2207,6 +2217,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=i386-pc-linux-gnu<br>
-target-cpu pentium4 < /dev/null | FileCheck -check-prefix I386-LINUX<br>
%s<br>
 //<br>
 // I386-LINUX-NOT:#define _LP64<br>
+// I386-LINUX:#define __BIGGEST_ALIGNMENT__ 16<br>
 // I386-LINUX:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // I386-LINUX:#define __CHAR16_TYPE__ unsigned short<br>
 // I386-LINUX:#define __CHAR32_TYPE__ unsigned int<br>
@@ -2393,6 +2404,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=i386-netbsd <<br>
/dev/null | FileCheck -check-prefix I386-NETBSD %s<br>
 //<br>
 // I386-NETBSD-NOT:#define _LP64<br>
+// I386-NETBSD:#define __BIGGEST_ALIGNMENT__ 16<br>
 // I386-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // I386-NETBSD:#define __CHAR16_TYPE__ unsigned short<br>
 // I386-NETBSD:#define __CHAR32_TYPE__ unsigned int<br>
@@ -2597,6 +2609,7 @@<br>
 // MIPS32BE:#define _MIPS_SZINT 32<br>
 // MIPS32BE:#define _MIPS_SZLONG 32<br>
 // MIPS32BE:#define _MIPS_SZPTR 32<br>
+// MIPS32BE:#define __BIGGEST_ALIGNMENT__ 8<br>
 // MIPS32BE:#define __BIG_ENDIAN__ 1<br>
 // MIPS32BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // MIPS32BE:#define __CHAR16_TYPE__ unsigned short<br>
@@ -2805,6 +2818,7 @@<br>
 // MIPS32EL:#define _MIPS_SZINT 32<br>
 // MIPS32EL:#define _MIPS_SZLONG 32<br>
 // MIPS32EL:#define _MIPS_SZPTR 32<br>
+// MIPS32EL:#define __BIGGEST_ALIGNMENT__ 8<br>
 // MIPS32EL:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // MIPS32EL:#define __CHAR16_TYPE__ unsigned short<br>
 // MIPS32EL:#define __CHAR32_TYPE__ unsigned int<br>
@@ -3010,6 +3024,7 @@<br>
 // MIPS64BE:#define _MIPS_SZINT 32<br>
 // MIPS64BE:#define _MIPS_SZLONG 64<br>
 // MIPS64BE:#define _MIPS_SZPTR 64<br>
+// MIPS64BE:#define __BIGGEST_ALIGNMENT__ 16<br>
 // MIPS64BE:#define __BIG_ENDIAN__ 1<br>
 // MIPS64BE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // MIPS64BE:#define __CHAR16_TYPE__ unsigned short<br>
@@ -3218,6 +3233,7 @@<br>
 // MIPS64EL:#define _MIPS_SZINT 32<br>
 // MIPS64EL:#define _MIPS_SZLONG 64<br>
 // MIPS64EL:#define _MIPS_SZPTR 64<br>
+// MIPS64EL:#define __BIGGEST_ALIGNMENT__ 16<br>
 // MIPS64EL:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // MIPS64EL:#define __CHAR16_TYPE__ unsigned short<br>
 // MIPS64EL:#define __CHAR32_TYPE__ unsigned int<br>
@@ -3583,6 +3599,7 @@<br>
 //<br>
 // MSP430:#define MSP430 1<br>
 // MSP430-NOT:#define _LP64<br>
+// MSP430:#define __BIGGEST_ALIGNMENT__ 2<br>
 // MSP430:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // MSP430:#define __CHAR16_TYPE__ unsigned short<br>
 // MSP430:#define __CHAR32_TYPE__ unsigned int<br>
@@ -3766,6 +3783,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=nvptx-none-none <<br>
/dev/null | FileCheck -check-prefix NVPTX32 %s<br>
 //<br>
 // NVPTX32-NOT:#define _LP64<br>
+// NVPTX32:#define __BIGGEST_ALIGNMENT__ 8<br>
 // NVPTX32:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // NVPTX32:#define __CHAR16_TYPE__ unsigned short<br>
 // NVPTX32:#define __CHAR32_TYPE__ unsigned int<br>
@@ -3952,6 +3970,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=nvptx64-none-none <<br>
/dev/null | FileCheck -check-prefix NVPTX64 %s<br>
 //<br>
 // NVPTX64:#define _LP64 1<br>
+// NVPTX64:#define __BIGGEST_ALIGNMENT__ 8<br>
 // NVPTX64:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // NVPTX64:#define __CHAR16_TYPE__ unsigned short<br>
 // NVPTX64:#define __CHAR32_TYPE__ unsigned int<br>
@@ -4143,6 +4162,7 @@<br>
 // PPC603E:#define _ARCH_PPCGR 1<br>
 // PPC603E:#define _BIG_ENDIAN 1<br>
 // PPC603E-NOT:#define _LP64<br>
+// PPC603E:#define __BIGGEST_ALIGNMENT__ 8<br>
 // PPC603E:#define __BIG_ENDIAN__ 1<br>
 // PPC603E:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // PPC603E:#define __CHAR16_TYPE__ unsigned short<br>
@@ -4340,6 +4360,7 @@<br>
 // PPC64:#define _ARCH_PWR7 1<br>
 // PPC64:#define _BIG_ENDIAN 1<br>
 // PPC64:#define _LP64 1<br>
+// PPC64:#define __BIGGEST_ALIGNMENT__ 8<br>
 // PPC64:#define __BIG_ENDIAN__ 1<br>
 // PPC64:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // PPC64:#define __CHAR16_TYPE__ unsigned short<br>
@@ -4542,6 +4563,7 @@<br>
 // PPC64LE:#define _CALL_ELF 2<br>
 // PPC64LE:#define _LITTLE_ENDIAN 1<br>
 // PPC64LE:#define _LP64 1<br>
+// PPC64LE:#define __BIGGEST_ALIGNMENT__ 8<br>
 // PPC64LE:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // PPC64LE:#define __CHAR16_TYPE__ unsigned short<br>
 // PPC64LE:#define __CHAR32_TYPE__ unsigned int<br>
@@ -4923,6 +4945,7 @@<br>
 // PPC64-LINUX:#define _ARCH_PPC64 1<br>
 // PPC64-LINUX:#define _BIG_ENDIAN 1<br>
 // PPC64-LINUX:#define _LP64 1<br>
+// PPC64-LINUX:#define __BIGGEST_ALIGNMENT__ 8<br>
 // PPC64-LINUX:#define __BIG_ENDIAN__ 1<br>
 // PPC64-LINUX:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // PPC64-LINUX:#define __CHAR16_TYPE__ unsigned short<br>
@@ -5127,6 +5150,7 @@<br>
 // PPC:#define _ARCH_PPC 1<br>
 // PPC:#define _BIG_ENDIAN 1<br>
 // PPC-NOT:#define _LP64<br>
+// PPC:#define __BIGGEST_ALIGNMENT__ 8<br>
 // PPC:#define __BIG_ENDIAN__ 1<br>
 // PPC:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // PPC:#define __CHAR16_TYPE__ unsigned short<br>
@@ -5317,6 +5341,7 @@<br>
 // PPC-LINUX:#define _ARCH_PPC 1<br>
 // PPC-LINUX:#define _BIG_ENDIAN 1<br>
 // PPC-LINUX-NOT:#define _LP64<br>
+// PPC-LINUX:#define __BIGGEST_ALIGNMENT__ 8<br>
 // PPC-LINUX:#define __BIG_ENDIAN__ 1<br>
 // PPC-LINUX:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // PPC-LINUX:#define __CHAR16_TYPE__ unsigned short<br>
@@ -5508,6 +5533,7 @@<br>
 //<br>
 // PPC-DARWIN:#define _ARCH_PPC 1<br>
 // PPC-DARWIN:#define _BIG_ENDIAN 1<br>
+// PPC-DARWIN:#define __BIGGEST_ALIGNMENT__ 16<br>
 // PPC-DARWIN:#define __BIG_ENDIAN__ 1<br>
 // PPC-DARWIN:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // PPC-DARWIN:#define __CHAR16_TYPE__ unsigned short<br>
@@ -5701,6 +5727,7 @@<br>
 //<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=s390x-none-none<br>
-fno-signed-char < /dev/null | FileCheck -check-prefix S390X %s<br>
 //<br>
+// S390X:#define __BIGGEST_ALIGNMENT__ 8<br>
 // S390X:#define __CHAR16_TYPE__ unsigned short<br>
 // S390X:#define __CHAR32_TYPE__ unsigned int<br>
 // S390X:#define __CHAR_BIT__ 8<br>
@@ -5882,6 +5909,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=sparc-none-none <<br>
/dev/null | FileCheck -check-prefix SPARC %s<br>
 //<br>
 // SPARC-NOT:#define _LP64<br>
+// SPARC:#define __BIGGEST_ALIGNMENT__ 8<br>
 // SPARC:#define __BIG_ENDIAN__ 1<br>
 // SPARC:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // SPARC:#define __CHAR16_TYPE__ unsigned short<br>
@@ -6069,6 +6097,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=tce-none-none <<br>
/dev/null | FileCheck -check-prefix TCE %s<br>
 //<br>
 // TCE-NOT:#define _LP64<br>
+// TCE:#define __BIGGEST_ALIGNMENT__ 4<br>
 // TCE:#define __BIG_ENDIAN__ 1<br>
 // TCE:#define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__<br>
 // TCE:#define __CHAR16_TYPE__ unsigned short<br>
@@ -6236,6 +6265,7 @@<br>
 //<br>
 // X86_64:#define _LP64 1<br>
 // X86_64-NOT:#define _LP32 1<br>
+// X86_64:#define __BIGGEST_ALIGNMENT__ 16<br>
 // X86_64:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // X86_64:#define __CHAR16_TYPE__ unsigned short<br>
 // X86_64:#define __CHAR32_TYPE__ unsigned int<br>
@@ -6437,6 +6467,7 @@<br>
 //<br>
 // X32:#define _ILP32 1<br>
 // X32-NOT:#define _LP64 1<br>
+// X32:#define __BIGGEST_ALIGNMENT__ 16<br>
 // X32:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // X32:#define __CHAR16_TYPE__ unsigned short<br>
 // X32:#define __CHAR32_TYPE__ unsigned int<br>
@@ -6630,6 +6661,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=x86_64-pc-linux-gnu<br>
< /dev/null | FileCheck -check-prefix X86_64-LINUX %s<br>
 //<br>
 // X86_64-LINUX:#define _LP64 1<br>
+// X86_64-LINUX:#define __BIGGEST_ALIGNMENT__ 16<br>
 // X86_64-LINUX:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // X86_64-LINUX:#define __CHAR16_TYPE__ unsigned short<br>
 // X86_64-LINUX:#define __CHAR32_TYPE__ unsigned int<br>
@@ -6828,6 +6860,7 @@<br>
 // RUN: %clang_cc1 -E -dM -ffreestanding -triple=x86_64-netbsd <<br>
/dev/null | FileCheck -check-prefix X86_64-NETBSD %s<br>
 //<br>
 // X86_64-NETBSD:#define _LP64 1<br>
+// X86_64-NETBSD:#define __BIGGEST_ALIGNMENT__ 16<br>
 // X86_64-NETBSD:#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__<br>
 // X86_64-NETBSD:#define __CHAR16_TYPE__ unsigned short<br>
 // X86_64-NETBSD:#define __CHAR32_TYPE__ unsigned int<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.7.7.6<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</font></span></blockquote></div></div>