Path: blob/main/contrib/llvm-project/libcxx/include/__mdspan/layout_left.h
35262 views
// -*- C++ -*-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-exception6//7// Kokkos v. 4.08// Copyright (2022) National Technology & Engineering9// Solutions of Sandia, LLC (NTESS).10//11// Under the terms of Contract DE-NA0003525 with NTESS,12// the U.S. Government retains certain rights in this software.13//14//===---------------------------------------------------------------------===//1516#ifndef _LIBCPP___MDSPAN_LAYOUT_LEFT_H17#define _LIBCPP___MDSPAN_LAYOUT_LEFT_H1819#include <__assert>20#include <__config>21#include <__fwd/mdspan.h>22#include <__mdspan/extents.h>23#include <__type_traits/is_constructible.h>24#include <__type_traits/is_convertible.h>25#include <__type_traits/is_nothrow_constructible.h>26#include <__utility/integer_sequence.h>27#include <array>28#include <cinttypes>29#include <cstddef>30#include <limits>3132#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)33# pragma GCC system_header34#endif3536_LIBCPP_PUSH_MACROS37#include <__undef_macros>3839_LIBCPP_BEGIN_NAMESPACE_STD4041#if _LIBCPP_STD_VER >= 234243template <class _Extents>44class layout_left::mapping {45public:46static_assert(__mdspan_detail::__is_extents<_Extents>::value,47"layout_left::mapping template argument must be a specialization of extents.");4849using extents_type = _Extents;50using index_type = typename extents_type::index_type;51using size_type = typename extents_type::size_type;52using rank_type = typename extents_type::rank_type;53using layout_type = layout_left;5455private:56_LIBCPP_HIDE_FROM_ABI static constexpr bool __required_span_size_is_representable(const extents_type& __ext) {57if constexpr (extents_type::rank() == 0)58return true;5960index_type __prod = __ext.extent(0);61for (rank_type __r = 1; __r < extents_type::rank(); __r++) {62bool __overflowed = __builtin_mul_overflow(__prod, __ext.extent(__r), &__prod);63if (__overflowed)64return false;65}66return true;67}6869static_assert(extents_type::rank_dynamic() > 0 || __required_span_size_is_representable(extents_type()),70"layout_left::mapping product of static extents must be representable as index_type.");7172public:73// [mdspan.layout.left.cons], constructors74_LIBCPP_HIDE_FROM_ABI constexpr mapping() noexcept = default;75_LIBCPP_HIDE_FROM_ABI constexpr mapping(const mapping&) noexcept = default;76_LIBCPP_HIDE_FROM_ABI constexpr mapping(const extents_type& __ext) noexcept : __extents_(__ext) {77// not catching this could lead to out-of-bounds access later when used inside mdspan78// mapping<dextents<char, 2>> map(dextents<char, 2>(40,40)); map(10, 3) == -12679_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(80__required_span_size_is_representable(__ext),81"layout_left::mapping extents ctor: product of extents must be representable as index_type.");82}8384template <class _OtherExtents>85requires(is_constructible_v<extents_type, _OtherExtents>)86_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)87mapping(const mapping<_OtherExtents>& __other) noexcept88: __extents_(__other.extents()) {89// not catching this could lead to out-of-bounds access later when used inside mdspan90// mapping<dextents<char, 2>> map(mapping<dextents<int, 2>>(dextents<int, 2>(40,40))); map(10, 3) == -12691_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(92__mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),93"layout_left::mapping converting ctor: other.required_span_size() must be representable as index_type.");94}9596template <class _OtherExtents>97requires(is_constructible_v<extents_type, _OtherExtents> && _OtherExtents::rank() <= 1)98_LIBCPP_HIDE_FROM_ABI constexpr explicit(!is_convertible_v<_OtherExtents, extents_type>)99mapping(const layout_right::mapping<_OtherExtents>& __other) noexcept100: __extents_(__other.extents()) {101// not catching this could lead to out-of-bounds access later when used inside mdspan102// Note: since this is constraint to rank 1, extents itself would catch the invalid conversion first103// and thus this assertion should never be triggered, but keeping it here for consistency104// layout_left::mapping<dextents<char, 1>> map(105// layout_right::mapping<dextents<unsigned, 1>>(dextents<unsigned, 1>(200))); map.extents().extent(0) ==106// -56107_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(108__mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),109"layout_left::mapping converting ctor: other.required_span_size() must be representable as index_type.");110}111112template <class _OtherExtents>113requires(is_constructible_v<extents_type, _OtherExtents>)114_LIBCPP_HIDE_FROM_ABI constexpr explicit(extents_type::rank() > 0)115mapping(const layout_stride::mapping<_OtherExtents>& __other) noexcept116: __extents_(__other.extents()) {117if constexpr (extents_type::rank() > 0) {118_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(119([&]() {120using _CommonType = common_type_t<typename extents_type::index_type, typename _OtherExtents::index_type>;121for (rank_type __r = 0; __r < extents_type::rank(); __r++)122if (static_cast<_CommonType>(stride(__r)) != static_cast<_CommonType>(__other.stride(__r)))123return false;124return true;125}()),126"layout_left::mapping from layout_stride ctor: strides are not compatible with layout_left.");127_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(128__mdspan_detail::__is_representable_as<index_type>(__other.required_span_size()),129"layout_left::mapping from layout_stride ctor: other.required_span_size() must be representable as "130"index_type.");131}132}133134_LIBCPP_HIDE_FROM_ABI constexpr mapping& operator=(const mapping&) noexcept = default;135136// [mdspan.layout.left.obs], observers137_LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __extents_; }138139_LIBCPP_HIDE_FROM_ABI constexpr index_type required_span_size() const noexcept {140index_type __size = 1;141for (size_t __r = 0; __r < extents_type::rank(); __r++)142__size *= __extents_.extent(__r);143return __size;144}145146template <class... _Indices>147requires((sizeof...(_Indices) == extents_type::rank()) && (is_convertible_v<_Indices, index_type> && ...) &&148(is_nothrow_constructible_v<index_type, _Indices> && ...))149_LIBCPP_HIDE_FROM_ABI constexpr index_type operator()(_Indices... __idx) const noexcept {150// Mappings are generally meant to be used for accessing allocations and are meant to guarantee to never151// return a value exceeding required_span_size(), which is used to know how large an allocation one needs152// Thus, this is a canonical point in multi-dimensional data structures to make invalid element access checks153// However, mdspan does check this on its own, so for now we avoid double checking in hardened mode154_LIBCPP_ASSERT_UNCATEGORIZED(__mdspan_detail::__is_multidimensional_index_in(__extents_, __idx...),155"layout_left::mapping: out of bounds indexing");156array<index_type, extents_type::rank()> __idx_a{static_cast<index_type>(__idx)...};157return [&]<size_t... _Pos>(index_sequence<_Pos...>) {158index_type __res = 0;159((__res = __idx_a[extents_type::rank() - 1 - _Pos] + __extents_.extent(extents_type::rank() - 1 - _Pos) * __res),160...);161return __res;162}(make_index_sequence<sizeof...(_Indices)>());163}164165_LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return true; }166_LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept { return true; }167_LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept { return true; }168169_LIBCPP_HIDE_FROM_ABI static constexpr bool is_unique() noexcept { return true; }170_LIBCPP_HIDE_FROM_ABI static constexpr bool is_exhaustive() noexcept { return true; }171_LIBCPP_HIDE_FROM_ABI static constexpr bool is_strided() noexcept { return true; }172173_LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const noexcept174requires(extents_type::rank() > 0)175{176// While it would be caught by extents itself too, using a too large __r177// is functionally an out of bounds access on the stored information needed to compute strides178_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(179__r < extents_type::rank(), "layout_left::mapping::stride(): invalid rank index");180index_type __s = 1;181for (rank_type __i = 0; __i < __r; __i++)182__s *= __extents_.extent(__i);183return __s;184}185186template <class _OtherExtents>187requires(_OtherExtents::rank() == extents_type::rank())188_LIBCPP_HIDE_FROM_ABI friend constexpr bool189operator==(const mapping& __lhs, const mapping<_OtherExtents>& __rhs) noexcept {190return __lhs.extents() == __rhs.extents();191}192193private:194_LIBCPP_NO_UNIQUE_ADDRESS extents_type __extents_{};195};196197#endif // _LIBCPP_STD_VER >= 23198199_LIBCPP_END_NAMESPACE_STD200201_LIBCPP_POP_MACROS202203#endif // _LIBCPP___MDSPAN_LAYOUT_LEFT_H204205206