Complex Numbers¶
MIPP has no complex type: a register of float32 or float64 holds complex numbers in the interleaved layout, the layout of std::complex<T> and C _Complex arrays in memory. The real part of each complex number is in an even lane, its imaginary part in the next odd lane: a register of N lanes holds N / 2 complex numbers.
1. Functions¶
| Function | Result, for each pair of lanes |
|---|---|
cmul(a, b) |
\(a b\) |
cmulconj(a, b) |
\(a \bar{b}\) |
cfmadd(a, b, c) |
\(a b + c\) |
cdiv(a, b) |
\(a / b\) |
conj(a) |
\(\bar{a}\) |
cnorm(a) |
\(\lvert a \rvert^2 = a_r^2 + a_i^2\), in both lanes |
cabs(a) |
\(\lvert a \rvert\), in both lanes |
cabsdoes not overflow when \(a_r^2 + a_i^2\) does: it divides the two parts by the larger one before squaring them (ashypot). It is \(+\infty\) when a part is infinite, and \(0\) for \(0\).cdivuses the algorithm of Smith: it divides by the part of \(b\) of the larger absolute value, which avoids the overflows and most of the losses of \(b_r^2 + b_i^2\). A division by \(0\) gives infinities or NaNs, as the scalar division.cnormandcabswrite the result in the two lanes of each pair, so that it can divide or scale the complex numbers directly (div(a, cabs(a))normalizes them).
The functions exist for float32 and float64, for every LMUL and mask variant; the masks are applied lane by lane, like any other function (use a mask whose lanes go by pairs to select complex numbers). A pair never straddles two registers of a group: with LMUL 2, the N / 2 first complex numbers are in the first register. The functions have no half-vector (d2) version.
2. Interleaved Layout¶
The complex numbers are loaded as they are in memory: Z below is an array of complex numbers {re0, im0, re1, im1, ...}.
vd_float32_t a = mipp_load_float32(Z); // re0 im0 re1 im1 ...
vd_float32_t b = mipp_load_float32(W);
vd_float32_t p = mipp_cmul_float32(a, b); // a * b
vd_float32_t q = mipp_cdiv_float32(a, b); // a / b
vd_float32_t s = mipp_cfmadd_float32(a, b, p); // a * b + p
vd_float32_t m = mipp_cabs_float32(a); // |a|, in both lanes of each pair
mipp::vd<float> a = mipp::load<float>(Z); // re0 im0 re1 im1 ...
mipp::vd<float> b = mipp::load<float>(W);
mipp::vd<float> p = mipp::cmul(a, b); // a * b
mipp::vd<float> q = mipp::cdiv(a, b); // a / b
mipp::vd<float> s = mipp::cfmadd(a, b, p); // a * b + p
mipp::vd<float> m = mipp::cabs(a); // |a|, in both lanes of each pair
3. Split Layout¶
When the real and imaginary parts are in two separate arrays (structure of arrays), the ordinary functions compute the complex operations on N complex numbers at a time, with all the lanes useful and without moving them: this is usually the fastest layout for long loops. loadu_unzip gives the two parts from interleaved data, and storeu_zip stores them back (see Rearrangements).
vd_float32_t ar = mipp_load_float32(AR), ai = mipp_load_float32(AI);
vd_float32_t br = mipp_load_float32(BR), bi = mipp_load_float32(BI);
vd_float32_t pr = mipp_fmsub_float32(ar, br, mipp_mul_float32(ai, bi)); // ar br - ai bi
vd_float32_t pi = mipp_fmadd_float32(ar, bi, mipp_mul_float32(ai, br)); // ar bi + ai br
4. Implementation Levels¶
cmul, cmulconj and cfmadd have a dedicated implementation (L1) on ARM: vcmlaq on NEON (AArch64 with the Armv8.3 complex extension, __ARM_FEATURE_COMPLEX) and svcmla on SVE, two instructions per product. On x86, cmul uses fmaddsub (with FMA) or addsub (SSE3), and moveldup / movehdup to duplicate the parts. The other functions, and every function on the other ISAs, are generic: they combine dupeven, dupodd, swap2 and arithmetic functions. On 32-bit NEON (ARMv7, no __aarch64__), the complex functions are scalar fallbacks (L3), except cnorm on float32 (and cmul, cmulconj, cfmadd on float32 when __ARM_FEATURE_COMPLEX is defined: vcmlaq). On RVV without 64-bit floating-point elements (__riscv_v_elen_fp < 64), they are scalar fallbacks on float64. The levels of each function are in its reference page.