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_vvaliases of its base names (mipp_sub_vv_float32_maskz_m2ismipp_sub_float32_maskz_m2), for code that spells all the forms. - C++ APIs. The forms are overloads of the function:
mipp::sub(a, s)andmipp::sub(s, b). The scalar must have the element type exactly in the masked calls and in the Object API (Tis deduced from it):mipp::sub<mipp::M>(m, 10.f, va)with afloatregister, not10. - Object API operators.
va - 1.f,10.f - vaandva < 0.falready broadcast the scalar: they compute the same thing as the forms. - Headers. Each form has its own header, named after it:
fun/add_vs.hin C,fun/add_vs.hppin the C++ APIs, andsub_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
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).
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.
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/.vfinstructions (vfrsub.vfforsub_sv,vfmadd.vfforfmadd_vsv,vmslt.vx...); - SVE:
_nintrinsics (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) |