[flang] Make it easier to enable minimal FP output by default for module files (but...
authorpeter klausler <pklausler@nvidia.com>
Fri, 23 Aug 2019 18:14:48 +0000 (11:14 -0700)
committerpeter klausler <pklausler@nvidia.com>
Fri, 23 Aug 2019 18:31:47 +0000 (11:31 -0700)
Original-commit: flang-compiler/f18@f6b640319b33008375ca719a88fd0580bcf90bba
Reviewed-on: https://github.com/flang-compiler/f18/pull/671

flang/lib/evaluate/real.cc
flang/lib/evaluate/real.h
flang/test/evaluate/real.cc

index fafe94d..0853c6c 100644 (file)
@@ -459,7 +459,8 @@ std::string Real<W, P, IM>::DumpHexadecimal() const {
 }
 
 template<typename W, int P, bool IM>
-std::ostream &Real<W, P, IM>::AsFortran(std::ostream &o, int kind) const {
+std::ostream &Real<W, P, IM>::AsFortran(
+    std::ostream &o, int kind, bool minimal) const {
   if (IsNotANumber()) {
     o << "(0._" << kind << "/0.)";
   } else if (IsInfinite()) {
@@ -476,8 +477,11 @@ std::ostream &Real<W, P, IM>::AsFortran(std::ostream &o, int kind) const {
     using B = decimal::BinaryFloatingPointNumber<P>;
     const auto *value{reinterpret_cast<const B *>(this)};
     char buffer[24000];  // accommodate real*16
-    auto result{decimal::ConvertToDecimal<P>(buffer, sizeof buffer,
-        static_cast<decimal::DecimalConversionFlags>(0),
+    decimal::DecimalConversionFlags flags{};  // default: exact representation
+    if (minimal) {
+      flags = decimal::Minimize;
+    }
+    auto result{decimal::ConvertToDecimal<P>(buffer, sizeof buffer, flags,
         static_cast<int>(sizeof buffer), decimal::RoundNearest, *value)};
     const char *p{result.str};
     if (DEREF(p) == '-' || *p == '+') {
index edaee19..abb844b 100644 (file)
@@ -362,7 +362,7 @@ public:
 
   // Emits a character representation for an equivalent Fortran constant
   // or parenthesized constant expression that produces this value.
-  std::ostream &AsFortran(std::ostream &, int kind) const;
+  std::ostream &AsFortran(std::ostream &, int kind, bool minimal = false) const;
 
 private:
   using Significand = Integer<significandBits>;  // no implicit bit
index 2a8ab67..882e2cb 100644 (file)
@@ -164,7 +164,7 @@ template<typename R> void basicTests(int rm, Rounding rounding) {
       MATCH(x, ivf.value.ToUInt64())(ldesc);
       if (rounding.mode == RoundingMode::TiesToEven) {  // to match stold()
         std::stringstream ss;
-        vr.value.AsFortran(ss, kind);
+        vr.value.AsFortran(ss, kind, false /*exact*/);
         std::string decimal{ss.str()};
         const char *p{decimal.data()};
         MATCH(x, static_cast<std::uint64_t>(std::stold(decimal)))
@@ -411,7 +411,7 @@ void subsetTests(int pass, Rounding rounding, std::uint32_t opds) {
 
       static constexpr int kind{REAL::bits / 8};
       std::stringstream ss, css;
-      x.AsFortran(ss, kind);
+      x.AsFortran(ss, kind, false /*exact*/);
       std::string s{ss.str()};
       if (IsNaN(rj)) {
         css << "(0._" << kind << "/0.)";