Skip to content

Masking Semantics

MIPP provides first-class vector masking and predication across all supported architectures, unifying disparate hardware models under a single abstraction.


1. The Four Execution Variants

Every conditional vector operation in MIPP adheres to one of four standardized execution models. The figure shows a binary operation \(f(a, b)\) on four lanes with the mask 1 0 1 0: active lanes always receive \(f(a, b)\), the variant decides what inactive lanes receive.

The four masking variants: where each lane of the result comes from

1.1. Semantics

For any binary operation \(f(a, b)\) and mask vector \(m \in \{0, 1\}^N\):

Variant C++ Variant C API Suffix Masking behavior
Unmasked (no mask argument) or U (none) \(\text{res}_i = f(a_i, b_i)\)
Merge-Masked M _maskm \(\text{res}_i = \begin{cases} f(a_i, b_i) & \text{if } m_i = 1 \\ a_i & \text{if } m_i = 0 \end{cases}\)
Zero-Masked Z _maskz \(\text{res}_i = \begin{cases} f(a_i, b_i) & \text{if } m_i = 1 \\ 0 & \text{if } m_i = 0 \end{cases}\)
Source-Masked S _masks \(\text{res}_i = \begin{cases} f(a_i, b_i) & \text{if } m_i = 1 \\ \text{src}_i & \text{if } m_i = 0 \end{cases}\)

An unmasked operation is a call without a mask. In C++, the masked signatures also accept the mipp::U variant: the mask (and src) are ignored and the call behaves like the unmasked one, which is useful when the variant is a template parameter of generic code.

The reductions (hadd, hadds, hmul, hmin, hmax) return a scalar, so they only have the Zero-Masked variant (_maskz, mipp::Z). For them, an inactive lane does not count as 0: it is ignored, i.e. replaced by the neutral element of the reduction (0 for hadd and hadds, 1 for hmul, the largest value of the type for hmin, the lowest for hmax). When no lane is active, the result is this neutral element (FLT_MAX / -FLT_MAX for hmin / hmax on float, not infinities). argmin and argmax also have only the Zero-Masked variant: they return the index of the first active lane holding the minimum / maximum of the active lanes, and \(-1\) when no lane is active; their optional output s0 then receives the neutral element of hmin / hmax.

The dot products (dot2, dot4, dot4us) take a mask of the type of the accumulator vd0 (32-bit lanes): one lane of the mask per lane of the result, i.e. per group of 2 (dot2) or 4 (dot4, dot4us) elements of vd1 and vd2. Merge-Masked keeps vd0 in the inactive lanes (nothing is accumulated). To handle the tail of a loop over the 8 / 16-bit elements, mask the inputs instead: a Zero-Masked loadu of vd1 and vd2 sets the elements past the end to 0, and they add nothing to the dot product.

The memory accesses have a subset of the variants: the loads (load, loadu, gather) are Zero- and Source-Masked, the stores (store, storeu, storent, scatter) Merge- and Zero-Masked (see Memory & Alignment). The comparisons (cmpeq... cmpge) are Zero-Masked only (§4). The following functions have no masked variant: the conversions (cvt, cvtfloor, cvtceil, cvtround, cvtroundeven, wcvt, ncvt, ncvts, ncvtsrshiftround, cast), the widening operations (wmul, wpadd), the classifications (isnan, isinf, isfinite, signbit), frexp, select, the initializations (set, set1, set0, iota), get, getfirst, insert, lo, hi, combine, compress, expand, the special loads and stores (loadu_strided, storeu_strided, loadu_ff, loadu_unzip to loadu_unzip4, storeu_zip to storeu_zip4, loadu_expand, storeu_compress, storeuf_compress), and the functions on masks (the _k functions, tovd, tovm, testz, none, any, all, count, findfirst, findlast, firstn).

Relation to AVX-512 terminology

The names borrow the AVX-512 terms, with one difference: an AVX-512 merge-masked intrinsic (_mm512_mask_add_ps(src, k, a, b)) takes the inactive lanes from an explicit src, which is MIPP's Source-Masked variant. MIPP's Merge-Masked variant merges into the first operand, i.e. the AVX-512 form with src = a.

Per-Function Mask Support Matrix

Not every MIPP function supports all four masking variants across all datatypes and architectures. Exact mask variant availability for any specific function is documented in the auto-generated reference pages under Function Support (e.g. funcs_support/arithmetic/add.md).


2. Syntax in Each API

Functions encode the variant directly in their name (_maskm, _maskz, _masks: mask followed by the letter of the variant, as in C++):

// Mask generation via comparison:
vm_float32_t m = mipp_cmplt_float32(va, vb);

// 1. Unmasked:
vd_float32_t res_u = mipp_add_float32(va, vb);

// 2. Merge-Masked: inactive lanes keep va
vd_float32_t res_m = mipp_add_float32_maskm(m, va, vb);

// 3. Zero-Masked: inactive lanes set to 0
vd_float32_t res_z = mipp_add_float32_maskz(m, va, vb);

// 4. Source-Masked: inactive lanes taken from vsrc
vd_float32_t res_s = mipp_add_float32_masks(m, vsrc, va, vb);

The mask variant is supplied as the first template argument (<mipp::M>, <mipp::Z>, <mipp::S>). Because mipp::vd<T, LMUL> is a type alias to an internal type trait (vd_type<T, LMUL>::type), C++ template argument deduction cannot deduce T. Therefore, T (and LMUL if different from 1) must be explicitly specified:

// Mask generation via comparison:
mipp::vm<float> m = mipp::cmplt(va, vb);

// 1. Unmasked:
mipp::vd<float> res_u = mipp::add(va, vb);

// 2. Merge-Masked: inactive lanes keep va
mipp::vd<float> res_m = mipp::add<mipp::M, float>(m, va, vb);

// 3. Zero-Masked: inactive lanes set to 0
mipp::vd<float> res_z = mipp::add<mipp::Z, float>(m, va, vb);

// 4. Source-Masked: inactive lanes taken from vsrc
mipp::vd<float> res_s = mipp::add<mipp::S, float>(m, vsrc, va, vb);

LMUL in C++ Functional API Calls

When \(\text{LMUL}=1\), it can be omitted (mipp::add<mipp::M, float>(m, va, vb)). For multi-register operations (\(\text{LMUL} > 1\)), both T and LMUL must be specified explicitly (e.g. mipp::add<mipp::M, float, 2>(m, va, vb)).

Because mipp::Vd<T, LMUL> and mipp::Vm<T, LMUL> are class templates rather than type aliases, the compiler automatically deduces both T and LMUL from the argument objects. Masked operations only require the variant template argument:

// Mask generation via comparison:
mipp::Vm<float> m = (va < vb);

// 1. Unmasked:
mipp::Vd<float> res_u = va + vb;

// 2. Merge-Masked: inactive lanes keep va
mipp::Vd<float> res_m = mipp::add<mipp::M>(m, va, vb);

// 3. Zero-Masked: inactive lanes set to 0
mipp::Vd<float> res_z = mipp::add<mipp::Z>(m, va, vb);

// 4. Source-Masked: inactive lanes taken from vsrc
mipp::Vd<float> res_s = mipp::add<mipp::S>(m, vsrc, va, vb);

Concise Mask Syntax & Automatic Template Deduction

With using mipp::M; (or Z, S), you can write clean, expressive calls without repeating datatypes or namespaces:

using mipp::M;
mipp::Vd<float> res = mipp::add<M>(m, va, vb);

The element datatype T and the register multiplier LMUL are automatically deduced from the Vm and Vd operand objects.


3. Architectural Hardware Mappings

The mask type follows the hardware of each target. How a masked operation is executed depends on the target and on the function: either a native predicated instruction (L0), or the unmasked operation followed by a select (L2), or the scalar fallback (L3):

Architecture Mask Representation in MIPP Execution of Masked Operations
RISC-V Vector 1.0 Boolean mask registers (vbool*_t). Native predicated instructions (vadd.vv ..., v0.t, _mu policy) for most functions: arithmetic, FMA, logic, shifts, clamp, copysign, the gathers of the rearrangements, reductions, memory accesses. The slides, zip / unzip and the compositions (math) run the unmasked operation, then vmerge.
x86 AVX-512 Opmask registers (__mmask8 ... __mmask64). Native masked intrinsics (_mm512_mask_add_ps, _mm512_maskz_permutexvar_ps...) for the functions whose instruction has a masked form at the granularity of the lanes. The others (math, complex numbers, functions with an immediate count) run the unmasked operation, then a select, which GCC and Clang fold into the masked instruction when the last instruction has one.
ARM SVE Predicate registers (svbool_t). Predicated instruction forms (svadd_f32_m, svnot_u32_z...) for the functions that have one. The rearrangements (svtbl, svzip1...) have none: unmasked operation, then svsel.
x86 AVX / AVX2 Full-vector masks: all bits of a lane set (active) or cleared (inactive). Unmasked operation, then _mm256_blendv_* (Merge- and Source-Masked) or an AND with the mask (Zero-Masked).
ARM NEON Full-vector masks (uint32x4_t, ...). Unmasked operation, then vbslq_* (Merge- and Source-Masked) or an AND with the mask (Zero-Masked).
x86 SSE Full-vector masks (__m128, __m128d, __m128i). Unmasked operation, then _mm_blendv_* (SSE4.1) or AND / ANDNOT (SSE2) (Merge- and Source-Masked), or an AND with the mask (Zero-Masked).
Scalar target Arrays of unsigned integers of the element width (uint32_t m[N], ...). Loop: res[i] = m[i] ? f(a[i], b[i]) : a[i].

The level of each masked function on each target is in the Merge-Masked, Zero-Masked and Source-Masked tabs of the Hardware Support pages (e.g. AVX-512).

Hardware and MIPP limitations

  • SSE, AVX and NEON have no predicated arithmetic: a blend (blendv, vbsl) after the unmasked operation is the expected implementation. The Zero-Masked variant needs no blend: it is the unmasked operation followed by an AND with the mask (andb(op, tovd(m))).
  • On AVX-512, SVE and RVV, a masked function without a predicated form of its instruction runs the unmasked operation, then a select; the Zero-Masked variant selects zeros (select(m, op, set0())), which costs one instruction on SVE and RVV and is folded into the zero-masked instruction ({z}) on AVX-512.

4. Mask Generation & Relational Comparisons

Masks are generated by element-wise comparison primitives returning a vm type:

Operation C API Function C++ Functional API C++ Object API Semantics
Equal mipp_cmpeq_[type]_[lmul] mipp::cmpeq(a, b) a == b \(m_i = (a_i == b_i)\)
Not Equal mipp_cmpneq_[type]_[lmul] mipp::cmpneq(a, b) a != b \(m_i = (a_i \neq b_i)\)
Less Than mipp_cmplt_[type]_[lmul] mipp::cmplt(a, b) a < b \(m_i = (a_i < b_i)\)
Less or Equal mipp_cmple_[type]_[lmul] mipp::cmple(a, b) a <= b \(m_i = (a_i \le b_i)\)
Greater Than mipp_cmpgt_[type]_[lmul] mipp::cmpgt(a, b) a > b \(m_i = (a_i > b_i)\)
Greater or Equal mipp_cmpge_[type]_[lmul] mipp::cmpge(a, b) a >= b \(m_i = (a_i \ge b_i)\)

The comparisons also have a Zero-Masked variant (_maskz, mipp::Z): the inactive lanes of the result mask are cleared, i.e. the result is andb_k(cmp(a, b), m).


5. Mask Boolean Algebra

Masks can be combined, inverted, and manipulated using boolean logical operations. In the C API, the mask functions carry the _k suffix, which distinguishes them from the bitwise operations on data vectors. The C++ Object API provides operators on Vm objects (~m1 can also be written !m1), except for AND-NOT, which remains a free function (see Programming Model).

vm_float32_t vm1 = mipp_cmplt_float32(va, vb);
vm_float32_t vm2 = mipp_cmpgt_float32(va, vc);

// Logical AND (vm1 AND vm2):
vm_float32_t m_and = mipp_andb_k_float32(vm1, vm2);

// Logical OR (vm1 OR vm2):
vm_float32_t m_or  = mipp_orb_k_float32(vm1, vm2);

// Logical XOR (vm1 XOR vm2):
vm_float32_t m_xor = mipp_xorb_k_float32(vm1, vm2);

// Logical NOT (~vm1):
vm_float32_t m_not = mipp_notb_k_float32(vm1);

// Logical AND-NOT ((NOT vm1) AND vm2):
vm_float32_t m_andn = mipp_andnb_k_float32(vm1, vm2);
mipp::vm<float> vm1 = mipp::cmplt(va, vb);
mipp::vm<float> vm2 = mipp::cmpgt(va, vc);

// Logical AND (vm1 AND vm2):
mipp::vm<float> m_and = mipp::andb(vm1, vm2);

// Logical OR (vm1 OR vm2):
mipp::vm<float> m_or  = mipp::orb(vm1, vm2);

// Logical XOR (vm1 XOR vm2):
mipp::vm<float> m_xor = mipp::xorb(vm1, vm2);

// Logical NOT (~vm1):
mipp::vm<float> m_not = mipp::notb(vm1);

// Logical AND-NOT ((NOT vm1) AND vm2):
mipp::vm<float> m_andn = mipp::andnb(vm1, vm2);
mipp::Vm<float> vm1 = (va < vb);
mipp::Vm<float> vm2 = (va > vc);

// Logical AND (vm1 AND vm2):
mipp::Vm<float> m_and = vm1 & vm2;

// Logical OR (vm1 OR vm2):
mipp::Vm<float> m_or  = vm1 | vm2;

// Logical XOR (vm1 XOR vm2):
mipp::Vm<float> m_xor = vm1 ^ vm2;

// Logical NOT (~vm1):
mipp::Vm<float> m_not = ~vm1;

// Logical AND-NOT ((NOT vm1) AND vm2):
mipp::Vm<float> m_andn = mipp::andnb(vm1, vm2);

6. Mask Conversion & Reinterpretation

MIPP provides explicit primitives to convert between vector registers (vd), mask registers (vm), and scalar representations:

6.1. Strong Mask Typing & cast_k (Mask-to-Mask Conversion)

In MIPP, mask types are strictly and strongly typed to their corresponding numeric datatype \(T\). For example, a vm<float> produced by comparing floating-point vectors cannot be implicitly passed to an integer operation expecting a vm<int32_t>, despite sharing the same 32-bit element size and element capacity.

To convert between mask types, MIPP provides explicit mask casting primitives (cast_k in C, cast_[type] in the C++ Functional API, cast<T> in the C++ Object API; the C++ functions are overloaded on masks and data vectors):

vm_float32_t m_float = mipp_cmplt_float32(va, vb);
vm_int32_t   m_int   = mipp_cast_k_float32_int32(m_float);
mipp::vm<float>   m_float = mipp::cmplt(va, vb);
mipp::vm<int32_t> m_int   = mipp::cast_int32(m_float);
mipp::Vm<float>   m_float = (va < vb);
mipp::Vm<int32_t> m_int   = mipp::cast<int32_t>(m_float);

Semantics

cast_k / cast_[type] is strictly an in-register bit reinterpretation (e.g., _mm256_castps_si256, __riscv_vreinterpret, or direct struct assignment) between mask types of matching element bitwidths (e.g. float32 \(\leftrightarrow\) int32 \(\leftrightarrow\) uint32, or float64 \(\leftrightarrow\) int64 \(\leftrightarrow\) uint64). For data vectors, see Type Conversions (cast, cvt, wcvt).

6.2. tovm (Data Vector to Mask)

Converts a data vector vd<T> into a mask register vm<T> of the matching type \(T\):

vd_float32_t raw_vector = mipp_load_float32(ptr);
vm_float32_t m = mipp_tovm_float32(raw_vector);
mipp::vd<float> raw_vector = mipp::load<float>(ptr);
mipp::vm<float> m = mipp::tovm(raw_vector);
mipp::Vd<float> raw_vector = mipp::load_obj<float>(ptr);
mipp::Vm<float> m = mipp::tovm(raw_vector);

A lane is active if its element has at least one bit set, inactive if all its bits are zero. The test is on the bit pattern, not on the value: -0.0 (sign bit set) and NaN give an active lane, +0.0 an inactive one. Any input is valid, and the result is always a well-formed mask (on targets that store masks in vector registers, each active lane has all its bits set), so tovm(tovd(m)) returns m and the mask algebra of §5 applies to the result. The behavior is the same on every target.

The converse does not hold in general: tovd(tovm(r)) returns r only if each lane of r is already 0 or has all its bits set. Any other non-zero lane becomes "all bits set": 1.0f gives a NaN pattern, the integer 1 gives -1.

6.3. tovd (Mask to Data Vector)

Reinterprets/converts a mask handle vm<T> into a full data vector vd<T> of the matching type \(T\) (where active lanes contain all-ones and inactive lanes contain 0):

vm_float32_t m = mipp_cmpeq_float32(va, vb);
vd_float32_t mask_vector = mipp_tovd_float32(m);
mipp::vm<float> m = mipp::cmpeq(va, vb);
mipp::vd<float> mask_vector = mipp::tovd(m);
mipp::Vm<float> m = (va == vb);
mipp::Vd<float> mask_vector = mipp::tovd(m);

6.4. select (Conditional Element Multiplexing)

Selects elements between two data vectors according to a mask (\(r_0\) when \(m_i = 1\), \(r_1\) when \(m_i = 0\)):

\[\text{res}_i = \begin{cases} r0_i & \text{if } m_i = 1 \\ r1_i & \text{if } m_i = 0 \end{cases}\]

The mask m is passed as the first argument, and the value for the active lanes comes first: select(m, vd0, vd1) reads as m ? vd0 : vd1. The x86 intrinsic _mm_blendv_ps(a, b, m) uses the opposite order (m ? b : a).

// Arguments: mask, if_true, if_false
vd_float32_t chosen = mipp_select_float32(m, if_true, if_false);
// Arguments: mask, if_true, if_false
mipp::vd<float> chosen = mipp::select(m, if_true, if_false);
// Arguments: mask, if_true, if_false
mipp::Vd<float> chosen = mipp::select(m, if_true, if_false);

7. Mask Initialization (set_k, set1_k, set0_k)

MIPP provides functions to construct and initialize mask registers from scalar values.

7.1. Function Signatures & Semantics

Function C API Function C++ Functional API C++ Object API Semantics
Array Init mipp_set_k_[type]_[lmul](vals) (int32_t vals[]) mipp::set_k<T, LMUL>(vals) (bool vals[]) mipp::set_k_obj<T, LMUL>(vals) or Vm<T, LMUL>(vals) (bool vals[]) Sets element \(i\) active if vals[i] is true (!= 0 in C), inactive otherwise.
Broadcast mipp_set1_k_[type]_[lmul](val) (int32_t val) mipp::set1_k<T, LMUL>(b) (bool b) mipp::set1_k_obj<T, LMUL>(b) or Vm<T, LMUL>(b) (bool b) Broadcasts: all elements active if b is true (val != 0 in C), else inactive.
Zero Init mipp_set0_k_[type]_[lmul]() mipp::set0_k<T, LMUL>() mipp::set0_k_obj<T, LMUL>() Clears all elements (all inactive).

7.2. Important Semantics & Particularities

  1. Fixed Input Array Type:

    • Regardless of the underlying vector datatype T (whether float64, int8, uint16, etc.), set_k always expects an array of int32_t in C and of bool in C++ (in the C++ Object API, set_k_obj and the Vm constructor taking an array call set_k). An int32_t array is rejected at compile time in C++:

      // Array of int32_t, one value per lane
      int32_t mask_values[MIPP_N_FLOAT32];
      for (uint32_t i = 0; i < MIPP_N_FLOAT32; i++)
          mask_values[i] = (i % 3 == 0) ? 1 : 0; // lanes 0, 3, 6, ... active
      vm_float32_t m = mipp_set_k_float32(mask_values);
      
      // Array of bool, one value per lane
      bool mask_values[mipp::N<float>()];
      for (uint32_t i = 0; i < mipp::N<float>(); i++)
          mask_values[i] = (i % 3 == 0); // lanes 0, 3, 6, ... active
      mipp::vm<float> m = mipp::set_k<float>(mask_values);
      
      // Array of bool, one value per lane
      bool mask_values[mipp::N<float>()];
      for (uint32_t i = 0; i < mipp::N<float>(); i++)
          mask_values[i] = (i % 3 == 0); // lanes 0, 3, 6, ... active
      mipp::Vm<float> m = mipp::set_k_obj<float>(mask_values);
      
  2. C Truthiness Evaluation:

    • In C, set_k evaluates the truthiness of each integer element rather than copying raw bit patterns. Any non-zero integer (vals[i] != 0) sets the corresponding mask element to active (one mask bit or predicate lane on RVV, AVX-512 and SVE, an all-ones ~0 element on the full-vector mask targets). A zero integer (vals[i] == 0) sets the element to inactive (0).
  3. The _k Suffix:
    • The _k naming convention reflects AVX-512 opmask / predicate register naming (k1..k7), distinguishing mask factory functions (set_k, set1_k, set0_k) from numeric vector factories (set, set1, set0).

8. Masking & Multi-Register LMUL Interoperability

Masking and the Length Multiplier (\(\text{LMUL}\)) are orthogonal and fully composable across all MIPP interfaces. Predicated operations execute on multi-register vectors (\(\text{LMUL} \in \{2, 4, 8\}\)) and on half-vectors (d2 suffix in C, \(\text{LMUL} = -2\) in C++), which every target provides (see Vector Types & LMUL).

8.1. Type Matching & Capacity

A comparison evaluated on an \(\text{LMUL}=k\) data vector produces an \(\text{LMUL}=k\) mask containing the exact matching element capacity:

\[\text{Capacity}(vm\langle T, \text{LMUL} \rangle) = \text{Capacity}(vd\langle T, \text{LMUL} \rangle) = N(T, \text{LMUL})\]
vd_float32_m2_t va  = mipp_load_float32_m2(ptr_a);
vd_float32_m2_t vb  = mipp_load_float32_m2(ptr_b);
vm_float32_m2_t m   = mipp_cmplt_float32_m2(va, vb);
vd_float32_m2_t res = mipp_add_float32_maskm_m2(m, va, vb);
mipp::vd<float, 2> va  = mipp::load<float, 2>(ptr_a);
mipp::vd<float, 2> vb  = mipp::load<float, 2>(ptr_b);
mipp::vm<float, 2> m   = mipp::cmplt(va, vb);
mipp::vd<float, 2> res = mipp::add<mipp::M, float, 2>(m, va, vb);
mipp::Vd<float, 2> va  = mipp::load_obj<float, 2>(ptr_a);
mipp::Vd<float, 2> vb  = mipp::load_obj<float, 2>(ptr_b);
mipp::Vm<float, 2> m   = (va < vb);
mipp::Vd<float, 2> res = mipp::add<mipp::M>(m, va, vb);

In C, \(\text{LMUL}\) is part of the type and function names. In the C++ Functional API, the masked operation needs T and LMUL explicitly (mipp::add<mipp::M, float, 2>). In the C++ Object API, both are deduced from the Vm and Vd operands.

8.2. Hardware Execution on Multi-Register Vectors

  • RISC-V Vector (RVV): Operates directly on hardware grouped registers (vfloat32m2_t) governed by the matching boolean mask register vboolN_t, with N = SEW / LMUL (vbool16_t for vfloat32m2_t).
  • Fixed-Width Architectures (x86, ARM): Multi-register masks (vm_*_m2_t) and data vectors (vd_*_m2_t) are structs of two halves of the LMUL below (lo and hi). The masked operation is applied independently to each register of the group (using the emulation strategy described in Section 3), with no abstraction overhead once inlined.

9. Mask Tests (testz, none, any, all)

Four functions reduce masks to a boolean, typically to leave a loop early or to skip a branch when no lane is concerned: an int32_t in C (1 for true, 0 for false), a bool in the C++ APIs:

Function C API Function C++ Functional API C++ Object API Semantics
None active mipp_none_[type]_[lmul](m) mipp::none(m) mipp::none(m) \(1\) if no lane of m is active, \(0\) otherwise.
Disjoint mipp_testz_[type]_[lmul](vm0, vm1) mipp::testz(vm0, vm1) mipp::testz(vm0, vm1) \(1\) if vm0 & vm1 has no active lane, \(0\) otherwise.
Any active mipp_any_[type]_[lmul](m) mipp::any(m) mipp::any(m) \(1\) if at least one lane of m is active, \(0\) otherwise.
All active mipp_all_[type]_[lmul](m) mipp::all(m) mipp::all(m) \(1\) if every lane of m is active, \(0\) otherwise.

none(m) is testz(m, m), and any(m) is !none(m). On half-vectors (d2), the four functions ignore the unused upper lanes (see Vector Types & LMUL §4.1).