jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
int_math.hpp File Reference
#include <cstdint>
#include <cmath>
#include <climits>
#include <jau/base_math.hpp>
#include <jau/int_math_ct.hpp>
Include dependency graph for int_math.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  jau
 __pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
 

Functions

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.
 
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>().
 
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>().
 
template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::equals (const T &a, const T &b) noexcept
 Returns true if both values are equal.
 
template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::equals (const T &a, const T &b, const T &allowed_deviation) noexcept
 Returns true if both values are equal, i.e.
 
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.
 
consteval_cxx20 bool jau::has_builtin_add_overflow () noexcept
 Query whether __builtin_add_overflow is available via __has_builtin(__builtin_add_overflow).
 
consteval_cxx20 bool jau::has_builtin_mul_overflow () noexcept
 Query whether __builtin_mul_overflow is available via __has_builtin(__builtin_mul_overflow).
 
consteval_cxx20 bool jau::has_builtin_sub_overflow () noexcept
 Query whether __builtin_sub_overflow is available via __has_builtin(__builtin_sub_overflow).
 
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).
 
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)
 
template<class T>
std::enable_if_t< std::is_integral_v< T >, bool > constexpr jau::is_zero (const T &a) noexcept
 base_math: arithmetic types, i.e.
 
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.
 
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.
 
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)
 
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.
 
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)
 
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.