From Jason Turner

[mdspan.accessor.aligned.overview]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpwo0wx6s8/{from.md → to.md} +88 -0
tmp/tmpwo0wx6s8/{from.md → to.md} RENAMED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###### Overview <a id="mdspan.accessor.aligned.overview">[[mdspan.accessor.aligned.overview]]</a>
2
+
3
+ ``` cpp
4
+ namespace std {
5
+ template<class ElementType, size_t ByteAlignment>
6
+ struct aligned_accessor {
7
+ using offset_policy = default_accessor<ElementType>;
8
+ using element_type = ElementType;
9
+ using reference = ElementType&;
10
+ using data_handle_type = ElementType*;
11
+
12
+ static constexpr size_t byte_alignment = ByteAlignment;
13
+
14
+ constexpr aligned_accessor() noexcept = default;
15
+ template<class OtherElementType, size_t OtherByteAlignment>
16
+ constexpr aligned_accessor(
17
+ aligned_accessor<OtherElementType, OtherByteAlignment>) noexcept;
18
+ template<class OtherElementType>
19
+ constexpr explicit aligned_accessor(default_accessor<OtherElementType>) noexcept;
20
+
21
+ template<class OtherElementType>
22
+ constexpr operator default_accessor<OtherElementType>() const noexcept;
23
+
24
+ constexpr reference access(data_handle_type p, size_t i) const noexcept;
25
+
26
+ constexpr typename offset_policy::data_handle_type offset(
27
+ data_handle_type p, size_t i) const noexcept;
28
+ };
29
+ }
30
+ ```
31
+
32
+ *Mandates:*
33
+
34
+ - `byte_alignment` is a power of two, and
35
+ - `byte_alignment >= alignof(ElementType)` is `true`.
36
+
37
+ `aligned_accessor` meets the accessor policy requirements.
38
+
39
+ `ElementType` is required to be a complete object type that is neither
40
+ an abstract class type nor an array type.
41
+
42
+ Each specialization of `aligned_accessor` is a trivially copyable type
43
+ that models `semiregular`.
44
+
45
+ \[`0`, n) is an accessible range for an object `p` of type
46
+ `data_handle_type` and an object of type `aligned_accessor` if and only
47
+ if
48
+
49
+ - \[`p`, `p + `n) is a valid range, and,
50
+ - if n is greater than zero, then
51
+ `is_sufficiently_aligned<byte_alignment>(p)` is `true`.
52
+
53
+ [*Example 1*:
54
+
55
+ The following function `compute` uses `is_sufficiently_aligned` to check
56
+ whether a given `mdspan` with `default_accessor` has a data handle with
57
+ sufficient alignment to be used with
58
+ `aligned_accessor<float, 4 * sizeof(float)>`. If so, the function
59
+ dispatches to a function `compute_using_fourfold_overalignment` that
60
+ requires fourfold over-alignment of arrays, but can therefore use
61
+ hardware-specific instructions, such as four-wide SIMD (Single
62
+ Instruction Multiple Data) instructions. Otherwise, `compute` dispatches
63
+ to a possibly less optimized function
64
+ `compute_without_requiring_overalignment` that has no over-alignment
65
+ requirement.
66
+
67
+ ``` cpp
68
+ void compute_using_fourfold_overalignment(
69
+ mdspan<float, dims<1>, layout_right, aligned_accessor<float, 4 * alignof(float)>> x);
70
+
71
+ void compute_without_requiring_overalignment(
72
+ mdspan<float, dims<1>, layout_right> x);
73
+
74
+ void compute(mdspan<float, dims<1>> x) {
75
+ constexpr auto byte_alignment = 4 * sizeof(float);
76
+ auto accessor = aligned_accessor<float, byte_alignment>{};
77
+ auto x_handle = x.data_handle();
78
+
79
+ if (is_sufficiently_aligned<byte_alignment>(x_handle)) {
80
+ compute_using_fourfold_overalignment(mdspan{x_handle, x.mapping(), accessor});
81
+ } else {
82
+ compute_without_requiring_overalignment(x);
83
+ }
84
+ }
85
+ ```
86
+
87
+ — *end example*]
88
+