Skip to content

Commit 0db9531

Browse files
committed
[APInt] add sfloordiv_ov APInt's member function
for mlir fold to avoid too many overflow state check
1 parent 4fef8c7 commit 0db9531

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

llvm/include/llvm/ADT/APInt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ class [[nodiscard]] APInt {
996996
APInt sshl_ov(unsigned Amt, bool &Overflow) const;
997997
APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
998998
APInt ushl_ov(unsigned Amt, bool &Overflow) const;
999+
APInt sfloordiv_ov(const APInt &RHS, bool &Overflow) const;
9991000

10001001
// Operations that saturate
10011002
APInt sadd_sat(const APInt &RHS) const;

llvm/lib/Support/APInt.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,14 @@ APInt APInt::ushl_ov(unsigned ShAmt, bool &Overflow) const {
20222022
return *this << ShAmt;
20232023
}
20242024

2025+
APInt APInt::sfloordiv_ov(const APInt &RHS, bool &Overflow) const {
2026+
auto quotient = sdiv_ov(RHS, Overflow);
2027+
if ((quotient * RHS != *this) && (isNegative() != RHS.isNegative()))
2028+
return quotient - 1;
2029+
else
2030+
return quotient;
2031+
}
2032+
20252033
APInt APInt::sadd_sat(const APInt &RHS) const {
20262034
bool Overflow;
20272035
APInt Res = sadd_ov(RHS, Overflow);

llvm/unittests/ADT/APIntTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "gtest/gtest.h"
1616
#include <array>
1717
#include <climits>
18+
#include <limits>
1819
#include <optional>
1920

2021
using namespace llvm;
@@ -3048,6 +3049,43 @@ TEST(APIntTest, smul_ov) {
30483049
}
30493050
}
30503051

3052+
TEST(APIntTest, sfloordiv_ov) {
3053+
// test negative quotient
3054+
{
3055+
APInt divisor(32, -3, true);
3056+
APInt dividend(32, 2, true);
3057+
bool Overflow = false;
3058+
auto quotient = divisor.sfloordiv_ov(dividend, Overflow);
3059+
EXPECT_FALSE(Overflow);
3060+
EXPECT_EQ(-2, quotient.getSExtValue());
3061+
}
3062+
// test positive quotient
3063+
{
3064+
APInt divisor(32, 3, true);
3065+
APInt dividend(32, 2, true);
3066+
bool Overflow = false;
3067+
auto quotient = divisor.sfloordiv_ov(dividend, Overflow);
3068+
EXPECT_FALSE(Overflow);
3069+
EXPECT_EQ(1, quotient.getSExtValue());
3070+
}
3071+
// test overflow
3072+
{
3073+
auto check_overflow_one = [](auto arg) {
3074+
using IntTy = decltype(arg);
3075+
APInt divisor(8 * sizeof(arg), std::numeric_limits<IntTy>::lowest(),
3076+
true);
3077+
APInt dividend(8 * sizeof(arg), IntTy(-1), true);
3078+
bool Overflow = false;
3079+
[[maybe_unused]] auto quotient = divisor.sfloordiv_ov(dividend, Overflow);
3080+
EXPECT_TRUE(Overflow);
3081+
};
3082+
auto check_overflow_all = [&](auto... args) {
3083+
(void)std::initializer_list<int>{(check_overflow_one(args), 0)...};
3084+
};
3085+
std::apply(check_overflow_all, std::tuple<char, short, int, int64_t>());
3086+
}
3087+
}
3088+
30513089
TEST(APIntTest, SolveQuadraticEquationWrap) {
30523090
// Verify that "Solution" is the first non-negative integer that solves
30533091
// Ax^2 + Bx + C = "0 or overflow", i.e. that it is a correct solution

0 commit comments

Comments
 (0)