Refactor to use new protocol module and sipo.
[bertos.git] / bertos / cfg / macros.h
index dda5fe0e635a023bfc1c3cb338b9d65d0a09ff8d..4af10246f2ad6e646a44f242259cf8d920b12b3e 100644 (file)
  *
  * -->
  *
+ * \defgroup macros General purpose macros
+ * \ingroup core
+ * \{
+ *
  * \brief Common and handy function macros
  *
- * \version $Id$
- * \author Bernardo Innocenti <bernie@develer.com>
+ * \author Bernie Innocenti <bernie@codewiz.org>
  * \author Giovanni Bajo <rasky@develer.com>
  */
 #ifndef CFG_MACROS_H
        #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
 #endif /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */
 
+/** Align \p value to the next \p align boundary */
+#define ALIGN_UP(value, align) (((value) & ((align) - 1)) ? \
+                               (((value) + ((align) - 1)) & ~((align) - 1)) : \
+                               (value))
+
 /** Bound \a x between \a min and \a max. */
 #define MINMAX(min,x,max)  (MIN(MAX(min, x), max))
 
 
 #endif /* COMPILER_TYPEOF */
 
+/**
+ * Shuffle the content of \a array that counts \a len elements.
+ */
+#define SHUFFLE(array, len) \
+       do { \
+               int i, j; \
+               for (i = (len) - 1; i > 0; i--) \
+               { \
+                       j = ((i + 1) * (rand() / (RAND_MAX + 1.0))); \
+                       SWAP((array)[i], (array)[j]); \
+               } \
+       } while (0)
+
 /**
  * Macro to swap \a a with \a b, with explicit type \a T for dumb C89 compilers.
  *
                (b) = tmp; \
        } while (0)
 
+/**
+ * Reverse the bits contained in b (LSB becomes the MSB and so on).
+ * \note \a b is evaluated twice
+ */
+#define REVERSE_UINT8(b) \
+       ((uint8_t)((((b) * 0x0802UL & 0x22110UL) | ((b) * 0x8020UL & 0x88440UL)) * 0x10101UL >> 16))
 
 #ifndef BV
        /** Convert a bit value to a binary flag. */
  */
 #define DIV_ROUND(dividend, divisor)  (((dividend) + (divisor) / 2) / (divisor))
 
+/**
+ * Perform an integer division rounding the result to the upper int value.
+ * \note \a divisor is evaluated twice.
+ */
+#define DIV_ROUNDUP(dividend, divisor)  (((dividend) + (divisor) - 1) / (divisor))
+
+
+/**
+ * Perform a multiply between the integer \a a and the float constant \a f.
+ *
+ * This macro can be used in order to avoid floating point arithmetics
+ * in expressions like this:
+ * \code
+ * int a, b;
+ * a = b * 0.5579652750;
+ * \endcode
+ *
+ * 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:
+ * \code
+ * a = b * 143 / 256;
+ * \endcode
+ *
+ * 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 \a f MUST be a constant in order gain performance benefits.
+ *
+ * \param a integer you want to multiply
+ * \param f floating point constant which you want to multply with \a a
+ * \param 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 INT_MULT(a, f, prec) (((a) * (long)((f) * (1 << (prec)) + 0.5)) >> (prec))
+
+
 /** Round up \a x to an even multiple of the 2's power \a pad. */
 #define ROUND_UP2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
 
-/* OBSOLETE */
-#define ROUND2 ROUND_UP2
-
 /**
  * \name Integer round macros.
  *
 /** Check if \a x is an integer power of 2. */
 #define IS_POW2(x)     (!(bool)((x) & ((x)-1)))
 
+/** Check if \a x is aligned to \a byte_count bytes */
+#define IS_ALIGNED(x, byte_count) ((uintptr_t)(const void *)(x) % (byte_count) == 0)
+
 /** Calculate a compile-time log2 for a uint8_t */
 #define UINT8_LOG2(x) \
        ((x) < 2 ? 0 : \
 
        #define BIT_MASK_SINGLE__(use_bv, index, max, arg) \
                ((index < max) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
-               /**/
+               /* */
 
        #define BIT_MASK_IF_SINGLE__(use_bv, index, max, arg) \
                (((index < max) && (BIT_EXTRACT_VALUE__ arg)) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
-               /**/
+               /* */
 
        #define BIT_ITER__2(macro, use_bv, max, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15, ...) \
                (macro(use_bv, 0, max, a0) | \
                macro(use_bv, 13, max, a13) | \
                macro(use_bv, 14, max, a14) | \
                macro(use_bv, 15, max, a15)) \
-               /**/
+               /* */
 
        #define BIT_ITER__(macro, use_bv, ...) \
                BIT_ITER__2(macro, use_bv, PP_COUNT(__VA_ARGS__), __VA_ARGS__, (0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1)) \
-               /**/
+               /* */
 
        #define BIT_MASKS__(use_bv, ...) \
                BIT_ITER__(BIT_MASK_SINGLE__, use_bv, __VA_ARGS__)
-               /**/
+               /* */
 
        #define BIT_MASKS_CONDITIONAL__(use_bv, ...) \
                BIT_ITER__(BIT_MASK_IF_SINGLE__, use_bv, __VA_ARGS__)
-               /**/
+               /* */
 
        #define BIT_CHANGE__(reg, use_bv, ...) \
                ((reg) = ((reg) & ~BIT_MASKS__(use_bv, __VA_ARGS__)) | BIT_MASKS_CONDITIONAL__(use_bv, __VA_ARGS__)) \
-               /**/
+               /* */
 
        #define BIT_CHANGE(reg, ...)        BIT_CHANGE__(reg, 0, __VA_ARGS__)
        #define BIT_CHANGE_BV(reg, ...)     BIT_CHANGE__(reg, 1, __VA_ARGS__)
  */
 typedef uint32_t id_t;
 
+/**
+ * Check if a pointer is aligned to a certain power-of-2 size
+ */
+INLINE bool is_aligned(const void *addr, size_t size)
+{
+       return ((size_t)addr & (size - 1)) == 0;
+}
+
+/**
+ * Convert one 32bit bcd numbert to int.
+ */
+#define BCD_TO_INT_32BIT(bcd)  \
+       ((uint32_t )((bcd) & 0xf) * 1 +  \
+       (((bcd) >> 4) & 0xf)  * 10 +      \
+       (((bcd) >> 8) & 0xf)  * 100 +     \
+       (((bcd) >> 12) & 0xf) * 1000 +   \
+       (((bcd) >> 16) & 0xf) * 10000 +   \
+       (((bcd) >> 20) & 0xf) * 100000 +  \
+       (((bcd) >> 24) & 0xf) * 1000000 + \
+       (((bcd) >> 28) & 0xf) * 10000000) \
+
+/**
+ * Extract chunk of bit from gived array (uint32_t type).
+ * \param resp array of bit 32bit aligned
+ * \param start bit position in array
+ * \param size of bit chuck from start
+ * \return uint32_t chunk value.
+ */
+#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;                     \
+    })
+
+
+/** \} */ //defgroup macros
+
+
+/**
+ * Macro to unpack the ip addres from lwip format in 4 int
+ * \param addr is struct ip_addr ip_addr;
+ * \return for int variable separated from comma
+ *
+ * \code
+ * //Upack into 4 int the lwip ip address
+ * LOG_INFO("dhcp ok: ip = %d.%d.%d.%d\n", IP_ADDR_TO_INT_TUPLE(netif.ip_addr.addr));
+ * \endcode
+ */
+#define IP_ADDR_TO_INT_TUPLE(addr) \
+               (int)((addr) >>  0 & 0xff), \
+               (int)((addr) >>  8 & 0xff), \
+               (int)((addr) >> 16 & 0xff), \
+               (int)((addr) >> 24 & 0xff)
+
 #endif /* MACROS_H */