| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14 |
| 10 | |
| 11 | // <experimental/simd> |
| 12 | // |
| 13 | // [simd.traits] |
| 14 | // template <class T, class U = typename T::value_type> struct memory_alignment; |
| 15 | // template <class T, class U = typename T::value_type> |
| 16 | // inline constexpr std::size_t memory_alignment_v = memory_alignment<T, U>::value; |
| 17 | |
| 18 | #include "../test_utils.h" |
| 19 | |
| 20 | namespace ex = std::experimental::parallelism_v2; |
| 21 | |
| 22 | template <class T, std::size_t N> |
| 23 | struct CheckMemoryAlignmentMask { |
| 24 | template <class SimdAbi> |
| 25 | void operator()() { |
| 26 | LIBCPP_STATIC_ASSERT( |
| 27 | ex::memory_alignment<ex::simd_mask<T, SimdAbi>>::value == bit_ceil(sizeof(bool) * ex::simd_size_v<T, SimdAbi>)); |
| 28 | LIBCPP_STATIC_ASSERT( |
| 29 | ex::memory_alignment_v<ex::simd_mask<T, SimdAbi>> == bit_ceil(sizeof(bool) * ex::simd_size_v<T, SimdAbi>)); |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | template <class T, std::size_t N> |
| 34 | struct CheckMemoryAlignmentLongDouble { |
| 35 | template <class SimdAbi> |
| 36 | void operator()() { |
| 37 | if constexpr (std::is_same_v<T, long double>) { |
| 38 | // on i686-w64-mingw32-clang++, the size of long double is 12 bytes. Disambiguation is needed. |
| 39 | static_assert( |
| 40 | ex::memory_alignment<ex::simd<T, SimdAbi>>::value == bit_ceil(sizeof(T) * ex::simd_size_v<T, SimdAbi>)); |
| 41 | static_assert(ex::memory_alignment_v<ex::simd<T, SimdAbi>> == bit_ceil(sizeof(T) * ex::simd_size_v<T, SimdAbi>)); |
| 42 | } |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | struct CheckMemAlignmentFixedDeduce { |
| 47 | template <class T, std::size_t N> |
| 48 | void check() { |
| 49 | if constexpr (!std::is_same_v<T, long double>) { |
| 50 | static_assert(ex::memory_alignment_v<ex::simd<T, ex::simd_abi::fixed_size<N>>> == sizeof(T) * bit_ceil(val: N), |
| 51 | "Memory Alignment mismatch with abi fixed_size" ); |
| 52 | static_assert(ex::memory_alignment<ex::simd<T, ex::simd_abi::fixed_size<N>>>::value == sizeof(T) * bit_ceil(val: N), |
| 53 | "Memory Alignment mismatch with abi fixed_size" ); |
| 54 | |
| 55 | static_assert(ex::memory_alignment_v<ex::simd<T, ex::simd_abi::deduce_t<T, N>>> == sizeof(T) * bit_ceil(val: N), |
| 56 | "Memory Alignment mismatch with abi deduce" ); |
| 57 | static_assert(ex::memory_alignment<ex::simd<T, ex::simd_abi::deduce_t<T, N>>>::value == sizeof(T) * bit_ceil(val: N), |
| 58 | "Memory Alignment mismatch with abi deduce" ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | template <class T, std::size_t... N> |
| 63 | void performChecks(std::index_sequence<N...>) { |
| 64 | (check<T, N + 1>(), ...); |
| 65 | } |
| 66 | |
| 67 | template <class T> |
| 68 | void operator()() { |
| 69 | performChecks<T>(std::make_index_sequence<max_simd_size>{}); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | struct CheckMemAlignmentScalarNativeCompatible { |
| 74 | template <class T> |
| 75 | void operator()() { |
| 76 | if constexpr (!std::is_same_v<T, long double>) { |
| 77 | static_assert(ex::memory_alignment<ex::simd<T, ex::simd_abi::scalar>>::value == sizeof(T)); |
| 78 | static_assert(ex::memory_alignment_v<ex::simd<T, ex::simd_abi::scalar>> == sizeof(T)); |
| 79 | |
| 80 | LIBCPP_STATIC_ASSERT(ex::memory_alignment<ex::simd<T, ex::simd_abi::compatible<T>>>::value == 16); |
| 81 | LIBCPP_STATIC_ASSERT(ex::memory_alignment_v<ex::simd<T, ex::simd_abi::compatible<T>>> == 16); |
| 82 | |
| 83 | LIBCPP_STATIC_ASSERT( |
| 84 | ex::memory_alignment<ex::simd<T, ex::simd_abi::native<T>>>::value == _LIBCPP_NATIVE_SIMD_WIDTH_IN_BYTES); |
| 85 | LIBCPP_STATIC_ASSERT( |
| 86 | ex::memory_alignment_v<ex::simd<T, ex::simd_abi::native<T>>> == _LIBCPP_NATIVE_SIMD_WIDTH_IN_BYTES); |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | template <class T, class U = typename T::value_type, class = void> |
| 92 | struct has_memory_alignment : std::false_type {}; |
| 93 | |
| 94 | template <class T, class U> |
| 95 | struct has_memory_alignment<T, U, std::void_t<decltype(ex::memory_alignment<T, U>::value)>> : std::true_type {}; |
| 96 | |
| 97 | struct CheckMemoryAlignmentTraits { |
| 98 | template <class T> |
| 99 | void operator()() { |
| 100 | static_assert(has_memory_alignment<ex::native_simd<T>>::value); |
| 101 | static_assert(has_memory_alignment<ex::fixed_size_simd_mask<T, 4>>::value); |
| 102 | static_assert(has_memory_alignment<ex::native_simd<T>, T>::value); |
| 103 | static_assert(has_memory_alignment<ex::fixed_size_simd_mask<T, 4>, bool>::value); |
| 104 | |
| 105 | static_assert(!has_memory_alignment<T, T>::value); |
| 106 | static_assert(!has_memory_alignment<T, bool>::value); |
| 107 | static_assert(!has_memory_alignment<ex::native_simd<T>, bool>::value); |
| 108 | static_assert(!has_memory_alignment<ex::fixed_size_simd_mask<T, 4>, T>::value); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | int main(int, char**) { |
| 113 | types::for_each(arithmetic_no_bool_types(), CheckMemAlignmentFixedDeduce()); |
| 114 | types::for_each(arithmetic_no_bool_types(), CheckMemAlignmentScalarNativeCompatible()); |
| 115 | test_all_simd_abi<CheckMemoryAlignmentMask>(); |
| 116 | test_all_simd_abi<CheckMemoryAlignmentLongDouble>(); |
| 117 | types::for_each(arithmetic_no_bool_types(), CheckMemoryAlignmentTraits()); |
| 118 | return 0; |
| 119 | } |
| 120 | |