Skip to content

Commit d85db4e

Browse files
klauslervdonaldson
authored andcommitted
[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
1 parent fe8c4bf commit d85db4e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

flang/include/flang/Evaluate/real.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ class Real : public common::RealDetails<PREC> {
125125
template <typename INT> constexpr INT EXPONENT() const {
126126
if (Exponent() == maxExponent) {
127127
return INT::HUGE();
128+
} else if (IsZero()) {
129+
return {0};
128130
} else {
129-
return {UnbiasedExponent()};
131+
return {UnbiasedExponent() + 1};
130132
}
131133
}
132134

@@ -308,6 +310,8 @@ class Real : public common::RealDetails<PREC> {
308310

309311
// Extracts unbiased exponent value.
310312
// Corrects the exponent value of a subnormal number.
313+
// Note that the result is one less than the EXPONENT intrinsic;
314+
// UnbiasedExponent(1.0) is 0, not 1.
311315
constexpr int UnbiasedExponent() const {
312316
int exponent{Exponent() - exponentBias};
313317
if (IsSubnormal()) {

flang/test/Evaluate/folding07.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ module m
197197
logical, parameter :: test_tiny10 = tiny10 == ztiny10
198198
logical, parameter :: test_tiny16 = tiny16 == ztiny16
199199

200+
logical, parameter :: test_exponent_0 = exponent(0.0) == 0
201+
logical, parameter :: test_exponent_r8 = exponent(0.125) == -2
202+
logical, parameter :: test_exponent_r4 = exponent(0.25) == -1
203+
logical, parameter :: test_exponent_r2 = exponent(0.5) == 0
204+
logical, parameter :: test_exponent_1 = exponent(1.0) == 1
205+
logical, parameter :: test_exponent_4 = exponent(4.1) == 3
206+
logical, parameter :: test_exponent_12 = exponent(12.9) == 4
207+
200208
integer, parameter :: &
201209
max2 = maxexponent(0._2), &
202210
max3 = maxexponent(0._3), &

0 commit comments

Comments
 (0)