All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Buesch <mb@bu3sch.de>
To: Harvey Harrison <harvey.harrison@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Jeff Garzik <jeff@garzik.org>,
	Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>,
	Mauro Carvalho Chehab <mchehab@infradead.org>
Subject: Re: [PATCH 1/6] kernel: add clamp(), clamp_t() and clamp_val() macros
Date: Wed, 12 Mar 2008 18:20:14 +0100	[thread overview]
Message-ID: <200803121820.14883.mb@bu3sch.de> (raw)
In-Reply-To: <1205340866.8603.9.camel@brick>

On Wednesday 12 March 2008 17:54:26 Harvey Harrison wrote:
> On Wed, 2008-03-12 at 16:13 +0100, Michael Buesch wrote:
> > So why not call it clamp_const()?
> > One could even use __builtin_constant_p() and make clamp() use
> > either clamp_const() or clamp_nonconst() from above automagically.
> > I'd prefer that.
> 
> Did you mean something like this?  No more clamp_val, just clamp and
> clamp_t.  clamp_t forces all the types, clamp looks at the min and max
> args, and if they are constants, uses the type of val instead.  If not
> a constant, the strict typechecking is done.

> +#define clamp(val, min, max) ({				\
> +	typeof(val) __val = (val);			\
> +							\
> +	if (__builtin_constant_p(min)) {		\
> +		typeof(val) __min = (min);		\
> +		__val = __val < __min ? __min: __val;	\
> +	} else {					\
> +		typeof(min) __min = (min);		\
> +		(void) (&__val == &__min);		\
> +		__val = __val < __min ? __min: __val;	\
> +	}						\
> +							\
> +	if (__builtin_constant_p(max)) {		\
> +		typeof(val) __max = (max);		\
> +		__val > __max ? __max: __val;		\
> +	} else {					\
> +		typeof(max) __max = (max);		\
> +		(void) (&__val == &__max);		\
> +		__val > __max ? __max: __val;		\
> +	} })

Yeah, something like that.
Does returning of the value work over an indentation level, too?
I dunno this detail of the language.
But I'd prefer the following for readability anyway:

+       if (__builtin_constant_p(max)) {                \
+               typeof(val) __max = (max);              \
+               __val = __val > __max ? __max: __val;           \
+       } else {                                        \
+               typeof(max) __max = (max);              \
+               (void) (&__val == &__max);              \
+               __val = __val > __max ? __max: __val;           \
+       }
+	__val; })

Probably you can also only put the pointer check into the constant check:

+#define clamp(val, min, max) ({                \
+       typeof(val) __val = (val);              \
+       typeof(min) __min = (min);              \
+       typeof(max) __max = (max);              \
+       if (!__builtin_constant_p(min))         \
+               (void) (&__val == &__min);      \
+       __val = __val < __min ? __min: __val;   \
+       if (!__builtin_constant_p(max))         \
+               (void) (&__val == &__max);      \
+       __val = __val > __max ? __max: __val;   \
+       __val; })

But it seems that this evaluates the arguments twice, so my idea turns out
to be not too good anyway. hm..

-- 
Greetings Michael.

  reply	other threads:[~2008-03-12 17:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-11 21:11 [PATCH 1/6] kernel: add clamp(), clamp_t() and clamp_val() macros Harvey Harrison
2008-03-12  5:08 ` Andrew Morton
2008-03-12 15:13 ` Michael Buesch
2008-03-12 16:54   ` Harvey Harrison
2008-03-12 17:20     ` Michael Buesch [this message]
2008-03-12 17:34       ` Harvey Harrison

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=200803121820.14883.mb@bu3sch.de \
    --to=mb@bu3sch.de \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=bzolnier@gmail.com \
    --cc=harvey.harrison@gmail.com \
    --cc=jeff@garzik.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab@infradead.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.