[llvm] MathExtras: avoid unnecessarily widening types (PR #95426)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 24 03:23:44 PDT 2024
================
@@ -386,19 +408,30 @@ inline uint64_t PowerOf2Ceil(uint64_t A) {
/// alignTo(321, 255) = 510
/// \endcode
///
-/// May overflow.
-inline uint64_t alignTo(uint64_t Value, uint64_t Align) {
+/// Will overflow only if result is not representable in T.
+template <typename U, typename V, typename T = common_uint<U, V>>
+constexpr T alignTo(U Value, V Align) {
assert(Align != 0u && "Align can't be 0.");
- return (Value + Align - 1) / Align * Align;
+ T Bias = (Value != 0);
+ T CeilDiv = (Value - Bias) / Align + Bias;
----------------
jayfoad wrote:
Why not call divideCeil, instead of reimplementing it here?
https://github.com/llvm/llvm-project/pull/95426
More information about the llvm-commits
mailing list