|
MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
|
exponent(m): the e with 2^e <= m < 2^(e+1), for a positive finite m. More...
#include <pow2_scaling.hpp>
Static Public Member Functions | |
| static int | exponent (T m) |
| static T | scale (T x, int k) |
exponent(m): the e with 2^e <= m < 2^(e+1), for a positive finite m.
scale(x, k): x * 2^k, exact whenever 2^k and the result are values of T.
The fallback works with two ladders of exact powers of two, 2^(2^i) and 2^-(2^i), each rung the square of the one before, kept while the square is exact, and built once per type. So both functions cost O(log |e|) multiplications, not |e| (#680).
A tapered type (posit, takum) is the hard case: near maxpos and minpos its values can lie farther apart than the largest exact rung, so m / 2^(2^i) rounds, and can round back to m. A search that steps m toward [1, 2) by powers of two, a binade or a rung at a time, then stalls or drifts (#680). So a T that converts to double reads e from that conversion, which is exact for any value inside double's range; the rungs only bring m into that range, which a type with a wider range (cfloat<256,19>) needs, and there the steps are exact. A T that does not convert to double is stepped by the rungs to [1, 2), and a step that does not move m ends the search, so it always terminates; it is exact where every step lands on a value of T, as in a binary floating-point type. A type with coarser ends than its rungs and no conversion should specialize the trait.
scale() multiplies x by 2^k once, so a tapered type rounds the result once: 2^k taken from double where T converts and 2^k is a value of T, otherwise formed from the ladder. Where 2^k itself is not a value of T (2^1074 in an IEEE-like double, which only scales subnormals), x is stepped by the rungs instead, largest first.
|
inlinestatic |
|
inlinestatic |