Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
Functions
Constant Time (CT) Integral Operations

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...
 

Detailed Description

Integral integer operations in constant time (CT), see also and meta-group Math Support.

Function Documentation

◆ ct_sign()

template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true>
constexpr int jau::ct_sign ( const T  x)
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.

Template Parameters
Tan integral number type
Parameters
xthe number
Returns
function result

Definition at line 67 of file int_math_ct.hpp.

◆ ct_abs()

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)
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

  • signed integral uses 2-complement branch-less conversion, bithacks Integer-Abs
  • signed floating-point uses x * sign(x)
  • unsigned just returns the value

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.

Template Parameters
Tan arithmetic number type
Parameters
xthe number
Returns
function result

Definition at line 95 of file int_math_ct.hpp.

Here is the caller graph for this function:

◆ ct_min()

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 
)
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.

Template Parameters
Tan integral number type
Parameters
xone number
xthe other number

Definition at line 133 of file int_math_ct.hpp.

Here is the caller graph for this function:

◆ ct_max()

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 
)
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.

Template Parameters
Tan integral number type
Parameters
xone number
xthe other number

Definition at line 151 of file int_math_ct.hpp.

Here is the caller graph for this function:

◆ ct_clamp()

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 
)
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.

Template Parameters
Tan integral number type
Parameters
xone number
min_valthe minimum limes, inclusive
max_valthe maximum limes, inclusive

Definition at line 171 of file int_math_ct.hpp.

Here is the caller graph for this function:

◆ ct_masked_merge()

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 ( mask,
a_if_masked,
b_if_unmasked 
)
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

Template Parameters
Tan unsigned integral number type
Parameters
mask1 where bits from a_if_masked should be selected; 0 where from b_if_unmasked.
a_if_maskedvalue to merge in masked bits
b_if_unmaskedvalue to merge in non-masked bits

Definition at line 189 of file int_math_ct.hpp.

Here is the caller graph for this function:

◆ ct_next_power_of_2()

constexpr uint32_t jau::ct_next_power_of_2 ( uint32_t  n)
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.

Here is the caller graph for this function:

◆ ct_bit_count()

constexpr uint32_t jau::ct_bit_count ( uint32_t  n)
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.

Here is the caller graph for this function:

◆ ct_expand_top_bit()

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 ( x)
inlineconstexpr

Returns ~0 (2-complement) if top bit of arg is set, otherwise 0 (w/o branching) in O(1) and constant time (CT).

Template Parameters
Tan unsigned integral number type

Definition at line 252 of file int_math_ct.hpp.

Here is the caller graph for this function:

◆ ct_is_zero()

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 ( x)
inlineconstexpr

Returns ~0 (2-complement) if arg is zero, otherwise 0 (w/o branching) in O(1) and constant time (CT).

Template Parameters
Tan unsigned integral number type

Definition at line 265 of file int_math_ct.hpp.

Here is the caller graph for this function: