]> git.droids-corp.org - dpdk.git/commitdiff
eal: add macro to align value to the nearest multiple
authorPavan Nikhilesh <pbhagavatula@marvell.com>
Sat, 16 Mar 2019 19:01:50 +0000 (19:01 +0000)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 27 Mar 2019 23:45:00 +0000 (00:45 +0100)
Add macro to align value to the nearest multiple of the given value,
resultant value might be greater than or less than the first parameter
whichever difference is the lowest.
Update unit test to include the new macro.

Signed-off-by: Pavan Nikhilesh <pbhagavatula@marvell.com>
app/test/test_common.c
lib/librte_eal/common/include/rte_common.h

index 94d3674712d7a8ab5f7c54ed8c2b8b02bd71327e..2b856f8ba576317b139a4c1b4795925df867d8fe 100644 (file)
@@ -199,6 +199,10 @@ test_align(void)
                        val = RTE_ALIGN_MUL_FLOOR(i, p);
                        if (val % p != 0 || val > i)
                                FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
+                       val = RTE_ALIGN_MUL_NEAR(i, p);
+                       if (val % p != 0 || ((val != RTE_ALIGN_MUL_CEIL(i, p))
+                               & (val != RTE_ALIGN_MUL_FLOOR(i, p))))
+                               FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p);
                }
        }
 
index 7178ba1e9d09e5901e48eafd13cf2b9b43d95a25..bcf8afd39a21c11105e4f93028c5a32aeb68b4da 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
  *