[llvm] [ADT][APInt] add sfloordiv_ov APInt's member function (PR #84720)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 13 13:06:16 PDT 2024


================
@@ -2928,6 +2929,43 @@ TEST(APIntTest, smul_ov) {
       }
 }
 
+TEST(APIntTest, sfloordiv_ov) {
+  // test negative quotient
+  {
+    APInt divisor(32, -3, true);
+    APInt dividend(32, 2, true);
+    bool Overflow = false;
+    auto quotient = divisor.sfloordiv_ov(dividend, Overflow);
+    EXPECT_FALSE(Overflow);
+    EXPECT_EQ(-2, quotient.getSExtValue());
+  }
+  // test positive quotient
+  {
+    APInt divisor(32, 3, true);
+    APInt dividend(32, 2, true);
+    bool Overflow = false;
+    auto quotient = divisor.sfloordiv_ov(dividend, Overflow);
+    EXPECT_FALSE(Overflow);
+    EXPECT_EQ(1, quotient.getSExtValue());
+  }
+  // test overflow
+  {
+    auto check_overflow_one = [](auto arg) {
+      using IntTy = decltype(arg);
+      APInt divisor(8 * sizeof(arg), std::numeric_limits<IntTy>::lowest(),
+                    true);
+      APInt dividend(8 * sizeof(arg), IntTy(-1), true);
+      bool Overflow = false;
+      [[maybe_unused]] auto quotient = divisor.sfloordiv_ov(dividend, Overflow);
----------------
kuhar wrote:

```suggestion
     (void)divisor.sfloordiv_ov(dividend, Overflow);
```

https://github.com/llvm/llvm-project/pull/84720


More information about the llvm-commits mailing list