eal: add macro to align value to the nearest multiple
[dpdk.git] / lib / librte_eal / common / include / rte_common.h
index d115b17..bcf8afd 100644 (file)
@@ -248,6 +248,18 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
        ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
 
+/**
+ * Macro to align value to the nearest multiple of the given value.
+ * The resultant value might be greater than or less than the first parameter
+ * whichever difference is the lowest.
+ */
+#define RTE_ALIGN_MUL_NEAR(v, mul)                             \
+       ({                                                      \
+               typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);    \
+               typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);  \
+               (ceil - v) > (v - floor) ? floor : ceil;        \
+       })
+
 /**
  * Checks if a pointer is aligned to a given power-of-two value
  *
@@ -270,16 +282,7 @@ rte_is_aligned(void *ptr, unsigned align)
 /**
  * Triggers an error at compilation time if the condition is true.
  */
-#ifndef __OPTIMIZE__
 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
-#else
-extern int RTE_BUILD_BUG_ON_detected_error;
-#define RTE_BUILD_BUG_ON(condition) do {             \
-       ((void)sizeof(char[1 - 2*!!(condition)]));   \
-       if (condition)                               \
-               RTE_BUILD_BUG_ON_detected_error = 1; \
-} while(0)
-#endif
 
 /**
  * Combines 32b inputs most significant set bits into the least
@@ -456,6 +459,30 @@ rte_bsf32(uint32_t v)
        return (uint32_t)__builtin_ctz(v);
 }
 
+/**
+ * Searches the input parameter for the least significant set bit
+ * (starting from zero). Safe version (checks for input parameter being zero).
+ *
+ * @warning ``pos`` must be a valid pointer. It is not checked!
+ *
+ * @param v
+ *     The input parameter.
+ * @param pos
+ *     If ``v`` was not 0, this value will contain position of least significant
+ *     bit within the input parameter.
+ * @return
+ *     Returns 0 if ``v`` was 0, otherwise returns 1.
+ */
+static inline int
+rte_bsf32_safe(uint64_t v, uint32_t *pos)
+{
+       if (v == 0)
+               return 0;
+
+       *pos = rte_bsf32(v);
+       return 1;
+}
+
 /**
  * Return the rounded-up log2 of a integer.
  *
@@ -491,6 +518,23 @@ rte_fls_u32(uint32_t x)
        return (x == 0) ? 0 : 32 - __builtin_clz(x);
 }
 
+/**
+ * Searches the input parameter for the least significant set bit
+ * (starting from zero).
+ * If a least significant 1 bit is found, its bit index is returned.
+ * If the content of the input parameter is zero, then the content of the return
+ * value is undefined.
+ * @param v
+ *     input parameter, should not be zero.
+ * @return
+ *     least significant set bit in the input parameter.
+ */
+static inline int
+rte_bsf64(uint64_t v)
+{
+       return (uint32_t)__builtin_ctzll(v);
+}
+
 /**
  * Searches the input parameter for the least significant set bit
  * (starting from zero). Safe version (checks for input parameter being zero).
@@ -511,10 +555,46 @@ rte_bsf64_safe(uint64_t v, uint32_t *pos)
        if (v == 0)
                return 0;
 
-       *pos = __builtin_ctzll(v);
+       *pos = rte_bsf64(v);
        return 1;
 }
 
+/**
+ * Return the last (most-significant) bit set.
+ *
+ * @note The last (most significant) bit is at position 64.
+ * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
+ *       rte_fls_u64(0x8000000000000000) = 64
+ *
+ * @param x
+ *     The input parameter.
+ * @return
+ *     The last (most-significant) bit set, or 0 if the input is 0.
+ */
+static inline int
+rte_fls_u64(uint64_t x)
+{
+       return (x == 0) ? 0 : 64 - __builtin_clzll(x);
+}
+
+/**
+ * Return the rounded-up log2 of a 64-bit integer.
+ *
+ * @param v
+ *     The input parameter.
+ * @return
+ *     The rounded-up log2 of the input, or 0 if the input is 0.
+ */
+static inline uint32_t
+rte_log2_u64(uint64_t v)
+{
+       if (v == 0)
+               return 0;
+       v = rte_align64pow2(v);
+       /* we checked for v being 0 already, so no undefined behavior */
+       return rte_bsf64(v);
+}
+
 #ifndef offsetof
 /** Return the offset of a field in a structure. */
 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)