modm API documentation
Utilities

Classes

struct  modm::uint_t< Bits >
 

Functions

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)
 
template<typename T >
const T & modm::max (const T &a, const T &b)
 
template<typename T >
constexpr T modm::max (const T a, const T b, const T c)
 
template<typename T , typename Compare >
const T & modm::min (const T &a, const T &b, Compare compare)
 
template<typename T , typename Compare >
const T & modm::max (const T &a, const T &b, Compare compare)
 
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...
 
template<typename T >
using modm::WideType = typename detail::WideType< T >::type
 
template<typename T >
using modm::SignedType = typename detail::MakeSigned< T >::type
 
template<typename T >
using modm::UnsignedType = typename detail::MakeUnsigned< T >::type
 
constexpr uint8_t modm::Bit0 = uint8_t(1) << 0
 
constexpr uint8_t modm::Bit1 = uint8_t(1) << 1
 
constexpr uint8_t modm::Bit2 = uint8_t(1) << 2
 
constexpr uint8_t modm::Bit3 = uint8_t(1) << 3
 
constexpr uint8_t modm::Bit4 = uint8_t(1) << 4
 
constexpr uint8_t modm::Bit5 = uint8_t(1) << 5
 
constexpr uint8_t modm::Bit6 = uint8_t(1) << 6
 
constexpr uint8_t modm::Bit7 = uint8_t(1) << 7
 
constexpr uint16_t modm::Bit8 = uint16_t(1) << 8
 
constexpr uint16_t modm::Bit9 = uint16_t(1) << 9
 
constexpr uint16_t modm::Bit10 = uint16_t(1) << 10
 
constexpr uint16_t modm::Bit11 = uint16_t(1) << 11
 
constexpr uint16_t modm::Bit12 = uint16_t(1) << 12
 
constexpr uint16_t modm::Bit13 = uint16_t(1) << 13
 
constexpr uint16_t modm::Bit14 = uint16_t(1) << 14
 
constexpr uint16_t modm::Bit15 = uint16_t(1) << 15
 
constexpr uint32_t modm::Bit16 = uint32_t(1) << 16
 
constexpr uint32_t modm::Bit17 = uint32_t(1) << 17
 
constexpr uint32_t modm::Bit18 = uint32_t(1) << 18
 
constexpr uint32_t modm::Bit19 = uint32_t(1) << 19
 
constexpr uint32_t modm::Bit20 = uint32_t(1) << 20
 
constexpr uint32_t modm::Bit21 = uint32_t(1) << 21
 
constexpr uint32_t modm::Bit22 = uint32_t(1) << 22
 
constexpr uint32_t modm::Bit23 = uint32_t(1) << 23
 
constexpr uint32_t modm::Bit24 = uint32_t(1) << 24
 
constexpr uint32_t modm::Bit25 = uint32_t(1) << 25
 
constexpr uint32_t modm::Bit26 = uint32_t(1) << 26
 
constexpr uint32_t modm::Bit27 = uint32_t(1) << 27
 
constexpr uint32_t modm::Bit28 = uint32_t(1) << 28
 
constexpr uint32_t modm::Bit29 = uint32_t(1) << 29
 
constexpr uint32_t modm::Bit30 = uint32_t(1) << 30
 
constexpr uint32_t modm::Bit31 = uint32_t(1) << 31
 
constexpr uint64_t modm::Bit32 = uint64_t(1) << 32
 
constexpr uint64_t modm::Bit33 = uint64_t(1) << 33
 
constexpr uint64_t modm::Bit34 = uint64_t(1) << 34
 
constexpr uint64_t modm::Bit35 = uint64_t(1) << 35
 
constexpr uint64_t modm::Bit36 = uint64_t(1) << 36
 
constexpr uint64_t modm::Bit37 = uint64_t(1) << 37
 
constexpr uint64_t modm::Bit38 = uint64_t(1) << 38
 
constexpr uint64_t modm::Bit39 = uint64_t(1) << 39
 
constexpr uint64_t modm::Bit40 = uint64_t(1) << 40
 
constexpr uint64_t modm::Bit41 = uint64_t(1) << 41
 
constexpr uint64_t modm::Bit42 = uint64_t(1) << 42
 
constexpr uint64_t modm::Bit43 = uint64_t(1) << 43
 
constexpr uint64_t modm::Bit44 = uint64_t(1) << 44
 
constexpr uint64_t modm::Bit45 = uint64_t(1) << 45
 
constexpr uint64_t modm::Bit46 = uint64_t(1) << 46
 
constexpr uint64_t modm::Bit47 = uint64_t(1) << 47
 
constexpr uint64_t modm::Bit48 = uint64_t(1) << 48
 
constexpr uint64_t modm::Bit49 = uint64_t(1) << 49
 
constexpr uint64_t modm::Bit50 = uint64_t(1) << 50
 
constexpr uint64_t modm::Bit51 = uint64_t(1) << 51
 
constexpr uint64_t modm::Bit52 = uint64_t(1) << 52
 
constexpr uint64_t modm::Bit53 = uint64_t(1) << 53
 
constexpr uint64_t modm::Bit54 = uint64_t(1) << 54
 
constexpr uint64_t modm::Bit55 = uint64_t(1) << 55
 
constexpr uint64_t modm::Bit56 = uint64_t(1) << 56
 
constexpr uint64_t modm::Bit57 = uint64_t(1) << 57
 
constexpr uint64_t modm::Bit58 = uint64_t(1) << 58
 
constexpr uint64_t modm::Bit59 = uint64_t(1) << 59
 
constexpr uint64_t modm::Bit60 = uint64_t(1) << 60
 
constexpr uint64_t modm::Bit61 = uint64_t(1) << 61
 
constexpr uint64_t modm::Bit62 = uint64_t(1) << 62
 
constexpr uint64_t modm::Bit63 = uint64_t(1) << 63
 
constexpr uint8_t modm::swap (uint8_t n)
 
constexpr void modm::swap (uint8_t &a, uint8_t &b)
 Exchange two byte.
 
constexpr uint16_t modm::swap (uint16_t n)
 
constexpr uint32_t modm::swap (uint32_t n)
 
constexpr void modm::swap (int16_t &a, int16_t &b)
 
uint8_t modm::bitReverse (uint8_t n)
 
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)
 
std::size_t modm::bitCount (uint16_t n)
 
std::size_t modm::bitCount (uint32_t n)
 Count the number of bits set.
 
uint8_t modm::math::crc8_ccitt_update (uint8_t crc, uint8_t data)
 
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.

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