4#ifndef TETL_NUMERIC_ADD_SAT_HPP
5#define TETL_NUMERIC_ADD_SAT_HPP
7#include <etl/_algorithm/clamp.hpp>
8#include <etl/_concepts/builtin_integer.hpp>
9#include <etl/_concepts/same_as.hpp>
10#include <etl/_cstdint/int_t.hpp>
11#include <etl/_cstdint/uint_t.hpp>
12#include <etl/_limits/numeric_limits.hpp>
13#include <etl/_type_traits/always_false.hpp>
14#include <etl/_type_traits/is_signed.hpp>
15#include <etl/_type_traits/is_unsigned.hpp>
21template <etl::builtin_integer Int>
22[[nodiscard]]
constexpr auto add_sat_fallback(Int x, Int y)
noexcept -> Int
27 if constexpr (
sizeof(Int) <
sizeof(
int)
and etl::same_as<
decltype(x + y),
int>) {
28 return Int(
etl::clamp(x + y,
int(min),
int(max)));
29 }
else if constexpr (
sizeof(Int) <
sizeof(
unsigned)
and etl::same_as<
decltype(x + y),
unsigned>) {
30 return Int(
etl::clamp(x + y,
unsigned(min),
unsigned(max)));
31 }
else if constexpr (
sizeof(Int) == 2
and is_signed_v<Int>) {
33 }
else if constexpr (
sizeof(Int) == 2
and is_unsigned_v<Int>) {
35 }
else if constexpr (
sizeof(Int) == 4
and is_signed_v<Int>) {
37 }
else if constexpr (
sizeof(Int) == 4
and is_unsigned_v<Int>) {
56template <
etl::builtin_integer Int>
57[[nodiscard]]
constexpr auto add_sat(Int x, Int y)
noexcept -> Int
59#if defined(__GNUC__) or defined(__clang__)
63 if (Int sum{0};
not __builtin_add_overflow(x, y, &sum)) {
66 if constexpr (is_unsigned_v<Int>) {
75 return etl::detail::add_sat_fallback(x, y);
constexpr auto clamp(Type const &v, Type const &lo, Type const &hi) noexcept -> Type const &
If v compares less than lo, returns lo; otherwise if hi compares less than v, returns hi; otherwise r...
Definition clamp.hpp:24
constexpr auto add_sat(Int x, Int y) noexcept -> Int
Definition add_sat.hpp:57
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18