Argon 0.1.0
Loading...
Searching...
No Matches
for_each.hpp File Reference

A loop over n elements a vector at a time, final partial vector included, shaped so that Helium compilers turn it into a low-overhead tail-predicated loop. More...

#include <algorithm>
#include <array>
#include <cstddef>
#include <type_traits>
#include "argon/argon_full.hpp"
#include "argon/store.hpp"
#include "argon/vectorize/first_n.hpp"
Include dependency graph for for_each.hpp:

Go to the source code of this file.

Classes

class  argon::vectorize::Step< ScalarType, full >
 One step of vectorize::for_each: the elements [index(), index() + count()) of the range. More...
class  argon::vectorize::InterleavedStep< ScalarType, Stride, full >
 One step of vectorize::for_each_interleaved: the frames [index(), index() + count()), each of Stride interleaved elements (a stereo pair, an RGB pixel), loaded as one vector per channel. More...

Namespaces

namespace  argon
 A 128-bit SIMD vector wrapping a scalar type, providing arithmetic, logical, and data-movement operations.

Functions

template<typename ScalarType, typename Body>
void argon::vectorize::for_each (std::ptrdiff_t n, Body &&body)
 Call body with a Step for each vector's worth of the elements [0, n), the final partial vector included.
template<typename ScalarType, size_t Stride, typename Body>
void argon::vectorize::for_each_interleaved (std::ptrdiff_t frames, Body &&body)
 Call body with an InterleavedStep for each vector's worth of the frames frames of Stride interleaved elements, the final partial group included.

Detailed Description

A loop over n elements a vector at a time, final partial vector included, shaped so that Helium compilers turn it into a low-overhead tail-predicated loop.

Function Documentation

◆ for_each()

template<typename ScalarType, typename Body>
void argon::vectorize::for_each ( std::ptrdiff_t n,
Body && body )
inline

Call body with a Step for each vector's worth of the elements [0, n), the final partial vector included.

Template Parameters
ScalarTypeThe lane type of the loop.
Parameters
nThe number of elements.
bodyA callable taking a Step by value, generic in it ([&](auto step) { ... }): on NEON it is called with full steps and then once with a partial one.

On Helium, GCC and clang compile this into a low-overhead tail-predicated loop (dlstp/letp), with no scalar epilogue: the loop is a signed counter, a vctp of the remaining count, and predicated accesses, the shape they recognise. (GCC does so for float loops only with -fno-trapping-math, since the inactive lanes compute too.) On NEON the whole vectors use plain loads and stores, and the last step loads and stores only its in-range lanes, branching on their count: active() is built only if the body asks for it.

step.Store(out, step.Load(a) + (step.Load(b) - step.Load(a)) * t);
});
void for_each(std::ptrdiff_t n, Body &&body)
Call body with a Step for each vector's worth of the elements [0, n), the final partial vector includ...
Definition for_each.hpp:132

◆ for_each_interleaved()

template<typename ScalarType, size_t Stride, typename Body>
void argon::vectorize::for_each_interleaved ( std::ptrdiff_t frames,
Body && body )
inline

Call body with an InterleavedStep for each vector's worth of the frames frames of Stride interleaved elements, the final partial group included.

Template Parameters
ScalarTypeThe element type.
Stride2, 3 or 4 elements per frame.
Parameters
bodyA callable taking an InterleavedStep by value, generic in it ([&](auto step) { ... }).

Whole groups use vld2-vld4 / vst2-vst4 in a low-overhead loop. The structured loads can't be predicated, so on NEON the last, partial group moves only its in-range frames with them, and on MVE it gathers and scatters each channel under a predicate (a dls/le loop rather than dlstp/letp). On MVE, which has no vld3, stride 3 gathers every group under a predicate instead, which can become a tail-predicated dlstp/letp loop. If every channel gets the same treatment, for_each over the interleaved data as flat elements is simpler and faster.

// swap the channels of interleaved stereo
auto [left, right] = step.Load(stereo);
step.Store(stereo, {right, left});
});
void for_each_interleaved(std::ptrdiff_t frames, Body &&body)
Call body with an InterleavedStep for each vector's worth of the frames frames of Stride interleaved ...
Definition for_each.hpp:258