[mdspan.accessor.aligned.overview] (original) (raw)

[Example 1:

The following function computeuses is_sufficiently_aligned to check whether a given mdspan with default_accessor has a data handle with sufficient alignment to be used with aligned_accessor<float, 4 * sizeof(float)>.

If so, the function dispatches to a function compute_using_fourfold_overalignmentthat requires fourfold over-alignment of arrays, but can therefore use hardware-specific instructions, such as four-wide SIMD (Single Instruction Multiple Data) instructions.

Otherwise, compute dispatches to a possibly less optimized function compute_without_requiring_overalignmentthat has no over-alignment requirement.

void compute_using_fourfold_overalignment( std::mdspan<float, std::dims<1>, std::layout_right, std::aligned_accessor<float, 4 * alignof(float)>> x);void compute_without_requiring_overalignment( std::mdspan<float, std::dims<1>, std::layout_right> x);void compute(std::mdspan<float, std::dims<1>> x) { constexpr auto byte_alignment = 4 * sizeof(float);auto accessor = std::aligned_accessor<float, byte_alignment>{};auto x_handle = x.data_handle();if (std::is_sufficiently_aligned<byte_alignment>(x_handle)) { compute_using_fourfold_overalignment(std::mdspan{x_handle, x.mapping(), accessor});} else { compute_without_requiring_overalignment(x);} } — _end example_]