[Mlir-commits] [llvm] [mlir] mlir/MathExtras: consolidate with llvm/MathExtras (PR #95087)

Nikita Popov llvmlistbot at llvm.org
Tue Jun 11 03:43:33 PDT 2024


================
@@ -424,11 +424,43 @@ template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
   return (Value + Align - 1) / Align * Align;
 }
 
-/// Returns the integer ceil(Numerator / Denominator).
+/// Returns the integer ceil(Numerator / Denominator). Unsigned integer version.
 inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
   return alignTo(Numerator, Denominator) / Denominator;
 }
 
+/// Returns the integer ceil(Numerator / Denominator). Signed integer version.
+inline int64_t divideCeilSigned(int64_t Numerator, int64_t Denominator) {
+  assert(Denominator);
+  if (!Numerator)
+    return 0;
+  // C's integer division rounds towards 0.
+  int64_t X = (Denominator > 0) ? -1 : 1;
+  bool SameSign = (Numerator > 0) == (Denominator > 0);
+  return SameSign ? ((Numerator + X) / Denominator) + 1
+                  : -(-Numerator / Denominator);
----------------
nikic wrote:

Can this be simplified to?
```suggestion
                  : (Numerator / Denominator);
```
If I understand correctly, then normal division is already ceil division if the result is negative.

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


More information about the Mlir-commits mailing list