| 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_DIV_PRIV_H_ | ||
| 20 | #define INCLUDE_SATOP_DIV_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 | /// @addtogroup libsatop | ||
| 33 | /// | ||
| 34 | /// @{ | ||
| 35 | |||
| 36 | /// Divide 2 values with saturation. | ||
| 37 | /// | ||
| 38 | /// @tparam T Type of arguments and the return value | ||
| 39 | /// | ||
| 40 | /// @param x A value to divtiply | ||
| 41 | /// @param y A value to divtiply | ||
| 42 | /// | ||
| 43 | /// @return If divtiply results causes overflow, returns max of T. | ||
| 44 | /// If underflow, returns min(lowest) of T. | ||
| 45 | /// If no overflow and no underflow, returns x + y. | ||
| 46 | template <typename T> | ||
| 47 | 18 | constexpr T div(T x, T y) { | |
| 48 | 18 | return static_cast<T>(x / y); | |
| 49 | } | ||
| 50 | |||
| 51 | /// @} | ||
| 52 | |||
| 53 | } // namespace saturated | ||
| 54 | |||
| 55 | #endif // INCLUDE_SATOP_DIV_PRIV_H_ | ||
| 56 |