xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/stdalign.in.h (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos /* A substitute for ISO C11 <stdalign.h>.
28dffb485Schristos 
3*4b169a6bSchristos    Copyright 2011-2022 Free Software Foundation, Inc.
48dffb485Schristos 
5*4b169a6bSchristos    This file is free software: you can redistribute it and/or modify
6*4b169a6bSchristos    it under the terms of the GNU Lesser General Public License as
7*4b169a6bSchristos    published by the Free Software Foundation; either version 2.1 of the
8*4b169a6bSchristos    License, or (at your option) any later version.
98dffb485Schristos 
10*4b169a6bSchristos    This file is distributed in the hope that it will be useful,
118dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
128dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*4b169a6bSchristos    GNU Lesser General Public License for more details.
148dffb485Schristos 
15*4b169a6bSchristos    You should have received a copy of the GNU Lesser General Public License
16*4b169a6bSchristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
178dffb485Schristos 
188dffb485Schristos /* Written by Paul Eggert and Bruno Haible.  */
198dffb485Schristos 
208dffb485Schristos #ifndef _GL_STDALIGN_H
218dffb485Schristos #define _GL_STDALIGN_H
228dffb485Schristos 
238dffb485Schristos /* ISO C11 <stdalign.h> for platforms that lack it.
248dffb485Schristos 
258dffb485Schristos    References:
268dffb485Schristos    ISO C11 (latest free draft
278dffb485Schristos    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf>)
288dffb485Schristos    sections 6.5.3.4, 6.7.5, 7.15.
298dffb485Schristos    C++11 (latest free draft
308dffb485Schristos    <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>)
318dffb485Schristos    section 18.10. */
328dffb485Schristos 
338dffb485Schristos /* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment
348dffb485Schristos    requirement of a structure member (i.e., slot or field) that is of
358dffb485Schristos    type TYPE, as an integer constant expression.
368dffb485Schristos 
37*4b169a6bSchristos    This differs from GCC's and clang's __alignof__ operator, which can
38*4b169a6bSchristos    yield a better-performing alignment for an object of that type.  For
39*4b169a6bSchristos    example, on x86 with GCC and on Linux/x86 with clang,
40*4b169a6bSchristos    __alignof__ (double) and __alignof__ (long long) are 8, whereas
41*4b169a6bSchristos    alignof (double) and alignof (long long) are 4 unless the option
42*4b169a6bSchristos    '-malign-double' is used.
438dffb485Schristos 
448dffb485Schristos    The result cannot be used as a value for an 'enum' constant, if you
458dffb485Schristos    want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc.
468dffb485Schristos 
478dffb485Schristos    Include <stddef.h> for offsetof.  */
488dffb485Schristos #include <stddef.h>
498dffb485Schristos 
508dffb485Schristos /* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
518dffb485Schristos    standard headers, defines conflicting implementations of _Alignas
528dffb485Schristos    and _Alignof that are no better than ours; override them.  */
538dffb485Schristos #undef _Alignas
548dffb485Schristos #undef _Alignof
558dffb485Schristos 
568dffb485Schristos /* GCC releases before GCC 4.9 had a bug in _Alignof.  See GCC bug 52023
57*4b169a6bSchristos    <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
58*4b169a6bSchristos    clang versions < 8.0.0 have the same bug.  */
598dffb485Schristos #if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
60*4b169a6bSchristos      || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
61*4b169a6bSchristos          && !defined __clang__) \
62*4b169a6bSchristos      || (defined __clang__ && __clang_major__ < 8))
638dffb485Schristos # ifdef __cplusplus
648dffb485Schristos #  if 201103 <= __cplusplus
658dffb485Schristos #   define _Alignof(type) alignof (type)
668dffb485Schristos #  else
678dffb485Schristos    template <class __t> struct __alignof_helper { char __a; __t __b; };
688dffb485Schristos #   define _Alignof(type) offsetof (__alignof_helper<type>, __b)
698dffb485Schristos #  endif
708dffb485Schristos # else
718dffb485Schristos #  define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
728dffb485Schristos # endif
738dffb485Schristos #endif
748dffb485Schristos #if ! (defined __cplusplus && 201103 <= __cplusplus)
758dffb485Schristos # define alignof _Alignof
768dffb485Schristos #endif
778dffb485Schristos #define __alignof_is_defined 1
788dffb485Schristos 
798dffb485Schristos /* alignas (A), also known as _Alignas (A), aligns a variable or type
808dffb485Schristos    to the alignment A, where A is an integer constant expression.  For
818dffb485Schristos    example:
828dffb485Schristos 
838dffb485Schristos       int alignas (8) foo;
848dffb485Schristos       struct s { int a; int alignas (8) bar; };
858dffb485Schristos 
868dffb485Schristos    aligns the address of FOO and the offset of BAR to be multiples of 8.
878dffb485Schristos 
888dffb485Schristos    A should be a power of two that is at least the type's alignment
898dffb485Schristos    and at most the implementation's alignment limit.  This limit is
908dffb485Schristos    2**28 on typical GNUish hosts, and 2**13 on MSVC.  To be portable
918dffb485Schristos    to MSVC through at least version 10.0, A should be an integer
928dffb485Schristos    constant, as MSVC does not support expressions such as 1 << 3.
938dffb485Schristos    To be portable to Sun C 5.11, do not align auto variables to
948dffb485Schristos    anything stricter than their default alignment.
958dffb485Schristos 
968dffb485Schristos    The following C11 requirements are not supported here:
978dffb485Schristos 
988dffb485Schristos      - If A is zero, alignas has no effect.
998dffb485Schristos      - alignas can be used multiple times; the strictest one wins.
1008dffb485Schristos      - alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
1018dffb485Schristos 
1028dffb485Schristos    */
1038dffb485Schristos 
1048dffb485Schristos #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
1058dffb485Schristos # if defined __cplusplus && 201103 <= __cplusplus
1068dffb485Schristos #  define _Alignas(a) alignas (a)
107*4b169a6bSchristos # elif (!defined __attribute__ \
108*4b169a6bSchristos         && ((defined __APPLE__ && defined __MACH__ \
1098dffb485Schristos              ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
1108dffb485Schristos              : __GNUC__ && !defined __ibmxl__) \
111*4b169a6bSchristos             || (4 <= __clang_major__) \
1128dffb485Schristos             || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
113*4b169a6bSchristos             || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
1148dffb485Schristos #  define _Alignas(a) __attribute__ ((__aligned__ (a)))
1158dffb485Schristos # elif 1300 <= _MSC_VER
1168dffb485Schristos #  define _Alignas(a) __declspec (align (a))
1178dffb485Schristos # endif
1188dffb485Schristos #endif
1198dffb485Schristos #if ((defined _Alignas && ! (defined __cplusplus && 201103 <= __cplusplus)) \
1208dffb485Schristos      || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
1218dffb485Schristos # define alignas _Alignas
1228dffb485Schristos #endif
1238dffb485Schristos #if defined alignas || (defined __cplusplus && 201103 <= __cplusplus)
1248dffb485Schristos # define __alignas_is_defined 1
1258dffb485Schristos #endif
1268dffb485Schristos 
1278dffb485Schristos #endif /* _GL_STDALIGN_H */
128