align.h 3.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/
#ifndef SWIFT_ALIGN_H
#define SWIFT_ALIGN_H

22 23 24 25
/**
 * @brief The default struct alignment in SWIFT.
 */
#define SWIFT_STRUCT_ALIGNMENT 32
26

27 28 29
/**
 * @brief Defines alignment of structures
 */
30
#define SWIFT_STRUCT_ALIGN __attribute__((aligned(SWIFT_STRUCT_ALIGNMENT)))
31

32 33 34 35 36 37 38 39 40 41 42
/**
 * @brief The default cache alignment in SWIFT.
 */
#define SWIFT_CACHE_ALIGNMENT 64

/**
 * @brief Defines alignment of caches
 */
#define SWIFT_CACHE_ALIGN __attribute__((aligned(SWIFT_CACHE_ALIGNMENT)))

/**
Matthieu Schaller's avatar
Matthieu Schaller committed
43 44
 * @brief Macro to tell the compiler that a given array has the specified
 * alignment.
45 46
 *
 * Note that this turns into a no-op but gives information to the compiler.
47 48
 * For GCC versions older than 4.6 this is ignored as the builtin does not
 * exist.
49
 *
50
 * @param type The type of the array.
51 52 53 54
 * @param array The array.
 * @param alignment The alignment in bytes of the array.
 */
#if defined(__ICC)
Matthieu Schaller's avatar
Matthieu Schaller committed
55
#define swift_align_information(type, array, alignment) \
56
  __assume_aligned(array, alignment);
57
#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
Matthieu Schaller's avatar
Matthieu Schaller committed
58 59
#define swift_align_information(type, array, alignment) \
  array = (type *)__builtin_assume_aligned(array, alignment);
60
#else
61
#define swift_align_information(type, array, alignment) ;
62 63
#endif

64
/**
James Willis's avatar
James Willis committed
65 66 67
 * @brief Macro to create a restrict pointer to an array and tell the compiler
 * that the given array has the specified
 * alignment.
68 69 70 71 72 73 74 75
 *
 * Note that this turns into a no-op but gives information to the compiler.
 *
 * @param array The array.
 * @param ptr Pointer to array
 * @param type Type of array
 * @param alignment The alignment in bytes of the array.
 */
James Willis's avatar
James Willis committed
76 77
#define swift_declare_aligned_ptr(type, array, ptr, alignment) \
  type *restrict array = ptr;                                  \
78
  swift_align_information(type, array, alignment);
79

80
/**
Matthieu Schaller's avatar
Matthieu Schaller committed
81 82
 * @brief Macro to tell the compiler that a given number is 0 modulo a given
 * size.
83 84 85 86 87 88 89 90
 *
 * Note that this turns into a no-op but gives information to the compiler.
 * GCC does not have the equivalent built-in so defaults to nothing.
 *
 * @param var The variable
 * @param size The modulo of interest.
 */
#if defined(__ICC)
Matthieu Schaller's avatar
Matthieu Schaller committed
91
#define swift_assume_size(var, size) __assume(var % size == 0);
92
#else
Matthieu Schaller's avatar
Matthieu Schaller committed
93
#define swift_assume_size(var, size) ;
94 95
#endif

96
#endif /* SWIFT_ALIGN_H */