macros.h File Reference

Common and handy function macros. More...

#include <cfg/compiler.h>

Go to the source code of this file.


Defines

#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 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 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 MAKE_ID(a, b, c, d)
 Make an id from 4 letters, useful for file formats and kfile ids.
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.

Typedefs

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

Detailed Description

Common and handy function macros.

Version:
Id
macros.h 2506 2009-04-15 08:29:07Z duplo
Author:
Bernie Innocenti <bernie@codewiz.org>

Giovanni Bajo <rasky@develer.com>

Definition in file macros.h.


Define Documentation

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

Convert a bit value to a binary flag.

Definition at line 160 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 177 of file macros.h.

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

Check if x is an integer power of 2.

Definition at line 195 of file macros.h.

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

Bound x between min and max.

Definition at line 99 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 180 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 147 of file macros.h.