Skip to content

[APFloat] Add APFloat::format #99088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
27 changes: 27 additions & 0 deletions llvm/include/llvm/ADT/APFloat.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/NativeFormatting.h"
#include "llvm/Support/float128.h"
#include <memory>

Expand Down Expand Up @@ -557,6 +559,15 @@ class IEEEFloat final : public APFloatBase {

cmpResult compareAbsoluteValue(const IEEEFloat &) const;

/// returns the exponent in the format's radix
ExponentType getExponent() const;

/// returns the significand
APInt getSignificand() const;

/// decompose to an integer and exponent to radix 2
bool decomposeToIntegerAndPowerOf2(APInt &, int &) const;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is a limitation of APFloat, but this should return std::optional<APInt, int>.


private:
/// \name Simple Queries
/// @{
Expand Down Expand Up @@ -784,6 +795,8 @@ class DoubleAPFloat final : public APFloatBase {

bool getExactInverse(APFloat *inv) const;

bool decomposeToIntegerAndPowerOf2(APInt &, int &) const;

LLVM_READONLY
int getExactLog2() const;
LLVM_READONLY
Expand Down Expand Up @@ -1381,6 +1394,20 @@ class APFloat : public APFloatBase {
toString(Str, FormatPrecision, FormatMaxPadding, TruncateZero));
}

/// Decomposes the value to an integer and an exponent such that
/// value = I * 2^exp
/// returns true on success, false otherwise (e.g., NaN, Infinity)
/// similar to the standard modf, but exponent does not
/// have to have the same sign as the value.
bool decomposeToIntegerAndPowerOf2(APInt &I, int &exp) const {
APFLOAT_DISPATCH_ON_SEMANTICS(decomposeToIntegerAndPowerOf2(I, exp));
}

SmallVectorImpl<char> &
format(SmallVectorImpl<char> &strout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why isn't this on raw_ostream, and using a raw small string?

llvm::FloatStyle style = llvm::FloatStyle::Exponent,
std::optional<size_t> precision = std::nullopt);

void print(raw_ostream &) const;
void dump() const;

Expand Down
Loading
Loading