| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <stdalign.h> |
| 22 |
|
| 23 |
#include <stddef.h> |
| 24 |
#include <stdint.h> |
| 25 |
|
| 26 |
#include "verify.h" |
| 27 |
|
| 28 |
#include "macros.h" |
| 29 |
|
| 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; |
| 35 |
|
| 36 |
verify (__alignof_is_defined == 1); |
| 37 |
#ifndef alignof |
| 38 |
# error "alignof is not a macro" |
| 39 |
#endif |
| 40 |
|
| 41 |
#if __alignas_is_defined |
| 42 |
verify (__alignas_is_defined == 1); |
| 43 |
# ifndef alignas |
| 44 |
# error "alignas is not a macro" |
| 45 |
# endif |
| 46 |
|
| 47 |
|
| 48 |
# define TEST_ALIGNMENT 8 |
| 49 |
#else |
| 50 |
# define _Alignas(alignment) |
| 51 |
# define alignas(alignment) |
| 52 |
# define TEST_ALIGNMENT 1 |
| 53 |
#endif |
| 54 |
|
| 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 |
| 62 |
|
| 63 |
#define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0) |
| 64 |
|
| 65 |
CHECK_STATIC (char); |
| 66 |
CHECK_STATIC (short); |
| 67 |
CHECK_STATIC (int); |
| 68 |
CHECK_STATIC (long); |
| 69 |
#ifdef INT64_MAX |
| 70 |
CHECK_STATIC (int64_t); |
| 71 |
#endif |
| 72 |
CHECK_STATIC (float); |
| 73 |
CHECK_STATIC (double); |
| 74 |
|
| 75 |
CHECK_STATIC (struct1); |
| 76 |
CHECK_STATIC (struct2); |
| 77 |
CHECK_STATIC (struct3); |
| 78 |
CHECK_STATIC (struct4); |
| 79 |
|
| 80 |
int |
| 81 |
main () |
| 82 |
{ |
| 83 |
#if defined __SUNPRO_C |
| 84 |
|
| 85 |
fputs ("Skipping test: known Sun C compiler bug\n", stderr); |
| 86 |
return 77; |
| 87 |
#elif defined __HP_cc && __ia64 |
| 88 |
|
| 89 |
|
| 90 |
fputs ("Skipping test: known HP-UX Itanium cc compiler bug\n", stderr); |
| 91 |
return 77; |
| 92 |
#else |
| 93 |
CHECK_ALIGNED (static_char_alignas); |
| 94 |
CHECK_ALIGNED (static_char_Alignas); |
| 95 |
CHECK_ALIGNED (static_short_alignas); |
| 96 |
CHECK_ALIGNED (static_short_Alignas); |
| 97 |
CHECK_ALIGNED (static_int_alignas); |
| 98 |
CHECK_ALIGNED (static_int_Alignas); |
| 99 |
CHECK_ALIGNED (static_long_alignas); |
| 100 |
CHECK_ALIGNED (static_long_Alignas); |
| 101 |
# ifdef INT64_MAX |
| 102 |
CHECK_ALIGNED (static_int64_t_alignas); |
| 103 |
CHECK_ALIGNED (static_int64_t_Alignas); |
| 104 |
# endif |
| 105 |
CHECK_ALIGNED (static_float_alignas); |
| 106 |
CHECK_ALIGNED (static_float_Alignas); |
| 107 |
CHECK_ALIGNED (static_double_alignas); |
| 108 |
CHECK_ALIGNED (static_double_Alignas); |
| 109 |
|
| 110 |
|
| 111 |
CHECK_ALIGNED (static_struct1_alignas); |
| 112 |
CHECK_ALIGNED (static_struct1_Alignas); |
| 113 |
CHECK_ALIGNED (static_struct2_alignas); |
| 114 |
CHECK_ALIGNED (static_struct2_Alignas); |
| 115 |
CHECK_ALIGNED (static_struct3_alignas); |
| 116 |
CHECK_ALIGNED (static_struct3_Alignas); |
| 117 |
CHECK_ALIGNED (static_struct4_alignas); |
| 118 |
CHECK_ALIGNED (static_struct4_Alignas); |
| 119 |
return 0; |
| 120 |
#endif |
| 121 |
} |