3 dnl Copyright 2011-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 # Check for alignas and alignof that conform to C23.
10 dnl Written by Paul Eggert and Bruno Haible.
12 # Prepare for substituting <stdalign.h> if it is not supported.
14 AC_DEFUN([gl_ALIGNASOF],
16 AC_CACHE_CHECK([for alignas and alignof],
17 [gl_cv_header_working_stdalign_h],
18 [gl_saved_CFLAGS=$CFLAGS
19 for gl_working in "yes, keywords" "yes, <stdalign.h> macros"; do
20 AS_CASE([$gl_working],
21 [*stdalign.h*], [CFLAGS="$gl_saved_CFLAGS -DINCLUDE_STDALIGN_H"])
25 #ifdef INCLUDE_STDALIGN_H
30 /* Test that alignof yields a result consistent with offsetof.
31 This catches GCC bug 52023
32 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. */
34 template <class t> struct alignof_helper { char a; t b; };
35 # define ao(type) offsetof (alignof_helper<type>, b)
37 # define ao(type) offsetof (struct { char a; type b; }, b)
39 char test_double[ao (double) % _Alignof (double) == 0 ? 1 : -1];
40 char test_long[ao (long int) % _Alignof (long int) == 0 ? 1 : -1];
41 char test_alignof[alignof (double) == _Alignof (double) ? 1 : -1];
43 /* Test alignas only on platforms where gnulib can help. */
45 ((defined __cplusplus && 201103 <= __cplusplus) \
46 || (__TINYC__ && defined __attribute__) \
47 || (defined __APPLE__ && defined __MACH__ \
48 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
50 || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
51 || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__ \
53 struct alignas_test { char c; char alignas (8) alignas_8; };
54 char test_alignas[offsetof (struct alignas_test, alignas_8) == 8
58 [gl_cv_header_working_stdalign_h=$gl_working],
59 [gl_cv_header_working_stdalign_h=no])
61 CFLAGS=$gl_saved_CFLAGS
62 test "$gl_cv_header_working_stdalign_h" != no && break
65 AS_CASE([$gl_cv_header_working_stdalign_h],
67 [AC_DEFINE([HAVE_C_ALIGNASOF], [1],
68 [Define to 1 if the alignas and alignof keywords work.])])
70 dnl The "zz" puts this toward config.h's end, to avoid potential
71 dnl collisions with other definitions.
72 AH_VERBATIM([zzalignas],
73 [#if !defined HAVE_C_ALIGNASOF \
74 && !(defined __cplusplus && 201103 <= __cplusplus) \
76 # if defined HAVE_STDALIGN_H
77 # include <stdalign.h>
80 /* ISO C23 alignas and alignof for platforms that lack it.
83 ISO C23 (latest free draft
84 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf>)
85 sections 6.5.3.4, 6.7.5, 7.15.
86 C++11 (latest free draft
87 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>)
90 /* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment
91 requirement of a structure member (i.e., slot or field) that is of
92 type TYPE, as an integer constant expression.
94 This differs from GCC's and clang's __alignof__ operator, which can
95 yield a better-performing alignment for an object of that type. For
96 example, on x86 with GCC and on Linux/x86 with clang,
97 __alignof__ (double) and __alignof__ (long long) are 8, whereas
98 alignof (double) and alignof (long long) are 4 unless the option
99 '-malign-double' is used.
101 The result cannot be used as a value for an 'enum' constant, if you
102 want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. */
104 /* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
105 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
106 clang versions < 8.0.0 have the same bug. */
107 # if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
108 || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
109 && !defined __clang__) \
110 || (defined __clang__ && __clang_major__ < 8))
113 # if (201103 <= __cplusplus || defined _MSC_VER)
114 # define _Alignof(type) alignof (type)
116 template <class __t> struct __alignof_helper { char __a; __t __b; };
117 # if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
118 # define _Alignof(type) __builtin_offsetof (__alignof_helper<type>, __b)
120 # define _Alignof(type) offsetof (__alignof_helper<type>, __b)
122 # define _GL_STDALIGN_NEEDS_STDDEF 1
125 # if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
126 # define _Alignof(type) __builtin_offsetof (struct { char __a; type __b; }, __b)
128 # define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
129 # define _GL_STDALIGN_NEEDS_STDDEF 1
133 # if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
135 # define alignof _Alignof
138 /* alignas (A), also known as _Alignas (A), aligns a variable or type
139 to the alignment A, where A is an integer constant expression. For
143 struct s { int a; int alignas (8) bar; };
145 aligns the address of FOO and the offset of BAR to be multiples of 8.
147 A should be a power of two that is at least the type's alignment
148 and at most the implementation's alignment limit. This limit is
149 2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable
150 to MSVC through at least version 10.0, A should be an integer
151 constant, as MSVC does not support expressions such as 1 << 3.
152 To be portable to Sun C 5.11, do not align auto variables to
153 anything stricter than their default alignment.
155 The following C23 requirements are not supported here:
157 - If A is zero, alignas has no effect.
158 - alignas can be used multiple times; the strictest one wins.
159 - alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
162 # if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
163 # if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
164 # define _Alignas(a) alignas (a)
165 # elif (!defined __attribute__ \
166 && ((defined __APPLE__ && defined __MACH__ \
167 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
168 : __GNUC__ && !defined __ibmxl__) \
169 || (4 <= __clang_major__) \
170 || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
171 || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
172 # define _Alignas(a) __attribute__ ((__aligned__ (a)))
173 # elif 1300 <= _MSC_VER
174 # define _Alignas(a) __declspec (align (a))
177 # if !defined HAVE_STDALIGN_H
178 # if ((defined _Alignas \
179 && !(defined __cplusplus \
180 && (201103 <= __cplusplus || defined _MSC_VER))) \
181 || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
182 # define alignas _Alignas
186 # if defined _GL_STDALIGN_NEEDS_STDDEF
192 AC_DEFUN([gl_STDALIGN_H],
194 AC_REQUIRE([gl_ALIGNASOF])
195 if test "$gl_cv_header_working_stdalign_h" = no; then
196 GL_GENERATE_STDALIGN_H=true
198 GL_GENERATE_STDALIGN_H=false
201 gl_CHECK_NEXT_HEADERS([stdalign.h])
202 if test $ac_cv_header_stdalign_h = yes; then
207 AC_SUBST([HAVE_STDALIGN_H])