macros.h File Reference
Common and handy function macros. More...
#include <cfg/compiler.h>Go to the source code of this file.
Defines | |
| #define | MAX(a, b) |
| \ The (typeof(_a)) cast in necessary: \ result type of conditional expressions is \ *NOT* the type of the value returned but \ the type that would be produced if _a and _b \ were mixed in an expression. | |
| #define | MINMAX(min, x, max) (MIN(MAX(min, x), max)) |
| \ The (typeof(_a)) cast in necessary: \ result type of conditional expressions is \ *NOT* the type of the value returned but \ the type that would be produced if _a and _b \ were mixed in an expression. | |
| #define | SWAP(a, b) |
| Type-generic macro to swap a with b. | |
| #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 | 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. | |
Integer round macros. | |
| #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. | |
Typedefs | |
| typedef uint32_t | id_t |
| Type for id generated by MAKE_ID(). | |
Detailed Description
Common and handy function macros.
- Version:
- Id
- macros.h 3379 2010-04-02 09:56:44Z batt
Definition in file macros.h.
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.
| #define BV | ( | x | ) | (1<<(x)) |
| #define DIV_ROUND | ( | dividend, | |||
| divisor | ) | (((dividend) + (divisor) / 2) / (divisor)) |
| #define DIV_ROUNDUP | ( | dividend, | |||
| divisor | ) | (((dividend) + (divisor) - 1) / (divisor)) |
| #define IS_POW2 | ( | x | ) | (!(bool)((x) & ((x)-1))) |
| #define MAX | ( | a, | |||
| b | ) |
({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
ASSERT_TYPE_EQUAL(_a, _b); \
\
((typeof(_a))((_a > _b) ? _a : _b)); \
})
\ The (typeof(_a)) cast in necessary: \ result type of conditional expressions is \ *NOT* the type of the value returned but \ the type that would be produced if _a and _b \ were mixed in an expression.
\ Even in _a and _b are of the same type, \ if mixed in an expression the type will be \ (at least) promoted to int! \
| #define MINMAX | ( | min, | |||
| x, | |||||
| max | ) | (MIN(MAX(min, x), max)) |
\ The (typeof(_a)) cast in necessary: \ result type of conditional expressions is \ *NOT* the type of the value returned but \ the type that would be produced if _a and _b \ were mixed in an expression.
\ Even in _a and _b are of the same type, \ if mixed in an expression the type will be \ (at least) promoted to int! \ Bound x between min and max.
| #define PP_COUNT | ( | ... | ) | PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0) |
| #define REVERSE_UINT8 | ( | b | ) | ((uint8_t)((((b) * 0x0802UL & 0x22110UL) | ((b) * 0x8020UL & 0x88440UL)) * 0x10101UL >> 16)) |
| #define ROUND_UP2 | ( | x, | |||
| pad | ) | (((x) + ((pad) - 1)) & ~((pad) - 1)) |
| #define SWAP | ( | a, | |||
| b | ) |
| #define SWAP_T | ( | a, | |||
| b, | |||||
| T | ) |
