1*700637cbSDimitry Andric // -*- C++ -*- 2*700637cbSDimitry Andric //===----------------------------------------------------------------------===// 3*700637cbSDimitry Andric // 4*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*700637cbSDimitry Andric // 8*700637cbSDimitry Andric // Kokkos v. 4.0 9*700637cbSDimitry Andric // Copyright (2022) National Technology & Engineering 10*700637cbSDimitry Andric // Solutions of Sandia, LLC (NTESS). 11*700637cbSDimitry Andric // 12*700637cbSDimitry Andric // Under the terms of Contract DE-NA0003525 with NTESS, 13*700637cbSDimitry Andric // the U.S. Government retains certain rights in this software. 14*700637cbSDimitry Andric // 15*700637cbSDimitry Andric //===---------------------------------------------------------------------===// 16*700637cbSDimitry Andric 17*700637cbSDimitry Andric #ifndef _LIBCPP___MDSPAN_ALIGNED_ACCESSOR_H 18*700637cbSDimitry Andric #define _LIBCPP___MDSPAN_ALIGNED_ACCESSOR_H 19*700637cbSDimitry Andric 20*700637cbSDimitry Andric #include <__config> 21*700637cbSDimitry Andric #include <__cstddef/size_t.h> 22*700637cbSDimitry Andric #include <__mdspan/default_accessor.h> 23*700637cbSDimitry Andric #include <__memory/assume_aligned.h> 24*700637cbSDimitry Andric #include <__type_traits/is_abstract.h> 25*700637cbSDimitry Andric #include <__type_traits/is_array.h> 26*700637cbSDimitry Andric #include <__type_traits/is_convertible.h> 27*700637cbSDimitry Andric #include <__type_traits/remove_const.h> 28*700637cbSDimitry Andric 29*700637cbSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 30*700637cbSDimitry Andric # pragma GCC system_header 31*700637cbSDimitry Andric #endif 32*700637cbSDimitry Andric 33*700637cbSDimitry Andric _LIBCPP_PUSH_MACROS 34*700637cbSDimitry Andric #include <__undef_macros> 35*700637cbSDimitry Andric 36*700637cbSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 37*700637cbSDimitry Andric 38*700637cbSDimitry Andric #if _LIBCPP_STD_VER >= 26 39*700637cbSDimitry Andric 40*700637cbSDimitry Andric template <class _ElementType, size_t _ByteAlignment> 41*700637cbSDimitry Andric struct aligned_accessor { 42*700637cbSDimitry Andric static_assert(_ByteAlignment != 0 && (_ByteAlignment & (_ByteAlignment - 1)) == 0, 43*700637cbSDimitry Andric "aligned_accessor: byte alignment must be a power of two"); 44*700637cbSDimitry Andric static_assert(_ByteAlignment >= alignof(_ElementType), "aligned_accessor: insufficient byte alignment"); 45*700637cbSDimitry Andric static_assert(!is_array_v<_ElementType>, "aligned_accessor: template argument may not be an array type"); 46*700637cbSDimitry Andric static_assert(!is_abstract_v<_ElementType>, "aligned_accessor: template argument may not be an abstract class"); 47*700637cbSDimitry Andric 48*700637cbSDimitry Andric using offset_policy = default_accessor<_ElementType>; 49*700637cbSDimitry Andric using element_type = _ElementType; 50*700637cbSDimitry Andric using reference = _ElementType&; 51*700637cbSDimitry Andric using data_handle_type = _ElementType*; 52*700637cbSDimitry Andric 53*700637cbSDimitry Andric static constexpr size_t byte_alignment = _ByteAlignment; 54*700637cbSDimitry Andric 55*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr aligned_accessor() noexcept = default; 56*700637cbSDimitry Andric 57*700637cbSDimitry Andric template <class _OtherElementType, size_t _OtherByteAlignment> 58*700637cbSDimitry Andric requires(is_convertible_v<_OtherElementType (*)[], element_type (*)[]> && _OtherByteAlignment >= byte_alignment) aligned_accessoraligned_accessor59*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr aligned_accessor(aligned_accessor<_OtherElementType, _OtherByteAlignment>) noexcept {} 60*700637cbSDimitry Andric 61*700637cbSDimitry Andric template <class _OtherElementType> requiresaligned_accessor62*700637cbSDimitry Andric requires(is_convertible_v<_OtherElementType (*)[], element_type (*)[]>) 63*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit constexpr aligned_accessor(default_accessor<_OtherElementType>) noexcept {} 64*700637cbSDimitry Andric 65*700637cbSDimitry Andric template <class _OtherElementType> requiresaligned_accessor66*700637cbSDimitry Andric requires(is_convertible_v<element_type (*)[], _OtherElementType (*)[]>) 67*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr operator default_accessor<_OtherElementType>() const noexcept { 68*700637cbSDimitry Andric return {}; 69*700637cbSDimitry Andric } 70*700637cbSDimitry Andric accessaligned_accessor71*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr reference access(data_handle_type __p, size_t __i) const noexcept { 72*700637cbSDimitry Andric return std::assume_aligned<byte_alignment>(__p)[__i]; 73*700637cbSDimitry Andric } 74*700637cbSDimitry Andric 75*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr typename offset_policy::data_handle_type offsetaligned_accessor76*700637cbSDimitry Andric offset(data_handle_type __p, size_t __i) const noexcept { 77*700637cbSDimitry Andric return std::assume_aligned<byte_alignment>(__p) + __i; 78*700637cbSDimitry Andric } 79*700637cbSDimitry Andric }; 80*700637cbSDimitry Andric 81*700637cbSDimitry Andric #endif // _LIBCPP_STD_VER >= 26 82*700637cbSDimitry Andric 83*700637cbSDimitry Andric _LIBCPP_END_NAMESPACE_STD 84*700637cbSDimitry Andric 85*700637cbSDimitry Andric _LIBCPP_POP_MACROS 86*700637cbSDimitry Andric 87*700637cbSDimitry Andric #endif // _LIBCPP___MDSPAN_ALIGNED_ACCESSOR_H 88