Skip to content

Scalar Forms

Many functions take several data vectors: sub(a, b), fmadd(a, b, c), cmplt(a, b)... Their scalar forms take a scalar instead of some of these vectors: the scalar is used for every lane, as if it had been broadcast with set1.


1. Names

A form is written with one letter per data vector of the function, in order: v for a vector, s for a scalar of the element type. The function itself is the form with only v letters. In C, its alias is always named _vv, whatever the number of vectors (mipp_fmadd_vv_float32, not _vvv).

Form Meaning C C++
vv \(a_i - b_i\) mipp_sub_float32(a, b), alias mipp_sub_vv_float32 mipp::sub(a, b)
vs \(a_i - s\) mipp_sub_vs_float32(a, s) mipp::sub(a, s)
sv \(s - b_i\) mipp_sub_sv_float32(s, b) mipp::sub(s, b)
vsv \(a_i \cdot s + c_i\) mipp_fmadd_vsv_float32(a, s, c) mipp::fmadd(a, s, c)
  • C API. The form comes right after the name of the function, before the type, the mask variant and the LMUL: mipp_sub_sv_float32_maskz_m2. Every function with scalar forms also has _vv aliases of its base names (mipp_sub_vv_float32_maskz_m2 is mipp_sub_float32_maskz_m2), for code that spells all the forms.
  • C++ APIs. The forms are overloads of the function: mipp::sub(a, s) and mipp::sub(s, b). The scalar must have the element type exactly in the masked calls and in the Object API (T is deduced from it): mipp::sub<mipp::M>(m, 10.f, va) with a float register, not 10.
  • Object API operators. va - 1.f, 10.f - va and va < 0.f already broadcast the scalar: they compute the same thing as the forms.
  • Headers. Each form has its own header, named after it: fun/add_vs.h in C, fun/add_vs.hpp in the C++ APIs, and sub_sv, fmadd_vsv... The header of the function (fun/add.hpp) does not provide its forms. The category headers (cat/arithmetic.hpp) and the umbrella headers (mipp.h, mipp.hpp, mipp_obj.hpp) include them (see Headers).

The forms exist for every datatype, mask variant and LMUL of their function. §6 lists them: the commutative functions have no sv form (add(s, b) is add(b, s)), and the functions whose scalar operand only gives a sign have the vs form only (copysign, xorsign).


2. Usage

vd_float32_t va = mipp_load_float32(A);
vd_float32_t vb = mipp_load_float32(B);
vd_float32_t vd1 = mipp_sub_vs_float32(va, 1.f);        // a - 1
vd_float32_t vd2 = mipp_sub_sv_float32(10.f, va);       // 10 - a
vd_float32_t vd3 = mipp_fmadd_vsv_float32(va, 2.f, vb); // a * 2 + b
vm_float32_t m = mipp_cmplt_vs_float32(va, 0.f);        // a < 0
mipp::vd<float> va = mipp::load<float>(A);
mipp::vd<float> vb = mipp::load<float>(B);
mipp::vd<float> vd1 = mipp::sub(va, 1.f);       // a - 1
mipp::vd<float> vd2 = mipp::sub(10.f, va);      // 10 - a
mipp::vd<float> vd3 = mipp::fmadd(va, 2.f, vb); // a * 2 + b
mipp::vm<float> m = mipp::cmplt(va, 0.f);       // a < 0
mipp::Vd<float> va(A);
mipp::Vd<float> vb(B);
mipp::Vd<float> vd1 = va - 1.f;                 // a - 1
mipp::Vd<float> vd2 = 10.f - va;                // 10 - a
mipp::Vd<float> vd3 = mipp::fmadd(va, 2.f, vb); // a * 2 + b
mipp::Vm<float> m = va < 0.f;                   // a < 0

3. Masked Forms

The mask variants apply to the forms. The merge masking (M, _maskm) keeps the first vector of the form in the inactive lanes: for sub_sv(s, b), the inactive lanes are \(b_i\) (a scalar cannot be merged into). It is the convention of SVE (svsubr_n_f32_m).

// merge masking keeps the register operand: m ? 10 - a : a
vd_float32_t vd4 = mipp_sub_sv_float32_maskm(m, 10.f, va);
// merge masking keeps the register operand: m ? 10 - a : a
mipp::vd<float> vd4 = mipp::sub<mipp::M>(m, 10.f, va);
// merge masking keeps the register operand: m ? 10 - a : a
mipp::Vd<float> vd4 = mipp::sub<mipp::M>(m, 10.f, va);

4. Shifts

The shifts take a count for each lane, in a register of the same type, or the same count for every lane:

Function Count C C++ Object API
lshift, rshift, rshiftl, rshiftround, rotl, rotr one per lane, a register mipp_lshift_int32(a, n) mipp::lshift(a, n) a << n
lshift_vs, rshift_vs, rshiftl_vs, rshiftround_vs, rotl_vs, rotr_vs the same for every lane, an int32_t mipp_lshift_vs_int32(a, 3) mipp::lshift(a, 3) a << 3

With a constant count, the C++ APIs also have mipp::lshift<3>(a) / mipp::rshift<3>(a) / mipp::rshiftl<3>(a) / mipp::rshiftround<3>(a) / mipp::rotl<3>(a) / mipp::rotr<3>(a), and the shift instructions with an immediate count are used (see Constant Counts). rshift is arithmetic (copies of the sign bit) for the signed types and logical (zeros) for the unsigned ones, like >> in C++20 (in C, the shift of a negative number is implementation-defined, and arithmetic with GCC, Clang and MSVC). rshiftl is logical for every type: a signed element is shifted as the unsigned integer of the same size, like (int32_t)((uint32_t)a >> n). rshiftround is rshift rounded to the nearest integer, ties up (\((a_i + 2^{n-1}) \gg n\) without overflow). rotl and rotr rotate the bits of each element (not the lanes, which is rotate). Only lshift and rshift have operators. A count must be between 0 and the number of bits of the type minus 1: other counts are undefined behavior (the hardware differs), except for rotl and rotr, whose count is taken modulo the number of bits. The per-lane shifts and the rotations exist for the integers; lshift_vs, rshift_vs and rshiftl_vs also shift the bits of the floating-point numbers.

vd_int32_t vi = mipp_load_int32(I);
vd_int32_t vn = mipp_load_int32(CNT);        // a count per lane, in [0, 31]
vd_int32_t s1 = mipp_lshift_int32(vi, vn);   // i << n, lane by lane
vd_int32_t s2 = mipp_rshift_vs_int32(vi, 1); // i >> 1, every lane
mipp::vd<int32_t> vi = mipp::load<int32_t>(I);
mipp::vd<int32_t> vn = mipp::load<int32_t>(CNT); // a count per lane, in [0, 31]
mipp::vd<int32_t> s1 = mipp::lshift(vi, vn);     // i << n, lane by lane
mipp::vd<int32_t> s2 = mipp::rshift(vi, 1);      // i >> 1, every lane
mipp::Vd<int32_t> vi(I);
mipp::Vd<int32_t> vn(CNT);       // a count per lane, in [0, 31]
mipp::Vd<int32_t> s1 = vi << vn; // i << n, lane by lane
mipp::Vd<int32_t> s2 = vi >> 1;  // i >> 1, every lane

5. Implementation Levels

Every form has a generic implementation: the function with the scalar broadcast (sub(a, set1(s))). The broadcast is free, so a form has the implementation level of its function. Some ISAs also have instructions that take a scalar operand, used directly:

  • RVV: .vx / .vf instructions (vfrsub.vf for sub_sv, vfmadd.vf for fmadd_vsv, vmslt.vx...);
  • SVE: _n intrinsics (svsubr_n, svmla_n, svcmplt_n...);
  • x86, NEON: the broadcast and the operation of the function.

6. Functions with Scalar Forms

Function Forms C (example) C++
add vs mipp_add_vs_float32 mipp::add(a, s)
adds vs mipp_adds_vs_int32 mipp::adds(a, s)
andb vs mipp_andb_vs_float32 mipp::andb(a, s)
andnb vs, sv mipp_andnb_vs_float32, mipp_andnb_sv_float32 mipp::andnb(a, s), mipp::andnb(s, b)
atan2 vs, sv mipp_atan2_vs_float32, mipp_atan2_sv_float32 mipp::atan2(a, s), mipp::atan2(s, b)
clamp vss mipp_clamp_vss_float32 mipp::clamp(a, s, s)
cmpeq vs mipp_cmpeq_vs_float32 mipp::cmpeq(a, s)
cmpge vs, sv mipp_cmpge_vs_float32, mipp_cmpge_sv_float32 mipp::cmpge(a, s), mipp::cmpge(s, b)
cmpgt vs, sv mipp_cmpgt_vs_float32, mipp_cmpgt_sv_float32 mipp::cmpgt(a, s), mipp::cmpgt(s, b)
cmple vs, sv mipp_cmple_vs_float32, mipp_cmple_sv_float32 mipp::cmple(a, s), mipp::cmple(s, b)
cmplt vs, sv mipp_cmplt_vs_float32, mipp_cmplt_sv_float32 mipp::cmplt(a, s), mipp::cmplt(s, b)
cmpneq vs mipp_cmpneq_vs_float32 mipp::cmpneq(a, s)
copysign vs mipp_copysign_vs_float32 mipp::copysign(a, s)
div vs, sv mipp_div_vs_float32, mipp_div_sv_float32 mipp::div(a, s), mipp::div(s, b)
fmadd vsv, vvs mipp_fmadd_vsv_float32, mipp_fmadd_vvs_float32 mipp::fmadd(a, s, c), mipp::fmadd(a, b, s)
fmsub vsv, vvs mipp_fmsub_vsv_float32, mipp_fmsub_vvs_float32 mipp::fmsub(a, s, c), mipp::fmsub(a, b, s)
fnmadd vsv, vvs mipp_fnmadd_vsv_float32, mipp_fnmadd_vvs_float32 mipp::fnmadd(a, s, c), mipp::fnmadd(a, b, s)
fnmsub vsv, vvs mipp_fnmsub_vsv_float32, mipp_fnmsub_vvs_float32 mipp::fnmsub(a, s, c), mipp::fnmsub(a, b, s)
hypot vs, sv mipp_hypot_vs_float32, mipp_hypot_sv_float32 mipp::hypot(a, s), mipp::hypot(s, b)
max vs mipp_max_vs_float32 mipp::max(a, s)
min vs mipp_min_vs_float32 mipp::min(a, s)
mul vs mipp_mul_vs_float32 mipp::mul(a, s)
orb vs mipp_orb_vs_float32 mipp::orb(a, s)
pow vs, sv mipp_pow_vs_float32, mipp_pow_sv_float32 mipp::pow(a, s), mipp::pow(s, b)
select vs, sv mipp_select_vs_float32, mipp_select_sv_float32 mipp::select(m, a, s), mipp::select(m, s, b)
sub vs, sv mipp_sub_vs_float32, mipp_sub_sv_float32 mipp::sub(a, s), mipp::sub(s, b)
subs vs, sv mipp_subs_vs_int32, mipp_subs_sv_int32 mipp::subs(a, s), mipp::subs(s, b)
xorb vs mipp_xorb_vs_float32 mipp::xorb(a, s)
xorsign vs mipp_xorsign_vs_float32 mipp::xorsign(a, s)