Skip to content

Commit

Permalink
Use std::gcd (NFC)
Browse files Browse the repository at this point in the history
To avoid changing semantics inadvertently, this patch casts arguments
to uint64_t before calling std::gcd.
  • Loading branch information
kazutakahirata committed Aug 28, 2022
1 parent 267f21a commit 4a2377a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion mlir/lib/Analysis/Presburger/IntegerRelation.cpp
Expand Up @@ -659,7 +659,7 @@ bool IntegerRelation::isEmptyByGCDTest() const {
for (unsigned i = 0, e = getNumEqualities(); i < e; ++i) {
uint64_t gcd = std::abs(atEq(i, 0));
for (unsigned j = 1; j < numCols - 1; ++j) {
gcd = llvm::GreatestCommonDivisor64(gcd, std::abs(atEq(i, j)));
gcd = std::gcd(gcd, (uint64_t)std::abs(atEq(i, j)));
}
int64_t v = std::abs(atEq(i, numCols - 1));
if (gcd > 0 && (v % gcd != 0)) {
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Analysis/Presburger/Utils.cpp
Expand Up @@ -316,7 +316,7 @@ SmallVector<int64_t, 8> presburger::getDivLowerBound(ArrayRef<int64_t> dividend,
int64_t presburger::gcdRange(ArrayRef<int64_t> range) {
int64_t gcd = 0;
for (int64_t elem : range) {
gcd = llvm::GreatestCommonDivisor64(gcd, std::abs(elem));
gcd = std::gcd((uint64_t)gcd, (uint64_t)std::abs(elem));
if (gcd == 1)
return gcd;
}
Expand Down
15 changes: 7 additions & 8 deletions mlir/lib/IR/AffineExpr.cpp
Expand Up @@ -16,6 +16,7 @@
#include "mlir/Support/MathExtras.h"
#include "mlir/Support/TypeID.h"
#include "llvm/ADT/STLExtras.h"
#include <numeric>

using namespace mlir;
using namespace mlir::detail;
Expand Down Expand Up @@ -235,9 +236,8 @@ int64_t AffineExpr::getLargestKnownDivisor() const {
[[fallthrough]];
case AffineExprKind::Mod: {
binExpr = cast<AffineBinaryOpExpr>();
return llvm::GreatestCommonDivisor64(
binExpr.getLHS().getLargestKnownDivisor(),
binExpr.getRHS().getLargestKnownDivisor());
return std::gcd((uint64_t)binExpr.getLHS().getLargestKnownDivisor(),
(uint64_t)binExpr.getRHS().getLargestKnownDivisor());
}
}
llvm_unreachable("Unknown AffineExpr");
Expand Down Expand Up @@ -267,9 +267,8 @@ bool AffineExpr::isMultipleOf(int64_t factor) const {
case AffineExprKind::CeilDiv:
case AffineExprKind::Mod: {
binExpr = cast<AffineBinaryOpExpr>();
return llvm::GreatestCommonDivisor64(
binExpr.getLHS().getLargestKnownDivisor(),
binExpr.getRHS().getLargestKnownDivisor()) %
return std::gcd((uint64_t)binExpr.getLHS().getLargestKnownDivisor(),
(uint64_t)binExpr.getRHS().getLargestKnownDivisor()) %
factor ==
0;
}
Expand Down Expand Up @@ -1201,7 +1200,7 @@ void SimpleAffineExprFlattener::visitModExpr(AffineBinaryOpExpr expr) {
SmallVector<int64_t, 8> floorDividend(lhs);
uint64_t gcd = rhsConst;
for (unsigned i = 0, e = lhs.size(); i < e; i++)
gcd = llvm::GreatestCommonDivisor64(gcd, std::abs(lhs[i]));
gcd = std::gcd(gcd, (uint64_t)std::abs(lhs[i]));
// Simplify the numerator and the denominator.
if (gcd != 1) {
for (unsigned i = 0, e = floorDividend.size(); i < e; i++)
Expand Down Expand Up @@ -1313,7 +1312,7 @@ void SimpleAffineExprFlattener::visitDivExpr(AffineBinaryOpExpr expr,
// common divisors of the numerator and denominator.
uint64_t gcd = std::abs(rhsConst);
for (unsigned i = 0, e = lhs.size(); i < e; i++)
gcd = llvm::GreatestCommonDivisor64(gcd, std::abs(lhs[i]));
gcd = std::gcd(gcd, (uint64_t)std::abs(lhs[i]));
// Simplify the numerator and the denominator.
if (gcd != 1) {
for (unsigned i = 0, e = lhs.size(); i < e; i++)
Expand Down
3 changes: 2 additions & 1 deletion polly/lib/Analysis/ScopInfo.cpp
Expand Up @@ -67,6 +67,7 @@
#include "isl/options.h"
#include "isl/set.h"
#include <cassert>
#include <numeric>

using namespace llvm;
using namespace polly;
Expand Down Expand Up @@ -292,7 +293,7 @@ void ScopArrayInfo::updateElementType(Type *NewElementType) {
if (NewElementSize % OldElementSize == 0 && NewElementSize < OldElementSize) {
ElementType = NewElementType;
} else {
auto GCD = GreatestCommonDivisor64(NewElementSize, OldElementSize);
auto GCD = std::gcd((uint64_t)NewElementSize, (uint64_t)OldElementSize);
ElementType = IntegerType::get(ElementType->getContext(), GCD);
}
}
Expand Down

0 comments on commit 4a2377a

Please sign in to comment.