From: Andrew Morton <akpm@linux-foundation.org>
To: Joe Perches <joe@perches.com>
Cc: herbert@gondor.apana.org.au, torvalds@linux-foundation.org,
linux-kernel@vger.kernel.org
Subject: Re: [KERNEL]: Avoid divide in IS_ALIGN
Date: Tue, 20 Nov 2007 12:22:43 -0800 [thread overview]
Message-ID: <20071120122243.a737c8a8.akpm@linux-foundation.org> (raw)
In-Reply-To: <1195582635.4675.145.camel@localhost>
On Tue, 20 Nov 2007 10:17:15 -0800
Joe Perches <joe@perches.com> wrote:
> On Tue, 2007-11-20 at 21:56 +0800, Herbert Xu wrote:
> > [KERNEL]: Avoid divide in IS_ALIGN
> > I was happy to discover the brand new IS_ALIGN macro and quickly
> > used it in my code. To my dismay I found that the generated code
> > used division to perform the test.
> > This patch fixes it by changing the % test to an &. This avoids
> > the division.
>
> Perhaps this should use is_power_of_2?
>
> #define IS_ALIGNED(x, a) \
> ({ typeof(x) _a = (typeof(x))(a); \
> is_power_of_2(_a) ? (((x) & (_a - 1)) == 0) \
> : (((x) % _a == 0); })
>
> gcc -o2/oS seems to do the right thing.
The other *ALIGN* macros in there are only good for power-of-2 alignments
so I guess Herbert's change is OK.
The whole thing's a bit crufty really - there's no reason why it _can't_
work on non-power-of-2 numbers 9via an implementation such as you suggest)
and there's no commentary in there explaining that this will presently
break things.
Those macros are sufficiently obscure, risky and undocumented as to make
them of dubious value IMO. I mean, anything which forces you to go and
stare at the definition for a while when reviewing a callsite isn't worth
it.
next prev parent reply other threads:[~2007-11-20 20:24 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-20 13:56 [KERNEL]: Avoid divide in IS_ALIGN Herbert Xu
2007-11-20 18:17 ` Joe Perches
2007-11-20 20:22 ` Andrew Morton [this message]
2007-11-20 21:43 ` linux-os (Dick Johnson)
2007-11-21 13:06 ` linux-os (Dick Johnson)
2007-11-21 13:35 ` Andreas Schwab
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=20071120122243.a737c8a8.akpm@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=herbert@gondor.apana.org.au \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox