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

DigitsType

Unsigned integer type used for the digit (mantissa) field.

Member Functions

Name

Description

ScaledNumber [constructor]

Constructors

compare

Compare this number to X.

compareTo

compareTo overloads

dump

Dump this number to the debug stream.

getDigits

Return the digit (mantissa) field of this number.

getScale

Return the scale (exponent) of this number.

inverse

Return the reciprocal of this number.

invert

Replace this number with its reciprocal.

isLargest

Return true if this number is the largest representable value.

isOne

Return true if this number equals one.

isZero

Return true if this number is zero.

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*=

Multiply this number by X using saturating arithmetic.

operator+=

Add X to this number using saturating arithmetic.

operator‐=

Subtract X from this number using saturating arithmetic.

operator/=

Divide this number by X using saturating arithmetic.

operator<<=

Shift this number left by Shift (or right if negative).

operator>>=

Shift this number right by Shift (or left if negative).

print

Print a decimal representation.

scale

scale overloads

scaleByInverse

scaleByInverse overloads

toInt

Convert to the given integer type.

toString

Convert to a decimal representation in a string.

operator!

Return true if this number is zero.

operator==

Return true if this number equals X.

operator!=

Return true if this number differs from X.

operator<

Return true if this number is strictly less than X.

operator<=

Return true if this number is less than or equal to X.

operator>

Return true if this number is strictly greater than X.

operator>=

Return true if this number is greater than or equal to X.

Static Member Functions

Name

Description

get

Construct a scaled number from the integer N.

getFraction

Return the scaled number representing the fraction N / D.

getInverse

Return the reciprocal of the integer N as a scaled number.

getLargest

Return the largest representable scaled number.

getOne

Return a scaled number representing one.

getZero

Return a scaled number representing zero.

Non-Member Functions

Name

Description

operator!=

Return true if L differs from R.

operator!=

Return true if L differs from R.

operator!=

Return true if L differs from R.

operator!=

Return true if L differs from R.

operator*

Return the product of L and R using saturating arithmetic.

operator+

Return the sum of L and R using saturating arithmetic.

operator‐

Return the difference of L and R using saturating arithmetic.

operator/

Return the quotient of L and R using saturating arithmetic.

operator<

Return true if L is strictly less than R.

operator<

Return true if L is strictly less than R.

operator<

Return true if L is strictly less than R.

operator<

Return true if L is strictly less than R.

operator<<

Return L shifted left by Shift places.

operator<=

Return true if L is less than or equal to R.

operator<=

Return true if L is less than or equal to R.

operator<=

Return true if L is less than or equal to R.

operator<=

Return true if L is less than or equal to R.

operator==

Return true if L equals R.

operator==

Return true if L equals R.

operator==

Return true if L equals R.

operator==

Return true if L equals R.

operator>

Return true if L is strictly greater than R.

operator>

Return true if L is strictly greater than R.

operator>

Return true if L is strictly greater than R.

operator>

Return true if L is strictly greater than R.

operator>=

Return true if L is greater than or equal to R.

operator>=

Return true if L is greater than or equal to R.

operator>=

Return true if L is greater than or equal to R.

operator>=

Return true if L is greater than or equal to R.

operator>>

Return L shifted right by Shift places.

Created with MrDocs