[PATCH] D48498: [APInt] Add helpers for rounding u/sdivs.

Sanjoy Das via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 22 14:26:18 PDT 2018


sanjoy added inline comments.


================
Comment at: llvm/include/llvm/ADT/APInt.h:2160
 
+/// Return A unsign-divided by B, rounded by the given roundining mode.
+APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
----------------
Nit: rounding

Did you consider implementing these in `sdiv` and `udiv` and passing in a defaulted `Rounded` parameter?  That seems a bit cleaner to me.

Otherwise can you please add comments on `sdiv` and `udiv` about their rounding behavior?


================
Comment at: llvm/lib/Support/APInt.cpp:2686
+    APInt Quo, Rem;
+    APInt::sdivrem(A, B, Quo, Rem);
+    if (Rem == 0)
----------------
I'm wondering if this can be simplified.  Is this logic correct:

```
Q, R = sdivrem(A, B);
if (R == 0) { return Q; }
if (Q < 0) { return RoundDown ? Q-1:Q; }
return RoundDown ? Q:Q-1;
```

the logic being that if R != 0 then the quotient is "off by one" depending on its sign.


https://reviews.llvm.org/D48498





More information about the llvm-commits mailing list