Blame gl/tests/test-stdalign.c

Packit aea12f
/* Test of <stdalign.h>.
Packit Service 991b93
   Copyright 2009-2020 Free Software Foundation, Inc.
Packit aea12f
Packit aea12f
   This program is free software: you can redistribute it and/or modify
Packit aea12f
   it under the terms of the GNU General Public License as published by
Packit aea12f
   the Free Software Foundation; either version 3 of the License, or
Packit aea12f
   (at your option) any later version.
Packit aea12f
Packit aea12f
   This program is distributed in the hope that it will be useful,
Packit aea12f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit aea12f
   GNU General Public License for more details.
Packit aea12f
Packit aea12f
   You should have received a copy of the GNU General Public License
Packit aea12f
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit aea12f
Packit aea12f
/* Written by Paul Eggert, inspired by Bruno Haible's test-alignof.c.  */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
Packit aea12f
#include <stdalign.h>
Packit aea12f
Packit aea12f
#include <stddef.h>
Packit aea12f
#include <stdint.h>
Packit aea12f
Packit aea12f
#include "verify.h"
Packit aea12f
Packit aea12f
#include "macros.h"
Packit aea12f
Packit aea12f
typedef long double longdouble;
Packit aea12f
typedef struct { char a[1]; } struct1;
Packit aea12f
typedef struct { char a[2]; } struct2;
Packit aea12f
typedef struct { char a[3]; } struct3;
Packit aea12f
typedef struct { char a[4]; } struct4;
Packit aea12f
Packit aea12f
verify (__alignof_is_defined == 1);
Packit aea12f
#ifndef alignof
Packit aea12f
# error "alignof is not a macro"
Packit aea12f
#endif
Packit aea12f
Packit aea12f
#if __alignas_is_defined
Packit aea12f
verify (__alignas_is_defined == 1);
Packit aea12f
# ifndef alignas
Packit aea12f
#  error "alignas is not a macro"
Packit aea12f
# endif
Packit aea12f
/* mingw can go up only to 8.  8 is all that GNU Emacs needs, so let's
Packit aea12f
   limit the test to 8 for now.  */
Packit aea12f
# define TEST_ALIGNMENT 8
Packit aea12f
#else
Packit aea12f
# define _Alignas(alignment)
Packit aea12f
# define alignas(alignment)
Packit aea12f
# define TEST_ALIGNMENT 1
Packit aea12f
#endif
Packit aea12f
Packit aea12f
#define CHECK_STATIC(type) \
Packit aea12f
  typedef struct { char slot1; type slot2; } type##_helper; \
Packit aea12f
  verify (alignof (type) == offsetof (type##_helper, slot2)); \
Packit aea12f
  verify (_Alignof (type) == alignof (type)); \
Packit aea12f
  const int type##_alignment = alignof (type); \
Packit aea12f
  type alignas (TEST_ALIGNMENT) static_##type##_alignas; \
Packit aea12f
  type _Alignas (TEST_ALIGNMENT) static_##type##_Alignas
Packit aea12f
Packit aea12f
#define CHECK_ALIGNED(var) ASSERT ((uintptr_t) &(var) % TEST_ALIGNMENT == 0)
Packit aea12f
Packit aea12f
CHECK_STATIC (char);
Packit aea12f
CHECK_STATIC (short);
Packit aea12f
CHECK_STATIC (int);
Packit aea12f
CHECK_STATIC (long);
Packit aea12f
#ifdef INT64_MAX
Packit aea12f
CHECK_STATIC (int64_t);
Packit aea12f
#endif
Packit aea12f
CHECK_STATIC (float);
Packit aea12f
CHECK_STATIC (double);
Packit aea12f
/* CHECK_STATIC (longdouble); */
Packit aea12f
CHECK_STATIC (struct1);
Packit aea12f
CHECK_STATIC (struct2);
Packit aea12f
CHECK_STATIC (struct3);
Packit aea12f
CHECK_STATIC (struct4);
Packit aea12f
Packit aea12f
int
Packit aea12f
main ()
Packit aea12f
{
Packit aea12f
#if defined __SUNPRO_C
Packit aea12f
  /* Avoid a test failure due to Sun Studio Developer Bug Report #2125432.  */
Packit aea12f
  fputs ("Skipping test: known Sun C compiler bug\n", stderr);
Packit aea12f
  return 77;
Packit aea12f
#elif defined __HP_cc && __ia64
Packit aea12f
  /* Avoid a test failure due to HP-UX Itanium cc bug; see:
Packit aea12f
     https://lists.gnu.org/r/bug-gnulib/2017-03/msg00078.html  */
Packit aea12f
  fputs ("Skipping test: known HP-UX Itanium cc compiler bug\n", stderr);
Packit aea12f
  return 77;
Packit aea12f
#else
Packit aea12f
  CHECK_ALIGNED (static_char_alignas);
Packit aea12f
  CHECK_ALIGNED (static_char_Alignas);
Packit aea12f
  CHECK_ALIGNED (static_short_alignas);
Packit aea12f
  CHECK_ALIGNED (static_short_Alignas);
Packit aea12f
  CHECK_ALIGNED (static_int_alignas);
Packit aea12f
  CHECK_ALIGNED (static_int_Alignas);
Packit aea12f
  CHECK_ALIGNED (static_long_alignas);
Packit aea12f
  CHECK_ALIGNED (static_long_Alignas);
Packit aea12f
# ifdef INT64_MAX
Packit aea12f
  CHECK_ALIGNED (static_int64_t_alignas);
Packit aea12f
  CHECK_ALIGNED (static_int64_t_Alignas);
Packit aea12f
# endif
Packit aea12f
  CHECK_ALIGNED (static_float_alignas);
Packit aea12f
  CHECK_ALIGNED (static_float_Alignas);
Packit aea12f
  CHECK_ALIGNED (static_double_alignas);
Packit aea12f
  CHECK_ALIGNED (static_double_Alignas);
Packit aea12f
  /* CHECK_ALIGNED (static_longdouble_alignas); */
Packit aea12f
  /* CHECK_ALIGNED (static_longdouble_Alignas); */
Packit aea12f
  CHECK_ALIGNED (static_struct1_alignas);
Packit aea12f
  CHECK_ALIGNED (static_struct1_Alignas);
Packit aea12f
  CHECK_ALIGNED (static_struct2_alignas);
Packit aea12f
  CHECK_ALIGNED (static_struct2_Alignas);
Packit aea12f
  CHECK_ALIGNED (static_struct3_alignas);
Packit aea12f
  CHECK_ALIGNED (static_struct3_Alignas);
Packit aea12f
  CHECK_ALIGNED (static_struct4_alignas);
Packit aea12f
  CHECK_ALIGNED (static_struct4_Alignas);
Packit aea12f
  return 0;
Packit aea12f
#endif
Packit aea12f
}