[flang-commits] [flang] e337dc8 - [flang] Fix folding of EXPONENT() intrinsic function

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Sat Dec 4 11:23:17 PST 2021


Author: Peter Klausler
Date: 2021-12-04T11:23:09-08:00
New Revision: e337dc8bfee0793fdeafd61da3ab658a0822a24c

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

LOG: [flang] Fix folding of EXPONENT() intrinsic function

The definition of the EXPONENT() intrinsic function differs by one
from the real arithmetic folding templates concept of an unbiased
exponent, and also needs special handling for zero.  Fix, and add
more tests.

Differential Revision: https://reviews.llvm.org/D115084

Added: 
    

Modified: 
    flang/include/flang/Evaluate/real.h
    flang/test/Evaluate/folding07.f90

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/real.h b/flang/include/flang/Evaluate/real.h
index 80e677780db40..8f497401b46e5 100644
--- a/flang/include/flang/Evaluate/real.h
+++ b/flang/include/flang/Evaluate/real.h
@@ -125,8 +125,10 @@ class Real : public common::RealDetails<PREC> {
   template <typename INT> constexpr INT EXPONENT() const {
     if (Exponent() == maxExponent) {
       return INT::HUGE();
+    } else if (IsZero()) {
+      return {0};
     } else {
-      return {UnbiasedExponent()};
+      return {UnbiasedExponent() + 1};
     }
   }
 
@@ -308,6 +310,8 @@ class Real : public common::RealDetails<PREC> {
 
   // Extracts unbiased exponent value.
   // Corrects the exponent value of a subnormal number.
+  // Note that the result is one less than the EXPONENT intrinsic;
+  // UnbiasedExponent(1.0) is 0, not 1.
   constexpr int UnbiasedExponent() const {
     int exponent{Exponent() - exponentBias};
     if (IsSubnormal()) {

diff  --git a/flang/test/Evaluate/folding07.f90 b/flang/test/Evaluate/folding07.f90
index e79dfaf9d4563..b8eca9509027b 100644
--- a/flang/test/Evaluate/folding07.f90
+++ b/flang/test/Evaluate/folding07.f90
@@ -197,6 +197,14 @@ module m
   logical, parameter :: test_tiny10 = tiny10 == ztiny10
   logical, parameter :: test_tiny16 = tiny16 == ztiny16
 
+  logical, parameter :: test_exponent_0 = exponent(0.0) == 0
+  logical, parameter :: test_exponent_r8 = exponent(0.125) == -2
+  logical, parameter :: test_exponent_r4 = exponent(0.25) == -1
+  logical, parameter :: test_exponent_r2 = exponent(0.5) == 0
+  logical, parameter :: test_exponent_1 = exponent(1.0) == 1
+  logical, parameter :: test_exponent_4 = exponent(4.1) == 3
+  logical, parameter :: test_exponent_12 = exponent(12.9) == 4
+
   integer, parameter :: &
     max2 = maxexponent(0._2), &
     max3 = maxexponent(0._3), &


        


More information about the flang-commits mailing list