diff --git a/mlir/include/mlir/Support/MathExtras.h b/mlir/include/mlir/Support/MathExtras.h --- a/mlir/include/mlir/Support/MathExtras.h +++ b/mlir/include/mlir/Support/MathExtras.h @@ -24,7 +24,8 @@ assert(rhs != 0); // C/C++'s integer division rounds towards 0. int64_t x = (rhs > 0) ? -1 : 1; - return (lhs * rhs > 0) ? ((lhs + x) / rhs) + 1 : -(-lhs / rhs); + return ((lhs != 0) && (lhs > 0) == (rhs > 0)) ? ((lhs + x) / rhs) + 1 + : -(-lhs / rhs); } /// Returns the result of MLIR's floordiv operation on constants. The RHS is @@ -33,7 +34,8 @@ assert(rhs != 0); // C/C++'s integer division rounds towards 0. int64_t x = (rhs < 0) ? 1 : -1; - return (lhs * rhs < 0) ? -((-lhs + x) / rhs) - 1 : lhs / rhs; + return ((lhs != 0) && ((lhs < 0) != (rhs < 0))) ? -((-lhs + x) / rhs) - 1 + : lhs / rhs; } /// Returns MLIR's mod operation on constants. MLIR's mod operation yields the diff --git a/mlir/unittests/Support/MathExtrasTest.cpp b/mlir/unittests/Support/MathExtrasTest.cpp --- a/mlir/unittests/Support/MathExtrasTest.cpp +++ b/mlir/unittests/Support/MathExtrasTest.cpp @@ -17,6 +17,8 @@ EXPECT_THAT(ceilDiv(14, -3), Eq(-4)); EXPECT_THAT(ceilDiv(-14, -3), Eq(5)); EXPECT_THAT(ceilDiv(-14, 3), Eq(-4)); + EXPECT_THAT(ceilDiv(0, 3), Eq(0)); + EXPECT_THAT(ceilDiv(0, -3), Eq(0)); } TEST(MathExtrasTest, FloorDivTest) { @@ -24,4 +26,6 @@ EXPECT_THAT(floorDiv(14, -3), Eq(-5)); EXPECT_THAT(floorDiv(-14, -3), Eq(4)); EXPECT_THAT(floorDiv(-14, 3), Eq(-5)); + EXPECT_THAT(floorDiv(0, 3), Eq(0)); + EXPECT_THAT(floorDiv(0, -3), Eq(0)); }