FastLED  3.1
Basic math operations

Fast, efficient 8-bit math functions specifically designed for high-performance LED programming. More... Collaboration diagram for Basic math operations:

## Functions

LIB8STATIC_ALWAYS_INLINE uint8_t qadd8 (uint8_t i, uint8_t j)
add one byte to another, saturating at 0xFF More...

LIB8STATIC_ALWAYS_INLINE int8_t qadd7 (int8_t i, int8_t j)
Add one byte to another, saturating at 0x7F. More...

LIB8STATIC_ALWAYS_INLINE uint8_t qsub8 (uint8_t i, uint8_t j)
subtract one byte from another, saturating at 0x00 More...

LIB8STATIC_ALWAYS_INLINE uint8_t add8 (uint8_t i, uint8_t j)
add one byte to another, with one byte result

LIB8STATIC_ALWAYS_INLINE uint8_t sub8 (uint8_t i, uint8_t j)
subtract one byte from another, 8-bit result

LIB8STATIC_ALWAYS_INLINE uint8_t avg8 (uint8_t i, uint8_t j)
Calculate an integer average of two unsigned 8-bit integer values (uint8_t). More...

LIB8STATIC_ALWAYS_INLINE uint16_t avg16 (uint16_t i, uint16_t j)
Calculate an integer average of two unsigned 16-bit integer values (uint16_t). More...

LIB8STATIC_ALWAYS_INLINE int8_t avg7 (int8_t i, int8_t j)
Calculate an integer average of two signed 7-bit integers (int8_t) If the first argument is even, result is rounded down. More...

LIB8STATIC_ALWAYS_INLINE int16_t avg15 (int16_t i, int16_t j)
Calculate an integer average of two signed 15-bit integers (int16_t) If the first argument is even, result is rounded down. More...

LIB8STATIC_ALWAYS_INLINE uint8_t mod8 (uint8_t a, uint8_t m)
Calculate the remainder of one unsigned 8-bit value divided by anoter, aka A % M. More...

LIB8STATIC uint8_t addmod8 (uint8_t a, uint8_t b, uint8_t m)
Add two numbers, and calculate the modulo of the sum and a third number, M. More...

LIB8STATIC_ALWAYS_INLINE uint8_t mul8 (uint8_t i, uint8_t j)
8x8 bit multiplication, with 8 bit result

LIB8STATIC_ALWAYS_INLINE uint8_t qmul8 (uint8_t i, uint8_t j)
saturating 8x8 bit multiplication, with 8 bit result More...

LIB8STATIC_ALWAYS_INLINE int8_t abs8 (int8_t i)
take abs() of a signed 8-bit uint8_t

LIB8STATIC uint8_t sqrt16 (uint16_t x)
square root for 16-bit integers About three times faster and five times smaller than Arduino's general sqrt on AVR. More...

## Detailed Description

Fast, efficient 8-bit math functions specifically designed for high-performance LED programming.

Because of the AVR(Arduino) and ARM assembly language implementations provided, using these functions often results in smaller and faster code than the equivalent program using plain "C" arithmetic and logic.

## Function Documentation

 LIB8STATIC uint8_t addmod8 ( uint8_t a, uint8_t b, uint8_t m )

Add two numbers, and calculate the modulo of the sum and a third number, M.

In other words, it returns (A+B) % M. It is designed as a compact mechanism for incrementing a 'mode' switch and wrapping around back to 'mode 0' when the switch goes past the end of the available range. e.g. if you have seven modes, this switches to the next one and wraps around if needed: mode = addmod8( mode, 1, 7); LIB8STATIC_ALWAYS_INLINESee 'mod8' for notes on performance.

Definition at line 276 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE int16_t avg15 ( int16_t i, int16_t j )

Calculate an integer average of two signed 15-bit integers (int16_t) If the first argument is even, result is rounded down.

If the first argument is odd, result is result up.

Definition at line 217 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE uint16_t avg16 ( uint16_t i, uint16_t j )

Calculate an integer average of two unsigned 16-bit integer values (uint16_t).

Fractional results are rounded down, e.g. avg16(20,41) = 30

Definition at line 169 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE int8_t avg7 ( int8_t i, int8_t j )

Calculate an integer average of two signed 7-bit integers (int8_t) If the first argument is even, result is rounded down.

If the first argument is odd, result is result up.

Definition at line 196 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE uint8_t avg8 ( uint8_t i, uint8_t j )

Calculate an integer average of two unsigned 8-bit integer values (uint8_t).

Fractional results are rounded down, e.g. avg8(20,41) = 30

Definition at line 148 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE uint8_t mod8 ( uint8_t a, uint8_t m )

Calculate the remainder of one unsigned 8-bit value divided by anoter, aka A % M.

Implemented by repeated subtraction, which is very compact, and very fast if A is 'probably' less than M. If A is a large multiple of M, the loop has to execute multiple times. However, even in that case, the loop is only two instructions long on AVR, i.e., quick.

Definition at line 249 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE int8_t qadd7 ( int8_t i, int8_t j )

Add one byte to another, saturating at 0x7F.

Parameters
 i - first byte to add j - second byte to add
Returns
the sum of i & j, capped at 0xFF

Definition at line 54 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE uint8_t qadd8 ( uint8_t i, uint8_t j )

add one byte to another, saturating at 0xFF

Parameters
 i - first byte to add j - second byte to add
Returns
the sum of i & j, capped at 0xFF

Definition at line 21 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE uint8_t qmul8 ( uint8_t i, uint8_t j )

saturating 8x8 bit multiplication, with 8 bit result

Returns
the product of i * j, capping at 0xFF

Definition at line 320 of file math8.h.

 LIB8STATIC_ALWAYS_INLINE uint8_t qsub8 ( uint8_t i, uint8_t j )

subtract one byte from another, saturating at 0x00

Returns
i - j with a floor of 0

Definition at line 86 of file math8.h.

 LIB8STATIC uint8_t sqrt16 ( uint16_t x )

square root for 16-bit integers About three times faster and five times smaller than Arduino's general sqrt on AVR.

Definition at line 379 of file math8.h.