[llvm] [APFloat]extract `fltSemantics::isRepresentableBy` to header (PR #122636)
Congcong Cai via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 12 16:07:44 PST 2025
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/122636
>From ded43fcebe6f65c9dd59b4d52694bde549218cd9 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sun, 12 Jan 2025 16:05:16 +0800
Subject: [PATCH] [APFloat]extract `fltSemantics::isRepresentableBy` to header
isRepresentableBy is useful to check float point type compatibility
---
llvm/include/llvm/ADT/APFloat.h | 5 +++++
llvm/lib/Support/APFloat.cpp | 20 +++++++++-----------
2 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/llvm/include/llvm/ADT/APFloat.h b/llvm/include/llvm/ADT/APFloat.h
index bf80fa5a06580b..9792749230cbf9 100644
--- a/llvm/include/llvm/ADT/APFloat.h
+++ b/llvm/include/llvm/ADT/APFloat.h
@@ -281,6 +281,11 @@ struct APFloatBase {
/// anything real.
static const fltSemantics &Bogus() LLVM_READNONE;
+ // Returns true if any number described by this semantics can be precisely
+ // represented by the specified semantics. Does not take into account
+ // the value of fltNonfiniteBehavior, hasZero, hasSignedRepr.
+ static bool isRepresentableBy(const fltSemantics &A, const fltSemantics &B);
+
/// @}
/// IEEE-754R 5.11: Floating Point Comparison Relations.
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index c9adfca8b3b768..b0d92ae37fe8f6 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -125,14 +125,6 @@ struct fltSemantics {
/* Whether this semantics can represent signed values */
bool hasSignedRepr = true;
-
- // Returns true if any number described by this semantics can be precisely
- // represented by the specified semantics. Does not take into account
- // the value of fltNonfiniteBehavior.
- bool isRepresentableBy(const fltSemantics &S) const {
- return maxExponent <= S.maxExponent && minExponent >= S.minExponent &&
- precision <= S.precision;
- }
};
static constexpr fltSemantics semIEEEhalf = {15, -14, 11, 16};
@@ -290,6 +282,12 @@ const fltSemantics &APFloatBase::x87DoubleExtended() {
}
const fltSemantics &APFloatBase::Bogus() { return semBogus; }
+bool APFloatBase::isRepresentableBy(const fltSemantics &A,
+ const fltSemantics &B) {
+ return A.maxExponent <= B.maxExponent && A.minExponent >= B.minExponent &&
+ A.precision <= B.precision;
+}
+
constexpr RoundingMode APFloatBase::rmNearestTiesToEven;
constexpr RoundingMode APFloatBase::rmTowardPositive;
constexpr RoundingMode APFloatBase::rmTowardNegative;
@@ -5527,7 +5525,7 @@ APFloat::opStatus APFloat::convertToInteger(APSInt &result,
double APFloat::convertToDouble() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEdouble)
return getIEEE().convertToDouble();
- assert(getSemantics().isRepresentableBy(semIEEEdouble) &&
+ assert(isRepresentableBy(getSemantics(), semIEEEdouble) &&
"Float semantics is not representable by IEEEdouble");
APFloat Temp = *this;
bool LosesInfo;
@@ -5541,7 +5539,7 @@ double APFloat::convertToDouble() const {
float128 APFloat::convertToQuad() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEquad)
return getIEEE().convertToQuad();
- assert(getSemantics().isRepresentableBy(semIEEEquad) &&
+ assert(isRepresentableBy(getSemantics(), semIEEEquad) &&
"Float semantics is not representable by IEEEquad");
APFloat Temp = *this;
bool LosesInfo;
@@ -5555,7 +5553,7 @@ float128 APFloat::convertToQuad() const {
float APFloat::convertToFloat() const {
if (&getSemantics() == (const llvm::fltSemantics *)&semIEEEsingle)
return getIEEE().convertToFloat();
- assert(getSemantics().isRepresentableBy(semIEEEsingle) &&
+ assert(isRepresentableBy(getSemantics(), semIEEEsingle) &&
"Float semantics is not representable by IEEEsingle");
APFloat Temp = *this;
bool LosesInfo;
More information about the llvm-commits
mailing list