1639a0986SChristian Trott //===----------------------------------------------------------------------===// 2639a0986SChristian Trott // 3639a0986SChristian Trott // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4639a0986SChristian Trott // See https://llvm.org/LICENSE.txt for license information. 5639a0986SChristian Trott // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6639a0986SChristian Trott // 7639a0986SChristian Trott //===----------------------------------------------------------------------===// 8639a0986SChristian Trott 9639a0986SChristian Trott // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 10639a0986SChristian Trott 11639a0986SChristian Trott // <mdspan> 12639a0986SChristian Trott 13639a0986SChristian Trott // template<class OtherMapping> 14639a0986SChristian Trott // friend constexpr bool operator==(const mapping& x, const OtherMapping& y) noexcept; 15639a0986SChristian Trott // 16639a0986SChristian Trott // Constraints: 17639a0986SChristian Trott // - layout-mapping-alike<OtherMapping> is satisfied. 18639a0986SChristian Trott // - rank_ == OtherMapping::extents_type::rank() is true. 19639a0986SChristian Trott // - OtherMapping::is_always_strided() is true. 20639a0986SChristian Trott // 21639a0986SChristian Trott // Preconditions: OtherMapping meets the layout mapping requirements ([mdspan.layout.policy.reqmts]). 22639a0986SChristian Trott // 23639a0986SChristian Trott // Returns: true if x.extents() == y.extents() is true, OFFSET(y) == 0 is true, and each of x.stride(r) == y.stride(r) is true for r in the range [0, x.extents().rank()). Otherwise, false. 24639a0986SChristian Trott 25639a0986SChristian Trott #include <mdspan> 26639a0986SChristian Trott #include <cassert> 27*5e19fd17SLouis Dionne #include <concepts> 28*5e19fd17SLouis Dionne #include <span> // dynamic_extent 29*5e19fd17SLouis Dionne #include <type_traits> 30639a0986SChristian Trott 31639a0986SChristian Trott #include "test_macros.h" 32639a0986SChristian Trott 33639a0986SChristian Trott #include "../CustomTestLayouts.h" 34639a0986SChristian Trott 35639a0986SChristian Trott template <class E1, class E2> 36639a0986SChristian Trott concept layout_mapping_comparable = requires( 37639a0986SChristian Trott E1 e1, 38639a0986SChristian Trott E2 e2, 39639a0986SChristian Trott std::array<typename E1::index_type, E1::rank()> s1, 40639a0986SChristian Trott std::array<typename E1::index_type, E1::rank()> s2) { 41639a0986SChristian Trott std::layout_stride::mapping<E1>(e1, s1) == std::layout_stride::mapping<E2>(e2, s2); 42639a0986SChristian Trott }; 43639a0986SChristian Trott 44639a0986SChristian Trott template <class T1, class T2> 45639a0986SChristian Trott constexpr void test_comparison_different_rank() { 46639a0986SChristian Trott constexpr size_t D = std::dynamic_extent; 47639a0986SChristian Trott 48639a0986SChristian Trott // sanity check same rank 49639a0986SChristian Trott static_assert(layout_mapping_comparable<std::extents<T1, D>, std::extents<T2, D>>); 50639a0986SChristian Trott static_assert(layout_mapping_comparable<std::extents<T1, 5>, std::extents<T2, D>>); 51639a0986SChristian Trott static_assert(layout_mapping_comparable<std::extents<T1, D>, std::extents<T2, 5>>); 52639a0986SChristian Trott static_assert(layout_mapping_comparable<std::extents<T1, 5>, std::extents<T2, 5>>); 53639a0986SChristian Trott 54639a0986SChristian Trott // not equality comparable when rank is not the same 55639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1>, std::extents<T2, D>>); 56639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1>, std::extents<T2, 1>>); 57639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, D>, std::extents<T2>>); 58639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, 1>, std::extents<T2>>); 59639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, D>, std::extents<T2, D, D>>); 60639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, 5>, std::extents<T2, 5, D>>); 61639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, 5>, std::extents<T2, 5, 1>>); 62639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, D, D>, std::extents<T2, D>>); 63639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, 5, D>, std::extents<T2, 5>>); 64639a0986SChristian Trott static_assert(!layout_mapping_comparable<std::extents<T1, 5, 1>, std::extents<T2, 5>>); 65639a0986SChristian Trott } 66639a0986SChristian Trott 67639a0986SChristian Trott template <class To, class From> 68639a0986SChristian Trott constexpr void test_comparison( 69639a0986SChristian Trott bool equal, 70639a0986SChristian Trott To dest_exts, 71639a0986SChristian Trott From src_exts, 72639a0986SChristian Trott std::array<int, To::rank()> dest_strides, 73639a0986SChristian Trott std::array<int, From::rank()> src_strides) { 74639a0986SChristian Trott std::layout_stride::mapping<To> dest(dest_exts, dest_strides); 75639a0986SChristian Trott std::layout_stride::mapping<From> src(src_exts, src_strides); 76639a0986SChristian Trott ASSERT_NOEXCEPT(dest == src); 77639a0986SChristian Trott assert((dest == src) == equal); 78639a0986SChristian Trott assert((dest != src) == !equal); 79639a0986SChristian Trott } 80639a0986SChristian Trott 81639a0986SChristian Trott template <class T1, class T2> 82639a0986SChristian Trott constexpr void test_comparison_same_rank() { 83639a0986SChristian Trott constexpr size_t D = std::dynamic_extent; 84639a0986SChristian Trott 85639a0986SChristian Trott test_comparison(true, std::extents<T1>(), std::extents<T2>(), std::array<int, 0>{}, std::array<int, 0>{}); 86639a0986SChristian Trott 87639a0986SChristian Trott test_comparison(true, std::extents<T1, D>(5), std::extents<T2, D>(5), std::array<int, 1>{1}, std::array<int, 1>{1}); 88639a0986SChristian Trott test_comparison(true, std::extents<T1, D>(0), std::extents<T2, D>(0), std::array<int, 1>{1}, std::array<int, 1>{1}); 89639a0986SChristian Trott test_comparison(true, std::extents<T1, 5>(), std::extents<T2, D>(5), std::array<int, 1>{3}, std::array<int, 1>{3}); 90639a0986SChristian Trott test_comparison(true, std::extents<T1, D>(5), std::extents<T2, 5>(), std::array<int, 1>{1}, std::array<int, 1>{1}); 91639a0986SChristian Trott test_comparison(true, std::extents<T1, 5>(), std::extents< T2, 5>(), std::array<int, 1>{1}, std::array<int, 1>{1}); 92639a0986SChristian Trott test_comparison(false, std::extents<T1, 5>(), std::extents<T2, D>(5), std::array<int, 1>{2}, std::array<int, 1>{1}); 93639a0986SChristian Trott test_comparison(false, std::extents<T1, D>(5), std::extents<T2, D>(5), std::array<int, 1>{2}, std::array<int, 1>{1}); 94639a0986SChristian Trott test_comparison(false, std::extents<T1, D>(5), std::extents<T2, D>(7), std::array<int, 1>{1}, std::array<int, 1>{1}); 95639a0986SChristian Trott test_comparison(false, std::extents<T1, 5>(), std::extents<T2, D>(7), std::array<int, 1>{1}, std::array<int, 1>{1}); 96639a0986SChristian Trott test_comparison(false, std::extents<T1, D>(5), std::extents<T2, 7>(), std::array<int, 1>{1}, std::array<int, 1>{1}); 97639a0986SChristian Trott test_comparison(false, std::extents<T1, 5>(), std::extents<T2, 7>(), std::array<int, 1>{1}, std::array<int, 1>{1}); 98639a0986SChristian Trott 99639a0986SChristian Trott test_comparison( 100639a0986SChristian Trott true, 101639a0986SChristian Trott std::extents<T1, D, D, D, D, D>(5, 6, 7, 8, 9), 102639a0986SChristian Trott std::extents<T2, D, D, D, D, D>(5, 6, 7, 8, 9), 103639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}, 104639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}); 105639a0986SChristian Trott test_comparison( 106639a0986SChristian Trott true, 107639a0986SChristian Trott std::extents<T1, D, 6, D, 8, D>(5, 7, 9), 108639a0986SChristian Trott std::extents<T2, 5, D, D, 8, 9>(6, 7), 109639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}, 110639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}); 111639a0986SChristian Trott test_comparison( 112639a0986SChristian Trott true, 113639a0986SChristian Trott std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), 114639a0986SChristian Trott std::extents<T2, 5, 6, 7, 8, 9>(), 115639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}, 116639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}); 117639a0986SChristian Trott test_comparison( 118639a0986SChristian Trott false, 119639a0986SChristian Trott std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), 120639a0986SChristian Trott std::extents<T2, 5, 6, 7, 8, 9>(), 121639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 20000, 2000}, 122639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}); 123639a0986SChristian Trott test_comparison( 124639a0986SChristian Trott false, 125639a0986SChristian Trott std::extents<T1, D, D, D, D, D>(5, 6, 7, 8, 9), 126639a0986SChristian Trott std::extents<T2, D, D, D, D, D>(5, 6, 3, 8, 9), 127639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}, 128639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}); 129639a0986SChristian Trott test_comparison( 130639a0986SChristian Trott false, 131639a0986SChristian Trott std::extents<T1, D, 6, D, 8, D>(5, 7, 9), 132639a0986SChristian Trott std::extents<T2, 5, D, D, 3, 9>(6, 7), 133639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}, 134639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}); 135639a0986SChristian Trott test_comparison( 136639a0986SChristian Trott false, 137639a0986SChristian Trott std::extents<T1, 5, 6, 7, 8, 9>(5, 6, 7, 8, 9), 138639a0986SChristian Trott std::extents<T2, 5, 6, 7, 3, 9>(), 139639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}, 140639a0986SChristian Trott std::array<int, 5>{2, 20, 200, 2000, 20000}); 141639a0986SChristian Trott } 142639a0986SChristian Trott 143639a0986SChristian Trott template <class OtherLayout, class E1, class E2, class... OtherArgs> 144639a0986SChristian Trott constexpr void test_comparison_with( 145639a0986SChristian Trott bool expect_equal, E1 e1, std::array<typename E1::index_type, E1::rank()> strides, E2 e2, OtherArgs... other_args) { 14602d513cfSStephan T. Lavavej std::layout_stride::mapping<E1> map(e1, strides); 147639a0986SChristian Trott typename OtherLayout::template mapping<E2> other_map(e2, other_args...); 148639a0986SChristian Trott 149639a0986SChristian Trott assert((map == other_map) == expect_equal); 150639a0986SChristian Trott } 151639a0986SChristian Trott 152639a0986SChristian Trott template <class OtherLayout> 153639a0986SChristian Trott constexpr void test_comparison_with() { 154639a0986SChristian Trott constexpr size_t D = std::dynamic_extent; 155639a0986SChristian Trott bool is_left_based = 156639a0986SChristian Trott std::is_same_v<OtherLayout, std::layout_left> || std::is_same_v<OtherLayout, always_convertible_layout>; 157639a0986SChristian Trott test_comparison_with<OtherLayout>(true, std::extents<int>(), std::array<int, 0>{}, std::extents<unsigned>()); 158639a0986SChristian Trott test_comparison_with<OtherLayout>(true, std::extents<int, 5>(), std::array<int, 1>{1}, std::extents<unsigned, 5>()); 159639a0986SChristian Trott test_comparison_with<OtherLayout>(true, std::extents<int, D>(5), std::array<int, 1>{1}, std::extents<unsigned, 5>()); 160639a0986SChristian Trott test_comparison_with<OtherLayout>(false, std::extents<int, D>(5), std::array<int, 1>{2}, std::extents<unsigned, 5>()); 161639a0986SChristian Trott test_comparison_with<OtherLayout>( 162639a0986SChristian Trott is_left_based, std::extents<int, D, D>(5, 7), std::array<int, 2>{1, 5}, std::extents<unsigned, D, D>(5, 7)); 163639a0986SChristian Trott test_comparison_with<OtherLayout>( 164639a0986SChristian Trott !is_left_based, std::extents<int, D, D>(5, 7), std::array<int, 2>{7, 1}, std::extents<unsigned, D, D>(5, 7)); 165639a0986SChristian Trott test_comparison_with<OtherLayout>( 166639a0986SChristian Trott false, std::extents<int, D, D>(5, 7), std::array<int, 2>{8, 1}, std::extents<unsigned, D, D>(5, 7)); 167639a0986SChristian Trott 168639a0986SChristian Trott if constexpr (std::is_same_v<OtherLayout, always_convertible_layout>) { 169639a0986SChristian Trott // test layout with strides not equal to product of extents 170639a0986SChristian Trott test_comparison_with<OtherLayout>( 171639a0986SChristian Trott true, std::extents<int, D, D>(5, 7), std::array<int, 2>{2, 10}, std::extents<unsigned, D, D>(5, 7), 0, 2); 172639a0986SChristian Trott // make sure that offset != 0 results in false 173639a0986SChristian Trott test_comparison_with<OtherLayout>( 174639a0986SChristian Trott false, std::extents<int, D, D>(5, 7), std::array<int, 2>{2, 10}, std::extents<unsigned, D, D>(5, 7), 1, 2); 175639a0986SChristian Trott } 176639a0986SChristian Trott } 177639a0986SChristian Trott 178639a0986SChristian Trott template <class T1, class T2> 179639a0986SChristian Trott constexpr void test_comparison_index_type() { 180639a0986SChristian Trott test_comparison_same_rank<T1, T2>(); 181639a0986SChristian Trott test_comparison_different_rank<T1, T2>(); 182639a0986SChristian Trott test_comparison_with<std::layout_right>(); 183639a0986SChristian Trott test_comparison_with<std::layout_left>(); 184639a0986SChristian Trott test_comparison_with<always_convertible_layout>(); 185639a0986SChristian Trott } 186639a0986SChristian Trott 187639a0986SChristian Trott constexpr bool test() { 188639a0986SChristian Trott test_comparison_index_type<int, int>(); 189639a0986SChristian Trott test_comparison_index_type<int, size_t>(); 190639a0986SChristian Trott test_comparison_index_type<size_t, int>(); 191639a0986SChristian Trott test_comparison_index_type<size_t, long>(); 192639a0986SChristian Trott return true; 193639a0986SChristian Trott } 194639a0986SChristian Trott 195639a0986SChristian Trott int main(int, char**) { 196639a0986SChristian Trott test(); 197639a0986SChristian Trott static_assert(test()); 198639a0986SChristian Trott return 0; 199639a0986SChristian Trott } 200