Rearrangements¶
The rearrangements move lanes inside a register, or between two registers: reverse the lanes, interleave two registers, extract a window, broadcast a lane...
1. Whole Register, Whole Group¶
A rearrangement works on the whole register, and on the whole group of N × LMUL lanes for an LMUL above 1: reverse of a float32 register of LMUL 2 reverses its 2N lanes, the lanes of the second register coming first. This is the only semantics that does not depend on the size of the registers (SVE and RVV) and on the way an ISA builds the LMUL groups (one register of RVV, several registers elsewhere).
Most functions also have a variant by block of 128 bits, suffix _b128: the operation is applied in each block of 128 bits of the register, independently. It is the native semantics of many x86 instructions (unpcklps, shufps, palignr), and the right one for data made of small fixed blocks (4 × float32 coordinates, pairs of float64 complex numbers). When the register is not larger than 128 bits (SSE, NEON, half-vectors), f_b128 is f.
| Function | Result | _b128 |
|---|---|---|
reverse(a) |
a[N-1], ..., a[0] |
yes |
swap2(a) |
a1, a0, a3, a2, ... |
(pairs) |
dupeven(a) / dupodd(a) |
a0, a0, a2, a2, ... / a1, a1, a3, a3, ... |
(pairs) |
ziplo(a, b) / ziphi(a, b) |
a0, b0, a1, b1, ... from the lower / upper halves |
yes |
unziplo(a, b) / unziphi(a, b) |
even / odd lanes of a then b |
yes |
zip(a) / unzip(a) |
planar to interleaved layout and back, in one register | — |
slide(a, b, n) |
window of N lanes at n in a then b (0 ≤ n ≤ N) |
yes |
slideup(a, n) / slidedown(a, n) |
lanes moved up / down by n, zeros entering |
yes |
rotate(a, n) |
lane i receives a[(i + n) mod N] (0 ≤ n < N) |
yes |
bcast(a, k) |
a[k] in every lane (0 ≤ k < N) |
yes |
insert(a, x, k) |
a with the lane k replaced by the scalar x (0 ≤ k < N), the inverse of get |
— |
loadu_unzip(p) / storeu_zip(p, a) |
unzip(loadu(p)) / storeu(p, zip(a)) |
— |
loadu_unzip2(p, a, b) / storeu_zip2(p, a, b) |
the same with one register per field: N structures of 2 fields separated into a, b / stored from them |
— |
loadu_unzip3(p, a, b, c) / storeu_zip3(p, a, b, c) |
N structures of 3 fields separated into a, b, c / stored from them (also 4: 4 fields) |
— |
permute(a, idx) |
a[idx[0]], a[idx[1]], ... (indices < N) |
yes |
permute2(a, b, idx) |
the same in a then b (indices < 2N) |
— |
compress(a, m) / expand(a, m) |
active lanes packed first, zeros after / the inverse | — |
storeu_compress(p, a, m) / storeuf_compress(p, a, m) |
active lanes stored, their count returned | — |
loadu_expand(p, m) |
count(m) elements loaded into the active lanes |
— |
swap2, dupeven and dupodd work on pairs of lanes, which never straddle two blocks: they have no _b128 variant. The numbers of lanes (n, k) are counted in the whole group, or in a block for the _b128 variants; a value out of the range is undefined behavior. They are ordinary int32_t arguments: a constant or a variable (see §3.1 for the constants).
The mask variants apply the mask after the rearrangement (the merge masking keeps the lanes of the first register).
The names describe what happens to the lanes, not the instructions of one ISA (Naming Conventions). The closest instructions, for the names that differ:
| MIPP | x86 | ARM | RVV |
|---|---|---|---|
slide(a, b, n) |
alignr |
ext |
vslidedown + vslideup |
slideup / slidedown |
slli_si128 / srli_si128 (bytes) |
ext with zeros |
vslideup / vslidedown |
bcast(a, k) |
shuffle / permutexvar |
dup (element) |
vrgather |
ziplo / ziphi |
unpacklo / unpackhi |
zip1 / zip2 |
— |
unziplo / unziphi |
shuffle |
uzp1 / uzp2 |
— |
dupeven / dupodd |
moveldup / movehdup |
trn1(a, a) / trn2(a, a) |
— |
swap2 |
shuffle |
rev64 |
— |
loadu_unzip / storeu_zip, loadu_unzip2 / storeu_zip2 |
— | vld2 / vst2 |
vlseg2 / vsseg2 |
loadu_unzip3 / storeu_zip3 |
— | vld3 / vst3 |
vlseg3 / vsseg3 |
loadu_unzip4 / storeu_zip4 |
— | vld4 / vst4 |
vlseg4 / vsseg4 |
compress |
vcompressps (AVX-512) |
svcompact (SVE) |
vcompress |
2. Named Rearrangements¶
Two registers interleaved, then separated again. The interleaving of two registers of LMUL 1 is a register of LMUL 2: combine(ziplo(a, b), ziphi(a, b)), whose halves lo and hi give back a and b with unziplo and unziphi.
vd_float32_t a = mipp_load_float32(A);
vd_float32_t b = mipp_load_float32(B);
vd_float32_t r = mipp_reverse_float32(a); // a[N-1] ... a[0]
vd_float32_m2_t z = mipp_combine_float32(mipp_ziplo_float32(a, b), mipp_ziphi_float32(a, b)); // a0 b0 a1 b1 ...
vd_float32_t e = mipp_unziplo_float32(mipp_lo_float32_m2(z), mipp_hi_float32_m2(z)); // a again
vd_float32_t o = mipp_unziphi_float32(mipp_lo_float32_m2(z), mipp_hi_float32_m2(z)); // b again
mipp::vd<float> a = mipp::load<float>(A);
mipp::vd<float> b = mipp::load<float>(B);
mipp::vd<float> r = mipp::reverse(a); // a[N-1] ... a[0]
mipp::vd<float, 2> z = mipp::combine(mipp::ziplo(a, b), mipp::ziphi(a, b)); // a0 b0 a1 b1 ...
mipp::vd<float> e = mipp::unziplo(mipp::lo(z), mipp::hi(z)); // a again
mipp::vd<float> o = mipp::unziphi(mipp::lo(z), mipp::hi(z)); // b again
mipp::Vd<float> a(A);
mipp::Vd<float> b(B);
mipp::Vd<float> r = mipp::reverse(a); // a[N-1] ... a[0]
mipp::Vd<float, 2> z = mipp::combine(mipp::ziplo(a, b), mipp::ziphi(a, b)); // a0 b0 a1 b1 ...
mipp::Vd<float> e = mipp::unziplo(z.lo(), z.hi()); // a again
mipp::Vd<float> o = mipp::unziphi(z.lo(), z.hi()); // b again
3. Windows and Broadcasts¶
slide(a, b, n) is the alignr of x86 and the ext of ARM; rotate(a, n) is slide(a, a, n), and slidedown(a, n) is slide(a, 0, n).
3.1. Constant Counts¶
The number of lanes of slide, slideup, slidedown, rotate, bcast (and their _b128 variants), the lane of get and insert and the count of lshift / rshift / rshiftl / rshiftround / rotl / rotr (lshift_vs ... rotr_vs in C) are often constants. Many instructions only exist with a constant encoded in them, an immediate: palignr, pshufd, pextrd, pinsrd, insertps on x86, ext, dup (element), umov, ins (vsetq_lane), shl / sshr / srshr on NEON. The same function serves both cases:
- with a variable, the function runs its dynamic body:
slidethroughpermute2and indicesi + n(pshufb,vpermps,vpermt2ps,tbl),svspliceon SVE,vslidedown/vslideupon RVV; - with a constant, once the function is inlined (any optimization level but
-O0), the immediate instruction: the function tests__builtin_constant_p(n)and selects it at compile time, at no run-time cost. These functions are declaredMIPP_FORCE_INLINE(always_inlinewith GCC and Clang,__forceinlinewith MSVC), so that the constant always reaches the test, even in large translation units; defineMIPP_FORCE_INLINEasinlinebefore including MIPP to let the compiler choose.getneeds no such test: its dynamic body (store, then read of the lane) already becomes an extraction (pextrd,vextracti128,umov...) when the lane is a constant, with GCC as with Clang.
The C++ APIs also have a template version, mipp::slide<2>(a, b), mipp::get<3>(r), mipp::insert<3>(r, x), mipp::lshift<4>(r): the value must be a constant, and a value out of the range is a compilation error (static_assert). It calls the same function with the constant.
The immediate instructions are written for the unmasked functions of LMUL 1 on SSE, AVX2, AVX-512 and NEON (AArch64), where the instruction exists for the type. The masked variants call them; with an LMUL above 1, slide (and rotate, slideup, slidedown) slides the registers of the groups two by two with the LMUL 1 function when the count is a constant, and the other functions work register by register: the constant reaches the immediate instructions through the inlining. With -O0, or when the compiler does not inline the function, the dynamic body runs: the result is the same.
4. Interleaved Data¶
loadu_unzip loads N elements and separates the even and the odd ones: with LMUL 2, the two registers of LMUL 1 are the even and the odd elements of 2N elements, as ARM vld2. Below, the real and imaginary parts of N complex numbers.
// N pairs (re, im): the real parts in the lower half of the LMUL 2 register, the imaginary ones in the upper half
vd_float32_m2_t c = mipp_loadu_unzip_float32_m2(X);
vd_float32_t re = mipp_lo_float32_m2(c);
vd_float32_t im = mipp_hi_float32_m2(c);
vd_float32_t mag2 = mipp_fmadd_float32(re, re, mipp_mul_float32(im, im)); // re^2 + im^2
// N pairs (re, im): the real parts in the lower half of the LMUL 2 register, the imaginary ones in the upper half
mipp::vd<float, 2> c = mipp::loadu_unzip<float, 2>(X);
mipp::vd<float> re = mipp::lo(c);
mipp::vd<float> im = mipp::hi(c);
mipp::vd<float> mag2 = mipp::fmadd(re, re, mipp::mul(im, im)); // re^2 + im^2
loadu_unzip2(p, a, b) and storeu_zip2(p, a, b) do the same with one register per field: a receives the N even elements and b the N odd ones, as the two halves of the LMUL 2 register of loadu_unzip, without the LMUL 2 register.
The structures of 3 or 4 fields (RGB or RGBA pixels, xyz points...) have functions of the same form, which give one register per field: loadu_unzip3(p, a, b, c) loads N structures, 3N elements, and writes the field 0 of the N structures in a, the field 1 in b and the field 2 in c (the lane i of a is p[3i]); storeu_zip3(p, a, b, c) stores them back as N structures. loadu_unzip4 and storeu_zip4 do the same with 4 fields. The registers are written through pointers in C and through references in C++, where their type gives the datatype and the LMUL: with LMUL 2, the registers hold 2N structures. They are native on NEON (vld3 / vld4), SVE (svld3 / svld4) and RVV (vlseg3 / vlseg4, and a strided load per field when LMUL × fields > 8, where the segment loads do not exist: from LMUL 4 with 3 or 4 fields, at LMUL 8 with 2); the other ISAs use a gather per field. Below, N pixels (r, g, b) stored as (b, g, r).
5. Permutations¶
permute(a, idx) gives a[idx[i]] in the lane i. The indices are a register of unsigned integers of the width of the elements (uint32_t for float32), below N, the number of lanes of the whole group (2N for permute2, the number of lanes of a block for permute_b128); other values are undefined behavior. The indices of the 8-bit types cannot reach the lanes from 256.
6. Compress and Expand¶
compress(a, m) packs the active lanes of a at the beginning of the register, in order, and zeroes the other lanes; expand(a, m) is the inverse. count(m) is the number of active lanes. To store the result:
storeu_compress(p, a, m)writes exactly thecount(m)active lanes and returns this count;storeuf_compress(p, a, m)writes a whole register (the lanes after the count are unspecified) and returns the count: faster on the ISAs without a native masked store (SSE, AVX2, NEON). The memory of N elements frompmust be writable: in a filter loop, each call overwrites the extra elements of the previous one, and the output needs N elements of margin.
loadu_expand(p, m) reads exactly count(m) elements. These functions have no half-vector (d2) version.
// keeps the positive elements of X: OUT needs one register of margin (storeuf_compress)
float* out = OUT;
for (size_t i = 0; i < n; i += MIPP_N_FLOAT32) {
vd_float32_t v = mipp_loadu_float32(X + i);
vm_float32_t m = mipp_cmpgt_float32(v, mipp_set0_float32());
out += mipp_storeuf_compress_float32(out, v, m);
}
7. Transpositions¶
mipp::transpose<T>(t) transposes a square matrix held in registers, in place: t is an array of N registers (N = N<T, LMUL>()), the rows of the matrix, and t[i] then holds its column i (the lane j of t[i] is the former lane i of t[j]).
mipp::transpose_b128<T>(t) takes K = 16 / sizeof(T) registers (4 for float32, 8 for int16, 16 for int8) and transposes the K × K matrix of each block of 128 bits, independently: an AVX register holds two such matrices side by side. When the registers have 128 bits (SSE, NEON), it is transpose. Its usual use is the passage from an array of small structures (points x, y, z, w) to one register per field, and back. With registers of more than 128 bits, the points are not in the order of the memory in the registers of the fields, which does not matter to the operations lane by lane: the second transposition puts them back.
In C, there is a function per datatype and LMUL: mipp_transpose_float32(t), mipp_transpose_b128_int8_m2(t)... (no _d2 version of transpose_b128 when the registers have 128 bits). In C++, the number of registers is part of the type of the array: a wrong number does not compile (in C, an array parameter is a pointer). The Object API deduces T and LMUL (mipp::transpose(t)); the Functional API needs them, as mipp::dump.
// M: a matrix of n x n floats (n = MIPP_N_FLOAT32), row-major; MT: its transpose
vd_float32_t rows[MIPP_N_FLOAT32];
for (size_t i = 0; i < n; i++) rows[i] = mipp_loadu_float32(&M[i * n]);
mipp_transpose_float32(rows); // rows[i]: the column i of M
for (size_t i = 0; i < n; i++) mipp_storeu_float32(&MT[i * n], rows[i]);
// n points (x, y, z, w), one after the other in memory: x, y and z divided by w
vd_float32_t v[4];
for (size_t i = 0; i < 4; i++) v[i] = mipp_loadu_float32(&PTS[i * n]);
mipp_transpose_b128_float32(v); // v[0]: x of n points, v[1]: y, v[2]: z, v[3]: w
for (size_t c = 0; c < 3; c++) v[c] = mipp_div_float32(v[c], v[3]);
mipp_transpose_b128_float32(v); // back to (x, y, z, w)
for (size_t i = 0; i < 4; i++) mipp_storeu_float32(&PTS[i * n], v[i]);
// m: a matrix of n x n floats (n = N<float>()), row-major; mt: its transpose
mipp::vd<float> rows[mipp::N<float>()];
for (size_t i = 0; i < n; i++) rows[i] = mipp::loadu<float>(&m[i * n]);
mipp::transpose<float>(rows); // rows[i]: the column i of m
for (size_t i = 0; i < n; i++) mipp::storeu(&mt[i * n], rows[i]);
// n points (x, y, z, w), one after the other in memory: x, y and z divided by w
mipp::vd<float> v[4];
for (size_t i = 0; i < 4; i++) v[i] = mipp::loadu<float>(&pts[i * n]);
mipp::transpose_b128<float>(v); // v[0]: x of n points, v[1]: y, v[2]: z, v[3]: w
for (size_t c = 0; c < 3; c++) v[c] = mipp::div(v[c], v[3]);
mipp::transpose_b128<float>(v); // back to (x, y, z, w)
for (size_t i = 0; i < 4; i++) mipp::storeu(&pts[i * n], v[i]);
// m: a matrix of n x n floats (n = N<float>()), row-major; mt: its transpose
mipp::Vd<float> rows[mipp::N<float>()];
for (size_t i = 0; i < n; i++) rows[i] = mipp::Vd<float>(&m[i * n]);
mipp::transpose(rows); // rows[i]: the column i of m
for (size_t i = 0; i < n; i++) mipp::storeu(&mt[i * n], rows[i]);
// n points (x, y, z, w), one after the other in memory: x, y and z divided by w
mipp::Vd<float> v[4];
for (size_t i = 0; i < 4; i++) v[i] = mipp::Vd<float>(&pts[i * n]);
mipp::transpose_b128(v); // v[0]: x of n points, v[1]: y, v[2]: z, v[3]: w
for (size_t c = 0; c < 3; c++) v[c] = v[c] / v[3];
mipp::transpose_b128(v); // back to (x, y, z, w)
for (size_t i = 0; i < 4; i++) mipp::storeu(&pts[i * n], v[i]);
Both are log2(K) steps of K / 2 ziplo / ziphi (_b128 for transpose_b128), K log2(K) rearrangements: the usual transpositions of SSE, NEON, SVE and AVX-512 (8 unpcklps / zip1... for 4 × 4 float32). On AVX, where ziplo crosses the two blocks of 128 bits, transpose applies transpose_b128 to each half of the rows, then exchanges the halves of the registers (lo, hi, combine): 24 rearrangements for 8 × 8 float32 with AVX2 and Clang (unpcklps, unpckhps, vperm2f128, vinsertf128), as MIPPv1, against 52 with ziplo. The arrays grow with the number of lanes: from 32 registers or so (int8 on AVX2, int16 on AVX-512), they do not fit in the register file and live in memory.
The transpositions are written once, on top of the generated functions: they are in mipp/c/transpose.h, mipp/cpp/transpose.hpp and mipp/obj/transpose.hpp, included by mipp.h, mipp.hpp and mipp_obj.hpp but not by the per-function headers.
8. Patterns Known at Compile Time (C++17)¶
With C++17, mipp::permute<P>(a) and mipp::permute2<P>(a, b) take the indices from a pattern P: a type with a function static constexpr int get(int i, int n) that gives the source lane of the lane i of the result, n being the number of lanes of the whole group ([0, n) for permute, [0, 2n) for permute2), or -1 for a zero. The registers can be of any LMUL, half-vectors included. The number of lanes is a constant of the compilation on every ISA (SVE and RVV with a fixed register size), so the indices are known by the compiler:
- a pattern of a named rearrangement is replaced by its function:
reverse,rotate,bcast,slideup,slidedown,swap2,dupeven,dupoddfor one register;slide,ziplo,ziphi,unziplo,unziphifor two registers (andpermutewhen all the indices ofpermute2are in one register); - the other patterns are a
permute/permute2with a constant table of indices, then aselectfor the zeros; with 8-bit elements and more than 256 lanes (or 128 forpermute2), through memory.
An index out of [-1, n) ([-1, 2n) for permute2) is a compilation error. The patterns are an option of C++17: the C++11 API has the functions with indices in a register (§5). They are in mipp/cpp/patterns.hpp and mipp/obj/patterns.hpp, included by mipp.hpp and mipp_obj.hpp but not by the per-function headers: they pull about twenty rearrangement functions.
// lane i of the result: lane get(i, n) of the source (n lanes), -1 for a zero
struct reversed { static constexpr int get(int i, int n) { return n - 1 - i; } };
struct shifted { static constexpr int get(int i, int n) { return i + 1 < n ? i + 1 : -1; } };
struct mixed { static constexpr int get(int i, int n) { return i % 2 ? n + i : i; } }; // two registers
mipp::vd<float> a = mipp::load<float>(A);
mipp::vd<float> b = mipp::load<float>(B);
mipp::vd<float> r = mipp::permute<reversed>(a); // mipp::reverse(a)
mipp::vd<float> s = mipp::permute<shifted>(a); // mipp::slidedown(a, 1)
mipp::vd<float> m = mipp::permute2<mixed>(a, b); // a0 b1 a2 b3 ...: permute2 and a table
// lane i of the result: lane get(i, n) of the source (n lanes), -1 for a zero
struct reversed { static constexpr int get(int i, int n) { return n - 1 - i; } };
struct shifted { static constexpr int get(int i, int n) { return i + 1 < n ? i + 1 : -1; } };
struct mixed { static constexpr int get(int i, int n) { return i % 2 ? n + i : i; } }; // two registers
mipp::Vd<float> a(A);
mipp::Vd<float> b(B);
mipp::Vd<float> r = mipp::permute<reversed>(a); // mipp::reverse(a)
mipp::Vd<float> s = mipp::permute<shifted>(a); // mipp::slidedown(a, 1)
mipp::Vd<float> m = mipp::permute2<mixed>(a, b); // a0 b1 a2 b3 ...: permute2 and a table
9. Implementation Levels¶
The rearrangements are native on most ISAs: svrev, svzip1, svuzp1, svsplice, svdup_lane, svtbl, svcompact on SVE; vzip1q, vuzp1q, vtrn1q, vrev64q, vqtbl1q on NEON (AArch64); unpcklps, shufps, vpermps, vpermt2ps, pshufb, vcompressps on x86; vrgather, vcompress, viota, vslideup / vslidedown on RVV. permute of SSE and NEON computes the byte indices of the lanes for pshufb / vqtbl1q, and compress of SSE, AVX2 and NEON (32 and 64-bit elements) reads the shuffle of the mask in a small table of the headers. The others are generic (a slide through memory, get and set1 for a broadcast) or, for some types and ISAs, written lane by lane (Level 3); the levels of each function are in its reference page.
With a software LMUL (every ISA but RVV), the LMUL > 1 versions combine the LMUL 1 functions on the registers of the group (reverse of LMUL 2: the two registers swapped and reversed). The half-vectors of SSE, NEON and SVE (a register of which only the lower half is used) go through the scalar functions.