BeRTOS
Defines | Typedefs | Functions
General purpose macros
BeRTOS core functionality

Common and handy function macros. More...

Defines

#define ALIGN_UP(value, align)
 Align value to the next align boundary.
#define MINMAX(min, x, max)   (MIN(MAX(min, x), max))
 Bound x between min and max.
#define SHUFFLE(array, len)
 Shuffle the content of array that counts len elements.
#define SWAP_T(a, b, T)
 Macro to swap a with b, with explicit type T for dumb C89 compilers.
#define REVERSE_UINT8(b)   ((uint8_t)((((b) * 0x0802UL & 0x22110UL) | ((b) * 0x8020UL & 0x88440UL)) * 0x10101UL >> 16))
 Reverse the bits contained in b (LSB becomes the MSB and so on).
#define BV(x)   (1<<(x))
 Convert a bit value to a binary flag.
#define BV32(x)   ((uint32_t)1<<(x))
 Same as BV() but with 32 bit result.
#define BV16(x)   ((uint16_t)1<<(x))
 Same as BV() but with 16 bit result.
#define BV8(x)   ((uint8_t)1<<(x))
 Same as BV() but with 8 bit result.
#define DIV_ROUND(dividend, divisor)   (((dividend) + (divisor) / 2) / (divisor))
 Perform an integer division rounding the result to the nearest int value.
#define DIV_ROUNDUP(dividend, divisor)   (((dividend) + (divisor) - 1) / (divisor))
 Perform an integer division rounding the result to the upper int value.
#define INT_MULT(a, f, prec)   (((a) * (long)((f) * (1 << (prec)) + 0.5)) >> (prec))
 Perform a multiply between the integer a and the float constant f.
#define ROUND_UP2(x, pad)   (((x) + ((pad) - 1)) & ~((pad) - 1))
 Round up x to an even multiple of the 2's power pad.
#define IS_POW2(x)   (!(bool)((x) & ((x)-1)))
 Check if x is an integer power of 2.
#define UINT8_LOG2(x)
 Calculate a compile-time log2 for a uint8_t.
#define UINT16_LOG2(x)   ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
 Calculate a compile-time log2 for a uint16_t.
#define UINT32_LOG2(x)   ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
 Calculate a compile-time log2 for a uint32_t.
#define PP_COUNT(...)   PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
 Count the number of arguments (up to 16).
#define BIT_CHANGE(reg,...)   BIT_CHANGE__(reg, 0, __VA_ARGS__)
 This macro allows for efficient and compact bit toggling in a hardware register.
#define BIT_CHANGE_BV(reg,...)   BIT_CHANGE__(reg, 1, __VA_ARGS__)
 Similar to BIT_CHANGE(), but get bits instead of masks (and applies BV() to convert them to masks).
#define MAKE_ID(a, b, c, d)
 Make an id from 4 letters, useful for file formats and kfile ids.

Typedefs

typedef uint32_t id_t
 Type for id generated by MAKE_ID().

Functions

bool is_aligned (const void *addr, size_t size)
 Check if a pointer is aligned to a certain power-of-2 size.

Integer round macros.

Round x to a multiple of base.

Note:
If x is signed these macros generate a lot of code.
#define ROUND_DOWN(x, base)   ( (x) - ((x) % (base)) )
#define ROUND_UP(x, base)   ( ((x) + (base) - 1) - (((x) + (base) - 1) % (base)) )
#define ROUND_NEAREST(x, base)   ( ((x) + (base) / 2) - (((x) + (base) / 2) % (base)) )
#define ROTR(var, rot)   (((var) >> (rot)) | ((var) << ((sizeof(var) * 8) - (rot))))
 Macro for rotating bit left or right.
#define ROTL(var, rot)   (((var) << (rot)) | ((var) >> ((sizeof(var) * 8) - (rot))))
 Macro for rotating bit left or right.

Detailed Description

Common and handy function macros.

Author:
Bernie Innocenti <bernie@codewiz.org>
Giovanni Bajo <rasky@develer.com>

Define Documentation

#define BIT_CHANGE (   reg,
  ... 
)    BIT_CHANGE__(reg, 0, __VA_ARGS__)

This macro allows for efficient and compact bit toggling in a hardware register.

It is meant to replace hand-coded cruft which toggles bits in sequence.

It is possible to specify an unlimited pair of (mask, value) parameters. For instance:

 void set_timer(bool start)
 {
     BIT_CHANGE(REG_CTRL_TIMER,
        (TIMER_MODE, MODE_COUNT),
        (OVL_IRQ, 1),
        (CMP_IRQ, 1),
        (START, start)
     );
 }

The macro expansion will be roughly the following:

 REG_CTRL_TIMER = (REG_CTRL_TIMER & ~(TIMER_MODE|OVL_IRQ|CMP_IRQ|START)
                  | (MODE_COUNT|OVL_IRQ|CMP_IRQ|(start ? START : 0));

It is up to the compiler to produce the optimal code. We checked that GCC produces the best code in most cases. We preferred this expansion over the use of a block with a local variable because CodeWarrior 6.1 was not able to remove completely the allocation of the local from the stack.

Note:
This macro is available only in C99 because it makes use of variadic macros. It would be possible to make up an implementation with a slightly different syntax for use with C90 compilers, through Boost Preprocessor.

Definition at line 373 of file macros.h.

#define BV (   x)    (1<<(x))

Convert a bit value to a binary flag.

Definition at line 174 of file macros.h.

#define DIV_ROUND (   dividend,
  divisor 
)    (((dividend) + (divisor) / 2) / (divisor))

Perform an integer division rounding the result to the nearest int value.

Note:
divisor should preferibly be a costant, otherwise this macro generates 2 division. Also divisor is evaluated twice.

Definition at line 191 of file macros.h.

#define DIV_ROUNDUP (   dividend,
  divisor 
)    (((dividend) + (divisor) - 1) / (divisor))

Perform an integer division rounding the result to the upper int value.

Note:
divisor is evaluated twice.

Definition at line 197 of file macros.h.

#define INT_MULT (   a,
  f,
  prec 
)    (((a) * (long)((f) * (1 << (prec)) + 0.5)) >> (prec))

Perform a multiply between the integer a and the float constant f.

This macro can be used in order to avoid floating point arithmetics in expressions like this:

 int a, b;
 a = b * 0.5579652750;

This macro rounds the floating point constant to a fraction, usign (2 ^ prec) as the denominator. For instance, with prec = 8, the constant 0.5579652750 will be rounded to: (143 / 256) = 0.55859375 So, the former code will be transformed to:

 a = b * 143 / 256;

Since the denominator is a power of 2, we rely on the compiler to optimize this to a right shift. So, when you have to multiply an integer by a float constant, this macro will not use the floating point arithmentics. The operation will be converted to a mul + shift, with a huge performance boost.

Note:
f MUST be a constant in order gain performance benefits.
Parameters:
ainteger you want to multiply
ffloating point constant which you want to multply with a
precconversion precision, ranges from 1 to the number of bits in a long. The higher, the better the approximation of the float constant will be.

Definition at line 232 of file macros.h.

#define IS_POW2 (   x)    (!(bool)((x) & ((x)-1)))

Check if x is an integer power of 2.

Definition at line 251 of file macros.h.

#define MINMAX (   min,
  x,
  max 
)    (MIN(MAX(min, x), max))

Bound x between min and max.

Definition at line 107 of file macros.h.

#define PP_COUNT (   ...)    PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)

Count the number of arguments (up to 16).

Definition at line 273 of file macros.h.

#define REVERSE_UINT8 (   b)    ((uint8_t)((((b) * 0x0802UL & 0x22110UL) | ((b) * 0x8020UL & 0x88440UL)) * 0x10101UL >> 16))

Reverse the bits contained in b (LSB becomes the MSB and so on).

Note:
b is evaluated twice

Definition at line 169 of file macros.h.

#define ROUND_UP2 (   x,
  pad 
)    (((x) + ((pad) - 1)) & ~((pad) - 1))

Round up x to an even multiple of the 2's power pad.

Definition at line 236 of file macros.h.

#define SWAP_T (   a,
  b,
 
)
Value:
do { \
        T tmp; \
        ASSERT_TYPE_IS(a, T); \
        ASSERT_TYPE_IS(b, T); \
        tmp = (a); \
        (a) = (b); \
        (b) = tmp; \
    } while (0)

Macro to swap a with b, with explicit type T for dumb C89 compilers.

Note:
Arguments are evaluated multiple times.

Definition at line 155 of file macros.h.