diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-07-28 03:30:35 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2021-07-28 08:52:03 +0200 |
commit | 44a4b028d094dda4dc4886a4670371175da8024f (patch) | |
tree | 46cf0563603bbdaa642a168890ef4509c6d1da8b /include | |
parent | d8fec570c21c5e45ba593ee96cc1ba5d45d77fc8 (diff) |
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 <sbergman@redhat.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/o3tl/safeint.hxx | 30 |
1 files changed, 2 insertions, 28 deletions
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<typename T> inline -typename std::enable_if<std::is_signed<T>::value, T>::type saturating_add( - T a, T b) +template <typename T> inline T saturating_add(T a, T b) { if (b >= 0) { if (a <= std::numeric_limits<T>::max() - b) { @@ -46,20 +44,7 @@ typename std::enable_if<std::is_signed<T>::value, T>::type saturating_add( } } -template<typename T> inline -typename std::enable_if<std::is_unsigned<T>::value, T>::type saturating_add( - T a, T b) -{ - if (a <= std::numeric_limits<T>::max() - b) { - return a + b; - } else { - return std::numeric_limits<T>::max(); - } -} - -template<typename T> inline -typename std::enable_if<std::is_signed<T>::value, T>::type saturating_sub( - T a, T b) +template <typename T> inline T saturating_sub(T a, T b) { if (b >= 0) { if (a >= std::numeric_limits<T>::min() + b) { @@ -77,17 +62,6 @@ typename std::enable_if<std::is_signed<T>::value, T>::type saturating_sub( } template<typename T> inline -typename std::enable_if<std::is_unsigned<T>::value, T>::type saturating_sub( - T a, T b) -{ - if (a >= std::numeric_limits<T>::min() + b) { - return a - b; - } else { - return std::numeric_limits<T>::min(); - } -} - -template<typename T> inline typename std::enable_if<std::is_signed<T>::value, T>::type saturating_toggle_sign( T a) { |