tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
cmp_equal.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_UTILITY_CMP_EQUAL_HPP
5#define TETL_UTILITY_CMP_EQUAL_HPP
6
7#include <etl/_concepts/builtin_integer.hpp>
8#include <etl/_type_traits/is_signed.hpp>
9#include <etl/_type_traits/make_unsigned.hpp>
10
11namespace etl {
12
13/// Compare the values of two integers t and u. Unlike builtin comparison
14/// operators, negative signed integers always compare less than (and not equal
15/// to) unsigned integers: the comparison is safe against lossy integer
16/// conversion.
17///
18/// https://en.cppreference.com/w/cpp/utility/intcmp
19///
20/// \ingroup utility
21template <builtin_integer T, builtin_integer U>
22[[nodiscard]] constexpr auto cmp_equal(T t, U u) noexcept -> bool
23{
24 using UT = etl::make_unsigned_t<T>;
25 using UU = etl::make_unsigned_t<U>;
26
27 if constexpr (etl::is_signed_v<T> == etl::is_signed_v<U>) {
28 return t == u;
29 } else if constexpr (etl::is_signed_v<T>) {
30 return t < 0 ? false : UT(t) == u;
31 } else {
32 return u < 0 ? false : t == UU(u);
33 }
34}
35
36} // namespace etl
37
38#endif // TETL_UTILITY_CMP_EQUAL_HPP
constexpr auto cmp_equal(T t, U u) noexcept -> bool
Compare the values of two integers t and u. Unlike builtin comparison operators, negative signed inte...
Definition cmp_equal.hpp:22
Definition adjacent_find.hpp:9