DPDK patches and discussions
 help / color / mirror / Atom feed
From: Pavan Nikhilesh Bhagavatula <pbhagavatula@marvell.com>
To: Jerin Jacob Kollanukkaran <jerinj@marvell.com>,
	"stephen@networkplumber.org" <stephen@networkplumber.org>,
	"thomas@monjalon.net" <thomas@monjalon.net>
Cc: "dev@dpdk.org" <dev@dpdk.org>,
	Pavan Nikhilesh Bhagavatula <pbhagavatula@marvell.com>
Subject: [dpdk-dev] [PATCH v2 1/2] eal: add macro to align value to the nearest multiple
Date: Sat, 16 Mar 2019 07:03:31 +0000	[thread overview]
Message-ID: <20190316070302.32432-1-pbhagavatula@marvell.com> (raw)
In-Reply-To: <20181129083138.23029-1-pbhagavatula@caviumnetworks.com>

From: Pavan Nikhilesh <pbhagavatula@marvell.com>

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>
---
 v2 Changes:
 - Spilt patch and add unit test for the new macro.

 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 = 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);
 		}
 	}

diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/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 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
  *
--
2.21.0

  parent reply	other threads:[~2019-03-16  7:03 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-29  8:32 [dpdk-dev] [PATCH] eal: roundup tsc frequency when estimating it Pavan Nikhilesh
2018-11-29  9:08 ` Jerin Jacob
2018-11-29 21:21 ` Stephen Hemminger
2018-11-30  7:17   ` Pavan Nikhilesh
2019-03-16  7:03 ` Pavan Nikhilesh Bhagavatula [this message]
2019-03-16  7:03   ` [dpdk-dev] [PATCH v2 1/2] eal: add macro to align value to the nearest multiple Pavan Nikhilesh Bhagavatula
2019-03-16  7:03   ` [dpdk-dev] [PATCH v2 2/2] eal: roundup tsc frequency when estimating it Pavan Nikhilesh Bhagavatula
2019-03-16  7:03     ` Pavan Nikhilesh Bhagavatula
2019-03-16 14:42     ` Wiles, Keith
2019-03-16 14:42       ` Wiles, Keith
2019-03-16 15:06       ` [dpdk-dev] [EXT] " Pavan Nikhilesh Bhagavatula
2019-03-16 15:06         ` Pavan Nikhilesh Bhagavatula
2019-03-16 17:18         ` Wiles, Keith
2019-03-16 17:18           ` Wiles, Keith
2019-03-16 17:56           ` Pavan Nikhilesh Bhagavatula
2019-03-16 17:56             ` Pavan Nikhilesh Bhagavatula
2019-03-16 18:22             ` Wiles, Keith
2019-03-16 18:22               ` Wiles, Keith
2019-03-16 18:27               ` Pavan Nikhilesh Bhagavatula
2019-03-16 18:27                 ` Pavan Nikhilesh Bhagavatula
2019-03-16 19:01 ` [dpdk-dev] [PATCH v3 1/2] eal: add macro to align value to the nearest multiple Pavan Nikhilesh Bhagavatula
2019-03-16 19:01   ` Pavan Nikhilesh Bhagavatula
2019-03-16 19:01   ` [dpdk-dev] [PATCH v3 2/2] eal: roundup tsc frequency when estimating Pavan Nikhilesh Bhagavatula
2019-03-16 19:01     ` Pavan Nikhilesh Bhagavatula
2019-03-27 22:43     ` Thomas Monjalon
2019-03-27 22:43       ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190316070302.32432-1-pbhagavatula@marvell.com \
    --to=pbhagavatula@marvell.com \
    --cc=dev@dpdk.org \
    --cc=jerinj@marvell.com \
    --cc=stephen@networkplumber.org \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).