Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions llvm/include/llvm/ADT/APFixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,41 @@ class FixedPointSemantics {

/// Check if the Semantic follow the requirements of an older more limited
/// version of this class
bool isValidLegacySema() const {
[[nodiscard]] bool isValidLegacySema() const {
return LsbWeight <= 0 && static_cast<int>(Width) >= -LsbWeight;
}
unsigned getWidth() const { return Width; }
unsigned getScale() const { assert(isValidLegacySema()); return -LsbWeight; }
int getLsbWeight() const { return LsbWeight; }
int getMsbWeight() const {
[[nodiscard]] unsigned getWidth() const { return Width; }
[[nodiscard]] unsigned getScale() const {
assert(isValidLegacySema());
return -LsbWeight;
}
[[nodiscard]] int getLsbWeight() const { return LsbWeight; }
[[nodiscard]] int getMsbWeight() const {
return LsbWeight + Width - 1 /*Both lsb and msb are both part of width*/;
}
bool isSigned() const { return IsSigned; }
bool isSaturated() const { return IsSaturated; }
bool hasUnsignedPadding() const { return HasUnsignedPadding; }
[[nodiscard]] bool isSigned() const { return IsSigned; }
[[nodiscard]] bool isSaturated() const { return IsSaturated; }
[[nodiscard]] bool hasUnsignedPadding() const { return HasUnsignedPadding; }

void setSaturated(bool Saturated) { IsSaturated = Saturated; }

/// return true if the first bit doesn't have a strictly positive weight
bool hasSignOrPaddingBit() const { return IsSigned || HasUnsignedPadding; }
[[nodiscard]] bool hasSignOrPaddingBit() const {
return IsSigned || HasUnsignedPadding;
}

/// Return the number of integral bits represented by these semantics. These
/// are separate from the fractional bits and do not include the sign or
/// padding bit.
unsigned getIntegralBits() const {
[[nodiscard]] unsigned getIntegralBits() const {
return std::max(getMsbWeight() + 1 - hasSignOrPaddingBit(), 0);
}

/// Return the FixedPointSemantics that allows for calculating the full
/// precision semantic that can precisely represent the precision and ranges
/// of both input values. This does not compute the resulting semantics for a
/// given binary operation.
LLVM_ABI FixedPointSemantics
[[nodiscard]] LLVM_ABI FixedPointSemantics
getCommonSemantics(const FixedPointSemantics &Other) const;

/// Print semantics for debug purposes
Expand All @@ -98,7 +103,8 @@ class FixedPointSemantics {
/// minimum integer representation of 127 and -128, respectively. If both of
/// these values can be represented (possibly inexactly) in the floating
/// point semantic without overflowing, this returns true.
LLVM_ABI bool fitsInFloatSemantics(const fltSemantics &FloatSema) const;
[[nodiscard]] LLVM_ABI bool
fitsInFloatSemantics(const fltSemantics &FloatSema) const;

/// Return the FixedPointSemantics for an integer type.
static FixedPointSemantics GetIntegerSemantics(unsigned Width,
Expand All @@ -119,7 +125,7 @@ class FixedPointSemantics {
/// The result is dependent on the host endianness and not stable across LLVM
/// versions. See getFromOpaqueInt() to convert it back to a
/// FixedPointSemantics object.
LLVM_ABI uint32_t toOpaqueInt() const;
[[nodiscard]] LLVM_ABI uint32_t toOpaqueInt() const;
/// Create a FixedPointSemantics object from an integer created via
/// toOpaqueInt().
LLVM_ABI static FixedPointSemantics getFromOpaqueInt(uint32_t);
Expand Down Expand Up @@ -177,16 +183,18 @@ class APFixedPoint {
APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}

APSInt getValue() const { return APSInt(Val, !Sema.isSigned()); }
inline unsigned getWidth() const { return Sema.getWidth(); }
inline unsigned getScale() const { return Sema.getScale(); }
int getLsbWeight() const { return Sema.getLsbWeight(); }
int getMsbWeight() const { return Sema.getMsbWeight(); }
inline bool isSaturated() const { return Sema.isSaturated(); }
inline bool isSigned() const { return Sema.isSigned(); }
inline bool hasPadding() const { return Sema.hasUnsignedPadding(); }
FixedPointSemantics getSemantics() const { return Sema; }
[[nodiscard]] inline unsigned getWidth() const { return Sema.getWidth(); }
[[nodiscard]] inline unsigned getScale() const { return Sema.getScale(); }
[[nodiscard]] int getLsbWeight() const { return Sema.getLsbWeight(); }
[[nodiscard]] int getMsbWeight() const { return Sema.getMsbWeight(); }
[[nodiscard]] inline bool isSaturated() const { return Sema.isSaturated(); }
[[nodiscard]] inline bool isSigned() const { return Sema.isSigned(); }
[[nodiscard]] inline bool hasPadding() const {
return Sema.hasUnsignedPadding();
}
[[nodiscard]] FixedPointSemantics getSemantics() const { return Sema; }

bool getBoolValue() const { return Val.getBoolValue(); }
[[nodiscard]] bool getBoolValue() const { return Val.getBoolValue(); }

// Convert this number to match the semantics provided. If the overflow
// parameter is provided, set this value to true or false to indicate if this
Expand Down Expand Up @@ -244,10 +252,11 @@ class APFixedPoint {

/// Convert this fixed point number to a floating point value with the
/// provided semantics.
LLVM_ABI APFloat convertToFloat(const fltSemantics &FloatSema) const;
[[nodiscard]] LLVM_ABI APFloat
convertToFloat(const fltSemantics &FloatSema) const;

LLVM_ABI void toString(SmallVectorImpl<char> &Str) const;
std::string toString() const {
[[nodiscard]] std::string toString() const {
SmallString<40> S;
toString(S);
return std::string(S);
Expand All @@ -260,7 +269,7 @@ class APFixedPoint {
#endif

// If LHS > RHS, return 1. If LHS == RHS, return 0. If LHS < RHS, return -1.
LLVM_ABI int compare(const APFixedPoint &Other) const;
[[nodiscard]] LLVM_ABI int compare(const APFixedPoint &Other) const;
bool operator==(const APFixedPoint &Other) const {
return compare(Other) == 0;
}
Expand Down
Loading