Mstdlib-1.24.0
Math

Macros

#define M_MIN(a, b)   ((a)<(b)?(a):(b))
 
#define M_MAX(a, b)   ((a)>(b)?(a):(b))
 
#define M_ABS(a)   ((a)<0?((a)*-1):(a))
 
#define M_CLAMP(x, l, h)   M_MIN(h,M_MAX(l,x))
 

Functions

M_uint64 M_uint64_exp (M_uint64 num, int exponent)
 
M_uint64 M_uint64_prec_round (M_uint64 num, int currentDecimals, int wantedDecimals)
 
M_uint64 M_uint64_round_up_to_nearest_multiple (M_uint64 n, M_uint64 mul)
 
M_bool M_uint32_is_power_of_two (M_uint32 n)
 
M_bool M_uint64_is_power_of_two (M_uint64 n)
 
M_bool M_size_t_is_power_of_two (size_t n)
 
M_uint32 M_uint32_round_up_to_power_of_two (M_uint32 n)
 
M_uint64 M_uint64_round_up_to_power_of_two (M_uint64 n)
 
size_t M_size_t_round_up_to_power_of_two (size_t n)
 
M_uint32 M_uint32_round_down_to_power_of_two (M_uint32 n)
 
M_uint64 M_uint64_round_down_to_power_of_two (M_uint64 n)
 
size_t M_size_t_round_down_to_power_of_two (size_t n)
 
M_uint8 M_uint32_log2 (M_uint32 n)
 
M_uint8 M_uint64_log2 (M_uint64 n)
 
M_int64 M_sign_extend (M_uint64 x, size_t num_bits)
 
int M_uint64_count_digits (M_uint64 num)
 
M_uint8 M_uint8_popcount (M_uint8 x)
 
M_uint8 M_uint64_popcount (M_uint64 num)
 
double M_math_modf (double x, double *iptr)
 
double M_math_round (double x)
 

Detailed Description

Mathmatic calculations and conversions.

Macro Definition Documentation

◆ M_MIN

#define M_MIN (   a,
 
)    ((a)<(b)?(a):(b))

◆ M_MAX

#define M_MAX (   a,
 
)    ((a)>(b)?(a):(b))

◆ M_ABS

#define M_ABS (   a)    ((a)<0?((a)*-1):(a))

◆ M_CLAMP

#define M_CLAMP (   x,
  l,
 
)    M_MIN(h,M_MAX(l,x))

Function Documentation

◆ M_uint64_exp()

M_uint64 M_uint64_exp ( M_uint64  num,
int  exponent 
)

Calculate the exponent of an integer x^y (num^exp).

Parameters
[in]numBase number.
[in]exponentExponent.
Returns
num ^ exp

◆ M_uint64_prec_round()

M_uint64 M_uint64_prec_round ( M_uint64  num,
int  currentDecimals,
int  wantedDecimals 
)

Round an integer with implied decimals.

Parameters
[in]numNumber to round.
[in]currentDecimalsCurrent number of implied decimal places.
[in]wantedDecimalsDesired number of decimal places in output. Must be <= currentDecimals.
Returns
num rounded to wantedDecimals (output will have wantedDecimals implied decimals).

◆ M_uint64_round_up_to_nearest_multiple()

M_uint64 M_uint64_round_up_to_nearest_multiple ( M_uint64  n,
M_uint64  mul 
)

Round an integer value up to a given multiple.

Parameters
[in]ninteger value from which to determine the next multiple.
[in]mulinteger multiple to use.
Returns
the next value greater than or equal to n that is evenly divisible by mul.

◆ M_uint32_is_power_of_two()

M_bool M_uint32_is_power_of_two ( M_uint32  n)

Determine if 32bit integer is a power of two.

Parameters
[in]ninteger to check
Returns
M_TRUE if power of 2, M_FALSE otherwise

◆ M_uint64_is_power_of_two()

M_bool M_uint64_is_power_of_two ( M_uint64  n)

Determine if 64bit integer is a power of two.

Parameters
[in]ninteger to check
Returns
M_TRUE if power of 2, M_FALSE otherwise

◆ M_size_t_is_power_of_two()

M_bool M_size_t_is_power_of_two ( size_t  n)

Determine if size_t is a power of two.

Parameters
[in]ninteger to check
Returns
M_TRUE if power of 2, M_FALSE otherwise

◆ M_uint32_round_up_to_power_of_two()

M_uint32 M_uint32_round_up_to_power_of_two ( M_uint32  n)

Round a 32bit integer value up to the next power of two.

Parameters
[in]ninteger value from which to determine the next power of two.
Returns
the next value greater than or equal to n that is a power of two.

◆ M_uint64_round_up_to_power_of_two()

M_uint64 M_uint64_round_up_to_power_of_two ( M_uint64  n)

Round a 64bit integer value up to the next power of two.

Parameters
[in]ninteger value from which to determine the next power of two.
Returns
the next value greater than or equal to n that is a power of two.

◆ M_size_t_round_up_to_power_of_two()

size_t M_size_t_round_up_to_power_of_two ( size_t  n)

Round a size_t value up to the next power of two.

Parameters
[in]ninteger value from which to determine the next power of two.
Returns
the next value greater than or equal to n that is a power of two.

◆ M_uint32_round_down_to_power_of_two()

M_uint32 M_uint32_round_down_to_power_of_two ( M_uint32  n)

Round a 32bit integer value down to the last power of two.

Parameters
[in]ninteger value from which to determine the next power of two.
Returns
the next value greater than or equal to n that is a power of two.

◆ M_uint64_round_down_to_power_of_two()

M_uint64 M_uint64_round_down_to_power_of_two ( M_uint64  n)

Round a 64bit integer value down to the last power of two.

Parameters
[in]ninteger value from which to determine the next power of two.
Returns
the next value greater than or equal to n that is a power of two.

◆ M_size_t_round_down_to_power_of_two()

size_t M_size_t_round_down_to_power_of_two ( size_t  n)

Round a size_t value down to the last power of two.

Parameters
[in]ninteger value from which to determine the next power of two.
Returns
the next value greater than or equal to n that is a power of two.

◆ M_uint32_log2()

M_uint8 M_uint32_log2 ( M_uint32  n)

Get the log2 of a 32bit integer. n is rounded down if not power of 2, can use M_uint32_round_up_to_power_of_two() to round n up first.

Parameters
[in]ninteger value to get the log2 for
Returns
log2 of n

◆ M_uint64_log2()

M_uint8 M_uint64_log2 ( M_uint64  n)

Get the log2 of a 64bit integer. n is rounded down if not power of 2, can use M_uint64_round_up_to_power_of_two() to round n up first.

Parameters
[in]ninteger value to get the log2 for
Returns
log2 of n

◆ M_sign_extend()

M_int64 M_sign_extend ( M_uint64  x,
size_t  num_bits 
)

Increase the number of bits while keeping the sign.

Parameters
[in]xValue to sign extend
[in]num_bitsThe number of bits to extend from
Returns
sign extended value

◆ M_uint64_count_digits()

int M_uint64_count_digits ( M_uint64  num)

Count number of decimal digits in an integer.

Parameters
[in]numNumber to count digits.
Returns
number of digits in integer.

◆ M_uint8_popcount()

M_uint8 M_uint8_popcount ( M_uint8  x)

Count number of set bits in a single byte.

Parameters
[in]xvalue to count bits in
Returns
number of set (1) bits in x

◆ M_uint64_popcount()

M_uint8 M_uint64_popcount ( M_uint64  num)

Count number of set bits in a 64bit integer.

Parameters
[in]numvalue to count bits in
Returns
number of set (1) bits in num

◆ M_math_modf()

double M_math_modf ( double  x,
double *  iptr 
)

Floating point modulus.

Splits floating point number into integer and fractional parts.

Parameters
[in]xNumber to split.
[out]iptrInteger part of number.
Returns
Fractional part of number.

◆ M_math_round()

double M_math_round ( double  x)

Floating point rounding.

Parameters
[in]xNumber to round.
Returns
Number rounded.