From 3f27c6068896e0f85b727455a527f15fb842f5b1 Mon Sep 17 00:00:00 2001 From: River Riddle Date: Sat, 4 May 2019 15:00:27 -0700 Subject: [PATCH] Add a static utility to FloatAttr for converting an APFloat to double. -- PiperOrigin-RevId: 246671765 --- mlir/include/mlir/IR/Attributes.h | 1 + mlir/lib/IR/Attributes.cpp | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h index 2de33d3..2ff4937 100644 --- a/mlir/include/mlir/IR/Attributes.h +++ b/mlir/include/mlir/IR/Attributes.h @@ -223,6 +223,7 @@ public: /// This function is used to convert the value to a double, even if it loses /// precision. double getValueAsDouble() const; + static double getValueAsDouble(APFloat val); /// Methods for support type inquiry through isa, cast, and dyn_cast. static bool kindof(Kind kind) { return kind == Kind::Float; } diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp index c69cb2b..ab15699 100644 --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -195,10 +195,11 @@ APFloat FloatAttr::getValue() const { } double FloatAttr::getValueAsDouble() const { - const auto &semantics = getType().cast().getFloatSemantics(); - auto value = getValue(); - bool losesInfo = false; // ignored - if (&semantics != &APFloat::IEEEdouble()) { + return getValueAsDouble(getValue()); +} +double FloatAttr::getValueAsDouble(APFloat value) { + if (&value.getSemantics() != &APFloat::IEEEdouble()) { + bool losesInfo = false; value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo); } -- 2.7.4