modm API documentation
Utilities

Modules

 Arithmetic Traits
 

Functions

uint8_t modm::math::crc8_ccitt_update (uint8_t crc, uint8_t data)
 
uint16_t modm::math::crc16_ccitt_update (uint16_t crc, uint8_t data)
 
uint32_t modm::math::crc32_update (uint32_t crc, uint8_t data)
 
uint8_t modm::math::crc8_ccitt (const uint8_t *data, size_t length)
 
uint16_t modm::math::crc16_ccitt (const uint8_t *data, size_t length)
 
uint32_t modm::math::crc32 (const uint8_t *data, size_t length)
 
bool modm::isPositive (const float &a)
 
constexpr uint32_t modm::pow (uint32_t base, uint8_t exponent)
 
template<typename T >
const T & modm::min (const T &a, const T &b)
 This does what you think it does. More...
 
template<typename T >
const T & modm::max (const T &a, const T &b)
 This does what you think it does. More...
 
template<typename T >
constexpr T modm::max (const T a, const T b, const T c)
 This does what you think it does. More...
 
template<typename T , typename Compare >
const T & modm::min (const T &a, const T &b, Compare compare)
 This does what you think it does. More...
 
template<typename T , typename Compare >
const T & modm::max (const T &a, const T &b, Compare compare)
 This does what you think it does. More...
 
template<typename Float >
requires constexpr std::is_floating_point_v< Float > Float modm::constexpr_fabs (Float number)
 constexpr implementation of fabs
 
uint16_t modm::math::sqrt (uint32_t a)
 Fast AVR integer square root assembly routines. More...
 
uint32_t modm::math::mul (uint16_t a, uint16_t b)
 unsigned 16bit x 16bit = 32bit multiplication More...
 
int32_t modm::math::mul (int16_t a, int16_t b)
 signed 16bit x 16bit = 32bit multiplication More...
 
int32_t modm::math::mac (int32_t result, int16_t a, int16_t b)
 Signed multiply accumulate of two 16bits numbers with a 32bits result. More...
 
constexpr uint8_t modm::swap (uint8_t n)
 Exchange the two nibbles of a byte. More...
 
constexpr void modm::swap (uint8_t &a, uint8_t &b)
 Exchange two byte.
 
constexpr uint16_t modm::swap (uint16_t n)
 Exchange the two bytes of a 16-bit integer. More...
 
constexpr uint32_t modm::swap (uint32_t n)
 Exchange the four bytes of a 32-bit integer. More...
 
constexpr void modm::swap (int16_t &a, int16_t &b)
 
uint8_t modm::bitReverse (uint8_t n)
 Reverse the bits in a byte. More...
 
uint16_t modm::bitReverse (uint16_t n)
 Reverse the bits in a 16-bit integer.
 
uint32_t modm::bitReverse (uint32_t n)
 Reverse the bits in a 32-bit integer.
 
constexpr int8_t modm::leftmostBit (uint32_t value)
 
std::size_t modm::bitCount (uint8_t n)
 Count the number of bits set. More...
 
std::size_t modm::bitCount (uint16_t n)
 Count the number of bits set. More...
 
std::size_t modm::bitCount (uint32_t n)
 Count the number of bits set.
 
constexpr bool modm::isBigEndian ()
 checks if current architecture is big endian
 
constexpr bool modm::isLittleEndian ()
 checks if current architecture is little endian
 
constexpr uint16_t modm::fromLittleEndian (uint16_t value)
 
constexpr int16_t modm::fromLittleEndian (int16_t value)
 
constexpr uint32_t modm::fromLittleEndian (uint32_t value)
 
constexpr int32_t modm::fromLittleEndian (int32_t value)
 
constexpr uint16_t modm::fromBigEndian (uint16_t value)
 
constexpr int16_t modm::fromBigEndian (int16_t value)
 
constexpr uint32_t modm::fromBigEndian (uint32_t value)
 
constexpr int32_t modm::fromBigEndian (int32_t value)
 
constexpr uint16_t modm::toLittleEndian (uint16_t value)
 
constexpr int16_t modm::toLittleEndian (int16_t value)
 
constexpr uint32_t modm::toLittleEndian (uint32_t value)
 
constexpr int32_t modm::toLittleEndian (int32_t value)
 
constexpr uint16_t modm::toBigEndian (uint16_t value)
 
constexpr int16_t modm::toBigEndian (int16_t value)
 
constexpr uint32_t modm::toBigEndian (uint32_t value)
 
constexpr int32_t modm::toBigEndian (int32_t value)
 

Detailed Description

lbuild module: modm:math:utils

Function Documentation

std::size_t modm::bitCount ( uint16_t  n)

Count the number of bits set.

33 clock cycles on an AVR, without call + return.

std::size_t modm::bitCount ( uint8_t  n)

Count the number of bits set.

16 clock cycles on an AVR, without call + return.

uint8_t modm::bitReverse ( uint8_t  n)
inline

Reverse the bits in a byte.

0b01110100 => 0b00101110

15 clock cycles on an AVR, without call + return.

uint32_t modm::math::crc32 ( const uint8_t *  data,
size_t  length 
)
inline

Slow, but table-less computation of CRC32.

uint32_t modm::math::crc32_update ( uint32_t  crc,
uint8_t  data 
)
inline
bool modm::isPositive ( const float &  a)
inline

Fast check if a float variable is positive

Checks only the sign bit for the AVR.

constexpr int8_t modm::leftmostBit ( uint32_t  value)
constexpr

Returns position of leftmost bit at compile time.

Return Values
-1if no bit set in value
nposition of leftmost bit
int32_t modm::math::mac ( int32_t  result,
int16_t  a,
int16_t  b 
)
inline

Signed multiply accumulate of two 16bits numbers with a 32bits result.

result += a * b;
See also
AVR201
template<typename T >
const T& modm::max ( const T &  a,
const T &  b 
)
inline

This does what you think it does.

Parameters
aA thing of arbitrary type.
bAnother thing of arbitrary type.
Returns
The greater of the parameters.

This is the simple classic generic implementation. It will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

template<typename T , typename Compare >
const T& modm::max ( const T &  a,
const T &  b,
Compare  compare 
)
inline

This does what you think it does.

Parameters
aA thing of arbitrary type.
bAnother thing of arbitrary type.
compareA comparison functor.
Returns
The greater of the parameters.

This will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

template<typename T >
constexpr T modm::max ( const T  a,
const T  b,
const T  c 
)
constexpr

This does what you think it does.

Parameters
aA thing of arbitrary type.
bAnother thing of arbitrary type.
cSomething else of arbitrary type.
Returns
The greater of the three parameters.

This is the simple classic generic implementation. It will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

template<typename T >
const T& modm::min ( const T &  a,
const T &  b 
)
inline

This does what you think it does.

Parameters
aA thing of arbitrary type.
bAnother thing of arbitrary type.
Returns
The lesser of the parameters.

This is the simple classic generic implementation. It will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

template<typename T , typename Compare >
const T& modm::min ( const T &  a,
const T &  b,
Compare  compare 
)
inline

This does what you think it does.

Parameters
aA thing of arbitrary type.
bAnother thing of arbitrary type.
compareA comparison functor.
Returns
The lesser of the parameters.

This will work on temporary expressions, since they are only evaluated once, unlike a preprocessor macro.

int32_t modm::math::mul ( int16_t  a,
int16_t  b 
)
inline

signed 16bit x 16bit = 32bit multiplication

See also
AVR201
uint32_t modm::math::mul ( uint16_t  a,
uint16_t  b 
)
inline

unsigned 16bit x 16bit = 32bit multiplication

See also
AVR201
constexpr uint32_t modm::pow ( uint32_t  base,
uint8_t  exponent 
)
constexpr

Compile time and runtime exponentiation

Calculates base raised to the power of exponent. If base and exponent are compile-time constants, the results is calculated at compile time. Otherwise the result is computed at runtime, which might be very expensive.

constexpr int value = modm::pow(10, 2);
uint16_t modm::math::sqrt ( uint32_t  a)
inline

Fast AVR integer square root assembly routines.

Square root calculation based on a implementation by Ruud v Gessel. The maximum execution time is 310 clock cycles (inclusive CALL+RET)

See also
Article on microcontroller.net
Original implementation
constexpr uint16_t modm::swap ( uint16_t  n)
constexpr

Exchange the two bytes of a 16-bit integer.

0xabcd => 0xcdab
constexpr uint32_t modm::swap ( uint32_t  n)
constexpr

Exchange the four bytes of a 32-bit integer.

0xabcdefgh => 0xghefcdab
constexpr uint8_t modm::swap ( uint8_t  n)
constexpr

Exchange the two nibbles of a byte.

0xab => 0xba