25#ifndef JAU_INT_MATH_CT_HPP_
26#define JAU_INT_MATH_CT_HPP_
66 std::enable_if_t< std::is_integral_v<T>,
bool> =
true>
69 return (x != 0) | -(int)((std::make_unsigned_t<T>)((T)x) >> (
sizeof(T) * CHAR_BIT - 1));
92 std::enable_if_t< std::is_arithmetic_v<T> &&
93 std::is_integral_v<T> &&
94 !std::is_unsigned_v<T>,
bool> =
true>
95 constexpr T
ct_abs(
const T x)
noexcept
97 using unsigned_T = std::make_unsigned_t<T>;
98 const T mask = x >> (
sizeof(T) * CHAR_BIT - 1 );
100 PRAGMA_DISABLE_WARNING_INT_OVERFLOW
101 return static_cast<unsigned_T
>( ( x + mask ) ^ mask );
104 template <
typename T,
105 std::enable_if_t< std::is_arithmetic_v<T> &&
106 !std::is_integral_v<T> &&
107 !std::is_unsigned_v<T>,
bool> =
true>
108 constexpr T
ct_abs(
const T x)
noexcept
110 return x * jau::ct_sign<T>(x);
112 template <
typename T,
113 std::enable_if_t< std::is_arithmetic_v<T> &&
114 std::is_unsigned_v<T>,
bool> =
true>
115 constexpr T
ct_abs(
const T x)
noexcept
131 template <
typename T,
132 std::enable_if_t< std::is_integral_v<T>,
bool> =
true>
133 constexpr T
ct_min(
const T x,
const T y)
noexcept
135 return y + ( (x - y) & ( (x - y) >> (
sizeof(T) * CHAR_BIT - 1 ) ) );
149 template <
typename T,
150 std::enable_if_t< std::is_integral_v<T>,
bool> =
true>
151 constexpr T
ct_max(
const T x,
const T y)
noexcept
153 return x - ( (x - y) & ( (x - y) >> (
sizeof(T) * CHAR_BIT - 1 ) ) );
169 template <
typename T,
170 std::enable_if_t< std::is_integral_v<T>,
bool> =
true>
171 constexpr T
ct_clamp(
const T x,
const T min_val,
const T max_val)
noexcept
173 return jau::ct_min<T>(jau::ct_max<T>(x, min_val), max_val);
187 template <
typename T,
188 std::enable_if_t< std::is_integral_v<T> && std::is_unsigned_v<T>,
bool> =
true>
190 return b_if_unmasked ^ ( mask & ( a_if_masked ^ b_if_unmasked ) );
236 n = n - ((n >> 1) & 0x55555555);
237 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
238 n = (n + (n >> 4)) & 0x0f0f0f0f;
250 template <
typename T,
251 std::enable_if_t< std::is_integral_v<T> && std::is_unsigned_v<T>,
bool> =
true>
254 return T(0) - ( x >> (
sizeof(T) * CHAR_BIT - 1 ) );
263 template <
typename T,
264 std::enable_if_t< std::is_integral_v<T> && std::is_unsigned_v<T>,
bool> =
true>
267 return jau::ct_expand_top_bit<T>( ~x & (x - 1) );
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_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 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 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_abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/o branching) in O(1) and constant time (CT),...
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 uint32_t ct_next_power_of_2(uint32_t n)
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 T ct_masked_merge(T mask, T a_if_masked, T b_if_unmasked)
Returns merged a_if_masked bits selected by mask 1 bits and b_if_unmasked bits selected by mask 0 bit...
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...
#define PRAGMA_DISABLE_WARNING_PUSH
#define PRAGMA_DISABLE_WARNING_POP
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.