Add a static utility to FloatAttr for converting an APFloat to double.
authorRiver Riddle <riverriddle@google.com>
Sat, 4 May 2019 22:00:27 +0000 (15:00 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Mon, 6 May 2019 15:29:28 +0000 (08:29 -0700)
--

PiperOrigin-RevId: 246671765

mlir/include/mlir/IR/Attributes.h
mlir/lib/IR/Attributes.cpp

index 2de33d3..2ff4937 100644 (file)
@@ -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; }
index c69cb2b..ab15699 100644 (file)
@@ -195,10 +195,11 @@ APFloat FloatAttr::getValue() const {
 }
 
 double FloatAttr::getValueAsDouble() const {
-  const auto &semantics = getType().cast<FloatType>().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);
   }