Test Generator¶
This page describes how gen_mipp_tests.py turns the function registry and the test specifications into Catch2 sources. The options of the script are in the Command-Line Reference, every field of tests_specs.json in Test Specifications, and building and running the tests in Setup.
1. Principle¶
Every generated test compares the API under test with the scalar reference on the same random inputs:
- fill the input arrays with random values, in the domain given by
tests_specs.json; - load them into the registers of the selected ISA, and into the registers of the scalar target (
mipp_scalar_*in C,mipp::ISA::SCALARin C++); - call the function on both;
- compare the results lane by lane: exactly, or within a tolerance for the floating-point functions.
The scalar target is generated from registry_scalar_implems.json, like the L3 fallback. The test generator does not need the candidate resolution: it only knows the registry, so the same sources test every ISA, and the compiler flags choose the ISA under test.
A function that reaches a stub on the ISA under test throws mipp::stub_exception: the test catches it and reports a Catch2 warning instead of a failure.
The input and output arrays are aligned on 64 bytes, which suits load and store on every ISA. For loadu, storeu, loadu_unzip and storeu_zip (_UNALIGNED_PTR_FUNCS in tests_builder.py), the call under test gets a pointer one element past an aligned buffer, never aligned on the register size; the scalar reference keeps the aligned arrays.
2. Generated Files¶
| API | Files (in tests/src/) |
Content |
|---|---|---|
| C | c/<category>/<m0\|m1\|m2\|m4\|m8\|d2>/<unmasked\|maskm\|maskz\|masks>/<function>.cpp |
One file per function, LMUL and mask variant; one test function per datatype. m0 is LMUL 0, which calls the names without suffix (mipp_add_float32), and m1 calls the _m1 names. |
| C++ Functional | cpp/<category>/<function>.cpp |
One file per function: one template test (<VARIANT, T, LMUL>), instantiated in nested SECTIONs for each mask variant, datatype and LMUL. |
| C++ Object | obj/<category>/<function>.cpp |
Same structure, with mipp::Vd and mipp::Vm. |
--lmul, --ldiv and --mask-kind restrict the generated combinations; a function is only tested for the mask variants of its variants. tests/CMakeLists.txt compiles every .cpp found under tests/src/, with the helpers of tests/src/utils/ (random generators, overflow checks, abs_diff).
Hand-written tests. tests/src/manual/ holds the tests that the generator cannot write. The clean step of gen_mipp_tests.py only removes tests/src/{c,cpp,obj}/, so these files are kept, and the GLOB of tests/CMakeLists.txt compiles them with the others:
| File | Tests |
|---|---|
complex.cpp |
Special values of the complex functions (a zero or an infinite part), that the random inputs do not draw. |
forms.cpp |
The scalar forms not covered by the generated tests: the C aliases of the base form vv, and the C++ overloads of the base function. |
immediates.cpp |
The immediate versions (codegen/imm.py): for every value of the integer argument, the call with a constant, the C++ templates f<S>(...) and the call with a variable give the same lanes. |
patterns.cpp |
The compile-time permutations permute<P> and permute2<P> (C++17, mipp/cpp/patterns.hpp). |
transpose.cpp |
transpose and transpose_b128, in the three APIs. |
utils.cpp |
The hand-written utilities (utils_generator.py): aligned allocation, printing, reduce, the initializer lists of the Object API. |
3. Test Specifications¶
helpers_tests/tests_specs.json has a default entry and one entry per function in functions. A property missing in a function entry takes its value in default. The keys unmasked, maskm, maskz and masks override the input properties (domain, input_domain, domain_src, distribution, mask_pattern) for one mask variant.
Domains (domain_resolver.py). The range of the random inputs is looked up in the section of the mask variant, then at the root of the entry, then in the unmasked section. Inside a domain, a matching rule of rules wins over by_datatype, which wins over by_lmul, which wins over default. A by_define entry becomes #if defined(<macro>) branches in the generated code, for the ranges that depend on the ISA (for example MIPP_AVX2). input_domain gives one domain per input, domain_src the domain of the source register of the Source-Masked variant.
Comparison (tests_builder.py). comparison selects the assertion:
comparison |
Assertion |
|---|---|
exact |
REQUIRE(res == ref) on each lane. |
tolerance |
On floating-point types, REQUIRE(abs_diff::within(res, ref, tol)): res equal to ref (the infinities, whose difference is NaN), both NaN, or abs(res - ref) <= tol, with tol computed from tolerance; integers stay exact. |
bitwise |
Bit-pattern equality (NaN-safe), possibly per datatype. |
logical |
Truth-value equality, used for masks. |
tolerance is exact, max_abs_diff (absolute value), relative_percent (fraction of |ref|) or hybrid (abs_value + rel_value * |ref|), possibly refined with by_datatype, by_lmul (a ternary on LMUL in C++) and by_define (preprocessor branches).
Choosing a tolerance. The inputs are random, with a new seed at each run (--rng-seed replays one): a tolerance taken from a few runs fails from time to time, on another seed. A tolerance is therefore derived from a bound of the error (the rounding of the operations, or the worst case measured over every input of the domain when it can be enumerated, as for the float32 inputs), with a margin, never from the largest error of a few seeds. The tolerances changed after a measure are listed below, with their reason:
| Function | Datatype | Before | After | Reason |
|---|---|---|---|---|
fmadd, fmsub, fnmadd, fnmsub (and their scalar forms) |
float32 |
relative_percent 1e-3 |
hybrid 6e-8 + 1e-3 × |ref| |
A fused multiply-add and the scalar reference, when the compiler does not fuse it, differ by the rounding of a * b, at most half an ulp of a * b: \(2^{-25} \approx 3 \cdot 10^{-8}\) for inputs in \([-1, 1]\). When a * b and c cancel, |ref| is tiny and a purely relative tolerance is below this difference: 28 seeds out of 300 failed on AVX-512 (largest difference \(3 \cdot 10^{-8}\)). The absolute part is \(2^{-24}\), twice the bound. November 2026. |
| same | float64 |
relative_percent 1e-4 |
hybrid 1.2e-16 + 1e-4 × |ref| |
Same bound in double precision: \(2^{-54}\), absolute part \(\approx 2^{-53}\). No failure seen yet, a cancellation being much rarer at this relative tolerance. November 2026. |
acosh |
float32 |
max_abs_diff 2e-6 |
max_abs_diff 3e-6 |
acosh is ill-conditioned near 1 (derivative \(1/\sqrt{x^2-1}\)). Exhaustive comparison of every float32 of the domain \([1, 10]\) with the scalar reference: largest difference \(2.403 \cdot 10^{-6}\) at \(x = 1.00017\), the same on AVX2, AVX-512, NEON, SVE and RVV, so 2e-6 failed on about 1 seed out of 150. November 2026. |
exp |
float32, float64 |
relative_percent 1e-4 on \([-3, 3]\) |
hybrid 1.5e-45 + 2.5e-7 × |ref| (float32), 5e-324 + 5e-16 × |ref| (float64), on \([-105, 90]\) / \([-750, 712]\) with the overflow and subnormal boundaries as special values |
exp follows the C library over its whole range since the fix of its reduction constant (\(\ln 2 = C_1 + C_2\)) and of its scaling by \(2^n\): measured 1.2e-7 and 2.2e-16 (one unit in the last place, about 2 for the tolerance); the absolute part is one subnormal step, for the results rounded among the subnormals. The previous domain and tolerance hid an error of 3e-10 in float64 and the wrong values beyond $ |
cosh, sinh |
float64 |
hybrid 1e-6 + 1e-8 × |ref| on \([-10, 10]\) |
hybrid 5e-324 + 1.5e-15 × |ref|, on \([-712, 712]\) (and \([-90, 90]\) in float32, whose tolerance is unchanged) with the overflow boundary and tiny values as special values |
Computed from $e^{ |
pow2 |
float32, float64 |
relative_percent 1e-4 / 1e-6 on \([0, 10]\) |
hybrid 1.5e-45 + 2.5e-7 × |ref| / 5e-324 + 5e-16 × |ref|, on \([-152, 130]\) / \([-1077, 1026]\) with the overflow and subnormal boundaries as special values (the unsigned types: exact on \([0, 10]\), unchanged) |
Computed as \(2^n \, 2^f\) with an exact reduction since its fix (before: \(e^{x \ln 2}\), 4e-6 and 8e-14): measured 1.2e-7 and 2.2e-16. |
log |
float32, float64 |
relative_percent 1e-4 on \([0.001, 1000]\) |
hybrid 1.5e-45 + 2.5e-7 × |ref| / 5e-324 + 5e-16 × |ref|, same domain, with 0, the infinities, NaN, the negative elements, the subnormals, the smallest normal number and the largest value as special values |
log follows the C library since its fix (fdlibm algorithm in float64, subnormals scaled, special values): measured 1.2e-7 and 2.2e-16. The previous tolerance hid an error of 3.4e-9 in float64 and the subnormals counted as the smallest normal number. |
log2, log10 |
float32, float64 |
hybrid 1e-6 + 1e-6 × |ref| / 1e-8 + 1e-8 × |ref| |
hybrid 1.5e-45 + 3.5e-7 × |ref| / 5e-324 + 6e-16 × |ref|, with the special values of log |
Computed from log (one more rounding): measured 1.6e-7 and 2.6e-16. |
log1p, expm1 |
float32, float64 |
hybrid 1e-6 + 1e-6 × |ref| / 1e-8 + 1e-8 × |ref| |
hybrid 1.5e-45 + 4e-7 × |ref| / 5e-324 + 8e-16 (log1p) or 7e-16 (expm1) × |ref|, with 0, the infinities, NaN, -1, tiny and subnormal values as special values |
Computed from log (and exp for expm1) with a division: measured 2e-7 and 3.8e-16. |
cbrt |
float32, float64 |
hybrid 1e-6 + 1e-6 × |ref| / 1e-12 + 1e-12 × |ref| |
hybrid 1.5e-45 + 2.5e-7 × |ref| / 5e-324 + 5e-16 × |ref|, with the subnormals, 0, the infinities, NaN and the largest values as special values |
Computed as the C library since its fix (estimate from the bits, polynomial, one Newton-like step) instead of $e^{\ln |
acosh, asinh, atanh |
float32, float64 |
max_abs_diff 3e-6 / 1e-6 / 1e-6 (float32), 1e-8 / 1e-8 / 1e-9 (float64) |
hybrid 1.5e-45 + 5e-7 × |ref| / 5e-324 + 1e-15 × |ref|, on \([1, 10^4]\) / \([-10, 10]\) / \([-0.9999, 0.9999]\), with the boundaries (\(\pm 1\), \(2^{24}\) / \(2^{53}\), the largest values), the infinities, NaN and tiny values as special values |
Computed with the formulas of fdlibm and log1p since their fix, accurate near 1 (acosh) and near 0 (asinh, atanh, where the error was relative up to 0.24 in float32): measured 2.4e-7 and 4.6e-16. |
Other properties. special_values mixes given values into the random inputs every other iteration (by_datatype; a key can also be a conversion pair "float32,uint32", chosen before the key of the source type); lane_index draws a scalar argument that is a number of lanes at run time (lt_n, le_n, lt_nb, le_nb: the register size depends on the ISA and the LMUL); nan_inf_skip ignores the lanes where the difference is NaN or infinite; skip_condition ignores some lanes: fractional_part (the reference has a fractional part close to a value: the halfway cases of round), subnormal_on_armv7 (on ARMv7 NEON only, which flushes the subnormals, the lanes whose input or reference is subnormal) and lanes_read (the lanes after the count written by loadu_ff); overflow_check ignores the lanes where an addition, subtraction or multiplication overflows (accumulate_add / accumulate_mul: the test of a reduction that overflows); disabled comments the test out, with disabled_reason; support_obj: false skips the Object API.
Output arguments. A function that also writes a scalar through a pointer (valout: the extremum of argmin) is called with the pointer and without it (NULL in C, the default nullptr in C++): both results must be equal, and the scalar equal to the one of the scalar reference. With "valout_count": true (loadu_ff, where the scalar is the number of lanes read), the count must be between 1 and the one of the scalar reference, and only these lanes are compared. A function that returns a register and also writes output registers (regout: the exponents of frexp) gets rout<k> / sout<k> registers (mipp::details::int_of<T> for an int output in C++), compared lane by lane with the scalar reference, with the lanes skipped by skip_condition for the result skipped for them too. The functions whose only outputs are regout registers (loadu_unzip3...) have their own module (see below).
Functions that change the LMUL or the type. wcvt, ncvt, ncvts, ncvtsrshiftround, lo, hi and combine (and the _k of the last three) return a register of another LMUL than their arguments, and the conversions also change the datatype (wmul has a module of its own, see below). Nothing is written per function: the result LMUL comes from ret.lmul in registry_protos.json, and the source/destination pairs from the datatypes of the function. The result is read with the lane count and type of its own LMUL (size_out, LMUL_OUT in C++), and compared with the scalar ISA like every other function. A test is not generated for an LMUL where the function does not exist (no wcvt from m8, no lo from d2). Since an ISA may not provide a function for an LMUL, a test (C) or section (C++ and Object) is wrapped in #if defined(MIPP_<ISA>) || ... when some ISA does not provide it, built from the same rule as the headers (isa_provides_lmul, isa_guard()); when every ISA provides it, as for all the current targets, there is no guard. In C++, test_helpers::<function><T_dst> forwards to the overload of the destination type (mipp::wcvt_int64, ...), because the destination is a template parameter of the test.
Indexed functions. gather and scatter take a pointer and a register of unsigned indices, which the generic builder cannot generate. They are generated for pairs "data,index", the index having its own LMUL when its width differs from the one of the data (mipp::details::idx_lmul<T, IDX>(LMUL) in C++); a pair is only tested for the LMULs where this LMUL exists, and the mask has the type of the data. helpers_tests/indexed_tests.py writes their tests, with the same principle as the others: the result of the ISA under test is compared with the scalar reference (gather: the stored register; scatter: the memory written by both targets), for every datatype, LMUL, d2 and mask variant, in the three APIs. The indices address a table larger than a register: \(2^8\) elements for 8-bit indices, \(2^{16}\) for 16-bit indices and \(2^{17}\) for wider ones (and at least N), so that the whole range of the 8 and 16-bit indices is covered, including the byte offsets that overflow the index width. The indices of a gather are random values of the table; the ones of a scatter are N distinct positions of the table, so that the result does not depend on the order of the writes, and the whole table is compared. They have no entry in tests_specs.json.
Permutations, compress and expand. permute, permute2, permute_b128, compress, expand, storeu_compress, storeuf_compress and loadu_expand take a register of indices or a mask that moves the lanes, which the generic builder cannot generate either. helpers_tests/perm_tests.py writes their tests, with the same principle: the result is compared with the scalar reference, for every datatype, LMUL, d2 (when the function has one) and mask variant, in the three APIs. The indices are drawn in [0, N) for permute, in the lanes of a block of 128 bits for permute_b128, and in [0, 2N) for permute2 (bounded by the largest index of the type). storeu_compress compares the returned count and the whole output buffer; storeuf_compress compares the count and the first count elements, since the next ones are unspecified.
Structures of 2 to 4 fields. loadu_unzip2, loadu_unzip3 and loadu_unzip4 write several registers (pointers in C, references in C++), and the memory of storeu_zip2, storeu_zip3 and storeu_zip4 holds 2 to 4 registers. helpers_tests/interleaved_tests.py writes their tests: the registers loaded from an unaligned pointer are stored one after the other, the memory written through an unaligned pointer is compared with one element of margin on each side (which must be unchanged), both against the scalar reference, for every datatype, LMUL and d2, in the three APIs. The scalar reference itself is checked against the definition (lane i of the register k: element n i + k).
Constant stride. The memory of loadu_strided and storeu_strided spans N s0 elements, not N. helpers_tests/strided_tests.py writes their tests, with a stride drawn in [0, 5] for the load and in [1, 5] for the store (a stride of 0 is undefined behavior for a store: every lane to the same element): the register loaded from an unaligned pointer, or the whole memory written (5 N + 2 elements, the ones between and after the written lanes must be unchanged), is compared with the scalar reference, and the scalar reference with the definition (lane i: element i s0), for every datatype, LMUL and d2, in the three APIs.
Registers of different types. The dot products dot2, dot4 and dot4us take an accumulator of 32-bit lanes and two registers of 16 or 8-bit lanes of the same size (dot4us: an unsigned and a signed one), and the widening multiplication wmul returns the type twice as wide with the LMUL above: the generic builder gives every register the type of the parameter. helpers_tests/dot_tests.py writes their tests: the result is compared with the scalar reference, and the scalar reference with the definition computed in the test (modulo \(2^{32}\) for the dot products, the exact product for wmul), for every pair of datatypes, LMUL and d2, in the three APIs.
Known gaps
overflow_checkwithadd,subormulis only applied in the C tests; the C++ tests only handle theaccumulate_*values (reductions).- The
definefield of a domain rule is accepted by the schema but ignored.
4. Code Templates¶
The skeletons of the C++ code of the tests come from helpers_tests/tests_templates.json (Test Templates): the file headers, the bodies of the test functions, the TEST_CASE and SECTION skeletons and the assertions. The rest is written in Python: part of the declarations (buffers, ...) by tests_builder.py, and the whole tests of the dedicated modules (indexed_tests.py, perm_tests.py, interleaved_tests.py, strided_tests.py, dot_tests.py). The templates contain {{placeholder}} variables filled by tests_builder.py, and %token<datatype,lmul>(argument)% markers that depend on the API (%load<...>(p)%, %reg_type<...>%, %set_k<...>(p)%, %N<...>%, ...).
These markers are resolved by the dialect adapters (dialect_adapters.py): for a load with LMUL 2, CDialectAdapter writes mipp_load_float32_m2(p), CppDialectAdapter writes mipp::load<T, 2>(p), CppObjDialectAdapter writes mipp::Vd<T, 2>(p). The adapters only format expressions; the structure of the test is the same for the three APIs.
5. Module Map¶
File (in generator/) |
Role |
|---|---|
gen_mipp_tests.py |
Command line; loops over the APIs, LMUL values and mask variants. |
helpers_tests/tests_builder.py |
TestsBuilderEngine: loads and validates the JSON files, renders the tests (TestsBuilderEngineC, TestsBuilderEngineCpp, TestsBuilderEngineObj), tolerances and assertions, test audits. |
helpers_tests/domain_resolver.py |
DomainResolver: input domains and mask patterns per datatype, LMUL, mask variant and ISA. |
helpers_tests/dialect_adapters.py |
Expression syntax of the C, C++ Functional and C++ Object APIs. |
helpers_tests/dot_tests.py |
Tests of dot2, dot4, dot4us and wmul, written in Python. |
helpers_tests/indexed_tests.py |
Tests of gather and scatter, written in Python. |
helpers_tests/interleaved_tests.py |
Tests of loadu_unzip2/3/4 and storeu_zip2/3/4, written in Python. |
helpers_tests/perm_tests.py |
Tests of permute, permute2, permute_b128, compress, expand, storeu_compress, storeuf_compress and loadu_expand, written in Python. |
helpers_tests/strided_tests.py |
Tests of loadu_strided and storeu_strided, written in Python. |
helpers_tests/tests_specs.json, tests_templates.json |
Test properties and code templates. |
helpers_tests/schemas/ |
JSON Schemas of the two files above. |