]> git.draconx.ca Git - dxcommon.git/blob - m4/align.m4
copysym: Merge tables into a single static allocation.
[dxcommon.git] / m4 / align.m4
1 # Copyright © 2024 Nick Bowler
2 #
3 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
4 # This is free software: you are free to do what the fuck you want to.
5 # There is NO WARRANTY, to the extent permitted by law.
6
7 # DX_C_ALIGNOF
8 #
9 # Probe whether the C compiler understands _Alignof(T).
10 #
11 # If not supported, _Alignof(T) is defined as a function-like macro which
12 # uses offsetof to guess the alignment of T, which must be a type such that
13 # T x; is a valid declaration of x as an object of type T.  It is therefore
14 # necessary that the caller includes <stddef.h> before using _Alignof.
15 #
16 # We skip the test if Autoconf has previously determined that the C compiler
17 # supports C11 or a newer standard, since the C11 test program checks this.
18 #
19 # Annoyingly, autoconf-2.71 removed the assignment of ac_cv_prog_cc_c11,
20 # even though this was actual documented behaviour...
21 AC_DEFUN([DX_C_ALIGNOF],
22 [AS_CASE([${ac_cv_prog_cc_c11-no}/${ac_prog_cc_stdc-no}],
23   [no/no|*/c89|*/c99],
24     [AC_CACHE_CHECK([if $CC supports _Alignof], [dx_cv_have_alignof],
25       [AC_COMPUTE_INT([_dx_tmp], [_Alignof(char)], [@&t@], [_dx_tmp=0])
26 AS_CASE([$_dx_tmp], [1], [dx_cv_have_alignof=yes], [dx_cv_have_alignof=no])])],
27   [dx_cv_have_alignof=yes])
28 AS_CASE([$dx_cv_have_alignof], [no], [AC_DEFINE([_Alignof(T)],
29   [offsetof(struct { char a; T b; }, b)],
30   [Define _Alignof(T) to a suitable fallback if _Alignof is unsupported.])])])
31
32 # DX_C_ALIGNAS
33 #
34 # Probe whether the C compiler understands _Alignas(X).
35 #
36 # If not supported, but the compiler supports the GNU __attribute__ or
37 # Microsoft __declspec methods to set alignment, _Alignas(X) is defined
38 # as a function-like macro which expands to such syntax.  These only
39 # work if X is an integer constant (as opposed to a type name).
40 #
41 # If there is no known method of declaring a variable with increased
42 # alignment, then _Alignas(X) is defined as a function-like macro which
43 # expands to nothing, which will compile but has no runtime effect.
44 #
45 # We skip the test if Autoconf has previously determined that the C
46 # compiler supports C11 or a newer standard, since the C11 test program
47 # checks this.
48 AC_DEFUN([DX_C_ALIGNAS],
49 [AS_CASE([${ac_cv_prog_cc_c11-no}/${ac_prog_cc_stdc-no}],
50   [no/no|*/c89|*/c99],
51     [AC_CACHE_CHECK([if $CC supports _Alignas], [dx_cv_have_alignas],
52 [dx_cv_have_alignas=no
53 for _dx_alignas
54 in '_Alignas(X)' '__attribute__((__aligned__(X)))' '__declspec(align(X))'
55 do
56 AC_COMPUTE_INT([_dx_tmp],
57   [sizeof (struct { char a; char ALIGNAS_TEST(4) b; }) >= 8],
58   [#define ALIGNAS_TEST(X) $_dx_alignas
59 ],[_dx_tmp=0])
60 AS_CASE([$_dx_tmp/$_dx_alignas],
61   [1/_Alignas*], [dx_cv_have_alignas=yes; break],
62   [1/*], [dx_cv_have_alignas=$_dx_alignas; break])
63 done])],
64   [dx_cv_have_alignas=yes])
65 AS_CASE([$dx_cv_have_alignas],
66   [*'(X)'*], [AC_DEFINE_UNQUOTED([_Alignas(X)], [$dx_cv_have_alignas],
67     [Define _Alignas(X) to a suitable fallback if _Alignas is unsupported.])],
68   [no], [AC_DEFINE([_Alignas(X)], [/**/])])])