Skip to content

Commit affd73a

Browse files
authored
MathExtras: MulOverflow: use builtin when available (NFC) (#95046)
This matches the other two functions AddOverflow and SubOverflow.
1 parent c63a622 commit affd73a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

llvm/include/llvm/Support/MathExtras.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ std::enable_if_t<std::is_signed_v<T>, T> SubOverflow(T X, T Y, T &Result) {
635635
/// result, returning true if an overflow ocurred.
636636
template <typename T>
637637
std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
638+
#if __has_builtin(__builtin_mul_overflow)
639+
return __builtin_mul_overflow(X, Y, &Result);
640+
#else
638641
// Perform the unsigned multiplication on absolute values.
639642
using U = std::make_unsigned_t<T>;
640643
const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
@@ -656,6 +659,7 @@ std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
656659
return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
657660
else
658661
return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
662+
#endif
659663
}
660664

661665
} // namespace llvm

0 commit comments

Comments
 (0)