Debian Codegraph @

m4/stdalign.m4

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