eal: add macro to align value to the nearest multiple
[dpdk.git] / lib / librte_eal / common / include / rte_common.h
index 7b50b84..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
  *
@@ -565,6 +577,24 @@ 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)