[llvm] MathExtras: rewrite some methods to never overflow (PR #95556)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 15 01:05:37 PDT 2024


================
@@ -424,33 +426,37 @@ template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
   return (Value + Align - 1) / Align * Align;
 }
 
-/// Returns the integer ceil(Numerator / Denominator). Unsigned integer version.
+/// Returns the integer ceil(Numerator / Denominator). Unsigned version.
+/// Guaranteed to never overflow.
 inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
-  return alignTo(Numerator, Denominator) / Denominator;
+  uint64_t Bias = (Numerator != 0);
----------------
jayfoad wrote:

Assert Denominator is non-zero, like the other methods do?

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


More information about the llvm-commits mailing list