Programming Model & APIs¶
MIPP provides three distinct API layers built on top of a unified code generation engine:
- C API (
<mipp.h>): Explicit type-suffixed functions and plain C structs. - C++ Functional API (
<mipp.hpp>): Type-parameterized template functions under namespacemipp::. - C++ Object API (
<mipp_obj.hpp>): Class wrappers (mipp::Vd<T, LMUL>,mipp::Vm<T, LMUL>) with arithmetic and relational operator overloading.
All three APIs share the same zero-overhead inlined execution path and map to identical machine instructions.
Shorter Namespace
MIPP does not define a shorter namespace, so as not to impose one on every program, but a C++ namespace alias gives one at no cost:
namespace mi = mipp; // once, in your own code (namespace or block scope)
mi::vd<float> a = mi::load<float>(ptr_a);
mi::vd<float> b = mi::load<float>(ptr_b);
mi::vd<float> c = mi::add(a, b);
The alias covers both C++ APIs (mi::Vd<float>, mi::load_obj<float>(ptr) in the Object API). This documentation keeps mipp::.
1. Headers¶
Each API has an umbrella header (<mipp.h>, <mipp.hpp>, <mipp_obj.hpp>) and finer headers under <mipp/c/>, <mipp/cpp/> and <mipp/obj/> that include a single category, function, masking variant or LMUL. The header tree, the available granularities and their effect on compile time are described in Headers & Compile Time.
2. C API (<mipp.h>)¶
The C API provides explicit, type-safe C11 function identifiers and vector types for every supported (operation, datatype, lmul, mask_variant) combination (each operation lists its datatypes and masking variants in Specifications).
2.1. Naming Convention¶
When operating at standard vector length (\(\text{LMUL}=1\)), the _m1 suffix is optional: functions without the suffix (e.g., mipp_add_float32) are direct aliases to their _m1 counterparts (mipp_add_float32_m1).
- Standard (\(\text{LMUL}=1\), unmasked):
mipp_add_float32(vd0, vd1)ormipp_add_float32_m1(vd0, vd1) - Multi-Register (\(\text{LMUL}=2, 4, 8\)):
mipp_add_float32_m2(vd0, vd1),mipp_add_float32_m4(vd0, vd1) - Merge-Masked (\(\text{LMUL}=1\)):
mipp_add_float32_maskm(vm0, vd0, vd1)ormipp_add_float32_maskm_m1(vm0, vd0, vd1) - Zero-Masked:
mipp_add_float32_maskz(vm0, vd0, vd1)ormipp_add_float32_maskz_m1(vm0, vd0, vd1) - Source-Masked:
mipp_add_float32_masks(vm0, vdsrc, vd0, vd1)ormipp_add_float32_masks_m1(vm0, vdsrc, vd0, vd1) - Merge-Masked + Multi-Register:
mipp_add_float32_maskm_m2(vm0, vd0, vd1) - Half-Vector:
mipp_add_float32_d2(vd0, vd1),mipp_add_float32_maskm_d2(vm0, vd0, vd1)
2.2. Vector & Mask Datatypes (vd and vm)¶
MIPP defines distinct data register (vd) and mask register (vm) types for each of the 10 supported data types across all LMUL configurations:
| Category | Type Suffix / Format | Concrete Examples | Description |
|---|---|---|---|
| Standard Data Vector | vd_[type]_t / vd_[type]_m1_t |
vd_float32_t, vd_int32_m1_t |
Single hardware vector register (\(\text{LMUL}=1\)). |
| Multi-Register Data | vd_[type]_m[2|4|8]_t |
vd_float32_m2_t, vd_float32_m4_t |
Group of \(2\times, 4\times, 8\times\) vector registers. |
| Standard Mask Vector | vm_[type]_t / vm_[type]_m1_t |
vm_float32_t, vm_int32_m1_t |
Single mask/predicate register (\(\text{LMUL}=1\)). |
| Multi-Register Mask | vm_[type]_m[2|4|8]_t |
vm_float32_m2_t, vm_float32_m4_t |
Group of \(2\times, 4\times, 8\times\) mask registers. |
| Half-Vector Data / Mask | vd_[type]_d2_t / vm_[type]_d2_t |
vd_float32_d2_t, vm_float32_d2_t |
Half of a register (half-vectors). |
Multi-register types (m2, m4, m8) map directly to native vector register groups on RVV 1.0, or to hierarchical zero-overhead structures on fixed-width ISAs. See Vector Types & LMUL for full structural details and memory layouts.
2.3. Vector Capacity Macros¶
Predefined compile-time constants provide the number of elements per vector type:
MIPP_N_FLOAT32orMIPP_N_FLOAT32_M1: Number offloatelements in an \(\text{LMUL}=1\) vector.MIPP_N_FLOAT32_M2,MIPP_N_FLOAT32_M4,MIPP_N_FLOAT32_M8: Multiplied element capacities.MIPP_N_FLOAT32_D2: Capacity of a half-vector (half ofMIPP_N_FLOAT32).- Equivalent macros exist for all 10 types (
MIPP_N_INT32,MIPP_N_FLOAT64,MIPP_N_UINT8, etc.).
2.4. Explicit Architecture Selection¶
The names of sections 2.1 and 2.2 always refer to the active instruction set (the C counterpart of mipp::DEFAULT_ISA, see section 3.3): mipp_add_float32_m1 forwards to the implementation of this ISA. The implementation of each ISA has its own names, with the ISA inserted after the mipp_ / vd_ / vm_ prefix:
mipp_[isa]_[func]_[datatype][_mask_variant][_lmul]
vd_[isa]_[datatype][_lmul]_t
vm_[isa]_[datatype][_lmul]_t
where [isa] is one of scalar, sse, avx, avx512, neon, sve or rvv (for example mipp_sse_add_float32_m1 on vd_sse_float32_m1_t, or mipp_scalar_add_float32_m1 on vd_scalar_float32_m1_t). This is the C equivalent of the ISA_TYPE template parameter of the C++ Functional API, with the same restrictions (see section 3.4).
3. C++ Functional API (<mipp.hpp>)¶
The C++ Functional API parameterizes operations over the element type T, an optional register multiplier LMUL (defaults to 1), and an optional target instruction set ISA_TYPE (defaults to mipp::DEFAULT_ISA).
3.1. Template Parameter Domains¶
| Parameter | Domain / Supported Values | Description & Constraints |
|---|---|---|
typename T |
float, double, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t |
10 standard numeric datatypes. Most operations support all of them; some only a subset (e.g. sqrt only the floating-point types, neg no unsigned type). |
int LMUL |
1, 2, 4, 8 (integer multipliers) and -2 for half-vectors (d2, provided by every target today, see §3.4) |
Vector length multiplier (register grouping, or half-vector). Defaults to 1. See Vector Types & LMUL. |
mipp::ISA ISA_TYPE |
mipp::ISA::SCALAR, mipp::ISA::SSE, mipp::ISA::AVX, mipp::ISA::AVX512, mipp::ISA::NEON, mipp::ISA::SVE, mipp::ISA::RVV |
Target instruction set architecture. Defaults to mipp::DEFAULT_ISA. |
3.2. Types & Function Signatures¶
- Data Vector:
mipp::vd<typename T, int LMUL = 1, mipp::ISA ISA_TYPE = mipp::DEFAULT_ISA> - Mask Vector:
mipp::vm<typename T, int LMUL = 1, mipp::ISA ISA_TYPE = mipp::DEFAULT_ISA> - Capacity Query:
mipp::N<typename T, int LMUL = 1, mipp::ISA ISA_TYPE = mipp::DEFAULT_ISA>() - Memory Alignment Query:
mipp::req_alignment<mipp::ISA ISA_TYPE = mipp::DEFAULT_ISA>()
3.3. DEFAULT_ISA Selection Priority¶
At compile time, mipp::DEFAULT_ISA automatically resolves to the highest hardware instruction set extension enabled by compiler flags:
- x86 / x86-64:
AVX-512>AVX2 / AVX>SSE - ARM:
SVE>NEON - RISC-V:
RVV - Scalar: Configured as
DEFAULT_ISAwhen compiling with-DMIPP_SCALAR. By default, scalar registers match the bitwidth of the highest detected host ISA (e.g. 256 bits on an AVX host, 128 bits on NEON/SSE). The bitwidth can be explicitly overridden at compile time via-DMIPP_SCALAR_SIZE=[bits](e.g.-DMIPP_SCALAR_SIZE=256); the value must be a power of two greater than or equal to 128, so that the half-vectors (d2) of 64-bit types hold at least one element.
3.4. Explicit Architecture Selection¶
The third template parameter ISA_TYPE defaults to mipp::DEFAULT_ISA, which is the only value that should be used in portable code. The C API encodes the ISA in the function and type names instead (see section 2.4); the C++ Object API has no equivalent (see section 4).
Portability Scope of ISA_TYPE
Explicitly hardcoding a specific hardware instruction set (such as mipp::ISA::SSE, mipp::ISA::AVX, mipp::ISA::NEON, or mipp::ISA::RVV) is non-portable by design: the code will only compile on toolchains and targets that provide that specific ISA's intrinsics and register sets. Always rely on the default mipp::DEFAULT_ISA in application code.
Selecting an ISA does not make its implementation available: the public headers only include the implementation of the active ISA. The other values are mainly used internally. MIPP's unit tests, for instance, compare each function to its scalar implementation (mipp::ISA::SCALAR in C++, mipp_scalar_* in C) by including the corresponding internal headers (<mipp/internal/simd_ext/scalar/...>), which are not a stable interface.
3.5. Template Deduction & auto Type Inference¶
When operations take vector arguments, the compiler automatically deduces T, LMUL, and ISA_TYPE. Using the auto keyword eliminates verbose vector type declarations:
For functions without vector arguments, explicit template parameters are supplied:
3.6. Masked Operations¶
Masked operations take the variant as first template argument (mipp::M, mipp::Z or mipp::S). Because mipp::vd<T, LMUL> is an alias to an internal type trait, T cannot be deduced from the arguments and must be given explicitly (plus LMUL when it is not 1):
The unmasked form is the call without a mask (mipp::add(a, b)). The masked signatures also accept mipp::U: the mask is then ignored and the call behaves like the unmasked one (mipp::add<mipp::U, float>(m, a, b) returns mipp::add(a, b)). This lets generic code take the variant as a template parameter, U included.
The four variants, their semantics and the syntax in each API are described in Masking Semantics.
4. C++ Object API (<mipp_obj.hpp>)¶
The Object API encapsulates raw SIMD registers and predicates inside high-level C++ classes:
mipp::Vd<T, LMUL>for numeric data vectors.mipp::Vm<T, LMUL>for boolean mask vectors.
Unlike the C++ Functional API, these classes have no ISA_TYPE parameter: they always hold registers of mipp::DEFAULT_ISA. Code that needs another ISA uses the C++ Functional API (see section 3.4).
4.1. Class Structure & Constructors¶
template <typename T, int LMUL = 1>
struct Vd {
using value_type = T;
static constexpr int lmul = LMUL;
vd<T, LMUL> vd; // Underlying low-level SIMD register
static constexpr int N(); // Number of elements (mipp::N<T, LMUL>())
// Constructors
Vd() = default;
Vd(vd<T, LMUL> vd0); // Wraps a low-level vd register
Vd(const T val); // Broadcasts a scalar (mipp::set1)
Vd(const T* p0); // Loads from memory, no alignment requirement (mipp::loadu)
Vd(std::initializer_list<T> l); // {l0, l1, ...}: the N() lanes in order; {val}: broadcast, as Vd(val)
// Implicit conversion to the low-level register (interoperability with the C++ Functional API)
operator vd<T, LMUL>() const;
Vd& operator=(const vd<T, LMUL>& vd0); // Assignment
Vd& operator=(const T val); // Broadcast
T operator[](const size_t index) const; // Reads one element (mipp::get)
// Change of LMUL (see Vector Types & LMUL, section 5.5)
Vd<T, /* LMUL below */> lo() const; // Lower half (mipp::lo)
Vd<T, /* LMUL below */> hi() const; // Upper half (mipp::hi)
Vd<T, /* LMUL above */> combine(const Vd& hi) const; // *this as lower half, hi as upper half (mipp::combine)
};
template <typename T, int LMUL = 1>
struct Vm {
using value_type = T;
static constexpr int lmul = LMUL;
vm<T, LMUL> vm; // Underlying low-level mask register
static constexpr int N(); // Number of elements (mipp::N<T, LMUL>())
// Constructors
Vm() = default;
Vm(vm<T, LMUL> vm0); // Wraps a low-level vm register
Vm(const bool val); // All lanes active or inactive (mipp::set1_k)
Vm(const bool vals[mipp::N<T, LMUL>()]); // Lane i active iff vals[i] (mipp::set_k)
Vm(std::initializer_list<bool> l); // {b0, b1, ...}: the N() lanes in order; {b}: broadcast
Vm(const int32_t*) = delete; // An int32_t array (C API) is rejected, see below
// Implicit conversion to the low-level register (interoperability with the C++ Functional API)
operator vm<T, LMUL>() const;
Vm& operator=(const vm<T, LMUL>& vm0); // Assignment
Vm& operator=(const bool val); // Broadcast
Vm& operator=(const int32_t*) = delete;
bool operator[](const size_t index) const; // Reads one lane: true if active
// Change of LMUL, as for Vd (mipp::lo, mipp::hi, mipp::combine on masks)
Vm<T, /* LMUL below */> lo() const;
Vm<T, /* LMUL below */> hi() const;
Vm<T, /* LMUL above */> combine(const Vm& hi) const;
};
Per-lane initialization takes bool, not int32_t
Like set_k in the C++ APIs, the per-lane constructor takes a bool array, whatever T (the C API takes an int32_t array). An int32_t array is rejected at compile time: Vm<float> m(int32_array) would otherwise decay to a pointer, convert to bool and activate all lanes through Vm(const bool), so the const int32_t* constructor and assignment are deleted.
Element access: v[i] and the free function mipp::get(v, i) return the element as T. On a mask, m[i] and mipp::get(m, i) return a bool: get taking a Vm is the C++ name of the C function get_k (see Memory & Alignment).
4.2. Overloaded Operators Reference¶
The C++ Object API provides operator overloads for data vectors (Vd) and mask vectors (Vm). In the table, T is a scalar of the element type: it must be exactly T, since it takes part in template deduction (a + 1.f is valid for Vd<float>, a + 1 is not).
| Operation Category | Supported Operators | Operands | Return Type |
|---|---|---|---|
| Arithmetic | +, -, *, / |
Vd<T> and Vd<T>, Vd<T> and T, T and Vd<T> |
Vd<T> |
| Negation | unary - |
Vd<T> |
Vd<T> |
| Bitwise | &, |, ^, ~ |
same as arithmetic / Vd<T> |
Vd<T> |
| Shifts | <<, >> |
Vd<T> and int32_t (same shift count for every lane), Vd<T> and Vd<T> (one count per lane) |
Vd<T> |
| Compound Assignment | +=, -=, *=, /=, &=, |=, ^= |
Vd<T>& and Vd<T> or T |
Vd<T>& |
| Compound Shift | <<=, >>= |
Vd<T>& and int32_t or Vd<T> |
Vd<T>& |
| Relational / Comparison | ==, !=, <, <=, >, >= |
same as arithmetic | Vm<T> (Mask) |
| Mask Logic | &, |, ^ |
Vm<T> and Vm<T> or bool |
Vm<T> |
| Mask Inversion | ~, ! |
Vm<T> |
Vm<T> |
| Mask Compound Assignment | &=, |=, ^= |
Vm<T>& and Vm<T> or bool |
Vm<T>& |
Bitwise operators and the shifts by an int32_t are defined for every element type: on floating-point types they act on the bit pattern. The shifts by a Vd<T> (a << b, one count per lane) exist for the integer types. >> is rshift: arithmetic on the signed types (copies of the sign bit), logical on the unsigned ones; the logical shift of the signed types is mipp::rshiftl(a, n), which has no operator.
4.3. Free Functions in Namespace mipp::¶
Every MIPP SIMD operation is exposed as a free function under namespace mipp:: that directly accepts Vd and Vm instances:
mipp::Vd<float> a = mipp::loadu_obj<float>(ptr_a);
mipp::Vd<float> b = mipp::loadu_obj<float>(ptr_b);
mipp::Vd<float> c = mipp::loadu_obj<float>(ptr_c);
// Free functions on Vd:
mipp::Vd<float> res_fma = mipp::fmadd(a, b, c); // Fused multiply-add
mipp::Vd<float> res_abs = mipp::abs(a); // Absolute value
mipp::Vd<float> res_min = mipp::min(a, b); // Element-wise minimum
float res_red = mipp::hadd(a); // Horizontal sum (returns a scalar)
// Store directly using free function:
mipp::store(ptr_out, res_fma);
The loads that take only a pointer (contiguous, strided or first-faulting), the set functions, iota and firstn build a register from memory or from scalars: without a Vd or Vm argument, they would have the same parameters as their C++ Functional API counterparts. In the Object API they have the _obj suffix (masked or not) and return a Vd or a Vm: load_obj, loadu_obj, loadu_unzip_obj, loadu_strided_obj, loadu_ff_obj, set_obj, set0_obj, set1_obj, iota_obj, set_k_obj, set0_k_obj, set1_k_obj and firstn_obj (e.g. mipp::loadu_obj<float>(ptr), mipp::firstn_obj<float>(n)). The stores keep their names (mipp::storeu(ptr, v)), as the loads that write Vd arguments (mipp::loadu_unzip2(ptr, a, b)).
4.4. Masked Operations¶
With Vm and Vd objects, the compiler deduces T and LMUL: only the variant is needed. The variant also has a default: mipp::M for a call with the mask alone (mipp::Z for the functions without M, such as the loads, the comparisons and the reductions), and mipp::S for a call with a source register vdsrc. mipp::add(m, a, b) is then a merge-masked add. Writing the variant keeps the intent visible.
See Masking Semantics for the four variants.
4.5. Printing and Generic Reductions¶
std::cout << vprints the lanes of aVdor of aVm(1for an active lane):[0, 1, 2, 3 | 4, 5, 6, 7], with a|between the 128-bit blocks. The C API hasmipp_dump[_k]_[type][_lmul](v)andmipp_fdump[_k]_[type][_lmul](FILE*, v), with_kfor the masks and no LMUL suffix for LMUL = 1 (mipp_dump_float32,mipp_dump_k_float32_m2), the C++ Functional APImipp::dump<T, LMUL>(v)andmipp::dump_k<T, LMUL>(m).mipp::reduce(v, op)reduces the lanes of a register, andmipp::reduce<T, LMUL>(ptr, n, op)thenelements of an array (n\(\geq 1\)), with an associative and commutative operationop(Vd, Vd) -> Vdthat accepts everyLMUL: one ofmipp::op::add,mul,min,max,andb,orb,xorb, or a functor with a templateoperator().
const mipp::Vd<float> v(A);
out << v << "\n"; // [-3, -2, -1, 0 | 1, 2, 3, -3 | ...]
out << (v > mipp::Vd<float>(0.f)) << "\n"; // [0, 0, 0, 0 | 1, 1, 1, 0 | ...]
float vmax = mipp::reduce(v, mipp::op::max()); // largest lane of v
float total = mipp::reduce(B, 100, mipp::op::add()); // sum of the 100 elements of B
4.6. Idiomatic Coding Guidelines¶
- Prefer Operator Overloads: When an operator exists (
a + b,a * b,a == b,m1 & m2), use it over named function calls for maximum readability. - Use Free Functions: For non-operator routines (e.g.
mipp::fmadd,mipp::sqrt,mipp::min,mipp::store), use free functions undernamespace mipp::. - Few Member Functions: Apart from
N(),operator[]and the LMUL changeslo(),hi()andcombine(), operations are not exposed as member functions (there is noa.add(b)); use operators and free functions.
5. Example: Remainder Handled with Masked Memory Accesses¶
Getting Started shows the same Leaky ReLU loop in the three APIs, with a scalar loop for the last elements. MIPP can also process these last elements with SIMD code: a mask selects the valid lanes, firstn(n - i) (the first n - i lanes active), and masked loads and stores never access memory outside them.
#include <stddef.h>
#include <stdint.h>
#include <mipp.h>
// y[i] = x[i] > 0 ? x[i] : alpha * x[i]
void leaky_relu(size_t n, float alpha, const float* x, float* y)
{
const size_t N = MIPP_N_FLOAT32;
const vd_float32_t vzero = mipp_set1_float32(0.f);
const vd_float32_t valpha = mipp_set1_float32(alpha);
size_t i = 0;
for (; i + N <= n; i += N) {
vd_float32_t vx = mipp_loadu_float32(x + i);
vm_float32_t pos = mipp_cmpgt_float32(vx, vzero);
mipp_storeu_float32(y + i, mipp_select_float32(pos, vx, mipp_mul_float32(vx, valpha)));
}
if (i < n) { // fewer than N elements left
vm_float32_t m = mipp_firstn_float32((int32_t)(n - i)); // lane j active iff i + j < n
vd_float32_t vx = mipp_loadu_float32_maskz(m, x + i); // inactive lanes are not read (set to 0)
vm_float32_t pos = mipp_cmpgt_float32(vx, vzero);
mipp_storeu_float32_maskm(m, y + i, mipp_select_float32(pos, vx, mipp_mul_float32(vx, valpha))); // inactive lanes are not written
}
}
#include <cstddef>
#include <cstdint>
#include <mipp.hpp>
// y[i] = x[i] > 0 ? x[i] : alpha * x[i]
void leaky_relu(std::size_t n, float alpha, const float* x, float* y)
{
constexpr std::size_t N = mipp::N<float>();
const mipp::vd<float> vzero = mipp::set1<float>(0.f);
const mipp::vd<float> valpha = mipp::set1<float>(alpha);
std::size_t i = 0;
for (; i + N <= n; i += N) {
mipp::vd<float> vx = mipp::loadu<float>(x + i);
mipp::vm<float> pos = mipp::cmpgt(vx, vzero);
mipp::storeu(y + i, mipp::select(pos, vx, mipp::mul(vx, valpha)));
}
if (i < n) { // fewer than N elements left
mipp::vm<float> m = mipp::firstn<float>((int32_t)(n - i)); // lane j active iff i + j < n
mipp::vd<float> vx = mipp::loadu<mipp::Z>(m, x + i); // inactive lanes are not read (set to 0)
mipp::vm<float> pos = mipp::cmpgt(vx, vzero);
mipp::storeu<mipp::M>(m, y + i, mipp::select(pos, vx, mipp::mul(vx, valpha))); // inactive lanes are not written
}
}
#include <cstddef>
#include <cstdint>
#include <mipp_obj.hpp>
// y[i] = x[i] > 0 ? x[i] : alpha * x[i]
void leaky_relu(std::size_t n, float alpha, const float* x, float* y)
{
constexpr std::size_t N = mipp::Vd<float>::N();
const mipp::Vd<float> vzero(0.f);
const mipp::Vd<float> valpha(alpha);
std::size_t i = 0;
for (; i + N <= n; i += N) {
mipp::Vd<float> vx(x + i);
mipp::Vm<float> pos = vx > vzero;
mipp::storeu(y + i, mipp::select(pos, vx, vx * valpha));
}
if (i < n) { // fewer than N elements left
mipp::Vm<float> m = mipp::firstn_obj<float>((int32_t)(n - i)); // lane j active iff i + j < n
mipp::Vd<float> vx = mipp::loadu_obj<mipp::Z>(m, x + i); // inactive lanes are not read (set to 0)
mipp::Vm<float> pos = vx > vzero;
mipp::storeu<mipp::M>(m, y + i, mipp::select(pos, vx, vx * valpha)); // inactive lanes are not written
}
}
Performance
The masked loadu / storeu of this example map to native instructions on RVV (masked vle / vse) and SVE (predicated ld1 / st1) for every type, on AVX-512 for the 10 datatypes (8-bit and 16-bit types require AVX-512BW), and on AVX for the 32-bit and 64-bit types (vmaskmov, and vpmaskmov for the integer types, which require AVX2). On SSE and NEON, and on AVX for the 8-bit and 16-bit types, they are currently emulated element by element, so the masked remainder is not faster than a scalar loop: its benefit is a single code path, with no scalar copy of the loop body. The implementation level of each target and variant is given in loadu and storeu.