| 1 |
dnl Written by Zack Weinberg <zackw at panix.com> in 2017. |
| 2 |
dnl To the extent possible under law, Zack Weinberg has waived all |
| 3 |
dnl copyright and related or neighboring rights to this work. |
| 4 |
dnl |
| 5 |
dnl See https://creativecommons.org/publicdomain/zero/1.0/ for further |
| 6 |
dnl details. |
| 7 |
dnl |
| 8 |
dnl Find out how to query and set data alignment. |
| 9 |
dnl Currently knows about C11's _Alignas and _Alignof, |
| 10 |
dnl and GCC's __attribute__ ((aligned)) and __alignof. |
| 11 |
dnl |
| 12 |
dnl Note: AC_PROG_CC_C11 includes a test for _Alignas and _Alignof, |
| 13 |
dnl but not for <stdalign.h>, which we don't bother using anyway. |
| 14 |
dnl |
| 15 |
dnl Compatibility note: alignas (TYPE) does not work when alignas() is |
| 16 |
dnl defined using __attribute__ ((aligned)). Use alignas (alignof (TYPE)) |
| 17 |
dnl instead. |
| 18 |
AC_DEFUN([zw_C_ALIGNAS], |
| 19 |
[AC_REQUIRE([AC_PROG_CC]) |
| 20 |
AC_CACHE_CHECK([how to control data alignment], [zw_cv_c_alignas], |
| 21 |
[AS_IF([test x$ac_prog_cc_stdc = xc11], |
| 22 |
[zw_cv_c_alignas=_Alignas], |
| 23 |
[AC_COMPILE_IFELSE( |
| 24 |
[AC_LANG_PROGRAM([[ |
| 25 |
int __attribute__((__aligned__(8))) global; |
| 26 |
struct __attribute__((__aligned__(8))) aggregate { int x, y; }; |
| 27 |
]], [[ |
| 28 |
int __attribute__((__aligned__(8))) local; |
| 29 |
]])], |
| 30 |
[zw_cv_c_alignas=__attribute__], |
| 31 |
[zw_cv_c_alignas=unknown]) |
| 32 |
]) |
| 33 |
]) |
| 34 |
AS_IF([test x$zw_cv_c_alignas = x_Alignas], |
| 35 |
[zw_c_alignas_expr="_Alignas(n)"], |
| 36 |
[test x$zw_cv_c_alignas = x__attribute__], |
| 37 |
[zw_c_alignas_expr="__attribute__((__aligned__(n)))"], |
| 38 |
[AC_MSG_FAILURE([could not find a way to control data alignment])]) |
| 39 |
AC_DEFINE_UNQUOTED([alignas(n)], [$zw_c_alignas_expr], |
| 40 |
[Define as a type specifier which sets the alignment of a variable or type |
| 41 |
to N bytes. C11 alignas(TYPE) does not need to be supported.]) |
| 42 |
]) |
| 43 |
|
| 44 |
AC_DEFUN([zw_C_ALIGNOF], |
| 45 |
[AC_REQUIRE([AC_PROG_CC]) |
| 46 |
AC_CACHE_CHECK([how to query data alignment], [zw_cv_c_alignof], |
| 47 |
[AS_IF([test x$ac_prog_cc_stdc = xc11], |
| 48 |
[zw_cv_c_alignof=_Alignof], |
| 49 |
[AC_COMPILE_IFELSE( |
| 50 |
[AC_LANG_PROGRAM([[ |
| 51 |
struct agg { int x, y; }; |
| 52 |
extern const char align_int[__alignof__(int)]; |
| 53 |
extern const char align_agg[__alignof__(struct agg)]; |
| 54 |
]], [[ |
| 55 |
double d; |
| 56 |
char align_var[__alignof__(d)]; |
| 57 |
]])], |
| 58 |
[zw_cv_c_alignof=__alignof__], |
| 59 |
[zw_cv_c_alignof=unknown]) |
| 60 |
]) |
| 61 |
]) |
| 62 |
AS_IF([test x$zw_cv_c_alignof = x_Alignof], |
| 63 |
[zw_c_alignof_expr="_Alignof(thing)"], |
| 64 |
[test x$zw_cv_c_alignof = x__alignof__], |
| 65 |
[zw_c_alignof_expr="__alignof__(thing)"], |
| 66 |
[AC_MSG_FAILURE([could not find a way to query data alignment])]) |
| 67 |
AC_DEFINE_UNQUOTED([alignof(thing)], [$zw_c_alignof_expr], |
| 68 |
[Define as an expression which evaluates to the alignment of THING. |
| 69 |
Must be computed at compile time (an "integer constant expression").]) |
| 70 |
]) |
| 71 |
|
| 72 |
AC_DEFUN([zw_C_MAX_ALIGN_T], |
| 73 |
[AC_REQUIRE([AC_PROG_CC]) |
| 74 |
AC_REQUIRE([zw_C_ALIGNOF]) |
| 75 |
AC_CACHE_CHECK([for max_align_t in stddef.h], [zw_cv_c_max_align_t], |
| 76 |
[AC_COMPILE_IFELSE( |
| 77 |
[AC_LANG_PROGRAM([[ |
| 78 |
#include <stddef.h> |
| 79 |
]], [[ |
| 80 |
max_align_t var; |
| 81 |
return alignof(var); |
| 82 |
]])], |
| 83 |
[zw_cv_c_max_align_t=yes], |
| 84 |
[zw_cv_c_max_align_t=no]) |
| 85 |
]) |
| 86 |
AS_IF([test x$zw_cv_c_max_align_t = xyes], |
| 87 |
[AC_DEFINE([HAVE_MAX_ALIGN_T], 1, |
| 88 |
[Define if stddef.h provides max_align_t.]) |
| 89 |
]) |
| 90 |
]) |