| 1 |
# Check for alignas and alignof that conform to C23. |
| 2 |
|
| 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 |
|
| 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 \ |
| 72 |
&& !(defined __cplusplus && 201103 <= __cplusplus) \ |
| 73 |
&& !defined alignof |
| 74 |
# if defined HAVE_STDALIGN_H |
| 75 |
# include <stdalign.h> |
| 76 |
# endif |
| 77 |
|
| 78 |
/* ISO C23 alignas and alignof for platforms that lack it. |
| 79 |
|
| 80 |
References: |
| 81 |
ISO C23 (latest free draft |
| 82 |
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf>) |
| 83 |
sections 6.5.3.4, 6.7.5, 7.15. |
| 84 |
C++11 (latest free draft |
| 85 |
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>) |
| 86 |
section 18.10. */ |
| 87 |
|
| 88 |
/* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment |
| 89 |
requirement of a structure member (i.e., slot or field) that is of |
| 90 |
type TYPE, as an integer constant expression. |
| 91 |
|
| 92 |
This differs from GCC's and clang's __alignof__ operator, which can |
| 93 |
yield a better-performing alignment for an object of that type. For |
| 94 |
example, on x86 with GCC and on Linux/x86 with clang, |
| 95 |
__alignof__ (double) and __alignof__ (long long) are 8, whereas |
| 96 |
alignof (double) and alignof (long long) are 4 unless the option |
| 97 |
'-malign-double' is used. |
| 98 |
|
| 99 |
The result cannot be used as a value for an 'enum' constant, if you |
| 100 |
want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. */ |
| 101 |
|
| 102 |
/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023 |
| 103 |
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. |
| 104 |
clang versions < 8.0.0 have the same bug. */ |
| 105 |
# if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \ |
| 106 |
|| (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \ |
| 107 |
&& !defined __clang__) \ |
| 108 |
|| (defined __clang__ && __clang_major__ < 8)) |
| 109 |
# undef/**/_Alignof |
| 110 |
# ifdef __cplusplus |
| 111 |
# if (201103 <= __cplusplus || defined _MSC_VER) |
| 112 |
# define _Alignof(type) alignof (type) |
| 113 |
# else |
| 114 |
template <class __t> struct __alignof_helper { char __a; __t __b; }; |
| 115 |
# if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__ |
| 116 |
# define _Alignof(type) __builtin_offsetof (__alignof_helper<type>, __b) |
| 117 |
# else |
| 118 |
# define _Alignof(type) offsetof (__alignof_helper<type>, __b) |
| 119 |
# endif |
| 120 |
# define _GL_STDALIGN_NEEDS_STDDEF 1 |
| 121 |
# endif |
| 122 |
# else |
| 123 |
# if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__ |
| 124 |
# define _Alignof(type) __builtin_offsetof (struct { char __a; type __b; }, __b) |
| 125 |
# else |
| 126 |
# define _Alignof(type) offsetof (struct { char __a; type __b; }, __b) |
| 127 |
# define _GL_STDALIGN_NEEDS_STDDEF 1 |
| 128 |
# endif |
| 129 |
# endif |
| 130 |
# endif |
| 131 |
# if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)) |
| 132 |
# undef/**/alignof |
| 133 |
# define alignof _Alignof |
| 134 |
# endif |
| 135 |
|
| 136 |
/* alignas (A), also known as _Alignas (A), aligns a variable or type |
| 137 |
to the alignment A, where A is an integer constant expression. For |
| 138 |
example: |
| 139 |
|
| 140 |
int alignas (8) foo; |
| 141 |
struct s { int a; int alignas (8) bar; }; |
| 142 |
|
| 143 |
aligns the address of FOO and the offset of BAR to be multiples of 8. |
| 144 |
|
| 145 |
A should be a power of two that is at least the type's alignment |
| 146 |
and at most the implementation's alignment limit. This limit is |
| 147 |
2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable |
| 148 |
to MSVC through at least version 10.0, A should be an integer |
| 149 |
constant, as MSVC does not support expressions such as 1 << 3. |
| 150 |
To be portable to Sun C 5.11, do not align auto variables to |
| 151 |
anything stricter than their default alignment. |
| 152 |
|
| 153 |
The following C23 requirements are not supported here: |
| 154 |
|
| 155 |
- If A is zero, alignas has no effect. |
| 156 |
- alignas can be used multiple times; the strictest one wins. |
| 157 |
- alignas (TYPE) is equivalent to alignas (alignof (TYPE)). |
| 158 |
|
| 159 |
*/ |
| 160 |
# if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 |
| 161 |
# if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER) |
| 162 |
# define _Alignas(a) alignas (a) |
| 163 |
# elif (!defined __attribute__ \ |
| 164 |
&& ((defined __APPLE__ && defined __MACH__ \ |
| 165 |
? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \ |
| 166 |
: __GNUC__ && !defined __ibmxl__) \ |
| 167 |
|| (4 <= __clang_major__) \ |
| 168 |
|| (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \ |
| 169 |
|| __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__)) |
| 170 |
# define _Alignas(a) __attribute__ ((__aligned__ (a))) |
| 171 |
# elif 1300 <= _MSC_VER |
| 172 |
# define _Alignas(a) __declspec (align (a)) |
| 173 |
# endif |
| 174 |
# endif |
| 175 |
# if !defined HAVE_STDALIGN_H |
| 176 |
# if ((defined _Alignas \ |
| 177 |
&& !(defined __cplusplus \ |
| 178 |
&& (201103 <= __cplusplus || defined _MSC_VER))) \ |
| 179 |
|| (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__)) |
| 180 |
# define alignas _Alignas |
| 181 |
# endif |
| 182 |
# endif |
| 183 |
|
| 184 |
# if defined _GL_STDALIGN_NEEDS_STDDEF |
| 185 |
# include <stddef.h> |
| 186 |
# endif |
| 187 |
#endif]) |
| 188 |
]) |
| 189 |
|
| 190 |
AC_DEFUN([gl_STDALIGN_H], |
| 191 |
[ |
| 192 |
AC_REQUIRE([gl_ALIGNASOF]) |
| 193 |
if test "$gl_cv_header_working_stdalign_h" = no; then |
| 194 |
GL_GENERATE_STDALIGN_H=true |
| 195 |
else |
| 196 |
GL_GENERATE_STDALIGN_H=false |
| 197 |
fi |
| 198 |
|
| 199 |
gl_CHECK_NEXT_HEADERS([stdalign.h]) |
| 200 |
if test $ac_cv_header_stdalign_h = yes; then |
| 201 |
HAVE_STDALIGN_H=1 |
| 202 |
else |
| 203 |
HAVE_STDALIGN_H=0 |
| 204 |
fi |
| 205 |
AC_SUBST([HAVE_STDALIGN_H]) |
| 206 |
]) |