Debian Codegraph @

m4/stdalign.m4

raw
1 # stdalign.m4
2 # serial 1
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.
7 dnl This file is offered as-is, without any warranty.
8
9 # Check for alignas and alignof that conform to C23.
10
11 dnl Written by Paul Eggert and Bruno Haible.
12
13 # Prepare for substituting <stdalign.h> if it is not supported.
14
15 AC_DEFUN([gl_ALIGNASOF],
16 [
17 AC_CACHE_CHECK([for alignas and alignof],
18 [gl_cv_header_working_stdalign_h],
19 [gl_saved_CFLAGS=$CFLAGS
20 for gl_working in "yes, keywords" "yes, <stdalign.h> macros"; do
21 AS_CASE([$gl_working],
22 [*stdalign.h*], [CFLAGS="$gl_saved_CFLAGS -DINCLUDE_STDALIGN_H"])
23 AC_COMPILE_IFELSE(
24 [AC_LANG_PROGRAM(
25 [[#include <stdint.h>
26 #ifdef INCLUDE_STDALIGN_H
27 #include <stdalign.h>
28 #endif
29 #include <stddef.h>
30
31 /* Test that alignof yields a result consistent with offsetof.
32 This catches GCC bug 52023
33 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. */
34 #ifdef __cplusplus
35 template <class t> struct alignof_helper { char a; t b; };
36 # define ao(type) offsetof (alignof_helper<type>, b)
37 #else
38 # define ao(type) offsetof (struct { char a; type b; }, b)
39 #endif
40 char test_double[ao (double) % _Alignof (double) == 0 ? 1 : -1];
41 char test_long[ao (long int) % _Alignof (long int) == 0 ? 1 : -1];
42 char test_alignof[alignof (double) == _Alignof (double) ? 1 : -1];
43
44 /* Test alignas only on platforms where gnulib can help. */
45 #if \
46 ((defined __cplusplus && 201103 <= __cplusplus) \
47 || (__TINYC__ && defined __attribute__) \
48 || (defined __APPLE__ && defined __MACH__ \
49 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
50 : __GNUC__) \
51 || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
52 || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__ \
53 || 1300 <= _MSC_VER)
54 struct alignas_test { char c; char alignas (8) alignas_8; };
55 char test_alignas[offsetof (struct alignas_test, alignas_8) == 8
56 ? 1 : -1];
57 #endif
58 ]])],
59 [gl_cv_header_working_stdalign_h=$gl_working],
60 [gl_cv_header_working_stdalign_h=no])
61
62 CFLAGS=$gl_saved_CFLAGS
63 test "$gl_cv_header_working_stdalign_h" != no && break
64 done])
65
66 AS_CASE([$gl_cv_header_working_stdalign_h],
67 [yes*keyword*],
68 [AC_DEFINE([HAVE_C_ALIGNASOF], [1],
69 [Define to 1 if the alignas and alignof keywords work.])])
70
71 dnl The "zz" puts this toward config.h's end, to avoid potential
72 dnl collisions with other definitions.
73 AH_VERBATIM([zzalignas],
74 [#if !defined HAVE_C_ALIGNASOF \
75 && !(defined __cplusplus && 201103 <= __cplusplus) \
76 && !defined alignof
77 # if defined HAVE_STDALIGN_H
78 # include <stdalign.h>
79 # endif
80
81 /* ISO C23 alignas and alignof for platforms that lack it.
82
83 References:
84 ISO C23 (latest free draft
85 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf>)
86 sections 6.5.3.4, 6.7.5, 7.15.
87 C++11 (latest free draft
88 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>)
89 section 18.10. */
90
91 /* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment
92 requirement of a structure member (i.e., slot or field) that is of
93 type TYPE, as an integer constant expression.
94
95 This differs from GCC's and clang's __alignof__ operator, which can
96 yield a better-performing alignment for an object of that type. For
97 example, on x86 with GCC and on Linux/x86 with clang,
98 __alignof__ (double) and __alignof__ (long long) are 8, whereas
99 alignof (double) and alignof (long long) are 4 unless the option
100 '-malign-double' is used.
101
102 The result cannot be used as a value for an 'enum' constant, if you
103 want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. */
104
105 /* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
106 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
107 clang versions < 8.0.0 have the same bug. */
108 # if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
109 || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
110 && !defined __clang__) \
111 || (defined __clang__ && __clang_major__ < 8))
112 # undef/**/_Alignof
113 # ifdef __cplusplus
114 # if (201103 <= __cplusplus || defined _MSC_VER)
115 # define _Alignof(type) alignof (type)
116 # else
117 template <class __t> struct __alignof_helper { char __a; __t __b; };
118 # if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
119 # define _Alignof(type) __builtin_offsetof (__alignof_helper<type>, __b)
120 # else
121 # define _Alignof(type) offsetof (__alignof_helper<type>, __b)
122 # endif
123 # define _GL_STDALIGN_NEEDS_STDDEF 1
124 # endif
125 # else
126 # if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
127 # define _Alignof(type) __builtin_offsetof (struct { char __a; type __b; }, __b)
128 # else
129 # define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
130 # define _GL_STDALIGN_NEEDS_STDDEF 1
131 # endif
132 # endif
133 # endif
134 # if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
135 # undef/**/alignof
136 # define alignof _Alignof
137 # endif
138
139 /* alignas (A), also known as _Alignas (A), aligns a variable or type
140 to the alignment A, where A is an integer constant expression. For
141 example:
142
143 int alignas (8) foo;
144 struct s { int a; int alignas (8) bar; };
145
146 aligns the address of FOO and the offset of BAR to be multiples of 8.
147
148 A should be a power of two that is at least the type's alignment
149 and at most the implementation's alignment limit. This limit is
150 2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable
151 to MSVC through at least version 10.0, A should be an integer
152 constant, as MSVC does not support expressions such as 1 << 3.
153 To be portable to Sun C 5.11, do not align auto variables to
154 anything stricter than their default alignment.
155
156 The following C23 requirements are not supported here:
157
158 - If A is zero, alignas has no effect.
159 - alignas can be used multiple times; the strictest one wins.
160 - alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
161
162 */
163 # if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
164 # if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
165 # define _Alignas(a) alignas (a)
166 # elif (!defined __attribute__ \
167 && ((defined __APPLE__ && defined __MACH__ \
168 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
169 : __GNUC__ && !defined __ibmxl__) \
170 || (4 <= __clang_major__) \
171 || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
172 || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
173 # define _Alignas(a) __attribute__ ((__aligned__ (a)))
174 # elif 1300 <= _MSC_VER
175 # define _Alignas(a) __declspec (align (a))
176 # endif
177 # endif
178 # if !defined HAVE_STDALIGN_H
179 # if ((defined _Alignas \
180 && !(defined __cplusplus \
181 && (201103 <= __cplusplus || defined _MSC_VER))) \
182 || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
183 # define alignas _Alignas
184 # endif
185 # endif
186
187 # if defined _GL_STDALIGN_NEEDS_STDDEF
188 # include <stddef.h>
189 # endif
190 #endif])
191 ])
192
193 AC_DEFUN([gl_STDALIGN_H],
194 [
195 AC_REQUIRE([gl_ALIGNASOF])
196 if test "$gl_cv_header_working_stdalign_h" = no; then
197 GL_GENERATE_STDALIGN_H=true
198 else
199 GL_GENERATE_STDALIGN_H=false
200 fi
201
202 gl_CHECK_NEXT_HEADERS([stdalign.h])
203 if test $ac_cv_header_stdalign_h = yes; then
204 HAVE_STDALIGN_H=1
205 else
206 HAVE_STDALIGN_H=0
207 fi
208 AC_SUBST([HAVE_STDALIGN_H])
209 ])