[Lldb-commits] [lldb] 88c8247 - [lldb/Utility] Simplify Scalar float accessors
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Wed Jul 8 04:57:42 PDT 2020
Author: Pavel Labath
Date: 2020-07-08T13:57:32+02:00
New Revision: 88c82474d2ef92d0cda7d902206d90a73eed9c9d
URL: https://github.com/llvm/llvm-project/commit/88c82474d2ef92d0cda7d902206d90a73eed9c9d
DIFF: https://github.com/llvm/llvm-project/commit/88c82474d2ef92d0cda7d902206d90a73eed9c9d.diff
LOG: [lldb/Utility] Simplify Scalar float accessors
Make use of APFloat conversion methods to avoid needing to switch based
on the stored value type.
Added:
Modified:
lldb/source/Utility/Scalar.cpp
Removed:
################################################################################
diff --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index 6e2715130c70..87ac6c23892d 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -23,6 +23,8 @@
using namespace lldb;
using namespace lldb_private;
+using llvm::APFloat;
+
namespace {
enum class Category { Void, Integral, Float };
}
@@ -641,98 +643,48 @@ llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
}
float Scalar::Float(float fail_value) const {
- switch (m_type) {
- case e_void:
+ switch (GetCategory(m_type)) {
+ case Category::Void:
break;
- case e_sint:
- case e_slong:
- case e_slonglong:
- case e_sint128:
- case e_sint256:
- case e_sint512:
- return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer);
-
- case e_uint:
- case e_ulong:
- case e_ulonglong:
- case e_uint128:
- case e_uint256:
- case e_uint512:
+ case Category::Integral:
+ if (IsSigned(m_type))
+ return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer);
return llvm::APIntOps::RoundAPIntToFloat(m_integer);
- case e_float:
- return m_float.convertToFloat();
- case e_double:
- return static_cast<float_t>(m_float.convertToDouble());
- case e_long_double:
- llvm::APInt ldbl_val = m_float.bitcastToAPInt();
- return ldbl_val.bitsToFloat();
+ case Category::Float: {
+ APFloat result = m_float;
+ bool losesInfo;
+ result.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
+ &losesInfo);
+ return result.convertToFloat();
+ }
}
return fail_value;
}
double Scalar::Double(double fail_value) const {
- switch (m_type) {
- case e_void:
+ switch (GetCategory(m_type)) {
+ case Category::Void:
break;
- case e_sint:
- case e_slong:
- case e_slonglong:
- case e_sint128:
- case e_sint256:
- case e_sint512:
- return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer);
-
- case e_uint:
- case e_ulong:
- case e_ulonglong:
- case e_uint128:
- case e_uint256:
- case e_uint512:
+ case Category::Integral:
+ if (IsSigned(m_type))
+ return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer);
return llvm::APIntOps::RoundAPIntToDouble(m_integer);
- case e_float:
- return static_cast<double_t>(m_float.convertToFloat());
- case e_double:
- return m_float.convertToDouble();
- case e_long_double:
- llvm::APInt ldbl_val = m_float.bitcastToAPInt();
- return ldbl_val.bitsToFloat();
+ case Category::Float: {
+ APFloat result = m_float;
+ bool losesInfo;
+ result.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
+ &losesInfo);
+ return result.convertToDouble();
+ }
}
return fail_value;
}
long double Scalar::LongDouble(long double fail_value) const {
- switch (m_type) {
- case e_void:
- break;
- case e_sint:
- case e_slong:
- case e_slonglong:
- case e_sint128:
- case e_sint256:
- case e_sint512:
- return static_cast<long_double_t>(
- llvm::APIntOps::RoundSignedAPIntToDouble(m_integer));
-
- case e_uint:
- case e_ulong:
- case e_ulonglong:
- case e_uint128:
- case e_uint256:
- case e_uint512:
- return static_cast<long_double_t>(
- llvm::APIntOps::RoundAPIntToDouble(m_integer));
-
- case e_float:
- return static_cast<long_double_t>(m_float.convertToFloat());
- case e_double:
- return static_cast<long_double_t>(m_float.convertToDouble());
- case e_long_double:
- llvm::APInt ldbl_val = m_float.bitcastToAPInt();
- return static_cast<long_double_t>(ldbl_val.bitsToDouble());
- }
- return fail_value;
+ /// No way to get more precision at the moment.
+ return static_cast<long double>(Double(fail_value));
}
Scalar &Scalar::operator+=(const Scalar &rhs) {
More information about the lldb-commits
mailing list