Skip to content

Commit 2ca17ee

Browse files
committed
More folding, more testing
1 parent 5d66679 commit 2ca17ee

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

flang/include/flang/Evaluate/complex.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ template <typename REAL_TYPE> class Complex {
6161

6262
template <typename INT>
6363
static ValueWithRealFlags<Complex> FromInteger(const INT &n,
64+
bool isUnsigned = false,
6465
Rounding rounding = TargetCharacteristics::defaultRounding) {
6566
ValueWithRealFlags<Complex> result;
66-
result.value.re_ =
67-
Part::FromInteger(n, rounding).AccumulateFlags(result.flags);
67+
result.value.re_ = Part::FromInteger(n, isUnsigned, rounding)
68+
.AccumulateFlags(result.flags);
6869
return result;
6970
}
7071

flang/include/flang/Evaluate/real.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ template <typename WORD, int PREC> class Real {
288288

289289
template <typename INT>
290290
static ValueWithRealFlags<Real> FromInteger(const INT &n,
291+
bool isUnsigned = false,
291292
Rounding rounding = TargetCharacteristics::defaultRounding) {
292-
bool isNegative{n.IsNegative()};
293+
bool isNegative{!isUnsigned && n.IsNegative()};
293294
INT absN{n};
294295
if (isNegative) {
295296
absN = n.Negate().value; // overflow is safe to ignore

flang/lib/Evaluate/fold-implementation.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,8 @@ Expr<TO> FoldOperation(
17351735
if (auto value{GetScalarConstantValue<Operand>(kindExpr)}) {
17361736
FoldingContext &ctx{msvcWorkaround.context};
17371737
if constexpr (TO::category == TypeCategory::Integer) {
1738-
if constexpr (FromCat == TypeCategory::Integer) {
1738+
if constexpr (FromCat == TypeCategory::Integer ||
1739+
FromCat == TypeCategory::Unsigned) {
17391740
auto converted{Scalar<TO>::ConvertSigned(*value)};
17401741
if (converted.overflow &&
17411742
msvcWorkaround.context.languageFeatures().ShouldWarn(
@@ -1762,9 +1763,20 @@ Expr<TO> FoldOperation(
17621763
}
17631764
return ScalarConstantToExpr(std::move(converted.value));
17641765
}
1766+
} else if constexpr (TO::category == TypeCategory::Unsigned) {
1767+
if constexpr (FromCat == TypeCategory::Integer ||
1768+
FromCat == TypeCategory::Unsigned) {
1769+
return Expr<TO>{
1770+
Constant<TO>{Scalar<TO>::ConvertUnsigned(*value).value}};
1771+
} else if constexpr (FromCat == TypeCategory::Real) {
1772+
return Expr<TO>{
1773+
Constant<TO>{value->template ToInteger<Scalar<TO>>().value}};
1774+
}
17651775
} else if constexpr (TO::category == TypeCategory::Real) {
1766-
if constexpr (FromCat == TypeCategory::Integer) {
1767-
auto converted{Scalar<TO>::FromInteger(*value)};
1776+
if constexpr (FromCat == TypeCategory::Integer ||
1777+
FromCat == TypeCategory::Unsigned) {
1778+
auto converted{Scalar<TO>::FromInteger(
1779+
*value, FromCat == TypeCategory::Unsigned)};
17681780
if (!converted.flags.empty()) {
17691781
char buffer[64];
17701782
std::snprintf(buffer, sizeof buffer,

flang/lib/Evaluate/tools.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,10 @@ std::optional<Expr<LogicalResult>> Relate(parser::ContextualMessages &messages,
643643
Expr<SomeInteger> &&iy) -> std::optional<Expr<LogicalResult>> {
644644
return PromoteAndRelate(opr, std::move(ix), std::move(iy));
645645
},
646+
[=](Expr<SomeUnsigned> &&ix,
647+
Expr<SomeUnsigned> &&iy) -> std::optional<Expr<LogicalResult>> {
648+
return PromoteAndRelate(opr, std::move(ix), std::move(iy));
649+
},
646650
[=](Expr<SomeReal> &&rx,
647651
Expr<SomeReal> &&ry) -> std::optional<Expr<LogicalResult>> {
648652
return PromoteAndRelate(opr, std::move(rx), std::move(ry));

flang/test/Evaluate/fold-unsigned.f90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! RUN: %python %S/test_folding.py %s %flang_fc1 -funsigned
2+
! UNSIGNED operations and intrinsic functions
3+
4+
module m
5+
6+
logical, parameter :: test_huge_1 = huge(0u_1) == 255u_1
7+
logical, parameter :: test_huge_2 = huge(0u_2) == 65535u_2
8+
logical, parameter :: test_huge_4 = huge(0u_4) == uint(huge(0_4),4) * 2u + 1u
9+
logical, parameter :: test_huge_8 = huge(0u_8) == uint(huge(0_8),8) * 2u + 1u
10+
logical, parameter :: test_huge_16 = huge(0u_16) == uint(huge(0_16),16) * 2u + 1u
11+
12+
end

flang/unittests/Evaluate/real.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ template <typename R> void basicTests(int rm, Rounding rounding) {
142142
Integer8 ix{x};
143143
TEST(!ix.IsNegative())(ldesc);
144144
MATCH(x, ix.ToUInt64())(ldesc);
145-
vr = R::FromInteger(ix, rounding);
145+
vr = R::FromInteger(ix, false, rounding);
146146
TEST(!vr.value.IsNegative())(ldesc);
147147
TEST(!vr.value.IsNotANumber())(ldesc);
148148
TEST(!vr.value.IsZero())(ldesc);
@@ -303,7 +303,7 @@ void inttest(std::int64_t x, int pass, Rounding rounding) {
303303
ScopedHostFloatingPointEnvironment fpenv;
304304
Integer8 ix{x};
305305
ValueWithRealFlags<REAL> real;
306-
real = real.value.FromInteger(ix, rounding);
306+
real = real.value.FromInteger(ix, false, rounding);
307307
#ifndef __clang__ // broken and also slow
308308
fpenv.ClearFlags();
309309
#endif

0 commit comments

Comments
 (0)