jaulib v1.3.0
Jau Support Library (C++, Java, ..)
|
Integral integer types and arithmetic. More...
Namespaces | |
namespace | jau::int_literals |
Classes | |
class | jau::mp::BigInt |
Arbitrary precision integer type. More... | |
struct | jau::float_bytes< bytesize > |
struct | jau::float_bytes< sizeof(double)> |
struct | jau::float_bytes< sizeof(float)> |
struct | jau::float_bytes< sizeof(long double)> |
struct | jau::sint_bytes< bytesize > |
struct | jau::sint_bytes< 4 > |
struct | jau::sint_bytes< 8 > |
struct | jau::uint128dp_t |
A 128-bit packed uint8_t data array. More... | |
struct | jau::uint192dp_t |
A 196-bit packed uint8_t data array. More... | |
struct | jau::uint256dp_t |
A 256-bit packed uint8_t data array. More... | |
struct | jau::uint_bytes< bytesize > |
struct | jau::uint_bytes< 4 > |
struct | jau::uint_bytes< 8 > |
Typedefs | |
typedef uint_fast32_t | jau::nsize_t |
Natural 'size_t' alternative using uint_fast32_t as its natural sized type. More... | |
typedef int_fast32_t | jau::snsize_t |
Natural 'ssize_t' alternative using int_fast32_t as its natural sized type. More... | |
Functions | |
template<typename T , std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true> | |
constexpr T | jau::abs (const T x) noexcept |
Returns the absolute value of an arithmetic number (w/ branching) in O(1) More... | |
mp::BigInt | jau::abs (mp::BigInt x) noexcept |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr bool | jau::add_overflow (const T a, const T b, T &res) noexcept |
Integer overflow aware addition returning true if overflow occurred, otherwise false having the result stored in res. More... | |
const mp::BigInt & | jau::clamp (const mp::BigInt &x, const mp::BigInt &min_val, const mp::BigInt &max_val) noexcept |
template<typename T , std::enable_if_t< std::is_arithmetic_v< T >, bool > = true> | |
constexpr T | jau::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 (w/ branching) in O(1). More... | |
mp::BigInt & | jau::clamp (mp::BigInt &x, mp::BigInt &min_val, mp::BigInt &max_val) noexcept |
void | jau::clear_bit_uint32 (const uint8_t nr, uint32_t &mask) |
void | jau::clear_bit_uint64 (const uint8_t nr, uint64_t &mask) |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr nsize_t | jau::digits10 (const T x, const bool sign_is_digit=true) noexcept |
Returns the number of decimal digits of the given integral value number using std::log10<T>(). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr nsize_t | jau::digits10 (const T x, const snsize_t x_sign, const bool sign_is_digit=true) noexcept |
Returns the number of decimal digits of the given integral value number using std::log10<T>(). More... | |
template<class T > | |
std::enable_if< std::is_integral_v< T >, bool >::type constexpr | jau::equals (const T &a, const T &b) noexcept |
Returns true if both values are equal. More... | |
template<class T > | |
std::enable_if< std::is_integral_v< T >, bool >::type constexpr | jau::equals (const T &a, const T &b, const T &allowed_deviation) noexcept |
Returns true if both values are equal, i.e. More... | |
mp::BigInt | jau::gcd (const mp::BigInt &a, const mp::BigInt &b) noexcept |
template<typename T , std::enable_if_t< std::is_integral_v< T > &&!std::is_unsigned_v< T >, bool > = true> | |
constexpr T | jau::gcd (T a, T b) noexcept |
Returns the greatest common divisor (GCD) of the two given integer values following Euclid's algorithm from Euclid's Elements ~300 BC, using the absolute positive value of given integers. More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true> | |
constexpr nsize_t | jau::high_bit (T x) |
Return the index of the highest set bit w/ branching (loop) in O(n), actually O(n/2). More... | |
template<class T , std::enable_if_t< std::is_arithmetic_v< T >, bool > = true> | |
bool | jau::in_range (const T &a, const T &b, const T &range) |
base_math: arithmetic types, i.e. More... | |
template<typename T , std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true> | |
constexpr T | jau::invert_sign (const T x) noexcept |
Safely inverts the sign of an arithmetic number w/ branching in O(1) More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true> | |
constexpr bool | jau::is_power_of_2 (const T x) noexcept |
Power of 2 test (w/o branching ?) in O(1) More... | |
template<class T > | |
std::enable_if< std::is_integral_v< T >, bool >::type constexpr | jau::is_zero (const T &a) noexcept |
base_math: arithmetic types, i.e. More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr bool | jau::lcm_overflow (const T a, const T b, T &result) noexcept |
Integer overflow aware calculation of least common multiple (LCM) following Euclid's algorithm from Euclid's Elements ~300 BC. More... | |
const mp::BigInt & | jau::max (const mp::BigInt &x, const mp::BigInt &y) noexcept |
template<typename T , std::enable_if_t< std::is_arithmetic_v< T >, bool > = true> | |
constexpr T | jau::max (const T x, const T y) noexcept |
Returns the maximum of two integrals (w/ branching) in O(1) More... | |
mp::BigInt & | jau::max (mp::BigInt &x, mp::BigInt &y) noexcept |
uint128dp_t | jau::merge_uint128 (uint16_t const uuid16, uint128dp_t const &base_uuid, nsize_t const uuid16_le_octet_index) |
Merge the given 'uuid16' into a 'base_uuid' copy at the given little endian 'uuid16_le_octet_index' position. More... | |
uint128dp_t | jau::merge_uint128 (uint32_t const uuid32, uint128dp_t const &base_uuid, nsize_t const uuid32_le_octet_index) |
Merge the given 'uuid32' into a 'base_uuid' copy at the given little endian 'uuid32_le_octet_index' position. More... | |
const mp::BigInt & | jau::min (const mp::BigInt &x, const mp::BigInt &y) noexcept |
template<typename T , std::enable_if_t< std::is_arithmetic_v< T >, bool > = true> | |
constexpr T | jau::min (const T x, const T y) noexcept |
Returns the minimum of two integrals (w/ branching) in O(1) More... | |
mp::BigInt & | jau::min (mp::BigInt &x, mp::BigInt &y) noexcept |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr bool | jau::mul_overflow (const T a, const T b, T &res) noexcept |
Integer overflow aware multiplication returning true if overflow occurred, otherwise false having the result stored in res. More... | |
mp::BigInt | jau::pow (mp::BigInt b, mp::BigInt e) |
template<typename T , typename U , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T > &&std::is_integral_v< U > &&std::is_unsigned_v< U >, bool > = true> | |
constexpr T | jau::round_down (T n, U align_to) |
Round down w/ branching in O(1) More... | |
constexpr uint32_t | jau::round_to_power_of_2 (const uint32_t n) |
If the given n is not is_power_of_2() return next_power_of_2(), otherwise return n unchanged. More... | |
template<typename T , typename U , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T > &&std::is_integral_v< U > &&std::is_unsigned_v< U >, bool > = true> | |
constexpr T | jau::round_up (const T n, const U align_to) |
Round up w/ branching in O(1) More... | |
void | jau::set_bit_uint32 (const uint8_t nr, uint32_t &mask) |
void | jau::set_bit_uint64 (const uint8_t nr, uint64_t &mask) |
template<typename T , std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_unsigned_v< T >, bool > = true> | |
constexpr int | jau::sign (const T x) noexcept |
Returns the value of the sign function (w/o branching ?) in O(1). More... | |
template<typename T , std::enable_if_t< std::is_integral_v< T >, bool > = true> | |
constexpr bool | jau::sub_overflow (const T a, const T b, T &res) noexcept |
Integer overflow aware subtraction returning true if overflow occurred, otherwise false having the result stored in res. More... | |
uint32_t | jau::test_bit_uint32 (const uint8_t nr, const uint32_t mask) |
uint64_t | jau::test_bit_uint64 (const uint8_t nr, const uint64_t mask) |
Integral integer types and arithmetic.
Further support is coming from Byte Utilities and meta-group Math Support
typedef uint_fast32_t jau::nsize_t |
Natural 'size_t' alternative using uint_fast32_t
as its natural sized type.
The leading 'n' stands for natural.
This is a compromise to indicate intend, but to avoid handling a multiple sized size_t
footprint where not desired.
Definition at line 53 of file int_types.hpp.
typedef int_fast32_t jau::snsize_t |
Natural 'ssize_t' alternative using int_fast32_t
as its natural sized type.
The leading 'n' stands for natural.
This is a compromise to indicate intend, but to avoid handling a multiple sized ssize_t
footprint where not desired.
Definition at line 65 of file int_types.hpp.
bool jau::in_range | ( | const T & | a, |
const T & | b, | ||
const T & | range | ||
) |
base_math: arithmetic types, i.e.
integral + floating point types int_math: integral types float_math: floating point types Returns true, if both integer point values differ less than the given range.
T | an arithmetic type |
a | value to compare |
b | value to compare |
range | the maximum difference both values may differ |
Definition at line 62 of file base_math.hpp.
|
constexprnoexcept |
Returns the value of the sign function (w/o branching ?) in O(1).
-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 arithmetic number type |
x | the arithmetic number |
Definition at line 84 of file base_math.hpp.
|
constexprnoexcept |
Safely inverts the sign of an arithmetic number w/ branching in O(1)
Implementation takes special care to have T_MIN, i.e. std::numeric_limits<T>::min(), converted to T_MAX, i.e. std::numeric_limits<T>::max().
This is necessary since T_MAX < | -T_MIN |
and the result would not fit in the return type T otherwise.
Hence for the extreme minimum case:
jau::invert_sign<int32_t>(INT32_MIN) = | INT32_MIN | - 1 = INT32_MAX
Otherwise with x < 0:
jau::invert_sign<int32_t>(x) = | x | = -x
and x >= 0:
jau::invert_sign<int32_t>(x) = -x
T | an unsigned arithmetic number type |
x | the number |
Definition at line 126 of file base_math.hpp.
|
constexprnoexcept |
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
This implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
T | an arithmetic number type |
x | the number |
Definition at line 155 of file base_math.hpp.
|
constexprnoexcept |
Returns the minimum of two integrals (w/ branching) in O(1)
T | an arithmetic number type |
x | one number |
x | the other number |
Definition at line 177 of file base_math.hpp.
|
constexprnoexcept |
Returns the maximum of two integrals (w/ branching) in O(1)
T | an arithmetic number type |
x | one number |
x | the other number |
Definition at line 191 of file base_math.hpp.
|
constexprnoexcept |
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1).
Implementation returns min(max(x, min_val), max_val)
, analog to GLSL's clamp()
T | an arithmetic number type |
x | one number |
min_val | the minimum limes, inclusive |
max_val | the maximum limes, inclusive |
Definition at line 208 of file base_math.hpp.
|
inline |
Definition at line 436 of file basic_types.hpp.
|
inline |
Definition at line 443 of file basic_types.hpp.
|
inline |
Definition at line 450 of file basic_types.hpp.
|
inline |
Definition at line 457 of file basic_types.hpp.
|
inline |
Definition at line 464 of file basic_types.hpp.
|
inline |
Definition at line 471 of file basic_types.hpp.
uint128dp_t jau::merge_uint128 | ( | uint16_t const | uuid16, |
uint128dp_t const & | base_uuid, | ||
nsize_t const | uuid16_le_octet_index | ||
) |
Merge the given 'uuid16' into a 'base_uuid' copy at the given little endian 'uuid16_le_octet_index' position.
The given 'uuid16' value will be added with the 'base_uuid' copy at the given position.
base_uuid: 00000000-0000-1000-8000-00805F9B34FB uuid16: DCBA uuid16_le_octet_index: 12 result: 0000DCBA-0000-1000-8000-00805F9B34FB LE: low-mem - FB349B5F8000-0080-0010-0000-ABCD0000 - high-mem ^ index 12 LE: uuid16 -> value.data[12+13] BE: low-mem - 0000DCBA-0000-1000-8000-00805F9B34FB - high-mem ^ index 2 BE: uuid16 -> value.data[2+3]
Definition at line 297 of file basic_types.cpp.
uint128dp_t jau::merge_uint128 | ( | uint32_t const | uuid32, |
uint128dp_t const & | base_uuid, | ||
nsize_t const | uuid32_le_octet_index | ||
) |
Merge the given 'uuid32' into a 'base_uuid' copy at the given little endian 'uuid32_le_octet_index' position.
The given 'uuid32' value will be added with the 'base_uuid' copy at the given position.
base_uuid: 00000000-0000-1000-8000-00805F9B34FB uuid32: 87654321 uuid32_le_octet_index: 12 result: 87654321-0000-1000-8000-00805F9B34FB LE: low-mem - FB349B5F8000-0080-0010-0000-12345678 - high-mem ^ index 12 LE: uuid32 -> value.data[12..15] BE: low-mem - 87654321-0000-1000-8000-00805F9B34FB - high-mem ^ index 0 BE: uuid32 -> value.data[0..3]
Definition at line 332 of file basic_types.cpp.
|
constexprnoexcept |
base_math: arithmetic types, i.e.
integral + floating point types int_math: integral types float_math: floating point types Returns true of the given integer value is zero.
Definition at line 58 of file int_math.hpp.
|
constexprnoexcept |
Returns true if both values are equal.
T | an integral type |
a | value to compare |
b | value to compare |
Definition at line 71 of file int_math.hpp.
|
constexprnoexcept |
Returns true if both values are equal, i.e.
their absolute delta <= allowed_deviation
.
T | an integral type |
a | value to compare |
b | value to compare |
allowed_deviation | allowed deviation |
Definition at line 85 of file int_math.hpp.
|
constexpr |
Round up w/ branching in O(1)
T | an unsigned integral number type |
U | an unsigned integral number type |
n | to be aligned number |
align_to | alignment boundary, must not be 0 |
Definition at line 101 of file int_math.hpp.
|
constexpr |
Round down w/ branching in O(1)
T | an unsigned integral number type |
U | an unsigned integral number type |
n | to be aligned number |
align_to | alignment boundary |
Definition at line 123 of file int_math.hpp.
|
constexprnoexcept |
Power of 2 test (w/o branching ?) in O(1)
Source: bithacks Test PowerOf2
Branching may occur due to relational operator.
T | an unsigned integral number type |
x | the unsigned integral number |
Definition at line 140 of file int_math.hpp.
|
constexpr |
If the given n
is not is_power_of_2() return next_power_of_2(), otherwise return n
unchanged.
return is_power_of_2(n) ? n : next_power_of_2(n);
Definition at line 152 of file int_math.hpp.
|
inlineconstexpr |
Return the index of the highest set bit w/ branching (loop) in O(n), actually O(n/2).
T | an unsigned integral number type |
x | value |
Definition at line 164 of file int_math.hpp.
|
constexprnoexcept |
Integer overflow aware addition returning true if overflow occurred, otherwise false having the result stored in res.
Implementation uses Integer Overflow Builtins if available, otherwise its own implementation.
T | an integral integer type |
param a operand a
b | operand b |
res | storage for result |
Definition at line 191 of file int_math.hpp.
|
constexprnoexcept |
Integer overflow aware subtraction returning true if overflow occurred, otherwise false having the result stored in res.
Implementation uses Integer Overflow Builtins if available, otherwise its own implementation.
T | an integral integer type |
param a operand a
b | operand b |
res | storage for result |
Definition at line 225 of file int_math.hpp.
|
constexprnoexcept |
Integer overflow aware multiplication returning true if overflow occurred, otherwise false having the result stored in res.
Implementation uses Integer Overflow Builtins if available, otherwise its own implementation.
T | an integral integer type |
param a operand a
b | operand b |
res | storage for result |
Definition at line 259 of file int_math.hpp.
|
constexprnoexcept |
Returns the greatest common divisor (GCD) of the two given integer values following Euclid's algorithm from Euclid's Elements ~300 BC, using the absolute positive value of given integers.
Returns zero if a and b is zero.
Note implementation uses modulo operator (a/b)*b + a % b = a
, i.e. remainder of the integer division - hence implementation uses abs(a) % abs(b)
in case the integral T is a signed type (dropped for unsigned).
Implementation is similar to std::gcd(), however, it uses a fixed common type T and a while loop instead of compile time evaluation via recursion.
T | integral type |
param a integral value a
b | integral value b |
Definition at line 298 of file int_math.hpp.
|
constexprnoexcept |
Integer overflow aware calculation of least common multiple (LCM) following Euclid's algorithm from Euclid's Elements ~300 BC.
T | integral type |
param result storage for lcm result: zero if a and b are zero, otherwise lcm of a and b
a | integral value a |
b | integral value b |
Definition at line 334 of file int_math.hpp.
|
constexprnoexcept |
Returns the number of decimal digits of the given integral value number using std::log10<T>().
If sign_is_digit == true (default), treats a potential negative sign as a digit.
x < 0: 1 + (int) ( log10( -x ) ) + ( sign_is_digit ? 1 : 0 ) x = 0: 1 x > 0: 1 + (int) ( log10( x ) )
Implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
Convenience method, reusing precomputed sign of value to avoid redundant computations.
T | an integral integer type |
x | the integral integer |
x_sign | the pre-determined sign of the given value x |
sign_is_digit | if true and value is negative, adds one to result for sign. Defaults to true. |
Definition at line 371 of file int_math.hpp.
|
constexprnoexcept |
Returns the number of decimal digits of the given integral value number using std::log10<T>().
If sign_is_digit == true (default), treats a potential negative sign as a digit.
x < 0: 1 + (int) ( log10( -x ) ) + ( sign_is_digit ? 1 : 0 ) x = 0: 1 x > 0: 1 + (int) ( log10( x ) )
Implementation uses jau::invert_sign() to have a safe absolute value conversion, if required.
T | an integral integer type |
x | the integral integer |
sign_is_digit | if true and value is negative, adds one to result for sign. Defaults to true. |
Definition at line 399 of file int_math.hpp.
|
inlinenoexcept |
Definition at line 1559 of file big_int.hpp.
|
inline |
|
inlinenoexcept |
Definition at line 1562 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 1565 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 1568 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 1572 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 1575 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 1578 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 1582 of file big_int.hpp.