From mboxrd@z Thu Jan 1 00:00:00 1970 From: Pavan Nikhilesh Bhagavatula Subject: [PATCH v3 1/2] eal: add macro to align value to the nearest multiple Date: Sat, 16 Mar 2019 19:01:50 +0000 Message-ID: <20190316190119.6142-1-pbhagavatula@marvell.com> References: <20181129083138.23029-1-pbhagavatula@caviumnetworks.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Cc: "dev@dpdk.org" , Pavan Nikhilesh Bhagavatula To: Jerin Jacob Kollanukkaran , "keith.wiles@intel.com" , "stephen@networkplumber.org" , "thomas@monjalon.net" Return-path: Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by dpdk.org (Postfix) with ESMTP id 13CE611A4 for ; Sat, 16 Mar 2019 20:01:58 +0100 (CET) In-Reply-To: <20181129083138.23029-1-pbhagavatula@caviumnetworks.com> Content-Language: en-US List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" From: Pavan Nikhilesh 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 --- v2 Changes: - Spilt patch and add unit test for the new macro. v3 Changes: - Rebase to ToT. - Use macro instead of 1E7. app/test/test_common.c | 4 ++++ lib/librte_eal/common/include/rte_common.h | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/test/test_common.c b/app/test/test_common.c index 94d367471..2b856f8ba 100644 --- a/app/test/test_common.c +++ b/app/test/test_common.c @@ -199,6 +199,10 @@ test_align(void) val =3D RTE_ALIGN_MUL_FLOOR(i, p); if (val % p !=3D 0 || val > i) FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p); + val =3D RTE_ALIGN_MUL_NEAR(i, p); + if (val % p !=3D 0 || ((val !=3D RTE_ALIGN_MUL_CEIL(i, p)) + & (val !=3D RTE_ALIGN_MUL_FLOOR(i, p)))) + FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p); } } diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/co= mmon/include/rte_common.h index 7178ba1e9..bcf8afd39 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -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 parame= ter + * whichever difference is the lowest. + */ +#define RTE_ALIGN_MUL_NEAR(v, mul) \ + ({ \ + typeof(v) ceil =3D RTE_ALIGN_MUL_CEIL(v, mul); \ + typeof(v) floor =3D 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 * -- 2.21.0