1 /* Test of <stdalign.h>.
2 Copyright 2009-2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert, inspired by Bruno Haible's test-alignof.c. */
30 typedef long double longdouble;
31 typedef struct { char a[1]; } struct1;
32 typedef struct { char a[2]; } struct2;
33 typedef struct { char a[3]; } struct3;
34 typedef struct { char a[4]; } struct4;
36 verify (__alignof_is_defined == 1);
38 # error "alignof is not a macro"
41 #if __alignas_is_defined
42 verify (__alignas_is_defined == 1);
44 # error "alignas is not a macro"
46 /* mingw can go up only to 8. 8 is all that GNU Emacs needs, so let's
47 limit the test to 8 for now. */
48 # define TEST_ALIGNMENT 8
50 # define _Alignas(alignment)
51 # define alignas(alignment)
52 # define TEST_ALIGNMENT 1
55 #define CHECK_STATIC(type) \
56 typedef struct { char slot1; type slot2; } type##_helper; \
57 verify (alignof (type) == offsetof (type##_helper, slot2)); \
58 verify (_Alignof (type) == alignof (type)); \
59 const int type##_alignment = alignof (type); \
60 type alignas (TEST_ALIGNMENT) static_##type##_alignas; \
61 type _Alignas (TEST_ALIGNMENT) static_##type##_Alignas
63 #define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)
70 CHECK_STATIC (int64_t);
73 CHECK_STATIC (double);
74 /* CHECK_STATIC (longdouble); */
75 CHECK_STATIC (struct1);
76 CHECK_STATIC (struct2);
77 CHECK_STATIC (struct3);
78 CHECK_STATIC (struct4);
83 #if defined __SUNPRO_C
84 /* Avoid a test failure due to Sun Studio Developer Bug Report #2125432. */
85 fputs ("Skipping test: known Sun C compiler bug\n", stderr);
88 CHECK_ALIGNED (static_char_alignas);
89 CHECK_ALIGNED (static_char_Alignas);
90 CHECK_ALIGNED (static_short_alignas);
91 CHECK_ALIGNED (static_short_Alignas);
92 CHECK_ALIGNED (static_int_alignas);
93 CHECK_ALIGNED (static_int_Alignas);
94 CHECK_ALIGNED (static_long_alignas);
95 CHECK_ALIGNED (static_long_Alignas);
97 CHECK_ALIGNED (static_int64_t_alignas);
98 CHECK_ALIGNED (static_int64_t_Alignas);
100 CHECK_ALIGNED (static_float_alignas);
101 CHECK_ALIGNED (static_float_Alignas);
102 CHECK_ALIGNED (static_double_alignas);
103 CHECK_ALIGNED (static_double_Alignas);
104 /* CHECK_ALIGNED (static_longdouble_alignas); */
105 /* CHECK_ALIGNED (static_longdouble_Alignas); */
106 CHECK_ALIGNED (static_struct1_alignas);
107 CHECK_ALIGNED (static_struct1_Alignas);
108 CHECK_ALIGNED (static_struct2_alignas);
109 CHECK_ALIGNED (static_struct2_Alignas);
110 CHECK_ALIGNED (static_struct3_alignas);
111 CHECK_ALIGNED (static_struct3_Alignas);
112 CHECK_ALIGNED (static_struct4_alignas);
113 CHECK_ALIGNED (static_struct4_Alignas);