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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 15 00:52:06 PDT 2024


================
@@ -385,6 +385,8 @@ inline uint64_t PowerOf2Ceil(uint64_t A) {
 ///   alignTo(~0LL, 8) = 0
 ///   alignTo(321, 255) = 510
 /// \endcode
+///
+/// May overflow.
----------------
jayfoad wrote:

FYI there is still room for improvement here. Overflow is unavoidable if the result is not representable, but the current implementation also overflows in some cases where the correct result would be representable.

Examples with 8-bit integers:
alignTo(200, 128) = 0 which is OK because the true result 256 is not representable
alignTo(200, 127) = 0 but the true result 254 is representable

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


More information about the llvm-commits mailing list