tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
div_sat.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3
4#ifndef TETL_NUMERIC_DIV_SAT_HPP
5#define TETL_NUMERIC_DIV_SAT_HPP
6
7#include <etl/_concepts/builtin_integer.hpp>
8#include <etl/_contracts/check.hpp>
9#include <etl/_limits/numeric_limits.hpp>
10#include <etl/_type_traits/is_signed.hpp>
11
12namespace etl {
13
14/// Computes the saturating division x / y.
15/// \ingroup numeric
16template <builtin_integer Int>
17[[nodiscard]] constexpr auto div_sat(Int x, Int y) noexcept -> Int
18{
19 TETL_PRECONDITION(y != 0);
20 if constexpr (etl::is_signed_v<Int>) {
21 if (x == etl::numeric_limits<Int>::min() and y == Int(-1)) {
22 return etl::numeric_limits<Int>::max();
23 }
24 }
25 return x / y;
26}
27
28} // namespace etl
29
30#endif // TETL_NUMERIC_DIV_SAT_HPP
constexpr auto div_sat(Int x, Int y) noexcept -> Int
Computes the saturating division x / y.
Definition div_sat.hpp:17
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18