class ScaledNumber

Declaration

template <class DigitsT>
class ScaledNumber : private ScaledNumberBase { /* full declaration omitted */ };

Description

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 \a 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 \c 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 \a APFloat. 2. Share the algorithm implementations with \a APFloat. 3. Allow \a ScaledNumber to represent a signed number.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:492

Inherits from: ScaledNumberBase

Templates

DigitsT

Member Variables

private llvm::ScaledNumber::DigitsType Digits = 0
private int16_t Scale = 0
private static const int Width = sizeof(type-parameter-0-0) * 8

Method Overview

  • public constexpr ScaledNumber<DigitsT>(llvm::ScaledNumber::DigitsType Digits, int16_t Scale)
  • private ScaledNumber<DigitsT>(const std::pair<DigitsT, int16_t> & X)
  • public ScaledNumber<DigitsT>()
  • private static ScaledNumber<DigitsT> adjustToWidth(uint64_t N, int32_t Shift)
  • public int compare(const ScaledNumber<DigitsT> & X) const
  • public int compareTo(int64_t N) const
  • public int compareTo(uint64_t N) const
  • private static int countLeadingZerosWidth(llvm::ScaledNumber::DigitsType Digits)
  • public void dump() const
  • public static ScaledNumber<DigitsT> get(uint64_t N)
  • public llvm::ScaledNumber::DigitsType getDigits() const
  • public static ScaledNumber<DigitsT> getFraction(llvm::ScaledNumber::DigitsType N, llvm::ScaledNumber::DigitsType D)
  • public static ScaledNumber<DigitsT> getInverse(uint64_t N)
  • public static ScaledNumber<DigitsT> getLargest()
  • public static ScaledNumber<DigitsT> getOne()
  • private static ScaledNumber<DigitsT> getProduct(llvm::ScaledNumber::DigitsType LHS, llvm::ScaledNumber::DigitsType RHS)
  • private static ScaledNumber<DigitsT> getQuotient(llvm::ScaledNumber::DigitsType Dividend, llvm::ScaledNumber::DigitsType Divisor)
  • private static ScaledNumber<DigitsT> getRounded(ScaledNumber<DigitsT> P, bool Round)
  • public int16_t getScale() const
  • public static ScaledNumber<DigitsT> getZero()
  • public ScaledNumber<DigitsT> inverse() const
  • public ScaledNumber<DigitsT> & invert()
  • public bool isLargest() const
  • public bool isOne() const
  • public bool isZero() const
  • public int32_t lg() const
  • public int32_t lgCeiling() const
  • public int32_t lgFloor() const
  • private ScaledNumber<DigitsT> matchScales(ScaledNumber<DigitsT> X)
  • public llvm::raw_ostream & print(llvm::raw_ostream & OS, unsigned int Precision = DefaultPrecision) const
  • public uint64_t scale(uint64_t N) const
  • public int64_t scale(int64_t N) const
  • public int64_t scaleByInverse(int64_t N) const
  • public uint64_t scaleByInverse(uint64_t N) const
  • private void shiftLeft(int32_t Shift)
  • private void shiftRight(int32_t Shift)
  • public template <class IntT>IntT toInt() const
  • public std::string toString(unsigned int Precision = DefaultPrecision)

Methods

constexpr ScaledNumber<DigitsT>(
    llvm::ScaledNumber::DigitsType Digits,
    int16_t Scale)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:512

Parameters

llvm::ScaledNumber::DigitsType Digits
int16_t Scale

ScaledNumber<DigitsT>(
    const std::pair<DigitsT, int16_t>& X)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:516

Parameters

const std::pair<DigitsT, int16_t>& X

ScaledNumber<DigitsT>()

Declared at: llvm/include/llvm/Support/ScaledNumber.h:510

static ScaledNumber<DigitsT> adjustToWidth(
    uint64_t N,
    int32_t Shift)

Description

Adjust a number to width, rounding up if necessary. Should only be called for \c Shift close to zero.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:700

Parameters

uint64_t N
int32_t Shift

int compare(const ScaledNumber<DigitsT>& X) const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:668

Parameters

const ScaledNumber<DigitsT>& X

int compareTo(int64_t N) const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:674

Parameters

int64_t N

int compareTo(uint64_t N) const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:671

Parameters

uint64_t N

static int countLeadingZerosWidth(
    llvm::ScaledNumber::DigitsType Digits)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:687

Parameters

llvm::ScaledNumber::DigitsType Digits

void dump() const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:606

static ScaledNumber<DigitsT> get(uint64_t N)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:525

Parameters

uint64_t N

llvm::ScaledNumber::DigitsType getDigits() const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:534

static ScaledNumber<DigitsT> getFraction(
    llvm::ScaledNumber::DigitsType N,
    llvm::ScaledNumber::DigitsType D)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:529

Parameters

llvm::ScaledNumber::DigitsType N
llvm::ScaledNumber::DigitsType D

static ScaledNumber<DigitsT> getInverse(
    uint64_t N)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:526

Parameters

uint64_t N

static ScaledNumber<DigitsT> getLargest()

Declared at: llvm/include/llvm/Support/ScaledNumber.h:522

static ScaledNumber<DigitsT> getOne()

Declared at: llvm/include/llvm/Support/ScaledNumber.h:521

static ScaledNumber<DigitsT> getProduct(
    llvm::ScaledNumber::DigitsType LHS,
    llvm::ScaledNumber::DigitsType RHS)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:680

Parameters

llvm::ScaledNumber::DigitsType LHS
llvm::ScaledNumber::DigitsType RHS

static ScaledNumber<DigitsT> getQuotient(
    llvm::ScaledNumber::DigitsType Dividend,
    llvm::ScaledNumber::DigitsType Divisor)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:683

Parameters

llvm::ScaledNumber::DigitsType Dividend
llvm::ScaledNumber::DigitsType Divisor

static ScaledNumber<DigitsT> getRounded(
    ScaledNumber<DigitsT> P,
    bool Round)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:708

Parameters

ScaledNumber<DigitsT> P
bool Round

int16_t getScale() const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:533

static ScaledNumber<DigitsT> getZero()

Declared at: llvm/include/llvm/Support/ScaledNumber.h:520

ScaledNumber<DigitsT> inverse() const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:677

ScaledNumber<DigitsT>& invert()

Declared at: llvm/include/llvm/Support/ScaledNumber.h:676

bool isLargest() const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:543

bool isOne() const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:544

bool isZero() const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:542

int32_t lg() const

Description

The log base 2, rounded. Get the lg of the scalar. lg 0 is defined to be INT32_MIN.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:553

int32_t lgCeiling() const

Description

The log base 2, rounded towards INT32_MAX. Get the lg ceiling. lg 0 is defined to be INT32_MIN.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:563

int32_t lgFloor() const

Description

The log base 2, rounded towards INT32_MIN. Get the lg floor. lg 0 is defined to be INT32_MIN.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:558

ScaledNumber<DigitsT> matchScales(
    ScaledNumber<DigitsT> X)

Description

Adjust two floats to have matching exponents. Adjust \c this and \c X to have matching exponents. Returns the new \c X by value. Does nothing if \a isZero() for either. The value that compares smaller will lose precision, and possibly become\a isZero().

Declared at: llvm/include/llvm/Support/ScaledNumber.h:643

Parameters

ScaledNumber<DigitsT> X

llvm::raw_ostream& print(
    llvm::raw_ostream& OS,
    unsigned int Precision =
        DefaultPrecision) const

Description

Print a decimal representation. Print a string. See toString for documentation.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:602

Parameters

llvm::raw_ostream& OS
unsigned int Precision = DefaultPrecision

uint64_t scale(uint64_t N) const

Description

Scale a large number accurately. Scale N (multiply it by this). Uses full precision multiplication, even if Width is smaller than 64, so information is not lost.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:653

Parameters

uint64_t N

int64_t scale(int64_t N) const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:659

Parameters

int64_t N

int64_t scaleByInverse(int64_t N) const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:663

Parameters

int64_t N

uint64_t scaleByInverse(uint64_t N) const

Declared at: llvm/include/llvm/Support/ScaledNumber.h:654

Parameters

uint64_t N

void shiftLeft(int32_t Shift)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:633

Parameters

int32_t Shift

void shiftRight(int32_t Shift)

Declared at: llvm/include/llvm/Support/ScaledNumber.h:634

Parameters

int32_t Shift

template <class IntT>
IntT toInt() const

Description

Convert to the given integer type. Convert to \c IntT using simple saturating arithmetic, truncating if necessary.

Declared at: llvm/include/llvm/Support/ScaledNumber.h:540

Templates

IntT

std::string toString(
    unsigned int Precision = DefaultPrecision)

Description

Convert to a decimal representation in a string. Convert to a string. Uses scientific notation for very large/small numbers. Scientific notation is used roughly for numbers outside of the range 2^-64 through 2^64. \c Precision indicates the number of decimal digits of precision to use; 0 requests the maximum available. As a special case to make debugging easier, if the number is small enough to convert without scientific notation and has more than \c Precision digits before the decimal place, it's printed accurately to the first digit past zero. E.g., assuming 10 digits of precision: 98765432198.7654... => 98765432198.8 8765432198.7654... => 8765432198.8 765432198.7654... => 765432198.8 65432198.7654... => 65432198.77 5432198.7654... => 5432198.765

Declared at: llvm/include/llvm/Support/ScaledNumber.h:595

Parameters

unsigned int Precision = DefaultPrecision