| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright 2021 Minoru Sekine | ||
| 3 | // | ||
| 4 | // This file is part of libsatop. | ||
| 5 | // | ||
| 6 | // libsatop is free software: you can redistribute it and/or modify | ||
| 7 | // it under the terms of the GNU Lesser General Public License as published by | ||
| 8 | // the Free Software Foundation, either version 3 of the License, or | ||
| 9 | // (at your option) any later version. | ||
| 10 | // | ||
| 11 | // libsatop is distributed in the hope that it will be useful, | ||
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | // GNU Lesser General Public License for more details. | ||
| 15 | // | ||
| 16 | // You should have received a copy of the GNU Lesser General Public License | ||
| 17 | // along with libsatop. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | #ifndef INCLUDE_SATOP_ADD_PRIV_H_ | ||
| 20 | #define INCLUDE_SATOP_ADD_PRIV_H_ | ||
| 21 | |||
| 22 | #ifndef SATOP_INTERNAL | ||
| 23 | #error Do not include this file directly, libsatop.h instead. | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #include <limits> | ||
| 27 | |||
| 28 | namespace saturated { | ||
| 29 | |||
| 30 | namespace impl { | ||
| 31 | |||
| 32 | template <typename T> | ||
| 33 | 140 | constexpr bool is_add_overflow(T x, T y) { | |
| 34 | return ((x > 0) | ||
| 35 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 5 times.
|
84 | && (y > 0) |
| 36 |
4/4✓ Branch 0 taken 42 times.
✓ Branch 1 taken 28 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 15 times.
|
224 | && (x >= std::numeric_limits<T>::max() - y)); |
| 37 | } | ||
| 38 | |||
| 39 | template <typename T> | ||
| 40 | 96 | constexpr bool is_add_underflow(T x, T y) { | |
| 41 | 24 | return (!(x >= 0) | |
| 42 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
36 | && !(y >= 0) |
| 43 |
4/4✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 11 times.
|
106 | && ((x < std::numeric_limits<T>::lowest() - y) |
| 44 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
|
106 | || (y < std::numeric_limits<T>::lowest() - x))); |
| 45 | } | ||
| 46 | |||
| 47 | } // namespace impl | ||
| 48 | |||
| 49 | /// @addtogroup libsatop | ||
| 50 | /// | ||
| 51 | /// @{ | ||
| 52 | |||
| 53 | /// Add 2 values with saturation. | ||
| 54 | /// | ||
| 55 | /// @tparam T Type of arguments and the return value | ||
| 56 | /// | ||
| 57 | /// @param x A value to add | ||
| 58 | /// @param y A value to add | ||
| 59 | /// | ||
| 60 | /// @return If addition results causes overflow, returns max of T. | ||
| 61 | /// If underflow, returns min(lowest) of T. | ||
| 62 | /// If no overflow and no underflow, returns x + y. | ||
| 63 | template <typename T> | ||
| 64 | 70 | constexpr T add(T x, T y) { | |
| 65 | using limits = std::numeric_limits<T>; | ||
| 66 | 70 | return (impl::is_add_overflow(x, y) | |
| 67 | 118 | ? limits::max() | |
| 68 | 48 | : (impl::is_add_underflow(x, y) | |
| 69 | 48 | ? limits::lowest() | |
| 70 | 76 | : static_cast<T>(x + y))); | |
| 71 | } | ||
| 72 | |||
| 73 | /// @} | ||
| 74 | |||
| 75 | } // namespace saturated | ||
| 76 | |||
| 77 | #endif // INCLUDE_SATOP_ADD_PRIV_H_ | ||
| 78 |