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