stdalign.m4 7.1 KB

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