Namespaces
Variants

std::simd::flags, std::simd::flag_default, std::simd::flag_convert, std::simd::flag_aligned, std::simd::flag_overaligned

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class... Flags >
struct flags;
(1) (since C++26)
inline constexpr flags<> flag_default {};
(2) (since C++26)
inline constexpr flags</*convert-flag*/> flag_convert {};
(3) (since C++26)
inline constexpr flags</*aligned-flag*/> flag_aligned {};
(4) (since C++26)
template< std::size_t N >
    requires (std::has_single_bit(N))
constexpr flags</*overaligned-flag*/<N>> flag_overaligned {};
(5) (since C++26)
Non-member functions
template< class... Other >
friend consteval auto operator|( flags a, flags<Other...> b );
(6) (since C++26)

Load and store flags are the last argument of the range constructor of std::simd::basic_vec and of the load, store, gather, and scatter functions. They allow conversions that are not value-preserving and state how the memory is aligned. The argument is optional, and its default is simd::flag_default.

1) The class template of the flag objects. Each type in Flags is one of the exposition-only types convert-flag, aligned-flag, and overaligned-flag<N>, which are the flags of (3-5).
2) No flags: a conversion between the element type of the memory and the element type of the basic_vec must be value-preserving, and the memory only needs the alignment of its element type.
3) Allows conversions that are not value-preserving. The elements are converted as if by static_cast.
4) States that the memory is aligned to simd::alignment_v<V, U>, where V is the basic_vec type and U is the element type of the memory.
5) States that the memory is aligned to N bytes.
If N is not a power of two, the program is ill-formed.
6) Returns a flags object that has every flag of a and every flag of b, so that simd::flag_convert | simd::flag_aligned allows conversions and states the alignment. If a and b contain the flags of flag_overaligned for two different N, the result can contain only the flag for the larger one.

If the memory is not aligned as stated by (4) or (5), the behavior is undefined.

Notes

Without (3), a load or store whose conversion is not value-preserving, such as from double or int to float, does not compile.

The alignment flags (4) and (5) do not align the memory: they promise that it is already aligned. Memory with that alignment can be declared with alignas, as in alignas(simd::alignment_v<V>) std::array<float, 64> a;.

Functions that pass the flags on to a load or store can take them as a parameter of type simd::flags<Flags...>, with Flags a template parameter pack and an empty default argument, as the standard library functions do.

GCC on x86-64:

  • loads and stores with movups (vmovups with AVX), which works at any address;
  • with (4), or with (5) for an N at least as large as the vector register, uses movaps (vmovaps) instead, which faults on memory that is not aligned, typically crashing the program. On most current processors, both are equally fast on aligned memory;
  • with SSE only, can also make an aligned load the memory operand of the instruction that uses it, as in addps xmm0, [rdi], saving an instruction. With AVX, loads become memory operands with or without a flag, and those do not fault on memory that is not aligned, so a broken alignment promise can go unnoticed;
  • with (3), converts with instructions such as cvtpd2ps (vcvtpd2ps with AVX) from double to float.
Feature-test macro Value Std Feature
__cpp_lib_simd 202411L (C++26) simd::flags, simd::flag_default, simd::flag_convert, simd::flag_aligned, and simd::flag_overaligned

Example

#include <array>
#include <print>
#include <simd>

namespace simd = std::simd;
using V = simd::vec<float, 8>;

// sums a contiguous range a chunk at a time, passing the caller's flags on
template<class... Flags>
float sum(const auto& r, simd::flags<Flags...> f = {})
{
    V acc{};
    for (std::size_t i = 0; i < r.size(); i += V::size())
        acc += simd::unchecked_load<V>(r.data() + i, V::size(), f);
    return simd::reduce(acc);
}

int main()
{
    alignas(simd::alignment_v<V>) std::array<float, 64> a;
    alignas(simd::alignment_v<V, double>) std::array<double, 64> d;
    for (int i = 0; i < 64; ++i)
    {
        a[i] = i * 0.5f;
        d[i] = i * 0.25;
    }
    std::println("{}", sum(a));
    std::println("{}", sum(a, simd::flag_aligned));
    // sum(d) does not compile: double to float is not value-preserving
    std::println("{}", sum(d, simd::flag_convert | simd::flag_aligned));

    alignas(64) std::array<float, 16> c; // aligned to a cache line
    c.fill(2.f);
    std::println("{}", sum(c, simd::flag_overaligned<64>));

    // a store that converts float to int, truncating each value
    std::array<int, 8> ints;
    V v([](int i) { return i * 1.5f; });
    simd::unchecked_store(v, ints, simd::flag_convert);
    std::println("{}", ints);
}

Output:

1008
1008
504
32
[0, 1, 3, 4, 6, 7, 9, 10]

See also

loads elements from a contiguous range to basic_vec
(function template) [edit]
stores elements from basic_vec to a contiguous range
(function template) [edit]
permutations of basic_vec from/to memory under optional write-mask
(function template) [edit]
obtains an appropriate alignment for simd::flag_aligned
(class template) [edit]