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

DigitsType

Member Functions

Name

Description

ScaledNumber [constructor]

Constructors

compare

compareTo

dump

getDigits

getScale

inverse

invert

isLargest

isOne

isZero

lg

The log base 2, rounded.

lgCeiling

The log base 2, rounded towards INT32_MAX.

lgFloor

The log base 2, rounded towards INT32_MIN.

operator*=

Multiplication assignment operator

operator+=

Addition assignment operator

operator‐=

Subtraction assignment operator

operator/=

Division assignment operator

operator<<=

Left shift assignment operator

operator>>=

Right shift operator

print

Print a decimal representation.

scale

Scale a large number accurately.

scaleByInverse

toInt

Convert to the given integer type.

toString

Convert to a decimal representation in a string.

operator!

Negation operator

operator==

Equality operator

operator!=

Inequality operator

operator<

Less‐than operator

operator<=

Less‐than‐or‐equal operator

operator>

Greater‐than operator

operator>=

Greater‐than‐or‐equal operator

Static Member Functions

Non-Member Functions

Name

Description

operator!=

Inequality operator

operator!=

Inequality operator

operator!=

Inequality operator

operator!=

Inequality operator

operator*

Multiplication operator

operator+

Addition operator

operator‐

Subtraction operator

operator/

Division operator

operator<

Less‐than operator

operator<

Less‐than operator

operator<

Less‐than operator

operator<

Less‐than operator

operator<<

Left shift operator

operator<=

Less‐than‐or‐equal operator

operator<=

Less‐than‐or‐equal operator

operator<=

Less‐than‐or‐equal operator

operator<=

Less‐than‐or‐equal operator

operator==

Equality operator

operator==

Equality operator

operator==

Equality operator

operator==

Equality operator

operator>

Greater‐than operator

operator>

Greater‐than operator

operator>

Greater‐than operator

operator>

Greater‐than operator

operator>=

Greater‐than‐or‐equal operator

operator>=

Greater‐than‐or‐equal operator

operator>=

Greater‐than‐or‐equal operator

operator>=

Greater‐than‐or‐equal operator

operator>>

Right shift operator

Created with MrDocs