LLVM API Documentation

Public Types | Public Member Functions | Static Public Member Functions
llvm::ScaledNumber< DigitsT > Class Template Reference

Simple representation of a scaled number. More...

#include <ScaledNumber.h>

Inheritance diagram for llvm::ScaledNumber< DigitsT >:
Inheritance graph
[legend]
Collaboration diagram for llvm::ScaledNumber< DigitsT >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef DigitsT DigitsType

Public Member Functions

 static_assert (!std::numeric_limits< DigitsT >::is_signed,"only unsigned floats supported")
 ScaledNumber ()
 ScaledNumber (DigitsType Digits, int16_t Scale)
int16_t getScale () const
DigitsType getDigits () const
template<class IntT >
IntT toInt () const
 Convert to the given integer type.
bool isZero () const
bool isLargest () const
bool isOne () const
int32_t lg () const
 The log base 2, rounded.
int32_t lgFloor () const
 The log base 2, rounded towards INT32_MIN.
int32_t lgCeiling () const
 The log base 2, rounded towards INT32_MAX.
bool operator== (const ScaledNumber &X) const
bool operator< (const ScaledNumber &X) const
bool operator!= (const ScaledNumber &X) const
bool operator> (const ScaledNumber &X) const
bool operator<= (const ScaledNumber &X) const
bool operator>= (const ScaledNumber &X) const
bool operator! () const
std::string toString (unsigned Precision=DefaultPrecision)
 Convert to a decimal representation in a string.
raw_ostreamprint (raw_ostream &OS, unsigned Precision=DefaultPrecision) const
 Print a decimal representation.
void dump () const
ScaledNumberoperator+= (const ScaledNumber &X)
ScaledNumberoperator-= (const ScaledNumber &X)
ScaledNumberoperator*= (const ScaledNumber &X)
ScaledNumberoperator/= (const ScaledNumber &X)
ScaledNumberoperator<<= (int16_t Shift)
ScaledNumberoperator>>= (int16_t Shift)
uint64_t scale (uint64_t N) const
 Scale a large number accurately.
uint64_t scaleByInverse (uint64_t N) const
int64_t scale (int64_t N) const
int64_t scaleByInverse (int64_t N) const
int compare (const ScaledNumber &X) const
int compareTo (uint64_t N) const
int compareTo (int64_t N) const
ScaledNumberinvert ()
ScaledNumber inverse () const

Static Public Member Functions

static ScaledNumber getZero ()
static ScaledNumber getOne ()
static ScaledNumber getLargest ()
static ScaledNumber get (uint64_t N)
static ScaledNumber getInverse (uint64_t N)
static ScaledNumber getFraction (DigitsType N, DigitsType D)

Detailed Description

template<class DigitsT>
class llvm::ScaledNumber< DigitsT >

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 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.

Definition at line 494 of file ScaledNumber.h.


Member Typedef Documentation

template<class DigitsT>
typedef DigitsT llvm::ScaledNumber< DigitsT >::DigitsType

Definition at line 499 of file ScaledNumber.h.


Constructor & Destructor Documentation

template<class DigitsT>
llvm::ScaledNumber< DigitsT >::ScaledNumber ( ) [inline]
template<class DigitsT>
llvm::ScaledNumber< DigitsT >::ScaledNumber ( DigitsType  Digits,
int16_t  Scale 
) [inline]

Definition at line 514 of file ScaledNumber.h.


Member Function Documentation

template<class DigitsT>
int llvm::ScaledNumber< DigitsT >::compare ( const ScaledNumber< DigitsT > &  X) const [inline]
template<class DigitsT>
int llvm::ScaledNumber< DigitsT >::compareTo ( uint64_t  N) const [inline]

Definition at line 673 of file ScaledNumber.h.

template<class DigitsT>
int llvm::ScaledNumber< DigitsT >::compareTo ( int64_t  N) const [inline]

Definition at line 683 of file ScaledNumber.h.

Referenced by llvm::ScaledNumber< uint64_t >::compareTo().

template<class DigitsT>
void llvm::ScaledNumber< DigitsT >::dump ( ) const [inline]

Definition at line 608 of file ScaledNumber.h.

Referenced by llvm::ScaledNumber< uint64_t >::dump().

template<class DigitsT>
static ScaledNumber llvm::ScaledNumber< DigitsT >::get ( uint64_t  N) [inline, static]

Definition at line 527 of file ScaledNumber.h.

Referenced by llvm::ScaledNumber< uint64_t >::invert().

template<class DigitsT>
DigitsType llvm::ScaledNumber< DigitsT >::getDigits ( ) const [inline]

Definition at line 536 of file ScaledNumber.h.

template<class DigitsT>
static ScaledNumber llvm::ScaledNumber< DigitsT >::getFraction ( DigitsType  N,
DigitsType  D 
) [inline, static]

Definition at line 531 of file ScaledNumber.h.

template<class DigitsT>
static ScaledNumber llvm::ScaledNumber< DigitsT >::getInverse ( uint64_t  N) [inline, static]

Definition at line 528 of file ScaledNumber.h.

template<class DigitsT>
static ScaledNumber llvm::ScaledNumber< DigitsT >::getLargest ( ) [inline, static]
template<class DigitsT>
static ScaledNumber llvm::ScaledNumber< DigitsT >::getOne ( ) [inline, static]

Definition at line 523 of file ScaledNumber.h.

template<class DigitsT>
int16_t llvm::ScaledNumber< DigitsT >::getScale ( ) const [inline]

Definition at line 535 of file ScaledNumber.h.

template<class DigitsT>
static ScaledNumber llvm::ScaledNumber< DigitsT >::getZero ( ) [inline, static]

Definition at line 522 of file ScaledNumber.h.

template<class DigitsT>
ScaledNumber llvm::ScaledNumber< DigitsT >::inverse ( ) const [inline]
template<class DigitsT>
ScaledNumber& llvm::ScaledNumber< DigitsT >::invert ( ) [inline]

Definition at line 685 of file ScaledNumber.h.

Referenced by llvm::ScaledNumber< uint64_t >::getInverse().

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::isLargest ( ) const [inline]

Definition at line 545 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::isOne ( ) const [inline]

Definition at line 546 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::isZero ( ) const [inline]
template<class DigitsT>
int32_t llvm::ScaledNumber< DigitsT >::lg ( ) const [inline]

The log base 2, rounded.

Get the lg of the scalar. lg 0 is defined to be INT32_MIN.

Definition at line 555 of file ScaledNumber.h.

template<class DigitsT>
int32_t llvm::ScaledNumber< DigitsT >::lgCeiling ( ) const [inline]

The log base 2, rounded towards INT32_MAX.

Get the lg ceiling. lg 0 is defined to be INT32_MIN.

Definition at line 565 of file ScaledNumber.h.

template<class DigitsT>
int32_t llvm::ScaledNumber< DigitsT >::lgFloor ( ) const [inline]

The log base 2, rounded towards INT32_MIN.

Get the lg floor. lg 0 is defined to be INT32_MIN.

Definition at line 560 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::operator! ( ) const [inline]

Definition at line 576 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::operator!= ( const ScaledNumber< DigitsT > &  X) const [inline]

Definition at line 571 of file ScaledNumber.h.

template<class DigitsT >
ScaledNumber< DigitsT > & llvm::ScaledNumber< DigitsT >::operator*= ( const ScaledNumber< DigitsT > &  X)
template<class DigitsT>
ScaledNumber& llvm::ScaledNumber< DigitsT >::operator+= ( const ScaledNumber< DigitsT > &  X) [inline]

Definition at line 610 of file ScaledNumber.h.

template<class DigitsT>
ScaledNumber& llvm::ScaledNumber< DigitsT >::operator-= ( const ScaledNumber< DigitsT > &  X) [inline]

Definition at line 618 of file ScaledNumber.h.

template<class DigitsT >
ScaledNumber< DigitsT > & llvm::ScaledNumber< DigitsT >::operator/= ( const ScaledNumber< DigitsT > &  X)
template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::operator< ( const ScaledNumber< DigitsT > &  X) const [inline]

Definition at line 570 of file ScaledNumber.h.

template<class DigitsT>
ScaledNumber& llvm::ScaledNumber< DigitsT >::operator<<= ( int16_t  Shift) [inline]

Definition at line 625 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::operator<= ( const ScaledNumber< DigitsT > &  X) const [inline]

Definition at line 573 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::operator== ( const ScaledNumber< DigitsT > &  X) const [inline]

Definition at line 569 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::operator> ( const ScaledNumber< DigitsT > &  X) const [inline]

Definition at line 572 of file ScaledNumber.h.

template<class DigitsT>
bool llvm::ScaledNumber< DigitsT >::operator>= ( const ScaledNumber< DigitsT > &  X) const [inline]

Definition at line 574 of file ScaledNumber.h.

template<class DigitsT>
ScaledNumber& llvm::ScaledNumber< DigitsT >::operator>>= ( int16_t  Shift) [inline]

Definition at line 629 of file ScaledNumber.h.

template<class DigitsT>
raw_ostream& llvm::ScaledNumber< DigitsT >::print ( raw_ostream OS,
unsigned  Precision = DefaultPrecision 
) const [inline]

Print a decimal representation.

Print a string. See toString for documentation.

Definition at line 604 of file ScaledNumber.h.

Referenced by llvm::ScaledNumber< uint64_t >::print().

template<class DigitsT >
uint64_t llvm::ScaledNumber< DigitsT >::scale ( uint64_t  N) const

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.

Definition at line 769 of file ScaledNumber.h.

References scale().

Referenced by llvm::ScaledNumber< uint64_t >::scale(), and llvm::ScaledNumber< uint64_t >::scaleByInverse().

template<class DigitsT>
int64_t llvm::ScaledNumber< DigitsT >::scale ( int64_t  N) const [inline]

Definition at line 661 of file ScaledNumber.h.

template<class DigitsT>
uint64_t llvm::ScaledNumber< DigitsT >::scaleByInverse ( uint64_t  N) const [inline]

Definition at line 656 of file ScaledNumber.h.

Referenced by llvm::ScaledNumber< uint64_t >::scaleByInverse().

template<class DigitsT>
int64_t llvm::ScaledNumber< DigitsT >::scaleByInverse ( int64_t  N) const [inline]

Definition at line 665 of file ScaledNumber.h.

template<class DigitsT>
llvm::ScaledNumber< DigitsT >::static_assert ( !std::numeric_limits< DigitsT >::is_signed  ,
"only unsigned floats supported"   
)
template<class DigitsT >
template<class IntT >
IntT llvm::ScaledNumber< DigitsT >::toInt ( ) const

Convert to the given integer type.

Convert to IntT using simple saturating arithmetic, truncating if necessary.

Definition at line 779 of file ScaledNumber.h.

References N.

template<class DigitsT>
std::string llvm::ScaledNumber< DigitsT >::toString ( unsigned  Precision = DefaultPrecision) [inline]

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.

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

Definition at line 597 of file ScaledNumber.h.

Referenced by llvm::ScaledNumber< uint64_t >::toString().


The documentation for this class was generated from the following file: