Getting Started with MIPP¶
1. Installation¶
MIPP is a header-only library: no separate compilation, no pre-built shared/static library, and no runtime dependency. The headers are generated from a declarative database by a Python tool and are not versioned in the Git repository (cloning the repository alone does not provide an include/ directory).
There are two ways to obtain them.
Option A: Download a Release Archive (Recommended)¶
Each tagged release on GitHub Releases ships pre-generated headers as mipp-<version>-headers.zip and mipp-<version>-headers.tar.gz. Extract the archive and add the resulting include/ directory to your compiler's include search path:
tar -xzf mipp-<version>-headers.tar.gz
g++ -std=c++11 -O3 -mavx2 -mfma -I/path/to/include main.cpp -o app
Option B: Generate the Headers from Source¶
Generating the headers requires Python 3 and the packages listed in generator/requirements.txt:
git clone -b develop https://github.com/aff3ct/MIPP.git # MIPPv2 is on develop (master is MIPPv1)
cd MIPP/generator
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
./gen_mipp_headers.py # Default (coarse) headers, emitted into ../include/
# or: ./gen_mipp_headers.py --granularity fine # Same, plus fine-grained headers (as in the release archives)
The generated include/ directory at the root of the repository is then ready to use.
Which Header to Include¶
Each API has a single umbrella header that brings every MIPP function into scope:
| Header File | API | Description |
|---|---|---|
<mipp.h> |
C API | C11 function prototypes with explicit type suffixes (e.g., mipp_add_float32(vd0, vd1)). |
<mipp.hpp> |
C++ Functional API | Overloaded functions under namespace mipp:: (e.g., mipp::add(vd0, vd1)). |
<mipp_obj.hpp> |
C++ Object API | Expressive mipp::Vd<T, LMUL> and mipp::Vm<T, LMUL> classes with operator overloading (+, -, *, ==, ...) and free functions. |
This is the simplest way to start. For larger projects, you can include only the functions you use (<mipp/obj/fun/add.hpp>), or even a single masking variant and LMUL (<mipp/obj/fun/m/m2/add.hpp>), which makes compilation 2× to 4× faster: see Headers & Compile Time.
Every public name starts with mipp (mipp_add_float32, mipp::add, MIPP_N_FLOAT32), except the C register types (vd_float32_t, vm_float32_t) and the C typedefs float32_t and float64_t. In C++, a shorter prefix is a namespace alias in your own code: with namespace mi = mipp;, write mi::add(a, b) or mi::Vd<float>. MIPP does not shorten its prefix itself, since a two-letter prefix would collide with other libraries in C (mi_ is the one of mimalloc): see Naming Conventions.
2. Compiler Flags & Target Architecture Selection¶
MIPP automatically detects the target instruction set architecture (ISA) at compile time based on standard compiler architecture flags.
2.1. Compilation Flags Matrix¶
| Target Architecture | Compiler Flags (GCC / Clang / ICX) | Detected MIPP Macro | Vector Bitwidth |
|---|---|---|---|
| x86 SSE | -msse4.2 (SSE2, the x86-64 default, is enough) |
MIPP_SSE |
128 bits |
| x86 AVX | -mavx |
MIPP_AVX |
256 bits |
| x86 AVX2 + FMA | -mavx2 -mfma |
MIPP_AVX (with MIPP_AVX2, MIPP_BW, MIPP_FMA) |
256 bits |
| x86 AVX-512 | -mavx512f -mavx512bw -mavx512dq |
MIPP_AVX512 |
512 bits |
| ARM NEON (v8-A) | -march=armv8-a+simd |
MIPP_NEON |
128 bits |
| ARM SVE | -march=armv8-a+sve -msve-vector-bits=256 |
MIPP_SVE |
Fixed at compile time (256 bits in the given compiler flags) |
| RISC-V Vector 1.0 | -march=rv64gcv_zvl256b -mrvv-vector-bits=zvl |
MIPP_RVV |
Fixed at compile time (\(\ge 128\text{ bits}\), 256 bits in the given compiler flags) |
| Scalar Target | -DMIPP_SCALAR [-DMIPP_SCALAR_SIZE=bits] |
MIPP_SCALAR |
Configurable width |
Recommended Optimization Flags for MIPP
To ensure proper inlining and vector code generation, compile with:
3. Real-World Tutorial: Vectorizing a Loop & Handling Tail Loops¶
In real applications, data arrays rarely have a size that is an exact multiple of the hardware vector register length (\(N = \text{mipp::N<T>()}\)).
Here is the standard, battle-tested pattern to vectorize any loop over an arbitrary array size \(S\) with MIPP:
- Vector Main Loop: Process contiguous chunks of \(N\) elements with SIMD.
- Tail Loop (Remainder): Handle remaining \(S \pmod N\) elements.
Complete Working Example (Leaky ReLU with Tail Loop)¶
The Leaky ReLU activation keeps the positive elements and scales the others: y[i] = x[i] > 0 ? x[i] : alpha * x[i]. In the vector loop, the comparison returns a mask (one boolean per lane), and select takes, lane by lane, x where the mask is set and alpha * x elsewhere: there is no branch.
The three programs are also in the repository (examples/doc/getting_started/c.c, fun.cpp and obj.cpp), where they are compiled and run by the documentation checks.
#include <stdio.h>
#include <stdlib.h>
#include <mipp.h>
// Computes y[i] = x[i] > 0 ? x[i] : alpha * x[i] (Leaky ReLU) over an arbitrary size
void leaky_relu(float alpha, const float* x, float* y, size_t size)
{
const size_t N = MIPP_N_FLOAT32; // elements per register (e.g. 8 with AVX2)
const size_t vec_end = (size / N) * N;
vd_float32_m1_t vzero = mipp_set1_float32_m1(0.f);
vd_float32_m1_t valpha = mipp_set1_float32_m1(alpha);
// 1. Vector main loop
for (size_t i = 0; i < vec_end; i += N) {
vd_float32_m1_t vx = mipp_loadu_float32_m1(&x[i]);
vm_float32_m1_t pos = mipp_cmpgt_float32_m1(vx, vzero); // comparison -> mask
vd_float32_m1_t vy = mipp_select_float32_m1(pos, vx, mipp_mul_float32_m1(vx, valpha)); // branch-free
mipp_storeu_float32_m1(&y[i], vy);
}
// 2. Scalar tail loop for the remaining elements
for (size_t i = vec_end; i < size; i++)
y[i] = x[i] > 0.f ? x[i] : alpha * x[i];
}
int main(void)
{
const size_t size = 1007;
float* x = (float*)malloc(size * sizeof(float));
float* y = (float*)malloc(size * sizeof(float));
for (size_t i = 0; i < size; i++)
x[i] = (i % 2) ? 2.0f : -2.0f;
leaky_relu(0.1f, x, y, size);
size_t errors = 0;
for (size_t i = 0; i < size; i++)
if (y[i] != ((i % 2) ? 2.0f : -0.2f)) errors++;
printf("y[0] = %g, y[1] = %g (expected -0.2, 2), %zu error(s)\n", y[0], y[1], errors);
free(x);
free(y);
return errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Compile and run (with the code saved as leaky_relu.c):
#include <cstdlib>
#include <iostream>
#include <vector>
#include <mipp.hpp>
// Computes y[i] = x[i] > 0 ? x[i] : alpha * x[i] (Leaky ReLU) over an arbitrary size
void leaky_relu(float alpha, const float* x, float* y, size_t size)
{
constexpr size_t N = mipp::N<float>(); // elements per register (e.g. 8 with AVX2)
const size_t vec_end = (size / N) * N;
mipp::vd<float> vzero = mipp::set1<float>(0.f);
mipp::vd<float> valpha = mipp::set1<float>(alpha);
// 1. Vector main loop
for (size_t i = 0; i < vec_end; i += N) {
mipp::vd<float> vx = mipp::loadu<float>(&x[i]);
mipp::vm<float> pos = mipp::cmpgt(vx, vzero); // comparison -> mask
mipp::vd<float> vy = mipp::select(pos, vx, mipp::mul(vx, valpha)); // branch-free
mipp::storeu(&y[i], vy);
}
// 2. Scalar tail loop for the remaining elements
for (size_t i = vec_end; i < size; i++)
y[i] = x[i] > 0.f ? x[i] : alpha * x[i];
}
int main()
{
std::vector<float> x(1007), y(1007);
for (size_t i = 0; i < x.size(); i++)
x[i] = (i % 2) ? 2.0f : -2.0f;
leaky_relu(0.1f, x.data(), y.data(), x.size());
size_t errors = 0;
for (size_t i = 0; i < y.size(); i++)
if (y[i] != ((i % 2) ? 2.0f : -0.2f)) errors++;
std::cout << "y[0] = " << y[0] << ", y[1] = " << y[1] << " (expected -0.2, 2), "
<< errors << " error(s)" << std::endl;
return errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Compile and run (with the code saved as leaky_relu.cpp):
#include <cstdlib>
#include <iostream>
#include <vector>
#include <mipp_obj.hpp>
// Computes y[i] = x[i] > 0 ? x[i] : alpha * x[i] (Leaky ReLU) over an arbitrary size
void leaky_relu(float alpha, const float* x, float* y, size_t size)
{
constexpr size_t N = mipp::Vd<float>::N(); // elements per register (e.g. 8 with AVX2)
const size_t vec_end = (size / N) * N;
// 1. Vector main loop
for (size_t i = 0; i < vec_end; i += N) {
mipp::Vd<float> vx(&x[i]); // load
mipp::Vm<float> pos = vx > 0.f; // comparison -> mask
mipp::Vd<float> vy = mipp::select(pos, vx, vx * alpha); // branch-free
mipp::storeu(&y[i], vy);
}
// 2. Scalar tail loop for the remaining elements
for (size_t i = vec_end; i < size; i++)
y[i] = x[i] > 0.f ? x[i] : alpha * x[i];
}
int main()
{
std::vector<float> x(1007), y(1007);
for (size_t i = 0; i < x.size(); i++)
x[i] = (i % 2) ? 2.0f : -2.0f;
leaky_relu(0.1f, x.data(), y.data(), x.size());
size_t errors = 0;
for (size_t i = 0; i < y.size(); i++)
if (y[i] != ((i % 2) ? 2.0f : -0.2f)) errors++;
std::cout << "y[0] = " << y[0] << ", y[1] = " << y[1] << " (expected -0.2, 2), "
<< errors << " error(s)" << std::endl;
return errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Compile and run (with the code saved as leaky_relu.cpp):
4. Architecture Introspection (mipp_info)¶
You can call mipp_info() at runtime to print the detected OS, compiler, active SIMD extension, and vector register bitwidth to stdout (mipp::info() in the C++ APIs, from mipp.hpp or mipp_obj.hpp):