1 # Check for alignas and alignof that conform to C23.
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.
8 # Prepare for substituting <stdalign.h> if it is not supported.
10 AC_DEFUN([gl_STDALIGN_H],
12 AC_CACHE_CHECK([for alignas and alignof],
13 [gl_cv_header_working_stdalign_h],
14 [gl_save_CFLAGS=$CFLAGS
15 for gl_working in "yes, keywords" "yes, <stdalign.h> macros"; do
16 AS_CASE([$gl_working],
17 [*stdalign.h*], [CFLAGS="$gl_save_CFLAGS -DINCLUDE_STDALIGN_H"])
21 #ifdef INCLUDE_STDALIGN_H
26 /* Test that alignof yields a result consistent with offsetof.
27 This catches GCC bug 52023
28 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. */
30 template <class t> struct alignof_helper { char a; t b; };
31 # define ao(type) offsetof (alignof_helper<type>, b)
33 # define ao(type) offsetof (struct { char a; type b; }, b)
35 char test_double[ao (double) % _Alignof (double) == 0 ? 1 : -1];
36 char test_long[ao (long int) % _Alignof (long int) == 0 ? 1 : -1];
37 char test_alignof[alignof (double) == _Alignof (double) ? 1 : -1];
39 /* Test alignas only on platforms where gnulib can help. */
41 ((defined __cplusplus && 201103 <= __cplusplus) \
42 || (__TINYC__ && defined __attribute__) \
43 || (defined __APPLE__ && defined __MACH__ \
44 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
46 || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
47 || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__ \
49 struct alignas_test { char c; char alignas (8) alignas_8; };
50 char test_alignas[offsetof (struct alignas_test, alignas_8) == 8
54 [gl_cv_header_working_stdalign_h=$gl_working],
55 [gl_cv_header_working_stdalign_h=no])
57 CFLAGS=$gl_save_CFLAGS
58 test "$gl_cv_header_working_stdalign_h" != no && break
61 GL_GENERATE_STDALIGN_H=false
62 AS_CASE([$gl_cv_header_working_stdalign_h],
64 [GL_GENERATE_STDALIGN_H=true],
66 [AC_DEFINE([HAVE_C_ALIGNASOF], [1],
67 [Define to 1 if the alignas and alignof keywords work.])])
69 AC_CHECK_HEADERS_ONCE([stdalign.h])
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 && __cplusplus < 201103 && !defined alignof
76 # include <stdalign.h>
78 /* Substitute. Keep consistent with gnulib/lib/stdalign.in.h. */
79 # ifndef _GL_STDALIGN_H
80 # define _GL_STDALIGN_H
83 # if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
84 || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
85 && !defined __clang__) \
86 || (defined __clang__ && __clang_major__ < 8))
88 # if (201103 <= __cplusplus || defined _MSC_VER)
89 # define _Alignof(type) alignof (type)
91 template <class __t> struct __alignof_helper { char __a; __t __b; };
92 # define _Alignof(type) offsetof (__alignof_helper<type>, __b)
93 # define _GL_STDALIGN_NEEDS_STDDEF 1
96 # if (defined __GNUC__ && 4 <= __GNUC__) || defined __clang__
97 # define _Alignof(type) __builtin_offsetof (struct { char __a; type __b; }, __b)
99 # define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
100 # define _GL_STDALIGN_NEEDS_STDDEF 1
104 # if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))
105 # define alignof _Alignof
107 # define __alignof_is_defined 1
108 # if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
109 # if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)
110 # define _Alignas(a) alignas (a)
111 # elif (!defined __attribute__ \
112 && ((defined __APPLE__ && defined __MACH__ \
113 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
114 : __GNUC__ && !defined __ibmxl__) \
115 || (4 <= __clang_major__) \
116 || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
117 || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
118 # define _Alignas(a) __attribute__ ((__aligned__ (a)))
119 # elif 1300 <= _MSC_VER
120 # define _Alignas(a) __declspec (align (a))
123 # if ((defined _Alignas \
124 && !(defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) \
125 || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
126 # define alignas _Alignas
128 # if (defined alignas \
129 || (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)))
130 # define __alignas_is_defined 1
132 # if _GL_STDALIGN_NEEDS_STDDEF
135 # endif /* _GL_STDALIGN_H */