LLVM: llvm::ScaledNumber< DigitsT > Class Template Reference (original) (raw)

template
class llvm::ScaledNumber< DigitsT >

Simple representation of a scaled number.

ScaledNumber is a number represented by digits and a scale. It uses simple saturation arithmetic and every operation is well-defined for every value. It's somewhat similar in behaviour to a soft-float, but is not a replacement for one. If you're doing numerics, look at APFloat instead. Nevertheless, we've found these semantics useful for modelling certain cost metrics.

The number is split into a signed scale and unsigned digits. The number represented is [getDigits()](#a774bd871787807c5006ac027c6d5e492)*2^getScale(). In this way, the digits are much like the mantissa in the x87 long double, but there is no canonical form so the same number can be represented by many bit representations.

ScaledNumber is templated on the underlying integer type for digits, which is expected to be unsigned.

Unlike APFloat, ScaledNumber does not model architecture floating point behaviour – while this might make it a little faster and easier to reason about, it certainly makes it more dangerous for general numerics.

ScaledNumber is totally ordered. However, there is no canonical form, so there are multiple representations of most scalars. E.g.:

ScaledNumber(8u, 0) == ScaledNumber(4u, 1) ScaledNumber(4u, 1) == ScaledNumber(2u, 2) ScaledNumber(2u, 2) == ScaledNumber(1u, 3)

ScaledNumber implements most arithmetic operations. Precision is kept where possible. Uses simple saturation arithmetic, so that operations saturate to 0.0 or getLargest() rather than under or overflowing. It has some extra arithmetic for unit inversion. 0.0/0.0 is defined to be 0.0. Any other division by 0.0 is defined to be getLargest().

As a convenience for modifying the exponent, left and right shifting are both implemented, and both interpret negative shifts as positive shifts in the opposite direction.

Scales are limited to the range accepted by x87 long double. This makes it trivial to add functionality to convert to APFloat (this is already relied on for the implementation of printing).

Possible (and conflicting) future directions:

  1. Turn this into a wrapper around APFloat.
  2. Share the algorithm implementations with APFloat.
  3. Allow ScaledNumber to represent a signed number.

Definition at line 496 of file ScaledNumber.h.