1 /* Test of <stdalign.h>.
2 Copyright 2009-2011 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 # define TEST_ALIGNMENT 16
48 # define _Alignas(alignment)
49 # define alignas(alignment)
50 # define TEST_ALIGNMENT 1
53 #define CHECK_STATIC(type) \
54 typedef struct { char slot1; type slot2; } type##_helper; \
55 verify (alignof (type) == offsetof (type##_helper, slot2)); \
56 verify (_Alignof (type) == alignof (type)); \
57 const int type##_alignment = alignof (type); \
58 type alignas (TEST_ALIGNMENT) static_##type##_alignas; \
59 type _Alignas (TEST_ALIGNMENT) static_##type##_Alignas
61 #define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)
68 CHECK_STATIC (int64_t);
71 CHECK_STATIC (double);
72 CHECK_STATIC (longdouble);
73 CHECK_STATIC (struct1);
74 CHECK_STATIC (struct2);
75 CHECK_STATIC (struct3);
76 CHECK_STATIC (struct4);
81 CHECK_ALIGNED (static_char_alignas);
82 CHECK_ALIGNED (static_char_Alignas);
83 CHECK_ALIGNED (static_short_alignas);
84 CHECK_ALIGNED (static_short_Alignas);
85 CHECK_ALIGNED (static_int_alignas);
86 CHECK_ALIGNED (static_int_Alignas);
87 CHECK_ALIGNED (static_long_alignas);
88 CHECK_ALIGNED (static_long_Alignas);
90 CHECK_ALIGNED (static_int64_t_alignas);
91 CHECK_ALIGNED (static_int64_t_Alignas);
93 CHECK_ALIGNED (static_float_alignas);
94 CHECK_ALIGNED (static_float_Alignas);
95 CHECK_ALIGNED (static_double_alignas);
96 CHECK_ALIGNED (static_double_Alignas);
97 CHECK_ALIGNED (static_longdouble_alignas);
98 CHECK_ALIGNED (static_longdouble_Alignas);
99 CHECK_ALIGNED (static_struct1_alignas);
100 CHECK_ALIGNED (static_struct1_Alignas);
101 CHECK_ALIGNED (static_struct2_alignas);
102 CHECK_ALIGNED (static_struct2_Alignas);
103 CHECK_ALIGNED (static_struct3_alignas);
104 CHECK_ALIGNED (static_struct3_Alignas);
105 CHECK_ALIGNED (static_struct4_alignas);
106 CHECK_ALIGNED (static_struct4_Alignas);