TSDuck v3.38-3699
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
tsIntegerUtils.h File Reference

Some utilities on integers. More...

Classes

struct  ts::make_signed< T >
 The meta-type ts::make_signed is a generalization of std::make_signed which works on floating point-types as well. More...
 
struct  ts::smaller_unsigned< BITS >
 Define the smaller unsigned integer type with at least a given number of bits. More...
 
struct  ts::static_power10< INT, POW >
 Static values of power of 10. More...
 

Namespaces

namespace  ts
 TSDuck namespace, containing all TSDuck classes and functions.
 

Macros

#define TS_TEMPLATE_IF_INTS(sign1, op, sign2)
 A convenience macro to declare a template with complex std::enable_if conditions on two integer types.
 

Functions

template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value, int >::type = 0>
INT ts::abs (INT a)
 Absolute value of integer types, also working on unsigned types.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
bool ts::add_overflow (INT a, INT b) platform_specific
 Check if an integer addition generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value, int >::type = 0>
bool ts::add_overflow (INT a, INT b, INT res)
 Check if an integer addition generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
size_t ts::BitSize (INT x)
 Get the size in bits of an integer value.
 
template<typename INT1 , typename INT2 , typename std::enable_if< std::is_integral< INT1 >::value &&std::is_integral< INT2 >::value, int >::type = 0>
bool ts::bound_check (INT2 x)
 Integer cross-type bound check.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
INT ts::bounded_add (INT a, INT b)
 Perform a bounded addition without overflow.
 
template<typename INT1 , typename INT2 , typename std::enable_if< std::is_integral< INT1 >::value &&std::is_integral< INT2 >::value, int >::type = 0>
INT1 ts::bounded_cast (INT2 x)
 Bounded integer cast.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
INT ts::bounded_sub (INT a, INT b)
 Perform a bounded subtraction without overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::debug_thow_neg_overflow (INT a)
 In debug mode, throw an exception if the negation (opposite sign) of an integer generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::debug_throw_add_overflow (INT a, INT b, INT res)
 In debug mode, throw an exception if an integer addition generates an overflow.
 
template<typename INT1 , typename INT2 , typename std::enable_if< std::is_integral< INT1 >::value &&std::is_integral< INT2 >::value, int >::type = 0>
void ts::debug_throw_bound_check (INT2 x)
 In debug mode, throw an exception if an integer value does not fall into the range of another integer type.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::debug_throw_div_zero (INT den)
 In debug mode, throw an exception if the denominator of an integer division is zero.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::debug_throw_mul_overflow (INT a, INT b, INT res)
 In debug mode, throw an exception if an integer multiplication generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::debug_throw_sub_overflow (INT a, INT b, INT res)
 In debug mode, throw an exception if an integer substraction generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
INT ts::GCD (INT x, INT y)
 Compute a greatest common denominator (GCD).
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
bool ts::is_negative (INT a)
 Check if an integer value is negative, optimized for signed or unsigned type.
 
size_t ts::MaxDecimalWidth (size_t typeSize, size_t digitSeparatorSize=0)
 Compute the maximum width of the decimal representation of an integer type.
 
size_t ts::MaxHexaWidth (size_t typeSize, size_t digitSeparatorSize=0)
 Compute the maximum width of the hexadecimal representation of an integer type.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
bool ts::mul_overflow (INT a, INT b) platform_specific
 Check if an integer multiplication generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
bool ts::mul_overflow (INT a, INT b, INT res)
 Check if an integer multiplication generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value, int >::type = 0>
bool ts::neg_overflow (INT a)
 Check if the negation (opposite sign) of an integer generates an overflow.
 
uint64_t ts::Power10 (size_t pow)
 Get a power of 10 using a fast lookup table.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
INT ts::round_down (INT x, INT f)
 Round x down to previous multiple of a factor f.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
INT ts::round_up (INT x, INT f)
 Round x up to next multiple of a factor f.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value >::type * = nullptr>
INT ts::rounded_div (INT a, INT b)
 Integer division with rounding to closest value (instead of truncating).
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value, int >::type = 0>
void ts::sign_reduce (INT &num, INT &den)
 Reduce the sign of an integer fraction.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value, int >::type = 0>
const UCharts::SignedDescription ()
 Get the signed/unsigned qualifier of an integer type as a string.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_signed< INT >::value >::type * = nullptr>
INT ts::SignExtend (INT x, size_t bits)
 Perform a sign extension on any subset of a signed integer.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
bool ts::sub_overflow (INT a, INT b) platform_specific
 Check if an integer substraction generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value &&std::is_unsigned< INT >::value, int >::type = 0>
bool ts::sub_overflow (INT a, INT b, INT res)
 Check if an integer substraction generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::thow_neg_overflow (INT a)
 Throw an exception if the negation (opposite sign) of an integer generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::throw_add_overflow (INT a, INT b, INT res)
 Throw an exception if an integer addition generates an overflow.
 
template<typename INT1 , typename INT2 , typename std::enable_if< std::is_integral< INT1 >::value &&std::is_integral< INT2 >::value, int >::type = 0>
void ts::throw_bound_check (INT2 x)
 Throw an exception if an integer value does not fall into the range of another integer type.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::throw_div_zero (INT den)
 Throw an exception if the denominator of an integer division is zero.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::throw_mul_overflow (INT a, INT b, INT res)
 Throw an exception if an integer multiplication generates an overflow.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value, int >::type = 0>
void ts::throw_sub_overflow (INT a, INT b, INT res)
 Throw an exception if an integer substraction generates an overflow.
 

Variables

constexpr size_t ts::MAX_POWER_10 = 19
 Largest representable power of 10 in integer types.
 

Detailed Description

Some utilities on integers.

Macro Definition Documentation

◆ TS_TEMPLATE_IF_INTS

#define TS_TEMPLATE_IF_INTS (   sign1,
  op,
  sign2 
)
Value:
template <typename INT1, \
typename INT2, \
typename std::enable_if<std::is_integral<INT1>::value && \
std::is_integral<INT2>::value && \
std::is_##sign1<INT1>::value && \
std::is_##sign2<INT2>::value && \
(sizeof(INT1) op sizeof(INT2)), \
int>::type = 0>

A convenience macro to declare a template with complex std::enable_if conditions on two integer types.

This macro declares two template integer types INT1 and INT2. It expands to:

template <typename INT1, typename INT2, typename std::enable_if<...>::type = 0>
Parameters
sign1Either signed or unsigned, condition on first integer type INT1.
opA comparison operator ('==', '<', '>=', etc) to apply on the sizes of INT1 and INT2.
sign2Either signed or unsigned, condition on second integer type INT2.