All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Randy Dunlap <randy.dunlap@oracle.com>
Cc: Jiri Kosina <trivial@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH V2] kernel.h: Convert rounding macros to statement expressions, add ADD_MOD
Date: Mon, 29 Mar 2010 15:18:34 -0700	[thread overview]
Message-ID: <1269901114.4558.47.camel@Joe-Laptop.home> (raw)
In-Reply-To: <20100329144924.d00f0680.randy.dunlap@oracle.com>

On Mon, 2010-03-29 at 14:49 -0700, Randy Dunlap wrote:
> > Do you prefer something like this?
> Yes, thanks.
> > +#define roundup(x, y)				\
> > +({						\
> > +	typeof(y) _y = y;			\
> > +	((x) + (_y - 1) / _y) * _y;		\
> Above needs an extra set of parens:
> 	(((x) + (_y - 1)) / _y) * _y;		\

Thanks.  I think it's clearer matched to the DIV_ROUND_UP style:
	(((x) + _y - 1) / _y) * _y;

----------------

Convert rounding macros to statement expressions
so arguments are only evaluated once.
Add kernel-doc to rounding macros
Add ADD_MOD statement expression for "(x + y) % y"

Signed-off-by: Joe Perches <joe@perches.com>
Reviewed-by: Randy Dunlap <randy.dunlap@oracle.com>
---
 include/linux/kernel.h |   66 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 57 insertions(+), 9 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index c96b1ac..029db3b 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -55,15 +55,63 @@ extern const char linux_proc_banner[];
 #define round_down(x, y) ((x) & ~__round_mask(x, y))
 
 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
-#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
-#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
-#define DIV_ROUND_CLOSEST(x, divisor)(			\
-{							\
-	typeof(divisor) __divisor = divisor;		\
-	(((x) + ((__divisor) / 2)) / (__divisor));	\
-}							\
-)
-#define ADD_MOD(x, y) (((x) + (y)) % (y))
+
+/**
+ * roundup() - Returns x rounded up to the next whole multiple of y
+ * @x:	dividend
+ * @y:	divisor
+ *
+ * ie: roundup(4, 2) is 4, roundup(5, 2) is 6
+ * c type conversion rules apply when x and y types differ
+ */
+#define roundup(x, y)				\
+({						\
+	typeof(y) _y = y;			\
+	(((x) + _y - 1) / _y) * _y;		\
+})
+
+/**
+ * DIV_ROUND_UP() - Returns x/y rounded up to the next whole number
+ * @x:	dividend
+ * @y:	divisor
+ *
+ * ie: DIV_ROUND_UP(4, 2) is 2, DIV_ROUND_UP(5, 2) is 3
+ * c type conversion rules apply when x and y types differ
+ */
+#define DIV_ROUND_UP(x, y)			\
+({						\
+	typeof(y) _y = y;			\
+	((x) + _y - 1) / _y;			\
+})
+
+/**
+ * DIV_ROUND_CLOSEST() - Returns x/y rounded to the nearest whole number
+ * @x:	dividend
+ * @y:	divisor
+ *
+ * ie: DIV_ROUND_CLOSEST(4, 3) is 1, DIV_ROUND_CLOSEST(5, 3) is 2
+ * Rounds up when .5, ie: DIV_ROUND_CLOSEST(5, 2) is 3
+ * c type conversion rules apply when x and y types differ
+ */
+#define DIV_ROUND_CLOSEST(x, y)			\
+({						\
+	typeof(y) _y = y;			\
+	((x) + (_y / 2)) / _y;			\
+})
+
+/**
+ * ADD_MOD() - Returns (x + y) % y
+ * @x:	initial value
+ * @y:	value added to x then used as modulo
+ *
+ * ie: ADD_MOD(4, 2) is 0, ADD_MOD(5, 2) is 1
+ * c type conversion rules apply when x and y types differ
+ */
+#define ADD_MOD(x, y)				\
+({						\
+	typeof(y) _y = y;			\
+	((x) + _y) % _y;			\
+})
 
 #define _RET_IP_		(unsigned long)__builtin_return_address(0)
 #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })



  reply	other threads:[~2010-03-29 22:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-28 21:57 [PATCH] treewide: Add and use ADD_MOD macro Joe Perches
2010-03-29 19:47 ` Randy Dunlap
2010-03-29 20:00   ` Joe Perches
2010-03-29 21:09     ` [PATCH] kernel.h: Convert rounding macros to statement expressions, add ADD_MOD Joe Perches
2010-03-29 21:49       ` Randy Dunlap
2010-03-29 22:18         ` Joe Perches [this message]
2010-04-05 22:12           ` [PATCH V2] " Andrew Morton

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=1269901114.4558.47.camel@Joe-Laptop.home \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=randy.dunlap@oracle.com \
    --cc=trivial@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.