Simple representation of a scaled number.
Synopsis
Declared in <llvm/Support/ScaledNumber.h>
template<class DigitsT>
class ScaledNumber;
Description
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()*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.
Type Aliases
Name |
Description |
Unsigned integer type used for the digit (mantissa) field. |
Member Functions
Name |
Description |
|
Constructors |
Compare this number to |
|
|
|
Dump this number to the debug stream. |
|
Return the digit (mantissa) field of this number. |
|
Return the scale (exponent) of this number. |
|
Return the reciprocal of this number. |
|
Replace this number with its reciprocal. |
|
Return true if this number is the largest representable value. |
|
Return true if this number equals one. |
|
Return true if this number is zero. |
|
The log base 2, rounded. |
|
The log base 2, rounded towards INT32_MAX. |
|
The log base 2, rounded towards INT32_MIN. |
|
Multiply this number by |
|
Add |
|
Subtract |
|
Divide this number by |
|
Shift this number left by |
|
Shift this number right by |
|
Print a decimal representation. |
|
|
|
|
|
Convert to the given integer type. |
|
Convert to a decimal representation in a string. |
|
Return true if this number is zero. |
|
Return true if this number equals |
|
Return true if this number differs from |
|
Return true if this number is strictly less than |
|
Return true if this number is less than or equal to |
|
Return true if this number is strictly greater than |
|
Return true if this number is greater than or equal to |
Static Member Functions
Name |
Description |
Construct a scaled number from the integer |
|
Return the scaled number representing the fraction |
|
Return the reciprocal of the integer |
|
Return the largest representable scaled number. |
|
Return a scaled number representing one. |
|
Return a scaled number representing zero. |
Non-Member Functions
Name |
Description |
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return the product of |
|
Return the sum of |
|
Return the difference of |
|
Return the quotient of |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return true if |
|
Return |
Created with MrDocs