From 63b8c6913cea1bca0507e8d11e17afbe7560721b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 15 Apr 2020 17:14:18 +0200 Subject: [PATCH] AK: Add Checked::multiplication_would_overflow() This allows you to comfortably test if multiply 2 or 3 values would cause arithmetic overflow. --- AK/Checked.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/AK/Checked.h b/AK/Checked.h index 062fb962f4..9514a8fb9d 100644 --- a/AK/Checked.h +++ b/AK/Checked.h @@ -234,6 +234,25 @@ public: return *this; } + template + static bool multiplication_would_overflow(U u, V v) + { + Checked checked; + checked = u; + checked *= v; + return checked.has_overflow(); + } + + template + static bool multiplication_would_overflow(U u, V v, X x) + { + Checked checked; + checked = u; + checked *= v; + checked *= x; + return checked.has_overflow(); + } + private: T m_value; bool m_overflow { false };