From 44a4b028d094dda4dc4886a4670371175da8024f Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Wed, 28 Jul 2021 03:30:35 +0200 Subject: Consolidate saturating_(add,sub) for signed and unsigned Compiler would optimize away the branch that is unreachable for current operand type anyway. Change-Id: Iac84057c1716990d107529d52dba1dd7603c5a32 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119448 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- include/o3tl/safeint.hxx | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) (limited to 'include') diff --git a/include/o3tl/safeint.hxx b/include/o3tl/safeint.hxx index 801b3dc6fdd5..85c61f8c33d3 100644 --- a/include/o3tl/safeint.hxx +++ b/include/o3tl/safeint.hxx @@ -27,9 +27,7 @@ namespace o3tl { -template inline -typename std::enable_if::value, T>::type saturating_add( - T a, T b) +template inline T saturating_add(T a, T b) { if (b >= 0) { if (a <= std::numeric_limits::max() - b) { @@ -46,20 +44,7 @@ typename std::enable_if::value, T>::type saturating_add( } } -template inline -typename std::enable_if::value, T>::type saturating_add( - T a, T b) -{ - if (a <= std::numeric_limits::max() - b) { - return a + b; - } else { - return std::numeric_limits::max(); - } -} - -template inline -typename std::enable_if::value, T>::type saturating_sub( - T a, T b) +template inline T saturating_sub(T a, T b) { if (b >= 0) { if (a >= std::numeric_limits::min() + b) { @@ -76,17 +61,6 @@ typename std::enable_if::value, T>::type saturating_sub( } } -template inline -typename std::enable_if::value, T>::type saturating_sub( - T a, T b) -{ - if (a >= std::numeric_limits::min() + b) { - return a - b; - } else { - return std::numeric_limits::min(); - } -} - template inline typename std::enable_if::value, T>::type saturating_toggle_sign( T a) -- cgit