#include #include #include #include #include #include enum class ISA { SSE, AVX, AVX512, MIC }; template constexpr auto isa_props = 8UL; template <> constexpr auto isa_props = 16UL; template <> constexpr auto isa_props = 32UL; template <> constexpr auto isa_props = 64UL; template <> constexpr auto isa_props = 64UL; constexpr ISA target_isa = ISA::AVX512; template struct alignas(isa_props) simd_t { static_assert(isa_props % sizeof(T) == 0, "VL must be an integral multiple of type size"); static_assert(std::is_arithmetic_v, "T must be an arithmetic type"); static constexpr unsigned nelems = isa_props / sizeof(T); std::array / sizeof(T)> data; }; auto alignof_address(void* ptr) -> size_t { size_t numeric = size_t(ptr); size_t al = 1; while ((numeric & 1) == 0) { al <<= 1; numeric >>= 1; } return al; } auto main() -> int { std::cout << "Alignment for simd type (float) : " << alignof(simd_t) << " with " << simd_t::nelems << " elements in vector\n"; std::cout << "Alignment for simd type (double) : " << alignof(simd_t) << " with " << simd_t::nelems << " elements in vector\n"; std::unique_ptr> harr { new simd_t[40] }; std::cout << "Alignment for heap allocated array of simd_t : " << alignof_address(harr.get()) << '\n'; std::vector v(400, 0.); std::cout << "Alignment of data section of std:.vector on the heap = " << alignof_address(v.data()) << '\n'; std::unique_ptr darr { new double[40] }; std::cout << "Alignment for heap allocated array of ordinary double : " << alignof_address(darr.get()) << '\n'; darr.reset(new double[40]); std::cout << "Alignment for heap allocated array of ordinary double : " << alignof_address(darr.get()) << '\n'; std::cout << "Dynamic allocation using aligned_alloc ... \n"; darr.reset(static_cast(aligned_alloc(64, 40 * sizeof(double)))); std::cout << "Alignment for heap allocated array of ordinary double (specified: 64) : " << alignof_address(darr.get()) << '\n'; std::cout << "Dynamic allocation using C++17 overaligned new ... \n"; darr.reset(new (static_cast(64ul)) double[40]); std::cout << "Alignment for heap allocated array of ordinary double (specified: 64) : " << alignof_address(darr.get()) << '\n'; std::vector> w(4); std::cout << "Alignment of data section of std:.vector for simd_t = " << alignof_address(w.data()) << '\n'; }