[Mlir-commits] [llvm] [mlir] mlir/MathExtras: consolidate with llvm/MathExtras (PR #95087)
Nikita Popov
llvmlistbot at llvm.org
Tue Jun 11 03:43:35 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);
+}
+
+/// Returns the integer floor(Numerator / Denominator). Signed integer version.
+inline int64_t divideFloorSigned(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 / Denominator
+ : -((-Numerator + X) / Denominator) - 1;
+}
+
+/// Returns the remainder of the Euclidean division of LHS by RHS. Result is
+/// always non-negative.
+inline int64_t mod(int64_t Numerator, int64_t Denominator) {
+ assert(Denominator >= 1);
+ return Numerator % Denominator < 0 ? Numerator % Denominator + Denominator
+ : Numerator % Denominator;
----------------
nikic wrote:
Store `Numerator % Denominator` in a variable? Otherwise this would perform quite terribly if it does not get CSEd.
https://github.com/llvm/llvm-project/pull/95087
More information about the Mlir-commits
mailing list