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 | IS_ALIGNED(x, byte_count) ((uintptr_t)(const void *)(x) % (byte_count) == 0) |
| Check if x is aligned to byte_count bytes. | |
| #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. | |
| #define | BCD_TO_INT_32BIT(bcd) |
| Convert one 32bit bcd numbert to int. | |
| #define | UNSTUFF_BITS(resp, start, size) |
| Extract chunk of bit from gived array (uint32_t type). | |
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.
| |
| #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.
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 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:
-
a integer you want to multiply f floating point constant which you want to multply with a prec conversion precision, ranges from 1 to the number of bits in a long. The higher, the better the approximation of the float constant will be.
| #define IS_POW2 | ( | x | ) | (!(bool)((x) & ((x)-1))) |
| #define MINMAX | ( | min, | |
| x, | |||
| max | |||
| ) | (MIN(MAX(min, x), 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_T | ( | a, | |
| b, | |||
| T | |||
| ) |
| #define UNSTUFF_BITS | ( | resp, | |
| start, | |||
| size | |||
| ) |
({ \
const uint32_t __size = size; \
const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1; \
const uint32_t __off = 3 - ((start) / 32); \
const uint32_t __shft = (start) & 31; \
uint32_t __res; \
\
__res = resp[__off] >> __shft; \
if (__size + __shft > 32) \
__res |= resp[__off-1] << ((32 - __shft) % 32); \
__res & __mask; \
})
Extract chunk of bit from gived array (uint32_t type).
- Parameters:
-
resp array of bit 32bit aligned start bit position in array size of bit chuck from start
- Returns:
- uint32_t chunk value.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/bertos_logo.png)