[flang-commits] [flang] 0ff3222 - [flang] Fix float-number representation bug

via flang-commits flang-commits at lists.llvm.org
Thu Apr 14 06:29:00 PDT 2022


Author: PeixinQiao
Date: 2022-04-14T21:28:30+08:00
New Revision: 0ff322246bcd5c20fdda5e6d1215ee961db2abdc

URL: https://github.com/llvm/llvm-project/commit/0ff322246bcd5c20fdda5e6d1215ee961db2abdc
DIFF: https://github.com/llvm/llvm-project/commit/0ff322246bcd5c20fdda5e6d1215ee961db2abdc.diff

LOG: [flang] Fix float-number representation bug

The float number is represented as (-1)^s * 1.f * 2^(-127) for 32-bit,
where s is the signed flag, f is the mantissa. When the exponent bits
are all zeros, the float number is represented as (-1)^s * 0.f *2^(-126)
for 32-bit, in which case, the intPart is '0'.

Reviewed By: Jean Perier

https://reviews.llvm.org/D123673

Added: 
    flang/test/Lower/Intrinsics/real.f90

Modified: 
    flang/lib/Evaluate/real.cpp
    flang/unittests/Evaluate/real.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/real.cpp b/flang/lib/Evaluate/real.cpp
index eb6bd148e341c..b8ce0ff8eae66 100644
--- a/flang/lib/Evaluate/real.cpp
+++ b/flang/lib/Evaluate/real.cpp
@@ -639,6 +639,9 @@ template <typename W, int P> std::string Real<W, P>::DumpHexadecimal() const {
     }
     result += 'p';
     int exponent = Exponent() - exponentBias;
+    if (intPart == '0') {
+      exponent += 1;
+    }
     result += Integer<32>{exponent}.SignedDecimal();
     return result;
   }

diff  --git a/flang/test/Lower/Intrinsics/real.f90 b/flang/test/Lower/Intrinsics/real.f90
new file mode 100644
index 0000000000000..7b9fab8a87712
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/real.f90
@@ -0,0 +1,20 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+! Tests REAL lowering
+subroutine test_real()
+  real(4) :: r4
+  real(8) :: r8
+
+  r4 = real(z'40', kind=4)
+  r8 = real(z'40', kind=8)
+
+end subroutine
+
+! CHECK-LABEL: func @_QPtest_real() {
+! CHECK:  %[[VAL_0:.*]] = fir.alloca f32 {bindc_name = "r4", uniq_name = "_QFtest_realEr4"}
+! CHECK:  %[[VAL_1:.*]] = fir.alloca f64 {bindc_name = "r8", uniq_name = "_QFtest_realEr8"}
+! CHECK:  %[[CST_0:.*]] = arith.constant 8.968310e-44 : f32
+! CHECK:  fir.store %[[CST_0]] to %[[VAL_0]] : !fir.ref<f32>
+! CHECK:  %[[CST_1:.*]] = arith.constant 3.162020e-322 : f64
+! CHECK:  fir.store %[[CST_1]] to %[[VAL_1]] : !fir.ref<f64>
+! CHECK:  return

diff  --git a/flang/unittests/Evaluate/real.cpp b/flang/unittests/Evaluate/real.cpp
index af06e7a688c30..1974f42624415 100644
--- a/flang/unittests/Evaluate/real.cpp
+++ b/flang/unittests/Evaluate/real.cpp
@@ -35,8 +35,8 @@ void dumpTest() {
       {0x3f000000, "0x1.0p-1"},
       {0x7f7fffff, "0x1.fffffep127"},
       {0x00800000, "0x1.0p-126"},
-      {0x00400000, "0x0.8p-127"},
-      {0x00000001, "0x0.000002p-127"},
+      {0x00400000, "0x0.8p-126"},
+      {0x00000001, "0x0.000002p-126"},
       {0, nullptr},
   };
   for (int j{0}; table[j].expected != nullptr; ++j) {


        


More information about the flang-commits mailing list