[flang-commits] [PATCH] D115084: [flang] Fix folding of EXPONENT() intrinsic function

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Dec 3 15:54:53 PST 2021


klausler created this revision.
klausler added a reviewer: PeteSteinfeld.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
klausler requested review of this revision.

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.


https://reviews.llvm.org/D115084

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


Index: flang/test/Evaluate/folding07.f90
===================================================================
--- flang/test/Evaluate/folding07.f90
+++ flang/test/Evaluate/folding07.f90
@@ -197,6 +197,14 @@
   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), &
Index: flang/include/flang/Evaluate/real.h
===================================================================
--- flang/include/flang/Evaluate/real.h
+++ flang/include/flang/Evaluate/real.h
@@ -125,8 +125,10 @@
   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 @@
 
   // 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()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115084.391766.patch
Type: text/x-patch
Size: 1693 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20211203/d7d42df4/attachment-0001.bin>


More information about the flang-commits mailing list