jaulib v1.3.0
Jau Support Library (C++, Java, ..)
|
Integral integer operations in constant time (CT), see also and meta-group Math Support. More...
Functions | |
template<typename T , std::enable_if_t< std::is_arithmetic_v< T > &&std::is_integral_v< T > &&!std::is_unsigned_v< T >, bool > = true> | |
constexpr T | jau::ct_abs (const T x) noexcept |
Returns the absolute value of an arithmetic number (w/o branching) in O(1) and constant time (CT), while ct_abs(INT_MIN) is undefined behavior (UB) instead of being mapped correctly to INT_MAX like jau::abs() does, see above. More... | |
constexpr uint32_t | jau::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 (CT). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr T | jau::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 <= MAX (w/o branching) in O(1) and constant time (CT). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true> | |
constexpr T | jau::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 time (CT). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true> | |
constexpr T | jau::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). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true> | |
constexpr T | jau::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 bits (w/o branching) in O(1) and constant time (CT). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr T | jau::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 time (CT). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr T | jau::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 time (CT). More... | |
constexpr uint32_t | jau::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 time (CT). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr int | jau::ct_sign (const T x) noexcept |
Returns the value of the sign function (w/o branching) in O(1) and constant time (CT) More... | |
Integral integer operations in constant time (CT), see also and meta-group Math Support.
|
constexprnoexcept |
Returns the value of the sign function (w/o branching) in O(1) and constant time (CT)
-1 for x < 0 0 for x = 0 1 for x > 0
Implementation is type safe.
Branching may occur due to relational operator.
T | an integral number type |
x | the number |
Definition at line 67 of file int_math_ct.hpp.
|
constexprnoexcept |
Returns the absolute value of an arithmetic number (w/o branching) in O(1) and constant time (CT), while ct_abs(INT_MIN) is undefined behavior (UB) instead of being mapped correctly to INT_MAX like jau::abs() does, see above.
This implementation is equivalent to std::abs(), i.e. unsafe
This implementation uses 2-complement branch-less conversion, bithacks Integer-Abs
Note: On an x86_64 architecture abs() w/ branching is of equal speed or even faster.
T | an arithmetic number type |
x | the number |
Definition at line 95 of file int_math_ct.hpp.
|
constexprnoexcept |
Returns the minimum of two integrals for MIN <= x - y <= MAX
(w/o branching) in O(1) and constant time (CT).
Source: bithacks Test IntegerMinOrMax
Note: On an x86_64 architecture min() w/ branching is of equal speed or even faster.
T | an integral number type |
x | one number |
x | the other number |
Definition at line 133 of file int_math_ct.hpp.
|
constexprnoexcept |
Returns the maximum of two integrals for MIN <= x - y <= MAX
(w/o branching) in O(1) and constant time (CT).
Source: bithacks Test IntegerMinOrMax
Note: On an x86_64 architecture max() w/ branching is of equal speed or even faster.
T | an integral number type |
x | one number |
x | the other number |
Definition at line 151 of file int_math_ct.hpp.
|
constexprnoexcept |
Returns constrained integral value to lie between given min- and maximum value for MIN <= x - y <= MAX
(w/o branching) in O(1) and constant time (CT).
Implementation returns ct_min(ct_max(x, min_val), max_val)
, analog to GLSL's clamp()
Note: On an x86_64 architecture clamp() w/ branching is of equal speed or even faster.
T | an integral number type |
x | one number |
min_val | the minimum limes, inclusive |
max_val | the maximum limes, inclusive |
Definition at line 171 of file int_math_ct.hpp.
|
constexpr |
Returns merged a_if_masked
bits selected by mask
1
bits and b_if_unmasked
bits selected by mask
0
bits (w/o branching) in O(1) and constant time (CT).
Source: bithacks MaskedMerge
T | an unsigned integral number type |
mask | 1 where bits from a_if_masked should be selected; 0 where from b_if_unmasked . |
a_if_masked | value to merge in masked bits |
b_if_unmasked | value to merge in non-masked bits |
Definition at line 189 of file int_math_ct.hpp.
|
constexpr |
Returns the next higher power of 2 of given unsigned 32-bit n
(w/o branching) in O(1) and constant time (CT).
Source: bithacks RoundUpPowerOf2
Definition at line 200 of file int_math_ct.hpp.
|
constexprnoexcept |
Returns the number of set bits within given 32bit integer (w/o branching) in O(1) and constant time (CT).
Uses a HAKEM 169 Bit Count inspired implementation:
http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html http://home.pipeline.com/~hbaker1/hakmem/hacks.html#item169 http://tekpool.wordpress.com/category/bit-count/ https://github.com/aistrate/HackersDelight/blob/master/Original/HDcode/pop.c.txt https://github.com/aistrate/HackersDelight/blob/master/Original/HDcode/newCode/popDiff.c.txt
Definition at line 223 of file int_math_ct.hpp.
|
inlineconstexpr |
Returns ~0 (2-complement) if top bit of arg is set, otherwise 0 (w/o branching) in O(1) and constant time (CT).
T | an unsigned integral number type |
Definition at line 252 of file int_math_ct.hpp.
|
inlineconstexpr |
Returns ~0 (2-complement) if arg is zero, otherwise 0 (w/o branching) in O(1) and constant time (CT).
T | an unsigned integral number type |
Definition at line 265 of file int_math_ct.hpp.