xref: /freebsd/contrib/llvm-project/libcxx/include/__mdspan/aligned_accessor.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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-exception
7 //
8 //                        Kokkos v. 4.0
9 //       Copyright (2022) National Technology & Engineering
10 //               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_H
18 #define _LIBCPP___MDSPAN_ALIGNED_ACCESSOR_H
19 
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_header
31 #endif
32 
33 _LIBCPP_PUSH_MACROS
34 #include <__undef_macros>
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 #if _LIBCPP_STD_VER >= 26
39 
40 template <class _ElementType, size_t _ByteAlignment>
41 struct 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)
aligned_accessoraligned_accessor59   _LIBCPP_HIDE_FROM_ABI constexpr aligned_accessor(aligned_accessor<_OtherElementType, _OtherByteAlignment>) noexcept {}
60 
61   template <class _OtherElementType>
requiresaligned_accessor62     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>
requiresaligned_accessor66     requires(is_convertible_v<element_type (*)[], _OtherElementType (*)[]>)
67   _LIBCPP_HIDE_FROM_ABI constexpr operator default_accessor<_OtherElementType>() const noexcept {
68     return {};
69   }
70 
accessaligned_accessor71   _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_type
offsetaligned_accessor76   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 >= 26
82 
83 _LIBCPP_END_NAMESPACE_STD
84 
85 _LIBCPP_POP_MACROS
86 
87 #endif // _LIBCPP___MDSPAN_ALIGNED_ACCESSOR_H
88