summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2020-04-30 11:50:26 +0300
committerAndreas Kling <kling@serenityos.org>2020-04-30 11:30:27 +0200
commit361a1b54d75de9fb9b9e04c82f87b58d37f4e2bc (patch)
tree31803adbbc3521959864e7c4c88f89e23b821b6f /AK
parent1b36ddce1d19b6d82bdfa6ebf39c311a4599a988 (diff)
downloadserenity-361a1b54d75de9fb9b9e04c82f87b58d37f4e2bc.zip
AK: Add Checked::addition_would_overflow()
And switch the two-argument version of Checked::multiplication_would_overflow() to use __builtin_mul_overflow_p(). This helps GCC optimize the code better.
Diffstat (limited to 'AK')
-rw-r--r--AK/Checked.h11
1 files changed, 7 insertions, 4 deletions
diff --git a/AK/Checked.h b/AK/Checked.h
index 9514a8fb9d..ac98519678 100644
--- a/AK/Checked.h
+++ b/AK/Checked.h
@@ -234,13 +234,16 @@ public:
return *this;
}
+ template<typename U, typename V>
+ static bool addition_would_overflow(U u, V v)
+ {
+ return __builtin_add_overflow_p(u, v, (T)0);
+ }
+
template<typename U, typename V, typename X>
static bool multiplication_would_overflow(U u, V v)
{
- Checked checked;
- checked = u;
- checked *= v;
- return checked.has_overflow();
+ return __builtin_mul_overflow_p(u, v, (T)0);
}
template<typename U, typename V, typename X>