| 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_MUL_PRIV_H_ | ||
| 20 | #define INCLUDE_SATOP_MUL_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 | #include "satop_sign_util-priv.h" | ||
| 29 | |||
| 30 | namespace saturated { | ||
| 31 | |||
| 32 | namespace impl { | ||
| 33 | |||
| 34 | template <typename T> | ||
| 35 | 108 | constexpr bool is_mul_overflow(T x, T y) { | |
| 36 | 108 | return ((csignbit(x) == csignbit(y)) | |
| 37 |
4/4✓ Branch 0 taken 42 times.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 18 times.
|
108 | && (x > std::numeric_limits<T>::max() / y)); |
| 38 | } | ||
| 39 | |||
| 40 | template <typename T> | ||
| 41 | 60 | constexpr bool is_mul_underflow(T x, T y) { | |
| 42 | 60 | return ((csignbit(x) != csignbit(y)) | |
| 43 |
4/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 3 times.
|
78 | && ((x < std::numeric_limits<T>::lowest() / y) |
| 44 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 6 times.
|
78 | || (y < std::numeric_limits<T>::lowest() / x))); |
| 45 | } | ||
| 46 | |||
| 47 | } // namespace impl | ||
| 48 | |||
| 49 | /// @addtogroup libsatop | ||
| 50 | /// | ||
| 51 | /// @{ | ||
| 52 | |||
| 53 | /// Multiply 2 values with saturation. | ||
| 54 | /// | ||
| 55 | /// @tparam T Type of arguments and the return value | ||
| 56 | /// | ||
| 57 | /// @param x A value to multiply | ||
| 58 | /// @param y A value to multiply | ||
| 59 | /// | ||
| 60 | /// @return If multiply 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 | 54 | constexpr T mul(T x, T y) { | |
| 65 | using limits = std::numeric_limits<T>; | ||
| 66 | 54 | return (impl::is_mul_overflow(x, y) | |
| 67 | 84 | ? limits::max() | |
| 68 | 30 | : (impl::is_mul_underflow(x, y) | |
| 69 | 30 | ? limits::lowest() | |
| 70 | 64 | : static_cast<T>(x * y))); | |
| 71 | } | ||
| 72 | |||
| 73 | /// @} | ||
| 74 | |||
| 75 | } // namespace saturated | ||
| 76 | |||
| 77 | #endif // INCLUDE_SATOP_MUL_PRIV_H_ | ||
| 78 |