|
Argon 0.1.0
|
The Argon library provides several vectorized views for processing data using SIMD operations. These views work with contiguous ranges and provide iterator-based access to the vectorized data. For hot loops, and for Helium's hardware loops, see also `vectorize::for_each`.
The load view presents a read-only view of a contiguous set of data (i.e. a std::ranges::input_range). Load is lazy, and occurs only on dereference (and on every dereference, as it is not memoized).
The store presents a write-only view of a contiguous set of data (i.e. a std::ranges::output_range). StoreTo is lazy, and occurs only on incrementing or decrementing the iterator.
The load_store view allows you to process data in-place using SIMD operations. Internally, a vector is stored in the iterator that is loaded on creation, and then stored on increment, before the next element is loaded. Dereferencing accesses the internal stored vector, and will not Load again without a manual .reload() call.
Note: If you do not need both read and write on the same range, use either vectorize::load or vectorize::store instead, as vectorize::load_store incurs the read latency and writeback penalty of them combined.
For working with interleaved data (like RGB values), Argon provides specialized views:
for_each<T>(n, body) visits the elements [0, n) a vector at a time, final partial vector included. The body receives a step: step.Load(ptr) and step.Store(ptr, v) access its elements of any array, step.LoadWiden and step.StoreNarrow do so through a narrower integer type, and step.index(), step.count() and step.active() describe it. Lanes past the end of the range load as zero and are never written.
Write the body as a generic lambda (auto step): on NEON it runs with plain whole-vector steps and then once with a partial step, so the main loop has no predication at all. The partial step's loads and stores branch on its lane count, a scalar, rather than on the lanes of a predicate: reading a predicate's lanes back into core registers (vmov.32 r, d[k]) stalls an in-order core such as the Cortex-A9. step.active() still builds the predicate when the body asks for it. A CTest codegen check (tail_loops_stay_on_neon, on A9 and A7) keeps the tail free of those transfers.
On Helium every step is predicated, and the loop is written in the shape GCC and clang turn into a low-overhead tail-predicated loop (dlstp/letp): the hardware counts the remaining elements and predicates the last vector, with no compare-and-branch and no scalar epilogue. A CTest codegen check (tail_loops_use_dlstp) keeps it that way. GCC tail-predicates float loops only with -fno-trapping-math (or -ffast-math), because the inactive lanes compute too; without it the loop is still predicated, just with an explicit loop counter.
for_each_interleaved<T, Stride>(frames, body) does the same for frames of 2–4 interleaved elements (stereo samples, RGB pixels): step.Load(ptr) returns one vector per channel and step.Store(ptr, channels) interleaves them back.
Whole groups use vld2/vld3/vld4 and the matching stores. The structured loads can't be predicated: on NEON the last, partial group uses them on just its in-range frames (the low doubleword's worth at once, then one frame to a lane), and on Helium it gathers and scatters each channel under a predicate, so strides 2 and 4 make a dls/le loop rather than dlstp/letp. Stride 3 has no vld3 on MVE, so there every group is gathered under a predicate, which can tail-predicate: float frames do with both compilers, 8-bit ones with clang only.
Both handle every element, with the same results. Prefer for_each for hot loops: its loop is the tightest on every platform (on AArch64, ldr q/op/str q/cmp/bne), it reliably becomes a hardware loop on Helium, and it works over any number of arrays. Use the with_tail() views when you want a range — range-for, std::ranges algorithms, or adding tail handling to existing view-based code. Their loop checks for the partial vector on every iteration (a well-predicted branch), and on Helium whether they become dlstp/letp depends on the compiler: GCC 16 converts them for 16- and 32-bit lanes but not 8-bit ones, and clang 23 keeps them predicated but doesn't form a hardware loop.
If every channel of interleaved data gets the same treatment (a gain, a clamp, a mix), treat it as flat data: for_each<int16_t>(frames * 2, ...) needs no de-interleaving and keeps the hardware loop.
load, store and load_store skip any elements after the last whole vector. Call with_tail() on them to visit those too, as one final partial vector. Each element is then an argon::vectorize::Partial: *p is the vector, p.count() how many of its leading lanes are in the range, and p.active() those lanes as an argon::Predicate. Lanes past the end of the range load as zero and are never written.
On Helium (MVE) every vector is loaded and stored under a vctp predicate, so there is no scalar epilogue; whether the loop also becomes dlstp/letp depends on the compiler (see above). On NEON the whole vectors use plain loads and stores; only the final partial vector is loaded and stored in pieces, branching on p.count() (a doubleword at once, then lane by lane), and p.active() is built only when you call it.
load_interleaved, store_interleaved and interleaved have with_tail() too. Their lanes are frames of Stride elements, each element is a Partial whose value is one vector per channel, and elements that don't complete a frame are not visited. Whole groups use vld2/vld3/vld4 and the matching stores; since those can't be predicated, the final partial group uses them frame by frame on NEON, and is gathered and scattered per channel on Helium.
The views handle common errors gracefully: