alpaka
Abstraction Library for Parallel Kernel Acceleration
Align.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Benjamin Worpitz, RenĂ© Widera, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
8 
9 #include <cstddef>
10 #include <type_traits>
11 
12 namespace alpaka::core
13 {
14  //! Rounds to the next higher power of two (if not already power of two).
15  // Adapted from llvm/ADT/SmallPtrSet.h
16  template<std::size_t N>
17  struct RoundUpToPowerOfTwo;
18 
19  //! Defines implementation details that should not be used directly by the user.
20  namespace detail
21  {
22  //! Base case for N being a power of two.
23  template<std::size_t N, bool TisPowerTwo>
24  struct RoundUpToPowerOfTwoHelper : std::integral_constant<std::size_t, N>
25  {
26  };
27 
28  //! Case for N not being a power of two.
29  // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets
30  // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
31  template<std::size_t N>
32  struct RoundUpToPowerOfTwoHelper<N, false>
33  : std::integral_constant<std::size_t, RoundUpToPowerOfTwo<(N | (N - 1)) + 1>::value>
34  {
35  };
36  } // namespace detail
37 
38  template<std::size_t N>
40  : std::integral_constant<std::size_t, detail::RoundUpToPowerOfTwoHelper<N, (N & (N - 1)) == 0>::value>
41  {
42  };
43 
44  //! The alignment specifics.
45  namespace align
46  {
47  //! Calculates the optimal alignment for data of the given size.
48  template<std::size_t TsizeBytes>
50  : std::integral_constant<
51  std::size_t,
52 #if BOOST_COMP_GNUC
53  // GCC does not support alignments larger then 128: "warning: requested alignment 256 is larger
54  // than 128[-Wattributes]".
55  (TsizeBytes > 64) ? 128 :
56 #endif
57  (RoundUpToPowerOfTwo<TsizeBytes>::value)>
58  {
59  };
60  } // namespace align
61 } // namespace alpaka::core
62 
63 // The optimal alignment for a type is the next higher or equal power of two.
64 #define ALPAKA_OPTIMAL_ALIGNMENT(...) \
65  ::alpaka::core::align::OptimalAlignment<sizeof(std::remove_cv_t<__VA_ARGS__>)>::value
Rounds to the next higher power of two (if not already power of two).
Definition: Align.hpp:41
Calculates the optimal alignment for data of the given size.
Definition: Align.hpp:58
Base case for N being a power of two.
Definition: Align.hpp:25