LLVM 22.0.0git
|
Simple representation of a scaled number. More...
#include "llvm/Support/ScaledNumber.h"
Public Types | |
typedef DigitsT | DigitsType |
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) |
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:
Definition at line 496 of file ScaledNumber.h.
typedef DigitsT llvm::ScaledNumber< DigitsT >::DigitsType |
Definition at line 501 of file ScaledNumber.h.
|
default |
|
inlineconstexpr |
Definition at line 516 of file ScaledNumber.h.
|
inline |
Definition at line 672 of file ScaledNumber.h.
Referenced by llvm::ScaledNumber< uint64_t >::operator!=(), llvm::ScaledNumber< uint64_t >::operator<(), llvm::ScaledNumber< uint64_t >::operator<=(), llvm::ScaledNumber< uint64_t >::operator==(), llvm::ScaledNumber< uint64_t >::operator>(), and llvm::ScaledNumber< uint64_t >::operator>=().
|
inline |
Definition at line 678 of file ScaledNumber.h.
Referenced by llvm::ScaledNumber< uint64_t >::compareTo().
|
inline |
Definition at line 675 of file ScaledNumber.h.
|
inline |
Definition at line 610 of file ScaledNumber.h.
|
inlinestatic |
Definition at line 529 of file ScaledNumber.h.
Referenced by llvm::ScaledNumber< uint64_t >::getInverse(), and llvm::ScaledNumber< uint64_t >::invert().
|
inline |
Definition at line 538 of file ScaledNumber.h.
|
inlinestatic |
Definition at line 533 of file ScaledNumber.h.
|
inlinestatic |
Definition at line 530 of file ScaledNumber.h.
|
inlinestatic |
Definition at line 526 of file ScaledNumber.h.
Referenced by llvm::ScaledNumber< uint64_t >::isLargest(), llvm::ScaledNumber< uint64_t >::operator+=(), and operator/=().
|
inlinestatic |
Definition at line 525 of file ScaledNumber.h.
|
inline |
Definition at line 537 of file ScaledNumber.h.
|
inlinestatic |
Definition at line 524 of file ScaledNumber.h.
|
inline |
Definition at line 681 of file ScaledNumber.h.
Referenced by llvm::BlockFrequencyInfoImplBase::computeLoopScale(), and llvm::ScaledNumber< uint64_t >::scaleByInverse().
|
inline |
Definition at line 680 of file ScaledNumber.h.
Referenced by llvm::ScaledNumber< uint64_t >::getInverse(), and llvm::ScaledNumber< uint64_t >::inverse().
|
inline |
Definition at line 547 of file ScaledNumber.h.
|
inline |
Definition at line 548 of file ScaledNumber.h.
|
inline |
Definition at line 546 of file ScaledNumber.h.
Referenced by llvm::ScaledNumber< uint64_t >::operator!().
|
inline |
The log base 2, rounded.
Get the lg of the scalar. lg 0 is defined to be INT32_MIN.
Definition at line 557 of file ScaledNumber.h.
|
inline |
The log base 2, rounded towards INT32_MAX.
Get the lg ceiling. lg 0 is defined to be INT32_MIN.
Definition at line 567 of file ScaledNumber.h.
|
inline |
The log base 2, rounded towards INT32_MIN.
Get the lg floor. lg 0 is defined to be INT32_MIN.
Definition at line 562 of file ScaledNumber.h.
|
inline |
Definition at line 578 of file ScaledNumber.h.
|
inline |
Definition at line 573 of file ScaledNumber.h.
ScaledNumber< DigitsT > & llvm::ScaledNumber< DigitsT >::operator*= | ( | const ScaledNumber< DigitsT > & | X | ) |
Definition at line 804 of file ScaledNumber.h.
References isZero(), ScaledNumber(), and X.
|
inline |
Definition at line 612 of file ScaledNumber.h.
|
inline |
Definition at line 620 of file ScaledNumber.h.
ScaledNumber< DigitsT > & llvm::ScaledNumber< DigitsT >::operator/= | ( | const ScaledNumber< DigitsT > & | X | ) |
Definition at line 821 of file ScaledNumber.h.
References getLargest(), isZero(), ScaledNumber(), and X.
|
inline |
Definition at line 572 of file ScaledNumber.h.
|
inline |
Definition at line 627 of file ScaledNumber.h.
|
inline |
Definition at line 575 of file ScaledNumber.h.
|
inline |
Definition at line 571 of file ScaledNumber.h.
|
inline |
Definition at line 574 of file ScaledNumber.h.
|
inline |
Definition at line 576 of file ScaledNumber.h.
|
inline |
Definition at line 631 of file ScaledNumber.h.
|
inline |
Print a decimal representation.
Print a string. See toString for documentation.
Definition at line 606 of file ScaledNumber.h.
|
inline |
Definition at line 663 of file ScaledNumber.h.
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 774 of file ScaledNumber.h.
References llvm::get(), N, ScaledNumber(), and toInt().
Referenced by llvm::ScaledNumber< uint64_t >::scale(), and llvm::ScaledNumber< uint64_t >::scaleByInverse().
|
inline |
Definition at line 667 of file ScaledNumber.h.
|
inline |
Definition at line 658 of file ScaledNumber.h.
Referenced by llvm::ScaledNumber< uint64_t >::scaleByInverse().
Convert to the given integer type.
Convert to IntT
using simple saturating arithmetic, truncating if necessary.
Definition at line 784 of file ScaledNumber.h.
Referenced by scale().
|
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 599 of file ScaledNumber.h.