25#ifndef JAU_INT_MATH_CT_HPP_
26#define JAU_INT_MATH_CT_HPP_
68 template <std::
integral T>
69 constexpr int ct_sign(
const T x)
noexcept {
70 return (x != 0) | -(int)((std::make_unsigned_t<T>)((T)x) >> (
sizeof(T) * CHAR_BIT - 1));
92 template <jau::req::
signed_
integral T>
93 constexpr T
ct_abs(
const T x)
noexcept {
94 using unsigned_T = std::make_unsigned_t<T>;
95 const T mask = x >> (
sizeof(T) * CHAR_BIT - 1 );
98 return static_cast<unsigned_T
>( ( x + mask ) ^ mask );
103 template <
typename T,
104 std::enable_if_t< std::is_arithmetic_v<T> &&
105 !std::is_integral_v<T> &&
106 !std::is_unsigned_v<T>,
bool> =
true>
107 constexpr T
ct_abs(
const T x)
noexcept
113 template <jau::req::
unsigned_arithmetic T>
114 constexpr T
ct_abs(
const T x)
noexcept {
129 template <std::
integral T>
130 constexpr T
ct_min(
const T x,
const T y)
noexcept {
131 return y + ( (x - y) & ( (x - y) >> (
sizeof(T) * CHAR_BIT - 1 ) ) );
145 template <std::
integral T>
146 constexpr T
ct_max(
const T x,
const T y)
noexcept {
147 return x - ( (x - y) & ( (x - y) >> (
sizeof(T) * CHAR_BIT - 1 ) ) );
163 template <std::
integral T>
164 constexpr T
ct_clamp(
const T x,
const T min_val,
const T max_val)
noexcept {
179 template <jau::req::
unsigned_
integral T>
181 return b_if_unmasked ^ ( mask & ( a_if_masked ^ b_if_unmasked ) );
197 return std::has_single_bit(n) ? std::bit_ceil(n+1) : std::bit_ceil(n);
212 return std::has_single_bit(n) ? std::bit_ceil(n+1) : std::bit_ceil(n);
253 n = n - ((n >> 1) & 0x55555555);
254 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
255 n = (n + (n >> 4)) & 0x0f0f0f0f;
267 template <jau::req::
unsigned_
integral T>
270 return T(0) - ( x >> (
sizeof(T) * CHAR_BIT - 1 ) );
279 template <jau::req::
unsigned_
integral T>
constexpr T ct_expand_top_bit(T x)
Returns ~0 (2-complement) if top bit of arg is set, otherwise 0 (w/o branching) in O(1) and constant ...
constexpr uint32_t ct_bit_count(uint32_t n) noexcept
Returns the number of set bits within given 32bit integer (w/o branching) in O(1) and constant time (...
constexpr T ct_masked_merge(T mask, T a_if_masked, T b_if_unmasked) noexcept
Returns merged a_if_masked bits selected by mask 1 bits and b_if_unmasked bits selected by mask 0 bit...
constexpr T ct_max(const T x, const T y) noexcept
Returns the maximum of two integrals for MIN <= x - y <= MAX (w/o branching) in O(1) and constant tim...
constexpr T ct_min(const T x, const T y) noexcept
Returns the minimum of two integrals for MIN <= x - y <= MAX (w/o branching) in O(1) and constant tim...
constexpr uint32_t ct_next_power_of_2(uint32_t n) noexcept
Returns the next higher power of 2 of given unsigned 32-bit n (w/o branching) in O(1) and constant ti...
constexpr T ct_is_zero(T x)
Returns ~0 (2-complement) if arg is zero, otherwise 0 (w/o branching) in O(1) and constant time (CT).
constexpr int ct_sign(const T x) noexcept
Returns the value of the sign function (w/o branching) in O(1) and constant time (CT)
constexpr T ct_clamp(const T x, const T min_val, const T max_val) noexcept
Returns constrained integral value to lie between given min- and maximum value for MIN <= x - y <= MA...
constexpr T ct_abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/o branching) in O(1) and constant time (CT),...
consteval_cxx20 bool is_cxx20() noexcept
Returns true if compiled with >= C++20.
#define PRAGMA_DISABLE_WARNING_PUSH
#define PRAGMA_DISABLE_WARNING_INT_OVERFLOW
#define PRAGMA_DISABLE_WARNING_POP
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.