Skip to main content

Module complex

Module complex 

Source
Expand description

Complex arithmetic, for folding a constant expression.

static double _Complex z = (1.0 + 2.0i) * (3.0 - 4.0i); has to become a literal: a Rust static initialiser is a constant expression, and crate::sema reduces every arithmetic one to a value before code generation ever sees it. So the front end needs the same arithmetic the runtime has, and this is it.

§Why it is written twice

The generated code calls cinrs_rt::complex, a no_std crate built around num_complex::Complex; this crate is the procedural macro’s own front end and has no business depending on the runtime — a build that said default-features = false would then still compile num-complex for a feature it had switched off. The two copies are kept honest by a test rather than by a shared crate: tests/complex.rs folds a table of constants through this code and computes the same products and quotients through the runtime at run time, and fails if any bit differs.

See cinrs_rt::complex for what the algorithms are and why: the naive product with Annex G.5.1’s infinity recovery, Smith’s algorithm for the double quotient, and the closed form in the wider format for the float one.

Functions§

div
The quotient of two double _Complex constants: Smith’s algorithm with Annex G.5.1’s recovery.
div_f32
The quotient of two float _Complex constants: the closed form in double, which cannot overflow for float operands, with the same recovery and a single rounding at the end.
mul
The product of two double _Complex constants.
mul_f32
The product of two float _Complex constants, computed in single precision.

Type Aliases§

Parts
The two parts of a complex value, always carried as f64.