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 |
Member Functions
Name |
Description |
|
Constructors |
The log base 2, rounded. |
|
The log base 2, rounded towards INT32_MAX. |
|
The log base 2, rounded towards INT32_MIN. |
|
Multiplication assignment operator |
|
Addition assignment operator |
|
Subtraction assignment operator |
|
Division assignment operator |
|
Left shift assignment operator |
|
Right shift operator |
|
Print a decimal representation. |
|
Scale a large number accurately. |
|
Convert to the given integer type. |
|
Convert to a decimal representation in a string. |
|
Negation operator |
|
Equality operator |
|
Inequality operator |
|
Less‐than operator |
|
Less‐than‐or‐equal operator |
|
Greater‐than operator |
|
Greater‐than‐or‐equal operator |
Static Member Functions
Non-Member Functions
Name |
Description |
Inequality operator |
|
Inequality operator |
|
Inequality operator |
|
Inequality operator |
|
Multiplication operator |
|
Addition operator |
|
Subtraction operator |
|
Division operator |
|
Less‐than operator |
|
Less‐than operator |
|
Less‐than operator |
|
Less‐than operator |
|
Left shift operator |
|
Less‐than‐or‐equal operator |
|
Less‐than‐or‐equal operator |
|
Less‐than‐or‐equal operator |
|
Less‐than‐or‐equal operator |
|
Equality operator |
|
Equality operator |
|
Equality operator |
|
Equality operator |
|
Greater‐than operator |
|
Greater‐than operator |
|
Greater‐than operator |
|
Greater‐than operator |
|
Greater‐than‐or‐equal operator |
|
Greater‐than‐or‐equal operator |
|
Greater‐than‐or‐equal operator |
|
Greater‐than‐or‐equal operator |
|
Right shift operator |
Created with MrDocs